|
13 | 13 | import org.gradle.api.artifacts.Configuration; |
14 | 14 | import org.gradle.api.artifacts.ConfigurationContainer; |
15 | 15 | import org.gradle.api.artifacts.ProjectDependency; |
| 16 | +import org.gradle.api.artifacts.VersionCatalog; |
16 | 17 | import org.gradle.api.artifacts.VersionCatalogsExtension; |
| 18 | +import org.gradle.api.artifacts.VersionConstraint; |
17 | 19 | import org.gradle.api.file.RegularFile; |
18 | 20 | import org.gradle.api.plugins.JavaPlugin; |
19 | 21 | import org.gradle.api.provider.Provider; |
|
32 | 34 | import static de.jjohannes.gradle.moduledependencies.JavaModuleDependenciesExtension.JAVA_MODULE_DEPENDENCIES; |
33 | 35 | import static de.jjohannes.gradle.moduledependencies.internal.utils.DependencyDeclarationsUtil.declaredDependencies; |
34 | 36 | import static de.jjohannes.gradle.moduledependencies.internal.utils.ModuleNamingUtil.sourceSetToModuleName; |
| 37 | +import static java.util.Optional.empty; |
35 | 38 | import static org.gradle.api.plugins.HelpTasksPlugin.HELP_GROUP; |
36 | 39 |
|
37 | 40 | @SuppressWarnings("unused") |
@@ -185,9 +188,38 @@ private void declareDependency(String moduleName, @Nullable String ownModuleName |
185 | 188 | warnVersionMissing(moduleName, gav.get(), moduleInfoFile, project, javaModuleDependencies); |
186 | 189 | } |
187 | 190 | } 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 | + } |
191 | 223 | } |
192 | 224 | } |
193 | 225 |
|
|
0 commit comments