How to run a gradle task from a java code?

本小妞迷上赌 提交于 2020-05-15 10:27:00

问题


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 ?


回答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.

Using the Gradle wrapper

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

Using the Greadle Tooling 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:

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.



来源:https://stackoverflow.com/questions/49876189/how-to-run-a-gradle-task-from-a-java-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!