Depending on mixed Groovy and Java library project (Gradle 4.10)

Hello, hello!

While modernizing one of my projects I found a curious issue. If you use both java-library plugin (to get the api configuration), and the groovy plugin (to compile some groovy code for your library), things work fine for producing a jar file and publishing to a repo.

What doesn’t work is if you try to consume this project within the same build. The best workaround I’ve found is to use compile instead of api.

Is it intentional? Am I missing something? What is the way forward?

Here is a repro:

settings.gradle

include 'subproject

build.gradle

plugins {
    id 'groovy' 
    id 'java-library'
}

dependencies {
    implementation localGroovy()
}

def j = file('src/main/java/Foo.java')
def g = file('src/main/groovy/Bar.groovy')
def d = file('subproject/src/main/java/Using.java')
[j, g, d].each { assert it.parentFile.directory || it.parentFile.mkdirs() }
j.text = 'class Foo {}'
g.text = 'class Bar {}'
d.text = 'class Using { Foo foo; Bar bar; }'

assemble{
    dependsOn('clean')
    doLast {
        assert fileTree('build/classes').asCollection().size()==2
    }
}

project(':subproject') {
    apply plugin: 'java'
    dependencies.implementation(rootProject)
    
    compileJava {
        doFirst {
            println classpath.asCollection().join('\n')
        }
    }    
}

The produced Groovy classes need to be added to the outgoing apiElements configuration.
The following does the trick with your example:

configurations {
    apiElements.outgoing.variants {
        classes {
            artifact file: compileGroovy.destinationDir, builtBy: compileGroovy
        }
    }
}

@CedricChampeau might have more to add on the topic though.

2 Likes

Thank you! As pointed on the Slack chat, this is also one of the documented known issues.

I’ve applied the workaround, is there any Github issue tracking this?

And thanks for pointing to the documentation on this! I failed to find it.