|
64 | 64 | import java.util.zip.ZipEntry; |
65 | 65 | import java.util.zip.ZipException; |
66 | 66 |
|
67 | | -import static org.gradlex.javamodule.moduleinfo.ModuleNameUtil.automaticModulNameFromFileName; |
68 | 67 | import static org.gradlex.javamodule.moduleinfo.FilePathToModuleCoordinates.gaCoordinatesFromFilePathMatch; |
69 | 68 | import static org.gradlex.javamodule.moduleinfo.FilePathToModuleCoordinates.versionFromFilePath; |
| 69 | +import static org.gradlex.javamodule.moduleinfo.ModuleNameUtil.automaticModulNameFromFileName; |
70 | 70 |
|
71 | 71 | /** |
72 | 72 | * An artifact transform that applies additional information to Jars without module information. |
@@ -130,18 +130,28 @@ public void transform(TransformOutputs outputs) { |
130 | 130 | if (realModule && !((ModuleInfo) moduleSpec).patchRealModule) { |
131 | 131 | throw new RuntimeException("Patching of real modules must be explicitly enabled with 'patchRealModule()'"); |
132 | 132 | } |
| 133 | + String definedName = moduleSpec.getModuleName(); |
| 134 | + String expectedName = autoModuleName(originalJar); |
| 135 | + if (expectedName != null && !definedName.equals(expectedName) && !moduleSpec.overrideModuleName) { |
| 136 | + throw new RuntimeException("The name '" + definedName + "' is different than the Automatic-Module-Name '" + expectedName + "'; explicitly allow override via 'overrideName()'"); |
| 137 | + } |
133 | 138 | addModuleDescriptor(originalJar, getModuleJar(outputs, originalJar), (ModuleInfo) moduleSpec); |
134 | 139 | } else if (moduleSpec instanceof AutomaticModuleName) { |
135 | 140 | if (realModule) { |
136 | 141 | throw new RuntimeException("Patching of real modules must be explicitly enabled with 'patchRealModule()' and can only be done with 'module()'"); |
137 | 142 | } |
| 143 | + String definedName = moduleSpec.getModuleName(); |
| 144 | + String expectedName = autoModuleName(originalJar); |
| 145 | + if (expectedName != null && (moduleSpec.getMergedJars().isEmpty() || !definedName.equals(expectedName)) && !moduleSpec.overrideModuleName) { |
| 146 | + throw new RuntimeException("'" + definedName + "' already has the Automatic-Module-Name '" + expectedName + "'; explicitly allow override via 'overrideName()'"); |
| 147 | + } |
138 | 148 | if (parameters.getFailOnAutomaticModules().get()) { |
139 | 149 | throw new RuntimeException("Use of 'automaticModule()' is prohibited. Use 'module()' instead: " + originalJar.getName()); |
140 | 150 | } |
141 | 151 | addAutomaticModuleName(originalJar, getModuleJar(outputs, originalJar), (AutomaticModuleName) moduleSpec); |
142 | 152 | } else if (realModule) { |
143 | 153 | outputs.file(originalJar); |
144 | | - } else if (isAutoModule(originalJar)) { |
| 154 | + } else if (autoModuleName(originalJar) != null) { |
145 | 155 | if (parameters.getFailOnAutomaticModules().get()) { |
146 | 156 | throw new RuntimeException("Found an automatic module: " + originalJar.getName()); |
147 | 157 | } |
@@ -210,10 +220,11 @@ private boolean containsMultiReleaseJarEntry(JarInputStream jarStream) { |
210 | 220 | return manifest != null && Boolean.parseBoolean(manifest.getMainAttributes().getValue("Multi-Release")); |
211 | 221 | } |
212 | 222 |
|
213 | | - private boolean isAutoModule(File jar) { |
| 223 | + @Nullable |
| 224 | + private String autoModuleName(File jar) { |
214 | 225 | try (JarInputStream inputStream = new JarInputStream(Files.newInputStream(jar.toPath()))) { |
215 | 226 | Manifest manifest = inputStream.getManifest(); |
216 | | - return manifest != null && manifest.getMainAttributes().getValue("Automatic-Module-Name") != null; |
| 227 | + return manifest != null ? manifest.getMainAttributes().getValue("Automatic-Module-Name") : null; |
217 | 228 | } catch (IOException e) { |
218 | 229 | throw new RuntimeException(e); |
219 | 230 | } |
|
0 commit comments