我正在創建簡單的Java類,我想通過使用gradle來創建開箱即用的啓動器。如何將命令行參數傳遞給gradle中的主類?
所以我希望能夠通過gradle這個運行Java程序:
gradlew clean run These are my command line arguments
其中These are my command line arguments
直接傳遞到我的public static void main(String... args)
方法。
我正在使用apply plugin: "application"
它給我run
任務。但是,當我運行這個「是」我有:
* What went wrong: Task 'These' not found in root project 'question1'. Some candidates are: 'test'.
這種方法可行,但問題在於args *總是*必須存在。如果你做了其他任何事情(例如'gradle tasks'),那麼操作失敗時會出現'NullPointerException',並且錯誤指向'build.gradle'中的上述'args'行。你知道一種避免這種情況的方法嗎? –
並回答我自己的問題:'if(project.hasProperty('runArgs'){args findProperty('runArgs')}'將執行此操作。 –