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)