How to ignore projects from Gradle dependency transformation

Viewed 13

I'm following the sample code in Gradle's Transform dependency artifacts on resolution page.

def artifactType = Attribute.of('artifactType', String)
def minified = Attribute.of('minified', Boolean)
dependencies {
    attributesSchema {
        attribute(minified)                      
    }
    artifactTypes.getByName("jar") {
        attributes.attribute(minified, false)    
    }
}

configurations.all {
    afterEvaluate {
        if (canBeResolved) {
            attributes.attribute(minified, true) 
        }
    }
}

dependencies {
    registerTransform(Minify) {
        from.attribute(minified, false).attribute(artifactType, "jar")
        to.attribute(minified, true).attribute(artifactType, "jar")
    }
}

dependencies {                                 
    implementation('com.google.guava:guava:27.1-jre')
    implementation(project(':my-sub-project'))
}

When I run gradlew :my-app:test I get:

FAILURE: Build failed with an exception.

* What went wrong:
Could not determine the dependencies of task ':my-app:test'.
> Could not resolve all task dependencies for configuration ':my-app:testRuntimeClasspath'.
   > Could not resolve project :my-sub-project.
     Required by:
         project :my-app
      > The consumer was configured to find a runtime of a library compatible with Java 17,
        packaged as a jar, preferably optimized for standard JVMs, and its dependencies 
        declared externally, as well as attribute 'minimized' with value 'true'. 
        However we cannot choose between the following variants of project :my-sub-project:
          - archives
          - checkstyle
          - default
          - jacocoAgent
          - jacocoAnt
          - runtimeElements
        All of them match the consumer attributes:
        ...
          - Variant 'runtimeElements' capability my_project:my-sub-project:unspecified declares a runtime of a library compatible with Java 17, packaged as a jar, and its dependencies declared externally:
              - Unmatched attributes:
                  - Doesn't say anything about its target Java environment (preferred optimized for standard JVMs)
                  - Doesn't say anything about minimized (required 'true')

How can I exclude our internal projects (and maybe even libraries within our group) from the requirement of being minimized?

1 Answers

I've managed to fix the problem with allprojects:

configurations.all {
    // Skip internal projects
    allprojects {
        attributes.attribute(minimized, true)
    }
    // Request minimized=true on all resolvable configurations
    afterEvaluate {
        if (canBeResolved) {
            attributes.attribute(minimized, true)
        }
    }
}
Related