美文网首页
Maven执行主类与exec plugin

Maven执行主类与exec plugin

作者: qingshuiting | 来源:发表于2019-03-26 10:54 被阅读0次

Maven执行主类

通常我们在使用Maven管理项目的时候,都希望直接使用Maven插件的方法来管理我们的代码,以及运行。
最常用的一个功能就是运行一个主类:

  1. 希望测试一个主类的输出,但是这个主类又依赖的很多的第三方的jar包,直接使用java 添加第三方的jar到对应的classPath下非常不方便。

  2. 通过写脚本的方式,运行我们package好的项目,需要在脚本中运行java的主类。

使用Maven运行主类

最简单的运行主类的方法就是:直接使用java 命令

Usage: java [-options] class [args...]
           (to execute a class)
   or  java [-options] -jar jarfile [args...]
           (to execute a jar file)

有一些重要的options:System变量,classPath等内容

这也是最直接的方法来运行一个主类。其中 java -jar jarfile的方式需要在打包jar文件的时候指定主类。

那么使用Maven运行主类的方法,本质上也是通过上面的操作运行的,只是Maven提供了一种插件,可以帮我们把一些命令/options等配置省略掉。

选择使用的插件就是:

exec-maven-plugin

这个插件使用比较方便,goal只有三个java,exec,help

如果想了解每一个goal的作用,可以使用 mvn pluginPrefix:help命令来查看某个插件的所有帮助,比如:

mvn exec:help

exec除了help以外有两个goal:javaexec。这两个gola在使用上有比较大的不同,具体的可以查阅Exec Usage

总体来说: exec:java 更加的方便,因为其指定好了使用java运行,只需要填写即可,可以认为是一种直接执行java命令的高级封装; exec:exec是一种表低级的接口,仅仅表示一种执行,所以具体执行的是哪个命令,命令后边的参数是什么内容都是不确定的。后边将具体解释下如何在pom中使用exec:exec

方法1

  1. 编译项目:maven compile

  2. 运行主类:

mvn exec:java -Dexec.mainClass="xxx.xxx.xxx" [-Dexec.args="arg1 arg2 arg3"]

方法2

在项目的pom.xml文件中指定运行的phase和goal

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.6.0</version>
        <executions>
          <execution>
            // 可以添加对应的执行阶段
            <phase>test</phase>
            ...
            <goals>
                // 指定来的 goal为java,表示运行java程序
              <goal>java</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
            // 指定了运行的main class
          <mainClass>com.example.Main</mainClass>
          // 执行运行 main class的参数
          // 其实就是传入main方法的String[]
          <arguments>
            <argument>argument1</argument>
            ...
          </arguments>
          // 运行java的程序的系统参数
          <systemProperties>
            <systemProperty>
              <key>myproperty</key>
              <value>myvalue</value>
            </systemProperty>
            ...
          </systemProperties>
        </configuration>
      </plugin>
    </plugins>
  </build>
   ...
</project>

方法3

使用指定的profile

<profiles>
 <profile>
  <id>my-exec</id>
    ....
    <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.6.0</version>
        <executions>
          <execution>
            // 可以添加对应的执行阶段
            // 也可以不添加执行阶段,使用mvn exec:exec运行
            <phase>test</phase>
            ...
            <goals>
                // 指定来的 goal为java,表示运行java程序
              <goal>java</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
            // 指定了运行的main class
          <mainClass>com.example.Main</mainClass>
          // 执行运行 main class的参数
          // 其实就是传入main方法的String[]
          <arguments>
            <argument>argument1</argument>
            ...
          </arguments>
          // 运行java的程序的系统参数
          <systemProperties>
            <systemProperty>
              <key>myproperty</key>
              <value>myvalue</value>
            </systemProperty>
            ...
          </systemProperties>
        </configuration>
      </plugin>
    </plugins>
  </build>
    ....
 </profile>
</profiles>

需要使用 mvn -Pmy-exec test

方法4

方法4实际上是使用了 exec:exec的方式运行,只是指定了运行方法为java

命令行的方式:

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

在exec.args中补充其他内容,主类,参数等等内容

配置pom.xml的方式

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.6.0</version>
        <executions>
          <execution>
            // 可以添加对应的执行阶段
            <phase>test</phase>
            ...
            <goals>
                // 指定来的 goal为java,表示运行java程序
              <goal>exec</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
            // 执行运行使用java命令
           <executable>java</executable>
           <arguments>
           <argument>-Dsysteproperty=xxx</argument>
            // 执行 java命令后边的参数
            // 其实就是把参数组合成 java命令运行程序的方式
            <argument>-classpath</argument>
            <!-- automatically creates the classpath using all project dependencies,
                 also adding the project build directory -->
            <classpath/>
            // 参数方式的主类
            <argument>com.example.Main</argument>
            <argument>arg1</argument>
            <argument>arg2</argument>
            </arguments>            
        </configuration>
      </plugin>
    </plugins>
  </build>
   ...
</project>

使用 exec:exec 需要注意:

Special treatment of some command line arguments and configuration parameters facilitate the running of Java programs in external processes.

Note: With the exec goal, there are some confusion caused by the almost duplication of functionality between the arguments and commandlineArgs configuration parameters.

If commandlineArgs is specified, it will be used as is, except for replacing %classpath and %modulepath with their matching path using dependencies
Otherwise if the property exec.args is specified, it will be used
Otherwise the list of argument, classpath and modulepath will be parsed and used

exec.args和arguments的区别。exec.args表示的就是在命令行中输入的参数,arguments表示的是在pom.xml中定一个的参数。

相关文章

网友评论

      本文标题:Maven执行主类与exec plugin

      本文链接:https://www.haomeiwen.com/subject/bqtgvqtx.html