Running buildSrc tasks from root project


(Andrew N) #1

I’m writing tests for some build code and trying to get coverage. When I add jacoco to buildSrc/build.gradle, it works fine when I run gradle from inside the buildSrc directory:

project/buildSrc$ ../gradlew jacocoTestReport
:buildSrc:compileJava UP-TO-DATE
:buildSrc:compileGroovy NO-SOURCE
:buildSrc:processResources NO-SOURCE
:buildSrc:classes UP-TO-DATE
:buildSrc:jar UP-TO-DATE
:buildSrc:assemble UP-TO-DATE
:buildSrc:compileTestJava UP-TO-DATE
:buildSrc:compileTestGroovy NO-SOURCE
:buildSrc:processTestResources NO-SOURCE
:buildSrc:testClasses UP-TO-DATE
:buildSrc:test UP-TO-DATE
:buildSrc:check UP-TO-DATE
:buildSrc:build UP-TO-DATE
:compileJava
:processResources NO-SOURCE
:classes
:jacocoTestReport

BUILD SUCCESSFUL

But when I try to do the same from a different directory, it’s an error:

project$ ./gradlew jacocoTestReport
:buildSrc:clean
...
:buildSrc:check
:buildSrc:build

FAILURE: Build failed with an exception.

* What went wrong:
Task 'jacocoTestReport' not found in root project 'project'.

Even if I try to specify the buildSrc subproject:

project$ ./gradlew :buildSrc:jacocoTestReport
:buildSrc:compileJava UP-TO-DATE
:buildSrc:compileGroovy NO-SOURCE
:buildSrc:processResources NO-SOURCE
:buildSrc:classes UP-TO-DATE
:buildSrc:jar UP-TO-DATE
:buildSrc:assemble UP-TO-DATE
:buildSrc:compileTestJava UP-TO-DATE
:buildSrc:compileTestGroovy NO-SOURCE
:buildSrc:processTestResources NO-SOURCE
:buildSrc:testClasses UP-TO-DATE
:buildSrc:test UP-TO-DATE
:buildSrc:check UP-TO-DATE
:buildSrc:build UP-TO-DATE

FAILURE: Build failed with an exception.

* What went wrong:
Project 'buildSrc' not found in root project 'project'.

Is there any way to run buildSrc tasks from outside the buildSrc directory, or get access the programmatically from a build script outside buildSrc?


Here are my sample files:

buildSrc/build.gradle

apply plugin: 'java'
apply plugin: 'jacoco'

repositories {
    mavenCentral()
}

dependencies {
    testCompile gradleTestKit()
    testCompile "junit:junit:4.12"
}

buildSrc/src/main/java/Example.java:

public class Example {
    public int getFoo() {
        return 2;
    }

    public int getBar() {
        return 3;
    }
}

buildSrc/src/test/java/ExampleTest.java:

import org.junit.Test;
import static org.junit.Assert.*;

public class ExampleTest {
    @Test
    public void testFoo() {
         assertEquals(2, new Example().getFoo());
    }
}