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