2016-11-15 17 views

回答

4

從你的問題中不太清楚你想達到什麼目的,但在我看來,你正在尋找像Gradle Tooling API這樣的東西。它允許:

  • 查詢的構建的細節,包括項目層次和項目的依賴,外部依賴(包括源和 的Javadoc罐),源目錄和每個項目的任務。
  • 執行構建並偵聽stdout和stderr日誌記錄和進度消息(例如,當您在命令行上運行 時,狀態欄中顯示的消息)。
  • 執行特定的測試類或測試方法。
  • 在構建執行時接收有趣的事件,例如項目配置,任務執行或測試執行。
  • 取消正在運行的版本。
  • 將多個獨立的Gradle構建合併爲一個複合構建。
  • 工具API可以下載並安裝適當的Gradle版本,類似於包裝。
  • 該實現是輕量級的,只有少量的依賴關係。它也是一個行爲良好的庫,並且不會對您的類加載器結構或日誌記錄配置進行假設。
  • 這使得API很容易嵌入到您的應用程序中。

有一些例子,你可以samples/toolingApi目錄搖籃分佈中找到。

至於你的任務,看來,你必須創建GradleConnector一個實例通過它的forProjectDirectory(File projectDir)方法,然後得到它的ProjectConnection(通過connect())和BuildLauncher(通過newBuild())。最後通過BuildLauncher的實例,你可以運行你需要的任何任務。下面是它的一個例子javadocs

try { 
    BuildLauncher build = connection.newBuild(); 

    //select tasks to run: 
    build.forTasks("clean", "test"); 

    //include some build arguments: 
    build.withArguments("--no-search-upward", "-i", "--project-dir", "someProjectDir"); 

    //configure the standard input: 
    build.setStandardInput(new ByteArrayInputStream("consume this!".getBytes())); 

    //in case you want the build to use java different than default: 
    build.setJavaHome(new File("/path/to/java")); 

    //if your build needs crazy amounts of memory: 
    build.setJvmArguments("-Xmx2048m", "-XX:MaxPermSize=512m"); 

    //if you want to listen to the progress events: 
    ProgressListener listener = null; // use your implementation 
    build.addProgressListener(listener); 

    //kick the build off: 
    build.run(); 
} finally { 
    connection.close(); 
} 
相關問題