Skip to content

Commit bca0a5e

Browse files
authored
Only configuration cache miss when necessary (#127)
1 parent feb0335 commit bca0a5e

File tree

13 files changed

+315
-30
lines changed

13 files changed

+315
-30
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Java Module Dependencies Gradle Plugin - Changelog
22

3+
## Version 1.8
4+
* [#127](https://github.com/gradlex-org/java-module-dependencies/issues/127) Less configuration cache misses when modifying `module-info.java` (Thanks [TheGoesen](https://github.com/TheGoesen))
5+
36
## Version 1.7.1
47
* Update module name mappings
58

build.gradle.kts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ testing.suites.named<JvmTestSuite>("test") {
7474
systemProperty("gradleVersionUnderTest", gradleVersionUnderTest)
7575
exclude("**/*SamplesTest.class") // Not yet cross-version ready
7676
exclude("**/initialization/**") // Settings plugin only for Gradle 8.8+
77+
if (gradleVersionUnderTest == "7.4") {
78+
// Configuration cache only "reliable" since 7.6 (?)
79+
// https://github.com/gradlex-org/java-module-dependencies/issues/129
80+
exclude("**/configcache/**")
81+
}
7782
}
7883
}
7984
}

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,14 +229,16 @@ private void setupOrderingCheckTasks(Project project, TaskProvider<Task> checkAl
229229
SourceSetContainer sourceSets = project.getExtensions().getByType(SourceSetContainer.class);
230230
ConfigurationContainer configurations = project.getConfigurations();
231231

232-
sourceSets.all(sourceSet -> {
232+
sourceSets.configureEach(sourceSet -> {
233233
TaskProvider<ModuleDirectivesOrderingCheck> checkModuleInfo = project.getTasks().register(sourceSet.getTaskName("check", "ModuleInfo"), ModuleDirectivesOrderingCheck.class, t -> {
234234
t.setGroup("java modules");
235235
t.setDescription("Check order of directives in 'module-info.java' in '" + sourceSet.getName() + "' source set");
236236

237237
ModuleInfo moduleInfo = javaModuleDependencies.getModuleInfoCache().get().get(sourceSet, project.getProviders());
238-
239-
t.getModuleInfoPath().convention(moduleInfo.getFilePath().getAbsolutePath());
238+
File folder = javaModuleDependencies.getModuleInfoCache().get().getFolder(sourceSet, project.getProviders());
239+
if (folder != null) {
240+
t.getModuleInfoPath().convention(new File(folder, "module-info.java").getAbsolutePath());
241+
}
240242
t.getModuleNamePrefix().convention(moduleInfo.moduleNamePrefix(project.getName(), sourceSet.getName(), false));
241243
t.getModuleInfo().convention(moduleInfo);
242244

@@ -266,11 +268,11 @@ private void readModuleInfo(ModuleInfo.Directive moduleDirective, SourceSet sour
266268
}
267269
ModuleInfo moduleInfo = javaModuleDependenciesExtension.getModuleInfoCache().get().get(sourceSet, project.getProviders());
268270
for (String moduleName : moduleInfo.get(moduleDirective)) {
269-
declareDependency(moduleName, moduleInfo.getFilePath(), project, sourceSet, configuration, javaModuleDependenciesExtension);
271+
declareDependency(moduleName, project, sourceSet, configuration, javaModuleDependenciesExtension);
270272
}
271273
}
272274

273-
private void declareDependency(String moduleName, File moduleInfoFile, Project project, SourceSet sourceSet, Configuration configuration, JavaModuleDependenciesExtension javaModuleDependencies) {
275+
private void declareDependency(String moduleName, Project project, SourceSet sourceSet, Configuration configuration, JavaModuleDependenciesExtension javaModuleDependencies) {
274276
if (JDKInfo.MODULES.contains(moduleName)) {
275277
// The module is part of the JDK, no dependency required
276278
return;
@@ -286,7 +288,7 @@ private List<BuildFileDependenciesGenerate.DependencyDeclaration> collectDepende
286288
File sourceSetDir = sourceSet.getJava().getSrcDirs().iterator().next().getParentFile();
287289
File whiteboxModuleInfoFile = new File(sourceSetDir, "java9/module-info.java");
288290
if (whiteboxModuleInfoFile.exists()) {
289-
moduleInfo = new ModuleInfo(project.getProviders().fileContents(project.getLayout().getProjectDirectory().file(whiteboxModuleInfoFile.getAbsolutePath())).getAsText().get(), whiteboxModuleInfoFile);
291+
moduleInfo = new ModuleInfo(project.getProviders().fileContents(project.getLayout().getProjectDirectory().file(whiteboxModuleInfoFile.getAbsolutePath())).getAsText().get());
290292
}
291293
}
292294
return moduleInfo.get(directive).stream()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ private void registerCatalogTask(Project project) {
132132
moduleInfoFile = new File(srcDirSet, "java9/module-info.java");
133133
}
134134
if (moduleInfoFile.exists()) {
135-
ModuleInfo moduleInfo = new ModuleInfo(project.getProviders().fileContents(project.getLayout().getProjectDirectory().file(moduleInfoFile.getAbsolutePath())).getAsText().get(), moduleInfoFile);
135+
ModuleInfo moduleInfo = new ModuleInfo(project.getProviders().fileContents(project.getLayout().getProjectDirectory().file(moduleInfoFile.getAbsolutePath())).getAsText().get());
136136
t.getEntries().addAll(collectCatalogEntriesFromModuleInfos(javaModuleDependencies, moduleInfo.get(REQUIRES_TRANSITIVE)));
137137
t.getEntries().addAll(collectCatalogEntriesFromModuleInfos(javaModuleDependencies, moduleInfo.get(REQUIRES)));
138138
t.getEntries().addAll(collectCatalogEntriesFromModuleInfos(javaModuleDependencies, moduleInfo.get(REQUIRES_STATIC_TRANSITIVE)));

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

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
package org.gradlex.javamodule.dependencies.internal.utils;
1818

1919
import javax.annotation.Nullable;
20-
import java.io.File;
2120
import java.io.Serializable;
2221
import java.util.ArrayList;
2322
import java.util.Arrays;
2423
import java.util.Collections;
2524
import java.util.List;
25+
import java.util.Objects;
2626

2727
import static org.gradlex.javamodule.dependencies.internal.utils.ModuleNamingUtil.sourceSetToModuleName;
2828

@@ -43,7 +43,7 @@ public String literal() {
4343

4444
public static final String RUNTIME_KEYWORD = "/*runtime*/";
4545

46-
public static final ModuleInfo EMPTY = new ModuleInfo("", new File(""));
46+
public static final ModuleInfo EMPTY = new ModuleInfo("");
4747

4848
private String moduleName = "";
4949
private final List<String> requires = new ArrayList<>();
@@ -52,12 +52,10 @@ public String literal() {
5252
private final List<String> requiresStaticTransitive = new ArrayList<>();
5353
private final List<String> requiresRuntime = new ArrayList<>();
5454

55-
private final File filePath;
5655

57-
public ModuleInfo(String moduleInfoFileContent, File filePath) {
58-
this.filePath = filePath;
56+
public ModuleInfo(String moduleInfoFileContent) {
5957
boolean insideComment = false;
60-
for(String line: moduleInfoFileContent.split("\n")) {
58+
for (String line : moduleInfoFileContent.split("\n")) {
6159
insideComment = parse(line, insideComment);
6260
}
6361
}
@@ -108,10 +106,6 @@ public String moduleNamePrefix(String projectName, String sourceSetName, boolean
108106
return null;
109107
}
110108

111-
public File getFilePath() {
112-
return filePath;
113-
}
114-
115109
/**
116110
* @return true, if we are inside a multi-line comment after this line
117111
*/
@@ -150,4 +144,29 @@ private boolean parse(String moduleLine, boolean insideComment) {
150144
}
151145
return moduleLine.lastIndexOf("/*") > moduleLine.lastIndexOf("*/");
152146
}
147+
148+
@Override
149+
public boolean equals(Object o) {
150+
if (this == o) return true;
151+
if (o == null || getClass() != o.getClass()) return false;
152+
ModuleInfo that = (ModuleInfo) o;
153+
return Objects.equals(moduleName, that.moduleName)
154+
&& Objects.equals(requires, that.requires)
155+
&& Objects.equals(requiresTransitive, that.requiresTransitive)
156+
&& Objects.equals(requiresStatic, that.requiresStatic)
157+
&& Objects.equals(requiresStaticTransitive, that.requiresStaticTransitive)
158+
&& Objects.equals(requiresRuntime, that.requiresRuntime);
159+
}
160+
161+
@Override
162+
public int hashCode() {
163+
return Objects.hash(
164+
moduleName,
165+
requires,
166+
requiresTransitive,
167+
requiresStatic,
168+
requiresStaticTransitive,
169+
requiresRuntime
170+
);
171+
}
153172
}

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.gradlex.javamodule.dependencies.internal.utils;
1818

19-
import org.gradle.api.file.RegularFileProperty;
2019
import org.gradle.api.logging.Logger;
2120
import org.gradle.api.model.ObjectFactory;
2221
import org.gradle.api.provider.Provider;
@@ -69,6 +68,15 @@ public ModuleInfo get(SourceSet sourceSet, ProviderFactory providers) {
6968
return ModuleInfo.EMPTY;
7069
}
7170

71+
public File getFolder(SourceSet sourceSet, ProviderFactory providers) {
72+
for (File folder : sourceSet.getJava().getSrcDirs()) {
73+
if (maybePutModuleInfo(folder, providers)) {
74+
return folder;
75+
}
76+
}
77+
return null;
78+
}
79+
7280
/**
7381
* @param projectRoot the project that should hold a Java module
7482
* @return parsed module-info.java for the given project assuming a standard Java project layout
@@ -102,15 +110,17 @@ public String getCapability(String moduleName) {
102110
}
103111

104112
private boolean maybePutModuleInfo(File folder, ProviderFactory providers) {
105-
RegularFileProperty moduleInfoFile = getObjects().fileProperty();
106-
moduleInfoFile.set(new File(folder, "module-info.java"));
107-
Provider<String> moduleInfoContent = providers.fileContents(moduleInfoFile).getAsText();
108-
if (moduleInfoContent.isPresent()) {
113+
Provider<ModuleInfo> moduleInfoProvider = provideModuleInfo(folder, providers);
114+
if (moduleInfoProvider.isPresent()) {
109115
if (!moduleInfo.containsKey(folder)) {
110-
moduleInfo.put(folder, new ModuleInfo(moduleInfoContent.get(), moduleInfoFile.get().getAsFile()));
116+
moduleInfo.put(folder, moduleInfoProvider.get() );
111117
}
112118
return true;
113119
}
114120
return false;
115121
}
122+
123+
private Provider<ModuleInfo> provideModuleInfo(File folder, ProviderFactory providers) {
124+
return providers.of(ValueSourceModuleInfo.class, spec -> spec.parameters(param -> param.getDir().set(folder)));
125+
}
116126
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright the GradleX team.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.gradlex.javamodule.dependencies.internal.utils;
18+
19+
import org.gradle.api.file.DirectoryProperty;
20+
import org.gradle.api.provider.ValueSource;
21+
import org.gradle.api.provider.ValueSourceParameters;
22+
import org.jetbrains.annotations.Nullable;
23+
24+
import java.io.File;
25+
import java.io.IOException;
26+
import java.util.Scanner;
27+
28+
public abstract class ValueSourceModuleInfo implements ValueSource<ModuleInfo, ValueSourceModuleInfo.Parameter> {
29+
30+
interface Parameter extends ValueSourceParameters {
31+
DirectoryProperty getDir();
32+
}
33+
34+
@Override
35+
public @Nullable ModuleInfo obtain() {
36+
Parameter parameters = getParameters();
37+
File file = new File(parameters.getDir().get().getAsFile(), "module-info.java");
38+
if (file.isFile()) {
39+
try {
40+
try (Scanner scan = new Scanner(file)) {
41+
scan.useDelimiter("\\Z");
42+
String content = scan.next();
43+
return new ModuleInfo(content);
44+
}
45+
} catch (IOException e) {
46+
throw new RuntimeException(e);
47+
}
48+
}
49+
return null;
50+
}
51+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
public abstract class ModuleDirectivesOrderingCheck extends DefaultTask {
3737

3838
@Input
39+
@Optional
3940
public abstract Property<String> getModuleInfoPath();
4041

4142
@Input

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(), false);
78+
ownModuleNamesPrefix = new ModuleInfo(fileContent).moduleNamePrefix(projectName, main.getName(), false);
7979
}
8080
break;
8181
}

src/main/resources/org/gradlex/javamodule/dependencies/unique_modules.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1151,7 +1151,7 @@ com.github.gv2011.util.gcol=com.github.gv2011:util-gcol
11511151
com.github.gv2011.util.html.imp=com.github.gv2011:util-html
11521152
com.github.gv2011.util.image=com.github.gv2011:util-image
11531153
com.github.gv2011.util.json.imp=com.github.gv2011:util-json
1154-
com.github.gv2011.util.swing=com.github.gv2011:util-swing
1154+
com.github.gv2011.util.swing.imp=com.github.gv2011:util-swing
11551155
com.github.gv2011.util.tika=com.github.gv2011:util-tika
11561156
com.github.gw2toolbelt.gw2ml=com.github.gw2toolbelt.gw2ml:gw2ml
11571157
com.github.hanshsieh.pixivj=com.github.hanshsieh:pixivj

0 commit comments

Comments
 (0)