Skip to content

Commit

Permalink
Support Capability Coordinates in mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
jjohannes committed Aug 8, 2023
1 parent b1270ee commit 19f8378
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

## Version 1.3.1

Expand Down
7 changes: 5 additions & 2 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,15 @@ The plugin already knows about Modules available on Maven Central. The informati
- [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
- [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)

You can define additional entries (or overwrite entries from the plugin) as follows:
You define additional entries (or overwrite entries from the plugin) as follows:

```
// optional configuration if required
javaModuleDependencies {
// Module Name to Component GA Coordinates
moduleNameToGA.put("org.apache.commons.lang3", "org.apache.commons:commons-lang3")
// Module Name to Component GA Coordinates & Capability GA Coordinates
moduleNameToGA.put("org.apache.commons.lang3.test.fixtures", "org.apache.commons:commons-lang3|org.apache.commons:commons-lang3-test-fixtures")
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ public abstract class JavaModuleDependenciesExtension {
private final ModuleInfoCache moduleInfoCache;

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

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

ModuleInfo moduleInfo = getModuleInfoCache().get(sourceSetWithModuleInfo);
String ownModuleNamesPrefix = moduleInfo.moduleNamePrefix(getProject().getName(), sourceSetWithModuleInfo.getName());
Expand All @@ -183,11 +187,24 @@ public Provider<Dependency> create(String moduleName, SourceSet sourceSetWithMod
allProjectNamesAndGroups.get(projectName) + ":" + capabilityName));
projectDependency.because(moduleName);
return projectDependency;
} else if (gav.isPresent()) {
Dependency dependency = getDependencies().create(gav.get());
} else if (coordinates.isPresent()) {
Map<String, Object> component;
String capability;
if (coordinates.get().contains("|")) {
String[] split = coordinates.get().split("\\|");
component = findGav(split[0], moduleName);
capability = split[1];
} else {
component = findGav(coordinates.get(), moduleName);
capability = null;
}
ModuleDependency dependency = (ModuleDependency) getDependencies().create(component);
dependency.because(moduleName);
if (!gav.get().containsKey(GAV.VERSION)) {
warnVersionMissing(moduleName, gav.get(), moduleInfo.getFilePath());
if (capability != null) {
dependency.capabilities(c -> c.requireCapability(capability));
}
if (!component.containsKey(GAV.VERSION)) {
warnVersionMissing(moduleName, component, moduleInfo.getFilePath());
}
return dependency;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,29 @@ class NonMainVariantsTest extends Specification {
then:
result.output.contains('[lib-extra-feature.jar')
}

def "finds published feature variant when corresponding mapping is defined"() {
// There are no modules published like this anywhere public right now.
// We test that the expected Jar file would have been downloaded if 'org.slf4j' would have test fixtures.
given:
appBuildFile << '''
javaModuleDependencies {
moduleNameToGA.put("org.slf4j.test.fixtures", "org.slf4j:slf4j-api|org.slf4j:slf4j-api-test-fixtures")
}
dependencies.constraints {
javaModuleDependencies { implementation(gav("org.slf4j", "2.0.3")) }
}
'''
appModuleInfoFile << '''
module org.gradlex.test.app {
requires org.slf4j.test.fixtures;
}
'''

when:
def result = fail()

then:
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')
}
}

0 comments on commit 19f8378

Please sign in to comment.