我正在使用Properties Maven Plugin读取一些属性,执行此插件后我执行另一个插件,之后我想用不同的配置再次执行这个插件
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>src/Args.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
执行maven-antrun-plugin,然后用配置&#34; src / Args2.properties&#34;回忆一下这个插件,因为在上一个插件中我声明了一个新的属性,并且在文件中使用了#&# 34; Args2.properties
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<taskdef resource="net/sf/antcontrib/antcontrib.properties"
classpathref="maven.plugin.classpath"/>
<if>
<equals arg1="${serviceType}" arg2="none"/>
<then>
<property name="prop" value="x"/>
</then>
<else>
<property name="prop"value="y"/>
</else>
</if>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
和Args2.properties:
prop2=${prop}/anotherValue
我希望在Antrun插件中为prop提供一个值,以便稍后从文件Args2中读取prop2
答案 0 :(得分:2)
在构建中再次执行Maven插件只需指定另一个<execution>
块,而不通过在POM中指定插件两次来完成:
<plugin>
<groupId>my.groupid</groupId>
<artifactId>my-plugin</artifactId>
<version>version</version>
<executions>
<execution>
<id>execution-1</id>
<phase>initialize</phase>
<!-- goals and configuration for this execution -->
</execution>
<execution>
<id>execution-2</id>
<phase>test</phase>
<!-- goals and configuration for this execution -->
</execution>
</executions>
</plugin>
例如,上面的配置将定义插件的两次执行,其中第一个绑定到initialize
阶段,而另一个绑定到test
阶段。当两个执行绑定到同一阶段时,它们将在POM中以其声明顺序执行:这意味着即使execution-2
将绑定到initialize
阶段,也就像execution-1
一样},它将在execution-1
之后执行。
在你的情况下,你有一些不常见的东西,在构建中读取多个属性文件,但在某种程度上交错执行maven-antrun-plugin
插件。这很麻烦,因为你不能在POM中两次声明插件,所以不可能在另一个插件B的两次执行之间插入插件A的执行,所有这些都在同一阶段。这是正常的,因为在给定阶段的执行是在POM中的声明顺序中执行的,并且插件必须声明一次;所以要么A在POM中的B之前,它将在之前被执行,这是不需要的(因为B之前需要执行一次B),或者B在A之前执行,而B的2次执行将是在A之前执行,这也是不需要的(因为A的执行需要在两者之间发生)。
解决此问题的一种方法是将执行绑定到不同的阶段:例如确保我们想要先运行的B的执行被绑定到某个阶段,并且我们想要运行的A的执行被绑定到构建中稍后发生的阶段,最后是B的最后一次执行也受到后期的影响。这不是一个干净的解决方案,因为你最终会滥用某些阶段去做那些目前不应该做的事情。
更好的解决方案是接受这样一个事实,即我们真正想要在单个步骤中执行一次执行:即在同一执行中加载属性,执行操作,加载其他属性。他们如此紧密地结合在一起,将它们整合在一起也是有意义的。这并不总是很容易完成 - 通常,您需要创建自己的Maven插件来执行此操作,以便所有这些任务都由它封装。
但是,在这种特定情况下,您可以重用maven-antrun-plugin
并在执行此插件时执行所有操作。在设置prop
属性之前和之后,Ant有一个任务loadproperties
,可用于加载属性文件。
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>load-properties</id>
<phase>initialize</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<loadproperties srcFile="${project.basedir}/src/Args.properties" />
<taskdef resource="net/sf/antcontrib/antcontrib.properties"
classpathref="maven.plugin.classpath" />
<if>
<equals arg1="${serviceType}" arg2="none" />
<then>
<property name="prop" value="x" />
</then>
<else>
<property name="prop" value="y" />
</else>
</if>
<loadproperties srcFile="${project.basedir}/src/Args2.properties" />
</target>
<exportAntProperties>true</exportAntProperties>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>ant-contrib</groupId>
<artifactId>ant-contrib</artifactId>
<version>1.0b3</version>
<exclusions>
<exclusion>
<groupId>ant</groupId>
<artifactId>ant</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</plugin>
上述声明的几个注释:插件已更新为最新版本的1.8
,并且需要依赖ant-contrib
来解析antcontrib.properties
文件。 ant
需要从ant-contrib
的依赖项中排除,因为AntRun插件使用Ant 1.9,但该依赖项会使插件继承旧版本。另请注意<exportAntProperties>
,它将使您的构建版能够使用在Ant任务中创建的属性,并使用<target>
而不是已弃用的<tasks>
。