maven-anturn-plugin插件使用

时间:2021-03-21 17:16:38

maven-anturn-plugin插件为maven提供了ant功能,它可以使旧项目的配置操作在新项目得以重用。

该插件主要有2中执行ant操作的方法:

1.直接在插件中配置编写,例如:

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-antrun-plugin</artifactId>
	<version>1.7</version>
	<executions>
		<execution>
			<id>install</id>
			<phase>install</phase>
			<configuration>
				<target>
					<echo message="********************copy profile propertie file *************************" />
					<copy file="webContent/WEB-INF/temporary-lib/dhcc-user-bundle-1.0.7.jar"
						tofile="webContent/WEB-INF/lib/dhcc-user-bundle-1.0.7.jar"
						overwrite="true" />
				</target>
			</configuration>
			<goals>
				<goal>run</goal>
			</goals>
		</execution>
	</executions>
</plugin>
上面的操作在项目编译的时候,复制了webContent/WEB-INF/temporary-lib/dhcc-user-bundle-1.0.7.jar文件到webContent/WEB-INF/lib/dhcc-user-bundle-1.0.7.jar。


2.调用ant脚本,即build.xml文件,并执行,例如:

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-antrun-plugin</artifactId>
	<version>1.7</version>
	<executions>
		<execution>
			<id>ant-magic</id>
			<phase>prepare-package</phase>
			<goals>
				<goal>run</goal>
			</goals>
			<configuration>
				<tasks>
					<property name="compile_classpath" refid="maven.compile.classpath" />
					<!-- <property name="outputDir" value="${project.build.outputDirectory}"/> 
					<property name="sourceDir" value="${project.build.sourceDirectory}" /> -->
					<ant antfile="${basedir}/build.xml" target="prepare-package-clean" />
				</tasks>
			</configuration>
		</execution>
		<execution>
			<id>ant-magic2</id>
			<phase>package</phase>
			<goals>
				<goal>run</goal>
			</goals>
			<configuration>
				<tasks>
					<property name="compile_classpath" refid="maven.compile.classpath" />
					<ant antfile="${basedir}/build.xml" target="package-clean" />
				</tasks>
			</configuration>
		</execution>
	</executions>
</plugin>

build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project default="prepare-package-clean" basedir=".">
	<description>
		dhcc build file
	</description>

	<property name="srcDir" location="src/main/java" />
	<property name="webDir" location="webContent" />
	<property name="lib.dir" location="${webDir}/WEB-INF/lib" />

	<property name="distDir" location="${webDir}" />
	<property name="dist.lib" location="${distDir}/WEB-INF/lib" />
	<property name="dist.classes" location="${distDir}/WEB-INF/classes" />
	<property name="targetDir" location="${distDir}/target" />

	<target name="prepare-package-clean">
		<echo message="clean lib jar and class start====" />
		<delete dir="${dist.classes}" />
		<delete dir="${dist.lib}\" />
		<echo message="clean lib jar and class end====" />
	</target>
	<target name="package-clean">
		<echo message="clean jersey and jar and other  jar start =====" />
		<delete file="${dist.lib}\jersey-core-1.17.1.jar" />
		<delete file="${dist.lib}\jersey-server-1.17.1.jar" />
		<delete file="${dist.lib}\jersey-servlet-1.17.1.jar" />
		<delete file="${dist.lib}\javassist-3.11.0.GA.jar" />
		<delete file="${dist.lib}\xml-apis-1.0.b2.jar" />
		<delete file="${dist.lib}\jackson-core-asl-1.9.11.jar" />
		<delete file="${dist.lib}\jackson-mapper-asl-1.9.11.jar" />
		<delete file="${dist.lib}\c3p0-0.9.1.1.jar" />
		<delete file="${dist.lib}\commons-fileupload-1.3.jar" />
		<delete file="${dist.lib}\jersey-core-1.18.jar" />
		<delete file="${dist.lib}\jersey-server-1.18.jar" />
		<delete file="${dist.lib}\jersey-servlet-1.18.jar" />
		<delete file="${dist.lib}\javassist-3.11.0.GA.jar" />
		<echo message="clean jersey and jar and other  jar end =====" />
	</target>
	<target name="init" depends="prepare-package-clean">
		<tstamp />
		<echo message="distDir ${distDir}" />
		<echo message="dist.classes dir ${dist.classes}" />
		<echo message="dist.lib ${dist.lib}" />

	</target>

	<target name="compile" depends="init">
		<echo message="compile java ================== " />
		<echo message="dist.lib:${dist.lib}" />
		<mkdir dir="${dist.lib}" />
		<mkdir dir="${dist.classes}" />
		<javac encoding="utf-8" srcdir="${srcDir}" destdir="${dist.classes}" >
        	<classpath refid="classpath"/>
		</javac>
	</target>

</project>


上面定义了2个节点(execution),分别在准备打包时执行prepare-package-clean任务,在打包的时候执行package-clean任务。

id:节点名

phase:任务执行时间,例如上面则是在执行maven package时运行任务

goal:只能为run,即运行时

configuration:任务配置

property:属性

echo:注释

ant:引入ant的脚本