I am converting an ant script to Maven and decided to use Maven ant run plugin. I am on the good way, but I hit the following problem. Ant source script uses target definition as follows:
<condition property="isWindows">
<os family="windows"/>
</condition>
<condition property="isUnix">
<os family="unix"/>
</condition>
<target name="init-windows" depends="" if="isWindows">
<property file="${user.home}/abc.properties"/>
</target>
<target name="init-unix" depends="" if="isUnix">
<property name="abc.home" value="${env.ABC_HOME}"/>
</target>
The point is to use property value
abc.home
later in the build cycle which is dependent on OS (Win,Linux). In the ant script it is ok,but maven ant run plugin does not enable using the multiple targets. I do not want to use Maven profile tags. I would like to use ant tag for this if there is any? Does anybody have a hint?
答案 0 :(得分:4)
我认为更简单的解决方案是 来使用maven-antrun-plugin
并使用profiles代替。
配置文件是根据某些激活属性获得不同属性的好方法。其中一个激活属性可以是运行构建的操作系统。
考虑以下POM:
<profiles>
<profile>
<id>windows</id>
<activation>
<os>
<family>windows</family>
</os>
</activation>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<!-- Associate the read-project-properties goal with the initialize phase, to read the properties file. -->
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>${user.home}/abc.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</profile>
<profile>
<id>unix</id>
<activation>
<os>
<family>unix</family>
</os>
</activation>
<properties>
<abc.home>${env.ABC_HOME}</abc.home>
<properties>
</profile>
</profiles>
windows
配置文件将被激活。使用properties-maven-plugin
读取属性文件,其内容将放在属性中,就像Ant <property>
任务一样。unix
配置文件将被激活,abc.home
属性将设置为${env.ABC_HOME}
。然后,在您的Maven构建中,您可以使用${abc.home}
,而无需担心您所处的个人资料。
话虽如此,另一种解决方案是运行maven-antrun-plugin
的多次执行。在第一次执行时,您可以决定是在Windows或Unix计算机上运行构建,然后相应地跳过以下执行。这将是一个示例配置,其中myPhase
将是您希望它运行的阶段。
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>myphase</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<condition property="isWindows">
<os family="windows" />
</condition>
<condition property="isUnix">
<os family="unix" />
</condition>
</target>
<exportAntProperties>true</exportAntProperties>
</configuration>
</execution>
<execution>
<phase>myphase</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target name="init-windows" depends="">
<property file="${user.home}/abc.properties" />
</target>
<exportAntProperties>true</exportAntProperties>
<skip>${isUnix}</skip>
</configuration>
</execution>
<execution>
<phase>myphase</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target name="init-unix" depends="">
<property name="abc.home" value="${env.ABC_HOME}" />
</target>
<exportAntProperties>true</exportAntProperties>
<skip>${isWindows}</skip>
</configuration>
</execution>
</executions>
</plugin>