maven-antrun-plugin用于在POM中执行Ant任务,通过配置的方式执行Ant任务,而非配置Ant原生的build.xml。
    为什么要使用maven-antrun-plugin?因为微服务把系统拆分成了多个独立子服务,每次编译后找target文件夹中的jar耗时耗力,所以要将编译好的jar统一复制固定文件夹中,省时省力,对接持续集成系统也很方便。

    pom中引入ant-contrib依赖:

    1. <ant-contrib.version>1.0b3</ant-contrib.version>
    2. <!--可以在pom里面写if判断-->
    3. <dependency>
    4. <groupId>ant-contrib</groupId>
    5. <artifactId>ant-contrib</artifactId>
    6. <version>${ant-contrib.version}</version>
    7. </dependency>

    maven-antrun-plugin的配置参考:

    1. <properties>
    2. <copy>true</copy>
    3. <localDir>final-target</localDir>
    4. </properties>
    5. <plugin>
    6. <artifactId>maven-antrun-plugin</artifactId>
    7. <executions>
    8. <execution>
    9. <id>copy</id><!--需要唯一-->
    10. <phase>package</phase><!--当执行package操作时执行一下任务-->
    11. <configuration>
    12. <tasks>
    13. <echo message="Start.................................."/>
    14. <echo message="Load maven plugin ant-contrib-1.0b3"/>
    15. <!--加载plugin ant-contrib的配置文件-->
    16. <taskdef resource="net/sf/antcontrib/antlib.xml">
    17. <classpath><!--加载jar包,${settings.localRepository}的值是你maven settings文件中配置的本地仓库位置-->
    18. <pathelement location="${settings.localRepository}/ant-contrib/ant-contrib/1.0b3/ant-contrib-1.0b3.jar"/>
    19. </classpath>
    20. </taskdef>
    21. <if><!--复制jar包-->
    22. <equals arg1="${copy}" arg2="true"/><!--是否复制jar包-->
    23. <then>
    24. <echo message="copy jar to your desired path."/>
    25. <copy todir="${project.parent.basedir}/${localDir}" overwrite="true"><!--执行复制操作,todir的值是将要复制jar包到的地方,overwrite是否重写-->
    26. <fileset dir="${project.build.directory}"><!--${project.build.directory}值是你的target目录-->
    27. <include name="*.jar"/><!--target目录下的jar包-->
    28. </fileset>
    29. </copy>
    30. </then>
    31. </if>
    32. </tasks>
    33. </configuration>
    34. <goals>
    35. <goal>run</goal>
    36. </goals>
    37. </execution>
    38. </executions>
    39. </plugin>

    todir=”${project.parent.basedir}/${localDir}”中的${project.parent.basedir}如果解析失败,可以用../${localDir}替换。