How to run a gradle task from a java code?
Asked Answered
D

1

11

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 ?

Dyestuff answered 17/4, 2018 at 10:51 Comment(4)
I am not sure what do you want to achieve by doing this. Can you give an example for this?Suttee
Possible duplicate of Call a gradle task from java classRetarder
In fact, from my Gradle project I manipulate other gradle projects; and I want to be able to run the eclipse task (the gradle eclipse plugin) on those Gradle projects so that those projects can be opened from eclipse, I hope you understand my problem.Dyestuff
@Retarder but there is no suggested solution.Dyestuff
R
16

The Gradle forum gives a nice example for doing this programmatically but since it disregards the projects individual gradle wrapper, it can't guarantee 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 BLANK = " ";
    private static String GRADLE_TASK = "jar";

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

Using the Gradle 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 demostrates 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.

Retarder answered 17/4, 2018 at 11:44 Comment(1)
"The downside of this approach is the location unawareness of gradle when executing a task." why so?Brigette

© 2022 - 2025 — McMap. All rights reserved.