How to execute a gradle task from Java code?

Dear Gradle experts,

I have a question regarding execution of a Gradle tasks (defined in build.gradle) from java code. In specific implementation - there is a Eclipse plugin with a java class that uses Gradle API. For example,

ProjectConnection connection = GradleConnector.newConnector()
             .forProjectDirectory(new File("C:\GradleAPITest"))
             .connect();
          try {
                            BuildLauncher build = connection.newBuild();
              build.forTasks("prototype");
              build.setStandardOutput(System.out);
              build.run();
          } finally {
             connection.close();
          }

One of the problems - org.gradle.tooling package is not exported… Any ideas or suggestions on this mater?

Regards, Roman

I don’t understand the question, as above code already shows how to execute a task. What exactly do you mean by “org.gradle.tooling package is not exported”, and what exact problem does it cause?

Dear Peter, thank you for the quick reply.

I get this error:

Exception in thread “main” java.lang.NoClassDefFoundError: org/gradle/internal/service/ServiceRegistry

at org.gradle.tooling.GradleConnector.newConnector(GradleConnector.java:64)

at gradle.tooling.api.RunGradle.main(RunGradle.java:16) Caused by: java.lang.ClassNotFoundException: org.gradle.internal.service.ServiceRegistry

at java.net.URLClassLoader$1.run(Unknown Source)

at java.net.URLClassLoader$1.run(Unknown Source)

at java.security.AccessController.doPrivileged(Native Method)

at java.net.URLClassLoader.findClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

… 2 more

Which version of the tooling API are you using? What’s the exact name of the Jar? What kind of app is are you writing? If it’s an Eclipse plugin, it might be an OSGi related problem.

I am using Eclipse 3.7.2 with Gradle Tooling API 3.1.0. Name of the jar “gradle-tooling-api-1.3.jar”. About the project: it should be an Eclipse plugin for software product line development tool (EASy Producer). But lets say for the first step lets just execute Gradle task from a plain Java project. No ODGi involved in this case. Result is the same: java.lang.NoClassDefFoundError

‘gradle-tooling-api-1.3’ has a number of dependencies (I’ve posted the POM below). I recommend to use 1.4 instead, which only has a single dependency, namely ‘org.slf4j:slf4j-api:1.7.2’.

gradle-tooling-api-1.3.pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.gradle</groupId>
  <artifactId>gradle-tooling-api</artifactId>
  <version>1.3</version>
  <dependencies>
    <dependency>
      <groupId>org.gradle</groupId>
      <artifactId>gradle-wrapper</artifactId>
      <version>1.3</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>org.gradle</groupId>
      <artifactId>gradle-messaging</artifactId>
      <version>1.3</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>org.gradle</groupId>
      <artifactId>gradle-core</artifactId>
      <version>1.3</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>org.gradle</groupId>
      <artifactId>gradle-base-services</artifactId>
      <version>1.3</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.6.6</version>
      <scope>runtime</scope>
    </dependency>
  </dependencies>
</project>

I have installed Gradle 1.4 and now using gradle-tooling-api-1.4.jar Still the same result: Exception in thread “main” java.lang.NoClassDefFoundError: org/gradle/internal/service/ServiceRegistry

at org.gradle.tooling.GradleConnector.newConnector(GradleConnector.java:64)

at gradle.tooling.api.RunGradle.main(RunGradle.java:18) Caused by: java.lang.ClassNotFoundException: org.gradle.internal.service.ServiceRegistry

at java.net.URLClassLoader$1.run(Unknown Source)

at java.net.URLClassLoader$1.run(Unknown Source)

at java.security.AccessController.doPrivileged(Native Method)

at java.net.URLClassLoader.findClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

… 2 more

Check out ‘samples/toolingApi/runBuild’ in the full Gradle 1.4 distribution. It works fine (try ‘gradle run’), and only puts the following Jars on the app’s class path:

gradle-tooling-api-1.4.jar
slf4j-simple-1.7.2.jar
slf4j-api-1.7.2.jar

Tried it. It does no t work, sorry… Same java.lang.NoClassDefFoundError. slf4j-simple-1.7.2.jar was missing in gradle-1.4\lib so I added it, but no changes overall.

What doesn’t work? The ‘toolingApi/runBuild’ sample, or your own code?

Both of them.

It’s hard to say why the sample wouldn’t work for you. Among other things, it’s tested regularly on all our CI platforms. Given the information I have, I can’t spot a problem on Gradle’s side.