Skip to content

Commit

Permalink
Improve analysis reporting for source sets without module-info.java
Browse files Browse the repository at this point in the history
  • Loading branch information
jjohannes committed Jun 4, 2023
1 parent d84494c commit 30d8278
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.gradle.api.tasks.TaskProvider;
import org.gradlex.javamodule.dependencies.tasks.ModuleDirectivesScopeCheck;

import java.io.File;

public class DependencyAnalysisBridge {

public static void registerDependencyAnalysisPostProcessingTask(Project project, TaskProvider<Task> checkAllModuleInfo) {
Expand All @@ -33,10 +35,13 @@ public static void registerDependencyAnalysisPostProcessingTask(Project project,
project.getTasks().register("checkModuleDirectivesScope", ModuleDirectivesScopeCheck.class);

sourceSets.all(sourceSet -> checkModuleDirectivesScope.configure(t -> {
File moduleInfo = new File(sourceSet.getJava().getSrcDirs().iterator().next(), "module-info.java");
if (!moduleInfo.exists()) {
moduleInfo = project.getBuildFile(); // no module-info: dependencies are declared in build file
}
t.getSourceSets().put(
sourceSet.getName(),
project.getLayout().getProjectDirectory().getAsFile().getParentFile().toPath().relativize(
sourceSet.getJava().getSrcDirs().iterator().next().toPath()).resolve("module-info.java").toString());
project.getLayout().getProjectDirectory().getAsFile().getParentFile().toPath().relativize(moduleInfo.toPath()).toString());

Configuration cpClasspath = project.getConfigurations().getByName(sourceSet.getCompileClasspathConfigurationName());
Configuration rtClasspath = project.getConfigurations().getByName(sourceSet.getRuntimeClasspathConfigurationName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,21 @@ public void analyze() {

StringBuilder message = new StringBuilder();
for (Map.Entry<String, String> sourceSet : getSourceSets().get().entrySet()) {
boolean inBuildFile = !sourceSet.getValue().endsWith("module-info.java");
List<String> toAdd = projectAdvice.stream().filter(a ->
a.getToConfiguration() != null && !RUNTIME_ONLY_CONFIGURATION_NAME.equals(getScope(a.getToConfiguration()).orElse(null))
).filter(a ->
sourceSet.getKey().equals(sourceSetName(a.getToConfiguration()))
).map(a ->
declaration(a.getToConfiguration(), a.getCoordinates().getIdentifier(), a.getCoordinates().getGradleVariantIdentification().getCapabilities())
declaration(a.getToConfiguration(), a.getCoordinates().getIdentifier(), a.getCoordinates().getGradleVariantIdentification().getCapabilities(), inBuildFile)
).sorted().collect(Collectors.toList());

List<String> toRemove = projectAdvice.stream().filter(a ->
a.getFromConfiguration() != null
).filter(a ->
sourceSet.getKey().equals(sourceSetName(a.getFromConfiguration()))
).map(a ->
declaration(a.getFromConfiguration(), a.getCoordinates().getIdentifier(), a.getCoordinates().getGradleVariantIdentification().getCapabilities())
declaration(a.getFromConfiguration(), a.getCoordinates().getIdentifier(), a.getCoordinates().getGradleVariantIdentification().getCapabilities(), inBuildFile)
).sorted().collect(Collectors.toList());

if (!toAdd.isEmpty() || !toRemove.isEmpty()) {
Expand All @@ -92,7 +93,7 @@ public void analyze() {
message.append("\n ").append(String.join("\n ", toAdd));
}
if (!toRemove.isEmpty()) {
message.append("\n\nPlease remove the following requires directives:");
message.append("\n\nPlease remove the following requires directives (or change to runtimeOnly):");
message.append("\n ").append(String.join("\n ", toRemove));
}
}
Expand All @@ -101,12 +102,20 @@ public void analyze() {
}
}

private String declaration(String conf, String coordinates, Set<String> capabilities) {
private String declaration(String conf, String coordinates, Set<String> capabilities, boolean inBuildFile) {
String capability = capabilities.isEmpty() ? coordinates : capabilities.iterator().next();
ResolvedArtifactResult moduleJar = getModuleArtifacts().get().stream().flatMap(c -> c.getArtifacts().stream()).filter(a ->
coordinatesEquals(coordinates, capability, a)).findFirst().orElse(null);
try {
return directive(conf) + " " + (moduleJar == null ? coordinates : readNameFromModuleFromJarFile(moduleJar.getFile())) + ";";
String moduleName = moduleJar == null ? coordinates : readNameFromModuleFromJarFile(moduleJar.getFile());
if (inBuildFile) {
return conf + (coordinates.startsWith(":")
? "(project(\"" + coordinates + "\"))"
: "(gav(\"" + moduleName + "\"))"
);
} else {
return directive(conf) + " " + moduleName + ";";
}
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down

0 comments on commit 30d8278

Please sign in to comment.