如何以编程方式从Restful methode或java类运行gradle构建任务

Duc*_*sus -1 java gradle

请问,有人知道如何以编程方式从Restful方法或java类运行gradle构建任务吗?谢谢.

Sta*_*lav 5

从你的问题中你不清楚你想要实现什么,但在我看来,你正在寻找像Gradle Tooling API这样的东西.它允许:

  • 查询构建的详细信息,包括项目层次结构和项目依赖项,外部依赖项(包括源和Javadoc jar),源项目和每个项目的任务.
  • 执行构建并侦听stdout和stderr日志记录和进度消息(例如,在命令行上运行时,"状态栏"中显示的消息).
  • 执行特定的测试类或测试方法.
  • 在构建执行时接收有趣的事件,例如项目配置,任务执行或测试执行.
  • 取消正在运行的构建.
  • 将多个单独的Gradle构建组合到一个复合构建中.
  • Tooling API可以下载并安装适当的Gradle版本,类似于包装器.
  • 实现是轻量级的,只有少量的依赖项.它也是一个表现良好的库,并不对您的类加载器结构或日志记录配置做出任何假设.
  • 这使API易于嵌入您的应用程序中.

您可以在samples/toolingApiGradle分发目录中找到一些示例.

至于你的任务,似乎你必须创建一个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();
 }
Run Code Online (Sandbox Code Playgroud)