mvn exec命令使用

mvn exec命令可以执行项目中的main函数。
1、首先需要编译java工程,生成class文件:mvn compile
2、不存在参数的情况下:mvn exec:java -Dexec.mainClass="com.testDemo.HelloWorld"
3、存在参数:mvn exec:java -Dexec.mainClass="com.testDemo.HelloWorld" -Dexec.args="arg0 arg1 arg2"
4、指定运行时库:mvn exec:java -Dexec.mainClass="com.testDemo.HelloWorld" -Dexec.classpathScope=runtime

这里面延伸一下,使用Maven运行Java main的3种方式,第一种就是刚才上面说到的,

第二种、在pom.xml中指定某个阶段执行

<build>  
 <plugins>  
  <plugin>  
   <groupId>org.codehaus.mojo</groupId>  
   <artifactId>exec-maven-plugin</artifactId>  
   <version>1.1.1</version>  
   <executions>  
    <execution>  
     <phase>test</phase>  
     <goals>  
      <goal>java</goal>  
     </goals>  
     <configuration>  
      <mainClass>com.vineetmanohar.module.CodeGenerator</mainClass>  
      <arguments>  
       <argument>arg0</argument>  
       <argument>arg1</argument>  
      </arguments>  
     </configuration>  
    </execution>  
   </executions>  
  </plugin>  
 </plugins>  
</build>  

将CodeGenerator.main()方法的执行绑定到maven的 test 阶段,通过下面的命令可以执行main方法:

mvn test

第三种、在pom.xml中指定某个配置来执行
[html] view plain copy 在CODE上查看代码片派生到我的代码片

<profiles>  
 <profile>  
  <id>code-generator</id>  
  <build>  
   <plugins>  
    <plugin>  
     <groupId>org.codehaus.mojo</groupId>  
     <artifactId>exec-maven-plugin</artifactId>  
     <version>1.1.1</version>  
     <executions>  
      <execution>  
       <phase>test</phase>  
       <goals>  
        <goal>java</goal>  
       </goals>  
       <configuration>  
        <mainClass>com.vineetmanohar.module.CodeGenerator</mainClass>  
        <arguments>  
         <argument>arg0</argument>  
         <argument>arg1</argument>  
        </arguments>  
       </configuration>  
      </execution>  
     </executions>  
    </plugin>  
   </plugins>  
  </build>  
 </profile>  
</profiles>  

将2中的配置用标签包裹后就能通过指定该配置文件来执行main方法,如下:
[plain] view plain copy 在CODE上查看代码片派生到我的代码片

mvn test -Pcode-generator 

注:通过以下命令可以获取mvn exec的其他配置参数说明。
[plain] view plain copy 在CODE上查看代码片派生到我的代码片

mvn exec:help -Ddetail=true -Dgoal=java  

其他两种从其他地方转过来的(http://blog.csdn.net/qbg19881206/article/details/19850857?utm_source=tuicool&utm_medium=referral)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lbxoqy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值