Skip to content

Commit 4a6913d

Browse files
authored
Fix requires scope calculation of 'requireAllDefinedDependencies' (#82)
Fixes #81
1 parent 05d754b commit 4a6913d

File tree

3 files changed

+59
-6
lines changed

3 files changed

+59
-6
lines changed

src/main/java/org/gradlex/javamodule/moduleinfo/ExtraJavaModuleInfoPlugin.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ private void registerTransform(String fileExtension, Project project, ExtraJavaM
184184
toStringMap(sourceSets.stream().flatMap(s -> filteredResolutionResult(configurations.getByName(s.getCompileClasspathConfigurationName()), componentsOfInterest(extension))))));
185185
p.getRuntimeClasspathDependencies().set(project.provider(() ->
186186
toStringMap(sourceSets.stream().flatMap(s -> filteredResolutionResult(configurations.getByName(s.getRuntimeClasspathConfigurationName()), componentsOfInterest(extension))))));
187+
p.getAnnotationProcessorClasspathDependencies().set(project.provider(() ->
188+
toStringMap(sourceSets.stream().flatMap(s -> filteredResolutionResult(configurations.getByName(s.getAnnotationProcessorConfigurationName()), componentsOfInterest(extension))))));
187189
});
188190
t.getFrom().attribute(artifactType, fileExtension).attribute(javaModule, false);
189191
t.getTo().attribute(artifactType, fileExtension).attribute(javaModule, true);

src/main/java/org/gradlex/javamodule/moduleinfo/ExtraJavaModuleInfoTransform.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ public interface Parameter extends TransformParameters {
9494
MapProperty<String, Set<String>> getCompileClasspathDependencies();
9595
@Input
9696
MapProperty<String, Set<String>> getRuntimeClasspathDependencies();
97+
@Input
98+
MapProperty<String, Set<String>> getAnnotationProcessorClasspathDependencies();
9799
}
98100

99101
@InputArtifact
@@ -320,8 +322,9 @@ private byte[] addModuleInfo(ModuleInfo moduleInfo, Map<String, List<String>> pr
320322
if (moduleInfo.requireAllDefinedDependencies) {
321323
Set<String> compileDependencies = getParameters().getCompileClasspathDependencies().get().get(moduleInfo.getIdentifier());
322324
Set<String> runtimeDependencies = getParameters().getRuntimeClasspathDependencies().get().get(moduleInfo.getIdentifier());
325+
Set<String> annotationProcessorDependencies = getParameters().getAnnotationProcessorClasspathDependencies().get().get(moduleInfo.getIdentifier());
323326

324-
if (compileDependencies == null && runtimeDependencies == null) {
327+
if (compileDependencies == null && runtimeDependencies == null && annotationProcessorDependencies == null) {
325328
throw new RuntimeException("[requires directives from metadata] " +
326329
"Cannot find dependencies for '" + moduleInfo.getModuleName() + "'. " +
327330
"Are '" + moduleInfo.getIdentifier() + "' the correct component coordinates?");
@@ -333,17 +336,22 @@ private byte[] addModuleInfo(ModuleInfo moduleInfo, Map<String, List<String>> pr
333336
if (runtimeDependencies == null) {
334337
runtimeDependencies = Collections.emptySet();
335338
}
339+
if (annotationProcessorDependencies == null) {
340+
annotationProcessorDependencies = Collections.emptySet();
341+
}
336342
Set<String> allDependencies = new TreeSet<>();
337343
allDependencies.addAll(compileDependencies);
338344
allDependencies.addAll(runtimeDependencies);
345+
allDependencies.addAll(annotationProcessorDependencies);
339346
for (String ga: allDependencies) {
340347
String moduleName = gaToModuleName(ga);
341-
if (compileDependencies.contains(ga) && runtimeDependencies.contains(ga)) {
342-
moduleVisitor.visitRequire(moduleName, Opcodes.ACC_TRANSITIVE, null);
343-
} else if (runtimeDependencies.contains(ga)) {
344-
moduleVisitor.visitRequire(moduleName, 0, null);
345-
} else if (compileDependencies.contains(ga)) {
348+
if (compileDependencies.contains(ga) && !runtimeDependencies.contains(ga)) {
346349
moduleVisitor.visitRequire(moduleName, Opcodes.ACC_STATIC_PHASE, null);
350+
} else {
351+
// We can currently not identify for sure if a 'requires' is NOT transitive.
352+
// For that, we would need the 'compile classpath' of the module we are looking at right now.
353+
// The 'compileDependencies' set is based only on the 'compile classpath' of the final consumer.
354+
moduleVisitor.visitRequire(moduleName, Opcodes.ACC_TRANSITIVE, null);
347355
}
348356
}
349357
}

src/test/groovy/org/gradlex/javamodule/moduleinfo/test/RequireAllDefinedDependenciesFunctionalTest.groovy

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,4 +295,47 @@ class RequireAllDefinedDependenciesFunctionalTest extends Specification {
295295
fail().output.contains("[requires directives from metadata] " +
296296
"The module name of the following component is not known: org.apache.httpcomponents:httpcore")
297297
}
298+
299+
def "requireAllDefinedDependencies work for annotation processor path"() {
300+
given:
301+
file("src/main/java/org/gradle/sample/app/Main.java") << """
302+
package org.gradle.sample.app;
303+
304+
public class Main {
305+
public static void main(String[] args) { }
306+
}
307+
"""
308+
file("src/main/java/module-info.java") << """
309+
module org.gradle.sample.app {
310+
exports org.gradle.sample.app;
311+
}
312+
"""
313+
buildFile << """
314+
dependencies {
315+
annotationProcessor("com.google.auto.service:auto-service:1.1.1")
316+
}
317+
318+
dependencies.components {
319+
withModule("com.google.guava:guava") {
320+
allVariants { withDependencies { removeAll { it.name != "failureaccess" } } }
321+
}
322+
}
323+
324+
extraJavaModuleInfo {
325+
automaticModule("com.google.auto.service:auto-service", "com.google.auto.service.processor")
326+
automaticModule("com.google.auto:auto-common", "com.google.auto.common")
327+
module("com.google.guava:guava", "com.google.common") {
328+
exportAllPackages()
329+
requireAllDefinedDependencies()
330+
requires("java.logging")
331+
}
332+
module("com.google.guava:failureaccess", "com.google.guava.failureaccess") {
333+
exportAllPackages()
334+
}
335+
}
336+
"""
337+
338+
expect:
339+
run().task(':run').outcome == TaskOutcome.SUCCESS
340+
}
298341
}

0 commit comments

Comments
 (0)