Maven——(maven-antrun-plugin、maven-dependency-plugin)插件的使用

 

前言

有时 Maven 项目中构建完后还需要做一些较复杂的文件操作,这时我们可以考虑使用 maven-antrun-plugin 插件在 Maven 的 pom 中配置调用 Ant 任务。

 

格式:

<build>
    <plugins>
        <!--
        ...
        -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.8</version>
            <executions>
                <execution>
                    <!-- 执行打包操作时执行的任务 -->
                    <phase>package</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <tasks>
                            <echo> 执行一些 Ant 任务:</echo>
                 <echo file="${basedir}/target/port.txt">${server.port}</echo> <mkdir dir="xxx"/> <copy file="test.properties" todir="xxx" overwrite="true" /> <copydir src="../xxx" dest="../yyy" /> <copy todir="../xxx" overwrite="true" > <fileset dir="../xxx" /> </copy> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build>

tasks 标签下的内容就是具体 Ant 执行的任务了。

更多可执行的任务标签可参考:http://ant.apache.org/manual/tasksoverview.html

使用实例1:

    <build>
        <plugins>
       ......
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
               <!-- 复制newrelic-agent.jar包到指定目录(target/)下 --> <phase>prepare-package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <includeArtifactIds>newrelic-agent</includeArtifactIds> <outputDirectory>${project.build.directory}</outputDirectory> <stripVersion>true</stripVersion> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <id>id.package</id>
               <!-- 执行打包操作时执行的任务 --> <phase>package</phase> <configuration> <target>
                   <!-- 把 ${basedir}\*.sh 文件复制到 ${basedir}\target 目录下 --> <copy todir="${basedir}/target" overwrite="true"> <fileset dir="${basedir}"> <include name="*.sh"/> </fileset> </copy>
                   <!-- 给 target/*.sh 授权 -->      <chmod file="target/*.sh" perm="755"/>
                   <!-- 替换指定文件中的8080为${server.port} --> <replace file="target/stop-${project.artifactId}.sh" token="8080" value="${server.port}"></replace> </target> </configuration> <goals> <goal>run</goal> </goals> </execution> <execution>
               <!-- 将应用中默认的newrelic.yml配置文件,复制到指定目录(target/)下 --> <id>id.newrelic</id> <phase>package</phase> <configuration> <target> <copy todir="${basedir}/target/"> <fileset dir="${basedir}/src/main/resources/conf_${spring.profiles.active}"> <include name="newrelic.yml"/> </fileset> </copy> </target> </configuration> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin> </plugins> </build>

使用实例2:

  <build>
        <plugins>
       ......
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
               <!-- 解压newrelic-agent.jar中包含的所有文件 到 应用的jar包中 --> <id>addExtractedJarOnRootLevel</id> <phase>package</phase> <configuration> <target> <zip destfile="${project.build.directory}/${project.artifactId}.jar" update="yes" compress="false"> <zipfileset src="${com.newrelic.agent.java:newrelic-agent:jar}" /> </zip> </target> </configuration> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin> </plugins> </build>

 

posted on 2021-12-02 14:50  曹伟雄  阅读(2224)  评论(0编辑  收藏  举报

导航