maven使用exec插件運行java main方法,如下是3種不一樣的操做方式。java
1、從命令行運行shell
一、運行前先編譯代碼,exec:java不會自動編譯代碼,你須要手動執行mvn compile來完成編譯。maven
mvn compile
二、編譯完成後,執行exec運行main方法。
ui
不須要傳遞參數:spa
mvn exec:java -Dexec.mainClass="com.vineetmanohar.module.Main"
須要傳遞參數:插件
mvn exec:java -Dexec.mainClass="com.vineetmanohar.module.Main" -Dexec.args="arg0 arg1 arg2"
指定對classpath的運行時依賴:命令行
mvn exec:java -Dexec.mainClass="com.vineetmanohar.module.Main" -Dexec.classpathScope=runtime
2、在pom.xml中指定某個階段執行code
<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方法:orm
mvn test
3、在pom.xml中指定某個配置來執行xml
<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中的配置用<profile>標籤包裹後就能經過指定該配置文件來執行main方法,以下:
mvn test -Pcode-generator
注:經過如下命令能夠獲取mvn exec的其餘配置參數說明。
mvn exec:help -Ddetail=true -Dgoal=java
英文地址:http://www.vineetmanohar.com/2009/11/3-ways-to-run-java-main-from-maven/