Skip to content

Commit 19f8378

Browse files
committed
Support Capability Coordinates in mappings
1 parent b1270ee commit 19f8378

File tree

4 files changed

+53
-7
lines changed

4 files changed

+53
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Version 1.4
44
* [#31](https://github.com/gradlex-org/java-module-dependencies/issues/31) DSL for module dependencies that cannot be defined in module-info
5+
* [#45](https://github.com/gradlex-org/java-module-dependencies/issues/45) Support Capability Coordinates in mappings
56

67
## Version 1.3.1
78

README.MD

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,15 @@ The plugin already knows about Modules available on Maven Central. The informati
121121
- [modules.properties](src/main/resources/org/gradlex/javamodule/dependencies/modules.properties) - [please open a PR](https://github.com/gradlex-org/extra-java-module-info/pulls) if you miss an entry
122122
- [unique_modules.properties](src/main/resources/org/gradlex/javamodule/dependencies/unique_modules.properties) - this information is extracted from [modules.properties](https://github.com/sormuras/modules/blob/main/com.github.sormuras.modules/com/github/sormuras/modules/modules.properties) by [@sormuras](https://github.com/sormuras)
123123

124-
You can define additional entries (or overwrite entries from the plugin) as follows:
124+
You define additional entries (or overwrite entries from the plugin) as follows:
125125

126126
```
127-
// optional configuration if required
128127
javaModuleDependencies {
128+
// Module Name to Component GA Coordinates
129129
moduleNameToGA.put("org.apache.commons.lang3", "org.apache.commons:commons-lang3")
130+
131+
// Module Name to Component GA Coordinates & Capability GA Coordinates
132+
moduleNameToGA.put("org.apache.commons.lang3.test.fixtures", "org.apache.commons:commons-lang3|org.apache.commons:commons-lang3-test-fixtures")
130133
}
131134
```
132135

src/main/java/org/gradlex/javamodule/dependencies/JavaModuleDependenciesExtension.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ public abstract class JavaModuleDependenciesExtension {
7171
private final ModuleInfoCache moduleInfoCache;
7272

7373
/**
74+
* Register mapping from Module Name to GA Coordinates (and optionally Capability Coordinates).
75+
* - moduleNameToGA.put("org.slf4j", "org.slf4j:slf4j-api")
76+
* - moduleNameToGA.put("org.slf4j.test.fixtures", "org.slf4j:slf4j-api|org.slf4j:slf4j-api-test-fixtures")
77+
*
7478
* @return the mappings from Module Name to GA coordinates; can be modified
7579
*/
7680
public abstract MapProperty<String, String> getModuleNameToGA();
@@ -156,7 +160,7 @@ public Provider<Dependency> create(String moduleName, SourceSet sourceSetWithMod
156160
Map<String, String> allProjectNamesAndGroups = getProject().getRootProject().getSubprojects().stream().collect(
157161
Collectors.toMap(Project::getName, p -> (String) p.getGroup()));
158162

159-
Provider<Map<String, Object>> gav = getModuleNameToGA().getting(moduleName).orElse(mapByPrefix(getProviders().provider(() -> moduleName))).map(ga -> findGav(ga, moduleName));
163+
Provider<String> coordinates = getModuleNameToGA().getting(moduleName).orElse(mapByPrefix(getProviders().provider(() -> moduleName)));
160164

161165
ModuleInfo moduleInfo = getModuleInfoCache().get(sourceSetWithModuleInfo);
162166
String ownModuleNamesPrefix = moduleInfo.moduleNamePrefix(getProject().getName(), sourceSetWithModuleInfo.getName());
@@ -183,11 +187,24 @@ public Provider<Dependency> create(String moduleName, SourceSet sourceSetWithMod
183187
allProjectNamesAndGroups.get(projectName) + ":" + capabilityName));
184188
projectDependency.because(moduleName);
185189
return projectDependency;
186-
} else if (gav.isPresent()) {
187-
Dependency dependency = getDependencies().create(gav.get());
190+
} else if (coordinates.isPresent()) {
191+
Map<String, Object> component;
192+
String capability;
193+
if (coordinates.get().contains("|")) {
194+
String[] split = coordinates.get().split("\\|");
195+
component = findGav(split[0], moduleName);
196+
capability = split[1];
197+
} else {
198+
component = findGav(coordinates.get(), moduleName);
199+
capability = null;
200+
}
201+
ModuleDependency dependency = (ModuleDependency) getDependencies().create(component);
188202
dependency.because(moduleName);
189-
if (!gav.get().containsKey(GAV.VERSION)) {
190-
warnVersionMissing(moduleName, gav.get(), moduleInfo.getFilePath());
203+
if (capability != null) {
204+
dependency.capabilities(c -> c.requireCapability(capability));
205+
}
206+
if (!component.containsKey(GAV.VERSION)) {
207+
warnVersionMissing(moduleName, component, moduleInfo.getFilePath());
191208
}
192209
return dependency;
193210
} else {

src/test/groovy/org/gradlex/javamodule/dependencies/test/NonMainVariantsTest.groovy

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,29 @@ class NonMainVariantsTest extends Specification {
6060
then:
6161
result.output.contains('[lib-extra-feature.jar')
6262
}
63+
64+
def "finds published feature variant when corresponding mapping is defined"() {
65+
// There are no modules published like this anywhere public right now.
66+
// We test that the expected Jar file would have been downloaded if 'org.slf4j' would have test fixtures.
67+
given:
68+
appBuildFile << '''
69+
javaModuleDependencies {
70+
moduleNameToGA.put("org.slf4j.test.fixtures", "org.slf4j:slf4j-api|org.slf4j:slf4j-api-test-fixtures")
71+
}
72+
dependencies.constraints {
73+
javaModuleDependencies { implementation(gav("org.slf4j", "2.0.3")) }
74+
}
75+
'''
76+
appModuleInfoFile << '''
77+
module org.gradlex.test.app {
78+
requires org.slf4j.test.fixtures;
79+
}
80+
'''
81+
82+
when:
83+
def result = fail()
84+
85+
then:
86+
result.output.contains('> Unable to find a variant of org.slf4j:slf4j-api:2.0.3 providing the requested capability org.slf4j:slf4j-api-test-fixtures')
87+
}
6388
}

0 commit comments

Comments
 (0)