maven-antrun-plugin用于在POM中执行Ant任务,通过配置
为什么要使用maven-antrun-plugin?因为微服务把系统拆分成了多个独立子服务,每次编译后找target文件夹中的jar耗时耗力,所以要将编译好的jar统一复制固定文件夹中,省时省力,对接持续集成系统也很方便。
pom中引入ant-contrib依赖:
<ant-contrib.version>1.0b3</ant-contrib.version><!--可以在pom里面写if判断--><dependency><groupId>ant-contrib</groupId><artifactId>ant-contrib</artifactId><version>${ant-contrib.version}</version></dependency>
maven-antrun-plugin的配置参考:
<properties><copy>true</copy><localDir>final-target</localDir></properties><plugin><artifactId>maven-antrun-plugin</artifactId><executions><execution><id>copy</id><!--需要唯一--><phase>package</phase><!--当执行package操作时执行一下任务--><configuration><tasks><echo message="Start.................................."/><echo message="Load maven plugin ant-contrib-1.0b3"/><!--加载plugin ant-contrib的配置文件--><taskdef resource="net/sf/antcontrib/antlib.xml"><classpath><!--加载jar包,${settings.localRepository}的值是你maven settings文件中配置的本地仓库位置--><pathelement location="${settings.localRepository}/ant-contrib/ant-contrib/1.0b3/ant-contrib-1.0b3.jar"/></classpath></taskdef><if><!--复制jar包--><equals arg1="${copy}" arg2="true"/><!--是否复制jar包--><then><echo message="copy jar to your desired path."/><copy todir="${project.parent.basedir}/${localDir}" overwrite="true"><!--执行复制操作,todir的值是将要复制jar包到的地方,overwrite是否重写--><fileset dir="${project.build.directory}"><!--${project.build.directory}值是你的target目录--><include name="*.jar"/><!--target目录下的jar包--></fileset></copy></then></if></tasks></configuration><goals><goal>run</goal></goals></execution></executions></plugin>
todir=”${project.parent.basedir}/${localDir}”中的${project.parent.basedir}如果解析失败,可以用../${localDir}替换。
