如何多次执行Maven插件 [英] How to execute maven Plugins multiple times
问题描述
我正在使用Properties Maven插件读取一些属性,执行此插件后,我将执行另一个插件,此后,我想以不同的配置再次执行此插件
I am using Properties Maven Plugin to read some Properties, after execute this plugin I execute another Plugin, after that I want to execute this plugin again with different configuration
<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,然后使用配置"src/Args2.properties"调用此插件,因为在上一个插件中我声明了一个新属性,并且已在文件"Args2.properties"中使用
execute maven-antrun-plugin and then recall this plugin with configuration "src/Args2.properties", because with last plugin i declared a new properties, and it's used in file "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:
and Args2.properties :
prop2=${prop}/anotherValue
我想在Antrun插件中给prop赋值后,稍后再从文件Args2中读取prop2
i want after giving a value to prop in Antrun plugin to read prop2 later from the file Args2
推荐答案
在构建中再次执行Maven插件只需指定另一个<execution>
块即可,而 not 可以通过指定在POM中两次插入插件:
Having another execution of a Maven plugin in the build is simply done by specifying another <execution>
block, and not by specifying the plugin twice in the 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
之后.
For example, the configuration above would define two execution of a plugin, where the first is bound to the initialize
phase, while the other is bound to the test
phase. When 2 executions are bound to the same phase, they will be executed in their declaration order in the POM: this means that even if execution-2
would be bound to the initialize
phase, just like execution-1
, it would be executed after execution-1
.
在您的情况下,您在读取构建中的多个属性文件方面并不是真正的常规,而是在两者之间以某种方式交错执行了maven-antrun-plugin
插件.这很麻烦,因为,由于无法在POM中两次声明该插件,因此不可能在同一阶段在另一个插件B的两次执行之间插入插件A的执行.这是正常的,因为在给定阶段的执行是按照POM中的声明顺序执行的,并且插件必须声明一次.因此,要么A在POM中位于B之前,然后在它之前被执行,这是不希望的(因为必须先执行B的一个执行),或者B在A之前,并且B的2个执行将是在A之一之前执行,而A也不想要(因为A的执行需要在两者之间进行).
In your case, you have something not really conventional of reading multiple properties file in the build but somehow interleaving an execution of the maven-antrun-plugin
plugin in between. This is troublesome, because, since you cannot declare the plugin twice in the POM, it isn't possible to insert an execution of a plugin A between two executions of another plugin B, all at the same phase. This is normal, since executions at a given phase are executed in the declaration order in the POM, and plugins have to be declared once; so either A is before B in the POM, and it'll be executed before, which isn't wanted (since one execution of B needed to be run before), or B is before A, and the 2 executions of B will be executed before the one of A, which isn't wanted either (since the execution of A needed to happen in between).
一种解决方法是将执行绑定到不同的阶段:确保我们要首先运行的B的执行被绑定到某个阶段,并且我们想要之后运行的A的执行被绑定到在构建的稍后阶段发生的阶段,最后确保B的最后一次执行也将绑定到下一个阶段.不过,这不是一个干净的解决方案,因为您最终会滥用某些阶段来执行此时不应执行的操作.
One way to solve this is to bind the executions to different phases: e.g. make sure that the execution of B we want to run first is bound to a certain phase, and that the execution of A we want to run after is bound to a phase that happens later in the build, and finally that the last execution of B is also bound to a later phase. This isn't a clean solution though, because you end-up abusing certain phases to do things that shouldn't be done at this time.
一个更好的解决方案是接受一个事实,即我们真正想要在一次执行中一步完成的事情:即在同一执行中加载属性,执行操作,加载另一个属性.它们是如此紧密地联系在一起,因此也有必要将它们集成在一起.这并非总是容易做到的-通常,您需要创建自己的Maven插件才能执行此操作,以便所有这些任务都由它封装.
A better solution is to accept the fact that what we really want to do in a single step with a single execution: i.e. load a properties, do things, load another properties, in the same execution. They are so tied together that it makes sense to integrate them together as well. This cannot always be done easily -- typically, you would need to create your own Maven plugin to do this, so that all thoses tasks are encapsulated by it.
但是,在这种特定情况下,您可以重用maven-antrun-plugin
并在此插件的执行中执行所有操作. Ant有一个任务 loadproperties
,可用于加载属性文件,在设置prop
属性之前和之后.
However, in this specific case, you could just reuse the maven-antrun-plugin
and do everything inside an execution of this plugin. Ant has a task loadproperties
that can be used to load the properties file, before and after setting the prop
property.
<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>
而不是已弃用的
A couple of notes with the declaration above: the plugin was updated to the latest version of 1.8
and the dependency to ant-contrib
is needed to resolve the antcontrib.properties
file. ant
needs to be excluded from the dependency to ant-contrib
because the AntRun plugin uses Ant 1.9, but that dependency would make the plugin inherit an older version. Note also the <exportAntProperties>
which will enable your build to use the properties created in the Ant task, and the use of <target>
instead of the deprecated <tasks>
.
这篇关于如何多次执行Maven插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!