Skip to content

Commit e56d36a

Browse files
committed
Add option to register mappings for a group of modules
1 parent 733e954 commit e56d36a

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

README.MD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ javaModuleDependencies {
8989
}
9090
```
9191

92+
There is also the option to register a mapping for all Modules that share a common _name prefix_ and _group_.
93+
For example: `moduleNamePrefixToGroup.put("com.example.product.module.", "com.example.product")`.
94+
9295
## Naming patterns for Modules in the build
9396

9497
This plugin makes the following assumption about _Module Names_ of your own Modules in the build to establish dependencies between them:

src/main/java/de/jjohannes/gradle/moduledependencies/JavaModuleDependenciesExtension.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ public abstract class JavaModuleDependenciesExtension {
3030
*/
3131
public abstract MapProperty<String, String> getModuleNameToGA();
3232

33+
/**
34+
* If your Module Names all start with a common prefix (e.g. 'com.example.product.module.') followed by a
35+
* name that corresponds to the artifact name and all have the group (e.g. 'com.example.product'), you can
36+
* register a mapping for all these Modules. and with that allow Gradle to map them correctly even if you
37+
* publish some of your Modules or use included builds.
38+
*
39+
* moduleNamePrefixToGroup.put("com.example.product.module.", "com.example.product")
40+
*
41+
* @return the mappings from 'Module Name Prefix' to 'group'
42+
*/
43+
public abstract MapProperty<String, String> getModuleNamePrefixToGroup();
44+
3345
/**
3446
* @return If a Version Catalog is used: print a WARN for missing versions (default is 'true')
3547
*/

src/main/java/de/jjohannes/gradle/moduledependencies/JavaModuleDependenciesPlugin.java

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
import org.gradle.api.artifacts.Configuration;
1414
import org.gradle.api.artifacts.ConfigurationContainer;
1515
import org.gradle.api.artifacts.ProjectDependency;
16+
import org.gradle.api.artifacts.VersionCatalog;
1617
import org.gradle.api.artifacts.VersionCatalogsExtension;
18+
import org.gradle.api.artifacts.VersionConstraint;
1719
import org.gradle.api.file.RegularFile;
1820
import org.gradle.api.plugins.JavaPlugin;
1921
import org.gradle.api.provider.Provider;
@@ -32,6 +34,7 @@
3234
import static de.jjohannes.gradle.moduledependencies.JavaModuleDependenciesExtension.JAVA_MODULE_DEPENDENCIES;
3335
import static de.jjohannes.gradle.moduledependencies.internal.utils.DependencyDeclarationsUtil.declaredDependencies;
3436
import static de.jjohannes.gradle.moduledependencies.internal.utils.ModuleNamingUtil.sourceSetToModuleName;
37+
import static java.util.Optional.empty;
3538
import static org.gradle.api.plugins.HelpTasksPlugin.HELP_GROUP;
3639

3740
@SuppressWarnings("unused")
@@ -185,9 +188,38 @@ private void declareDependency(String moduleName, @Nullable String ownModuleName
185188
warnVersionMissing(moduleName, gav.get(), moduleInfoFile, project, javaModuleDependencies);
186189
}
187190
} else {
188-
project.getLogger().lifecycle(
189-
"[WARN] [Java Module Dependencies] No mapping registered for module: " + moduleDebugInfo(moduleName, moduleInfoFile, project.getRootDir()) +
190-
" - use 'javaModuleDependencies.moduleNameToGA.put(\"" + moduleName + "\", \"group:artifact\")' to add mapping.");
191+
Optional<Map.Entry<String, String>> prefixToGroupMapping =
192+
javaModuleDependencies.getModuleNamePrefixToGroup().get().entrySet().stream()
193+
.filter(e -> moduleName.startsWith(e.getKey())).findFirst();
194+
if (prefixToGroupMapping.isPresent()) {
195+
String prefix = prefixToGroupMapping.get().getKey();
196+
197+
String group = prefixToGroupMapping.get().getValue();
198+
String artifactName = moduleName.substring(prefix.length());
199+
200+
VersionCatalog catalog = null;
201+
VersionCatalogsExtension versionCatalogs = project.getExtensions().findByType(VersionCatalogsExtension.class);
202+
if (versionCatalogs != null) {
203+
String catalogName = javaModuleDependencies.getVersionCatalogName().get();
204+
catalog = versionCatalogs.named(catalogName);
205+
}
206+
Optional<VersionConstraint> version = catalog == null ? empty() : catalog.findVersion(prefix.replace('_', '.'));
207+
208+
Map<String, Object> gavFromPrefixMapping = new HashMap<>();
209+
gavFromPrefixMapping.put(GAV.GROUP, group);
210+
gavFromPrefixMapping.put(GAV.ARTIFACT, artifactName);
211+
version.ifPresent(versionConstraint -> gavFromPrefixMapping.put(GAV.VERSION, versionConstraint));
212+
213+
project.getDependencies().add(configuration.getName(), gavFromPrefixMapping);
214+
215+
if (!gavFromPrefixMapping.containsKey(GAV.VERSION)) {
216+
warnVersionMissing(prefix, gavFromPrefixMapping, moduleInfoFile, project, javaModuleDependencies);
217+
}
218+
} else {
219+
project.getLogger().lifecycle(
220+
"[WARN] [Java Module Dependencies] No mapping registered for module: " + moduleDebugInfo(moduleName, moduleInfoFile, project.getRootDir()) +
221+
" - use 'javaModuleDependencies.moduleNameToGA.put(\"" + moduleName + "\", \"group:artifact\")' to add mapping.");
222+
}
191223
}
192224
}
193225

0 commit comments

Comments
 (0)