使用maven運行Java Main的三種方法解析
maven使用exec插件運行java main方法,以下是3種不同的操作方式。
一、從命令行運行
1、運行前先編譯代碼,exec:java不會自動編譯代碼,你需要手動執(zhí)行mvn compile來完成編譯。
mvn compile
2、編譯完成后,執(zhí)行exec運行main方法。
不需要傳遞參數(shù):
mvn exec:java -Dexec.mainClass="com.vineetmanohar.module.Main"
需要傳遞參數(shù):
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
二、在pom.xml中指定某個階段執(zhí)行
<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()方法的執(zhí)行綁定到maven的 test 階段,通過下面的命令可以執(zhí)行main方法:
mvn test
三、在pom.xml中指定某個配置來執(zhí)行
<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>標(biāo)簽包裹后就能通過指定該配置文件來執(zhí)行main方法,如下:
mvn test -Pcode-generator
注:通過以下命令可以獲取mvn exec的其他配置參數(shù)說明。
mvn exec:help -Ddetail=true -Dgoal=java
總結(jié)
以上就是本文關(guān)于使用maven運行Java Main的三種操作方式解析的全部內(nèi)容,如有不足之處,歡迎留言指出。感興趣的朋友可以繼續(xù)參閱:Java利用future及時獲取多線程運行結(jié)果、淺談Java中static和非static的區(qū)別、Java多線程ForkJoinPool實例詳解等,希望對大家有所幫助。感謝朋友們對本站的支持!
相關(guān)文章
java自動生成編號的實現(xiàn)(格式:yyMM+四位流水號)
這篇文章主要介紹了java自動生成編號的實現(xiàn)(格式:yyMM+四位流水號),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
SpringBoot下使用MyBatis-Puls代碼生成器的方法
這篇文章主要介紹了SpringBoot下使用MyBatis-Puls代碼生成器的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10
SpringBoot下RabbitMq實現(xiàn)定時任務(wù)
這篇文章主要為大家詳細(xì)介紹了SpringBoot下RabbitMq實現(xiàn)定時任務(wù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-11-11
子類繼承父類時構(gòu)造函數(shù)相關(guān)問題解析
這篇文章主要介紹了子類繼承父類時構(gòu)造函數(shù)相關(guān)問題解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-11-11
SpringBoot實現(xiàn)郵件發(fā)送功能的姿勢分享
我們在日常開發(fā)中,經(jīng)常會碰到email郵件發(fā)送的場景,如發(fā)送驗證碼,向客戶發(fā)送郵件等等,這篇文章主要給大家介紹了關(guān)于SpringBoot實現(xiàn)郵件發(fā)送的相關(guān)資料,需要的朋友可以參考下2021-08-08
ReentrantReadWriteLock?讀寫鎖分析總結(jié)
這篇文章主要介紹了ReentrantReadWriteLock 讀寫鎖分析總結(jié),ReentranReadWriteLock中有兩把鎖,一把讀鎖,一把寫鎖,關(guān)于這兩把鎖的介紹,需要的小伙伴可以參考一下2022-05-05

