2016-11-15 15 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(); 
} 
相关问题