maven插件exec-maven-plugin、maven-antrun-plugin使用详解

文章目录

  • 前言
  • 一、exec-maven-plugin使用
  • exec:java的使用
  • id
  • goals
  • phase
  • configuration
  • exec:exec的使用
  • 使用exec-maven-plugin来构建前端项目
  • 直接用mvn命令来使用exec-maven-plugin插件
  • 二、maven-antrun-plugin使用
  • echo打印功能
  • 拷贝文件
  • 拷贝文件夹
  • ftp/scp/sshexec
  • 总结

  • 前言

    在使用maven构建项目时,有时我们可能需要执行一些系统命令来协助完成。这个时候就可以使用exec-maven-plugin和maven-antrun-plugin这两个插件。
    exec-maven-plugin的主要功能类似于一个后台控制台,我们在控制台执行的命令,都可以借助它来完成。
    maven-antrun-plugin的主要功能是执行一些ant任务,在maven还没诞生的时候Java代码主要编译工具是ant,因此为了要兼容老的ant编译,使用maven-antrun-plugin就能完成。


    一、exec-maven-plugin使用

    exec-maven-plugin有两个执行目标,分别是exec和java
    如果要执行java,配置如下
    在pom.xml中加入类似下面的配置:

    exec:java的使用

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.1.1</version>
        <executions>
            <execution>
                <goals>
                    <goal>java</goal>
                </goals>
                <phase>test</phase>
            </execution>
        </executions>
        <configuration>
            <includeProjectDependencies>true</includeProjectDependencies>
            <includePluginDependencies>true</includePluginDependencies>
            <executableDependency>
                <groupId>commons-lang</groupId>
                <artifactId>commons-lang</artifactId>
            </executableDependency>
            <mainClass>com.example.testmvnpkgexespringboot.TestMvnPkgExeSpringbootApplication</mainClass>
            <arguments>
                <argument>1</argument>
                <argument>2</argument>
            </arguments>
            <systemProperties>
                <systemProperty>
                    <key>password</key>
                    <value>123456</value>
                </systemProperty>
            </systemProperties>
        </configuration>
        <dependencies>
            <dependency>
                <groupId>commons-lang</groupId>
                <artifactId>commons-lang</artifactId>
                <version>2.6</version>
                <type>jar</type>
            </dependency>
        </dependencies>
    
    </plugin>
    
    

    上面这段配置是我们启动一个Java程序。同样版本号3.0.0是可以省略的,如果省略,maven会自动去寻找合适的版本。
    在executions标签中我们可以添加多个execution用来执行多个任务。标签的解释如下:

    id

    表示任务的唯一标识,可以自定义

    goals

    goal可取值:exec/java/help

    phase

    maven的生命周期段

    configuration

    详细的配置

  • includeProjectDependencies
    是否包含项目中的依赖
  • includePluginDependencies
    是否包含插件的依赖
  • executableDependency
    当执行Java程序时的具体依赖,这些依赖包需要添加后后面的dependencies标签中
  • mainClass
    java程序的执行类,这个类必须包含启动的main方法
  • arguments
    传递的参数
  • systemProperties
    执行Java程序时可以设置的系统变量
  • dependencies
    运行时的依赖包
  • 我们可以使用Java打包命令来执行上面配置:

    mvn clean -DskipTests package
    

    exec:exec的使用

    除了执行Java程序,exec-maven-plugin还可以执行其他系统命令,配置如下:

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.1.1</version>
        <executions>
            <execution>
                <id>exec-npm-run-build</id>
                <phase>test</phase>
                <goals>
                    <goal>exec</goal>
                </goals>
                <configuration>
                    <executable>ls</executable>
                    <arguments>
                        <argument>-alh</argument>
                    </arguments>
                    <workingDirectory>${basedir}</workingDirectory>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    上面的配置,我们执行了一个Linux下的命令。
    具体配置如下:

  • executable
    表示执行的命令,比如上面的java表示要执行java这个命令,当然这里可以时任意的控制台命令
  • workingDirectory
    当前命令执行时的工作目录
  • arguments
    当前命令执行时传递的参数
  • 这里需要注意:executable标签里面的命令必须要在系统中能找到,也就是环境变量中要配置相关的命令,这个与本地的操作系统有关系,比如在Linux系统下的命令,大部分在Windows下是无法运行的

    exec其实也能完成exec:java的功能,和在控制台使用Java命令来启动Java程序的配置一样,大家可以自行研究

    使用exec-maven-plugin来构建前端项目

    这里我给出一个实例,使用exec-maven-plugin插件来构建Vue项目

    首先我们准备一个Vue项目,然后在项目的目录下加入pom.xml文件,
    项目结构如下:

    pom.xml文件如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
       <modelVersion>4.0.0</modelVersion>
        <groupId>com.test</groupId>
        <artifactId>test-vue</artifactId>
        <version>1.0.0</version>
        <name>test-vue</name>
        <description>test-vue</description>
        <build>
            <finalName>test-vue</finalName>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>exec-npm-run-install</id>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>exec</goal>
                            </goals>
                            <configuration>
                                <executable>yarn</executable>
                                <arguments>
                                    <argument>install</argument>
                                </arguments>
                                <workingDirectory>${basedir}</workingDirectory>
                            </configuration>
                        </execution>
                        <execution>
                            <id>exec-npm-run-build</id>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>exec</goal>
                            </goals>
                            <configuration>
                                <executable>yarn</executable>
                                <arguments>
                                    <argument>run</argument>
                                    <argument>build</argument>
                                </arguments>
                                <workingDirectory>${basedir}</workingDirectory>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </project>
    
    

    这里我们添加了两个execution
    第一步:执行yarn install
    第二步:执行yarn build
    通过maven我们可以将两步一次性操作
    这样我们直接在项目目录下执行maven打包命令:

    mvn clean -DskipTests package
    

    执行完成后,项目目录下就会生成dist文件夹,说明打包完成。我们在进行前后端分离开发的时候,就可以和我们的后台项目一起来编译。在后续的章节中我会给大家继续介绍。

    直接用mvn命令来使用exec-maven-plugin插件

    除了上面的pom.xml来配置插件使用外,我们还可以直接使用mvn命令来使用exec-maven-plugin插件,比如我要要执行exec:java的golas,可以使用下面命令:

    mvn exec:exec -Dexec.executable="java" [...] -Dexec.args="%classpath"
    

    上面的classpath需要替换成系统的,同样,可以将java换成exec

    这种方式不是很常用,因为在使用过程中没有pom.xml方式灵活。

    二、maven-antrun-plugin使用

    在早期的Java程序编译中,用的最多的是使用ant来构建项目,关于ant的详细使用大家可以点击官网查看,ant目前我们已经很少使用了,基本上被maven替代,但是ant中有些工具还是很方便,所以在maven中我们依然可以来使用,下面我介绍几个在ant中常用的工具如何在maven中使用。
    首先在pom.xml中加入配置:

    <plugin>
      <artifactId>maven-antrun-plugin</artifactId>
      <version>3.1.0</version>
      <executions>
        <execution>
          <phase>test</phase>
          <configuration>
            <target>
    
              <!--
                Place any Ant task here. You can add anything
                you can add between <target> and </target> in a
                build.xml.
              -->
    
            </target>
          </configuration>
          <goals>
            <goal>run</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
    

    在上面的配置中,我们就可在target标签里面加入ant的任务来执行。

    echo打印功能

    加入下面配置:

     <target>
        <echo>this is ant 1</echo>
        <echo>this is ant 2</echo>
        <echo>this is ant 3</echo>
        <property name="p1">4</property>
        <echo message="p1 is: ${p1}"/>
    </target>
    

    然后运行:

    mvn clean -DskipTests package
    

    查看控制台输出:

    [INFO] --- maven-antrun-plugin:3.1.0:run (default) @ test-mvn-pkg-exe-springboot ---
    [INFO] Executing tasks
    [WARNING]      [echo] this is ant 1
    [WARNING]      [echo] this is ant 2
    [WARNING]      [echo] this is ant 3
    [WARNING]      [echo] p1 is: 4
    [INFO] Executed tasks
    
    

    拷贝文件

    加入配置:

    <copy todir="${project.build.directory}" overwrite="true">
       <fileset file="${basedir}/assembly/package.xml"/>
    </copy>
    

    这里会把项目目录下assembly目录下的package.xml文件拷贝到target目录下
    其中:

  • todir
    拷贝的目的地
  • overwrite
    是否覆盖已有的文件,默认为false
  • fileset
    单个文件,如果有多个可以添加多个fileset
    单文件拷贝还有一种配置,可以进行重命名:
  • <copy file="${basedir}/assembly/package.xml" tofile="${project.build.directory}/package.xml" overwrite="true"/>
    

    拷贝文件夹

    配置如下:

    <copy todir="${project.build.directory}/assembly" overwrite="true">
       <fileset dir="${basedir}/assembly"/>
    </copy>
    
  • todir
    拷贝的目的地
  • overwrite
    是否覆盖
  • dir
    源文件夹
  • 拷贝文件(文件夹)的操作还可以使用excludes配合正则来使用,大家可以参照ant官网

    ftp/scp/sshexec

    ant还能通过远程操作命令,来执行不同的任务,具体命令的使用大家可以参照ant的官网
    下面的配置是上传ftp的一个例子:

     <ftp action="send" server="192.168.101.110" remotedir="/home/" userid="root" password="123456" depends="yes" verbose="yes">
        <fileset dir="${project.build.directory}">
            <include name="*.jar" />
        </fileset>
    </ftp>
    

    总结

    exec-maven-plugin和maven-antrun-plugin都是用来执行额外脚本的插件,我们在编译大项目时,这两个插件的使用还是很频繁,他们各自都有很强大的功能。

    作者:流年五十前

    物联沃分享整理
    物联沃-IOTWORD物联网 » maven插件exec-maven-plugin、maven-antrun-plugin使用详解

    发表回复