`
liugang594
  • 浏览: 989707 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

maven-antrun-plugin的使用

 
阅读更多

maven-antrun-plugin提供在maven中运行ant任务的能力。

 

基本配置如下:

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-antrun-plugin</artifactId>
				<version>1.7</version>
				<executions>
					<execution>
						<phase>prepare-package</phase>
						<goals>
							<goal>run</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
					...
				</configuration>
			</plugin>
		</plugins>
	</build>

这里phase是一个maven生命周期中的一个phase,maven-antrun-plugin的一个最有用的goal就是run (还有一个goal是help)。 

 

剩下的就是configuration的配置了。

maven-antrun-plugin可用的配置不太多(在1.7上只有7个有效的配置):

Name Type Since Description
customTaskPrefix String 1.5 The xml tag prefix to use for the built in Ant tasks. This prefix needs to be prepended to each task referenced in the antrun target config. For example, a prefix of "mvn" means that the attachartifact task is referenced by "<mvn:attachartifact>" The default value of an empty string means that no prefix is used for the tasks.
exportAntProperties boolean 1.7 Specifies whether the Ant properties should be propagated to the Maven properties.
Default value isfalse.
failOnError boolean 1.7 Specifies whether a failure in the ant build leads to a failure of the Maven build. If this value is 'true', the Maven build will proceed even if the ant build fails. If it is 'false', then the Maven build fails if the ant build fails.
Default value istrue.
propertyPrefix String 1.4 String to prepend to project and dependency property names.
skip boolean 1.7 Specifies whether the Antrun execution should be skipped.
Default value isfalse.
target PlexusConfiguration 1.5 The XML for the Ant target. You can add anything you can add between <target> and </target> in a build.xml.
versionsPropertyName String - The name of a property containing the list of all dependency versions. This is used for the removing the versions from the filenames.
Default value ismaven.project.dependencies.versions.

其中最有用的就是target参数了,在target参数中可以写任何在ant的build.xml里可以设置的内容。

下面是一个示例:

 

打印内容

可以用echo来打印内容:

				<configuration>
					<target>
						<echo level="warning" message="running" />
					</target>
				</configuration>

运行这个tast可以看到在最后有输出:

main:

     [echo] running

 

替换

上一篇文章里我们介绍一replacer插件的用法。用ant也可以执行一些replace的任务。

					<target>
						<copy todir="src/main/resources">
							<fileset dir="src/test/resources" includes="**/*.txt" />
						</copy>
						<replace dir="src/main/resources">
							<replacefilter token="{book.name}" value="Thinkin in Java" />
							<replacefilter token="{author.name}" value="Bruce Eckel" />
						</replace>
					</target>

因为ant 的 replace 命令不支持指定输入目录,所以需要先做一个拷贝。这里先把所有的src/test/resources下的以 .txt 结尾的文件拷到 src/main/resources 目录下;然后在 src/main/resources 目录下做替换。

这里是使用的多文件多替换,也可以像replacer插件一样,单文件单替换:

					<target>
						<replace file="a.txt" token="{book.name}" value="Thinkin in Java" />
					</target>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics