如何从Java代码运行gradle任务? [英] How to run a gradle task from a java code?

查看:535
本文介绍了如何从Java代码运行gradle任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过java方法将gradle eclipse任务运行到外部gradle项目,是否可以使用Gradle Tooling API来做到这一点?

I need to run the gradle eclipse task to an external gradle project from a java method, is it possible to do it using the Gradle Tooling API ?

推荐答案

Gradle论坛给出了一个很好的以编程方式进行此操作的示例,但由于它不考虑项目的各个gradle包装器,因此无法保证构建的顺利执行,甚至无法破坏您的应用程序.有关更多信息,为什么您始终应该依赖gradle包装器,请在此处阅读和此处.

The Gradle forum gives a nice example for doing this programmatically but since it disregards the projects individual gradle wrapper, it can't gurantee the smooth execution of your build and even break your application. For more information why you always should rely on the gradle wrapper read here and here.

使用Gradle包装器

推荐的方法是运行 exec ,然后在将任务作为参数传递时调用项目包装器.本示例调用当前项目包装器,并将jar作为参数传递:

The recommended approach is to run exec and call the projects wrapper while passing the task as a parameter. This example calls the current projects wrapper and passes jar as a parameter:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Main
{
    private static String PATH_TO_GRADLE_PROJECT = "./";
    private static String GRADLEW_EXECUTABLE = "gradlew.bat";
    private static String BALNK = " ";
    private static String GRADLE_TASK = "jar";

    public static void main(String[] args)
    {
        String command = PATH_TO_GRADLE_PROJECT + GRADLEW_EXECUTABLE + BALNK + GRADLE_TASK;
        try
        {
            Runtime.getRuntime().exec(command);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

使用Greadle Tooling API

要在外部项目上使用Gradle工具api,只需定义GradleConnector对象的属性forProjectDirectory.要运行任务,请在BuildLauncher对象上调用run().下面的示例演示了基本原理:

To use the Gradle tooling api on a external project, you simply have to define the property forProjectDirectory of your GradleConnectorobject. To run a task call run() on the BuildLauncher object. The example below demostrats the basic principle:

import org.gradle.tooling.BuildLauncher;
import org.gradle.tooling.GradleConnector;
import org.gradle.tooling.ProjectConnection;

import java.io.File;

public class ToolingAPI
{
    private static final String GRADLE_INSTALLATION = "C:\\Program Files\\Gradle";
    private static final String GRADLE_PROJECT_DIRECTORY = "path_to_root_of_a_gradle_project";
    private static final String GRADLE_TASK = "help";

    private GradleConnector connector;

    public ToolingAPI(String gradleInstallationDir, String projectDir)
    {
        connector = GradleConnector.newConnector();
        connector.useInstallation(new File(gradleInstallationDir));
        connector.forProjectDirectory(new File(projectDir));
    }

    public void executeTask(String... tasks)
    {
        ProjectConnection connection = connector.connect();
        BuildLauncher build = connection.newBuild();
        build.forTasks(tasks);

        build.run();
        connection.close();
    }

    public static void main(String[] args)
    {
        ToolingAPI toolingAPI = new ToolingAPI(GRADLE_INSTALLATION, GRADLE_PROJECT_DIRECTORY);
        toolingAPI.executeTask(GRADLE_TASK);
    }
}

这种方法的缺点是执行任务时不知​​道gradle的位置.如果您在自定义任务(如new File("somefile"))中调用任何文件创建或修改方法,则会引发异常.

The downside of this approach is the location unawareness of gradle when executing a task. In case you call any file creation or modification method in a custom task like new File("somefile") a exception will be raised.

这篇关于如何从Java代码运行gradle任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆