Skip to content

Commit 0e871ef

Browse files
committed
Error if Module Name does not match Gradle project and source set names
Resolves #65
1 parent 5e873fb commit 0e871ef

File tree

5 files changed

+18
-6
lines changed

5 files changed

+18
-6
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ public abstract class JavaModuleDependenciesExtension {
113113
*/
114114
public abstract Property<String> getVersionCatalogName();
115115

116+
/**
117+
* Fail the build if a Module Name does not fit the corresponding project and source set names;
118+
* defaults to 'true'.
119+
*/
120+
public abstract Property<Boolean> getModuleNameCheck();
121+
116122
/**
117123
* Set this to true to use the analytic help tasks (like :moduleDependencies) of the plugin without performing
118124
* the actual dependency calculation.
@@ -126,6 +132,7 @@ public JavaModuleDependenciesExtension(VersionCatalogsExtension versionCatalogs)
126132
this.moduleInfoCache = getObjects().newInstance(ModuleInfoCache.class);
127133
getModulesProperties().set(getProject().getRootProject().getLayout().getProjectDirectory().file("gradle/modules.properties"));
128134
getVersionCatalogName().convention("libs");
135+
getModuleNameCheck().convention(true);
129136
getAnalyseOnly().convention(false);
130137
getModuleNameToGA().putAll(SharedMappings.mappings);
131138
getModuleNameToGA().putAll(parsedModulesProperties().orElse(Collections.emptyMap()));
@@ -207,7 +214,7 @@ public Provider<Dependency> create(String moduleName, SourceSet sourceSetWithMod
207214
Provider<String> coordinates = getModuleNameToGA().getting(moduleName).orElse(mapByPrefix(getProviders().provider(() -> moduleName)));
208215

209216
ModuleInfo moduleInfo = getModuleInfoCache().get(sourceSetWithModuleInfo);
210-
String ownModuleNamesPrefix = moduleInfo.moduleNamePrefix(getProject().getName(), sourceSetWithModuleInfo.getName());
217+
String ownModuleNamesPrefix = moduleInfo.moduleNamePrefix(getProject().getName(), sourceSetWithModuleInfo.getName(), getModuleNameCheck().get());
211218

212219
String moduleNameSuffix = ownModuleNamesPrefix == null ? null :
213220
moduleName.startsWith(ownModuleNamesPrefix + ".") ? moduleName.substring(ownModuleNamesPrefix.length() + 1) :

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ private void setupOrderingCheckTasks(Project project, TaskProvider<Task> checkAl
215215
ModuleInfo moduleInfo = javaModuleDependencies.getModuleInfoCache().get(sourceSet);
216216

217217
t.getModuleInfoPath().convention(moduleInfo.getFilePath().getAbsolutePath());
218-
t.getModuleNamePrefix().convention(moduleInfo.moduleNamePrefix(project.getName(), sourceSet.getName()));
218+
t.getModuleNamePrefix().convention(moduleInfo.moduleNamePrefix(project.getName(), sourceSet.getName(), false));
219219
t.getModuleInfo().convention(moduleInfo);
220220
});
221221

src/main/java/org/gradlex/javamodule/dependencies/internal/utils/ModuleInfo.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public List<String> get(Directive directive) {
8282
}
8383

8484
@Nullable
85-
public String moduleNamePrefix(String projectName, String sourceSetName) {
85+
public String moduleNamePrefix(String projectName, String sourceSetName, boolean fail) {
8686
if (moduleName.equals(projectName)) {
8787
return "";
8888
}
@@ -97,6 +97,10 @@ public String moduleNamePrefix(String projectName, String sourceSetName) {
9797
if (moduleName.endsWith("." + projectName)) {
9898
return moduleName.substring(0, moduleName.length() - projectName.length() - 1);
9999
}
100+
if (this != EMPTY && fail) {
101+
throw new RuntimeException("Module name '" + moduleName + "' does not fit the project and source set names; " +
102+
"expected name '<optional.prefix.>" + projectPlusSourceSetName + "'.");
103+
}
100104
return null;
101105
}
102106

@@ -115,6 +119,7 @@ private boolean parse(String moduleLine, boolean insideComment) {
115119
List<String> tokens = Arrays.asList(moduleLine
116120
.replace(";", "")
117121
.replace("{", "")
122+
.replace("}", "")
118123
.replace(RUNTIME_KEYWORD, "runtime")
119124
.replaceAll("/\\*.*?\\*/", " ")
120125
.trim().split("\\s+"));

src/main/java/org/gradlex/javamodule/dependencies/tasks/ModulePathAnalysis.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public void report() throws IOException {
7575
if (file.exists()) {
7676
try(Stream<String> lines = Files.lines(file.toPath())) {
7777
String fileContent = lines.collect(Collectors.joining("\n"));
78-
ownModuleNamesPrefix = new ModuleInfo(fileContent, file).moduleNamePrefix(projectName, main.getName());
78+
ownModuleNamesPrefix = new ModuleInfo(fileContent, file).moduleNamePrefix(projectName, main.getName(), false);
7979
}
8080
break;
8181
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class ModuleInfoParseTest extends Specification {
2121
''', new File(''))
2222

2323
expect:
24-
moduleInfo.moduleNamePrefix("thing", "main") == "some"
24+
moduleInfo.moduleNamePrefix("thing", "main", false) == "some"
2525
moduleInfo.get(REQUIRES) == []
2626
moduleInfo.get(REQUIRES_TRANSITIVE) == ["foo.bar.la"]
2727
moduleInfo.get(REQUIRES_STATIC) == []
@@ -38,7 +38,7 @@ class ModuleInfoParseTest extends Specification {
3838
''', new File(''))
3939

4040
expect:
41-
moduleInfo.moduleNamePrefix("thing", "main") == "some"
41+
moduleInfo.moduleNamePrefix("thing", "main", false) == "some"
4242
moduleInfo.get(REQUIRES) == []
4343
moduleInfo.get(REQUIRES_TRANSITIVE) == ["foo.bar.la"]
4444
moduleInfo.get(REQUIRES_STATIC) == []

0 commit comments

Comments
 (0)