如何从java代码运行gradle任务?

时间:2021-04-14 02:26:33

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 ?

我需要从java方法将gradle eclipse任务运行到外部gradle项目,是否可以使用Gradle Tooling API执行此操作?

1 个解决方案

#1


1  

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论坛提供了一个以编程方式执行此操作的好例子,但由于它忽略了单个gradle包装器的项目,因此无法保证构建的顺利执行甚至破坏您的应用程序。有关更多信息,请务必依赖于此处和此处阅读的gradle包装器。

Using the Gradle wrapper

使用Gradle包装器

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:

建议的方法是运行exec并在将任务作为参数传递时调用项目包装器。此示例调用当前项目包装器并将jar作为参数传递:

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();
        }
    }
}

Using the Greadle Tooling API

使用Greadle工具API

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:

要在外部项目上使用Gradle工具api,只需定义GradleConnector对象的ProjectProirectory属性即可。要在BuildLauncher对象上运行任务调用run()。下面的例子说明了基本原则:

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);
    }
}

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.

这种方法的缺点是在执行任务时没有意识到gradle。如果您在自定义任务(如新文件(“somefile”)中调用任何文件创建或修改方法,则会引发异常。

#1


1  

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论坛提供了一个以编程方式执行此操作的好例子,但由于它忽略了单个gradle包装器的项目,因此无法保证构建的顺利执行甚至破坏您的应用程序。有关更多信息,请务必依赖于此处和此处阅读的gradle包装器。

Using the Gradle wrapper

使用Gradle包装器

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:

建议的方法是运行exec并在将任务作为参数传递时调用项目包装器。此示例调用当前项目包装器并将jar作为参数传递:

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();
        }
    }
}

Using the Greadle Tooling API

使用Greadle工具API

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:

要在外部项目上使用Gradle工具api,只需定义GradleConnector对象的ProjectProirectory属性即可。要在BuildLauncher对象上运行任务调用run()。下面的例子说明了基本原则:

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);
    }
}

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.

这种方法的缺点是在执行任务时没有意识到gradle。如果您在自定义任务(如新文件(“somefile”)中调用任何文件创建或修改方法,则会引发异常。