Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Java Module Dependencies Gradle Plugin - Changelog

## Version 1.8
* [#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))

## Version 1.7.1
* Update module name mappings

Expand Down
5 changes: 5 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ testing.suites.named<JvmTestSuite>("test") {
systemProperty("gradleVersionUnderTest", gradleVersionUnderTest)
exclude("**/*SamplesTest.class") // Not yet cross-version ready
exclude("**/initialization/**") // Settings plugin only for Gradle 8.8+
if (gradleVersionUnderTest == "7.4") {
// Configuration cache only "reliable" since 7.6 (?)
// https://github.com/gradlex-org/java-module-dependencies/issues/129
exclude("**/configcache/**")
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,16 @@ private void setupOrderingCheckTasks(Project project, TaskProvider<Task> checkAl
SourceSetContainer sourceSets = project.getExtensions().getByType(SourceSetContainer.class);
ConfigurationContainer configurations = project.getConfigurations();

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

ModuleInfo moduleInfo = javaModuleDependencies.getModuleInfoCache().get().get(sourceSet, project.getProviders());

t.getModuleInfoPath().convention(moduleInfo.getFilePath().getAbsolutePath());
File folder = javaModuleDependencies.getModuleInfoCache().get().getFolder(sourceSet, project.getProviders());
if (folder != null) {
t.getModuleInfoPath().convention(new File(folder, "module-info.java").getAbsolutePath());
}
t.getModuleNamePrefix().convention(moduleInfo.moduleNamePrefix(project.getName(), sourceSet.getName(), false));
t.getModuleInfo().convention(moduleInfo);

Expand Down Expand Up @@ -266,11 +268,11 @@ private void readModuleInfo(ModuleInfo.Directive moduleDirective, SourceSet sour
}
ModuleInfo moduleInfo = javaModuleDependenciesExtension.getModuleInfoCache().get().get(sourceSet, project.getProviders());
for (String moduleName : moduleInfo.get(moduleDirective)) {
declareDependency(moduleName, moduleInfo.getFilePath(), project, sourceSet, configuration, javaModuleDependenciesExtension);
declareDependency(moduleName, project, sourceSet, configuration, javaModuleDependenciesExtension);
}
}

private void declareDependency(String moduleName, File moduleInfoFile, Project project, SourceSet sourceSet, Configuration configuration, JavaModuleDependenciesExtension javaModuleDependencies) {
private void declareDependency(String moduleName, Project project, SourceSet sourceSet, Configuration configuration, JavaModuleDependenciesExtension javaModuleDependencies) {
if (JDKInfo.MODULES.contains(moduleName)) {
// The module is part of the JDK, no dependency required
return;
Expand All @@ -286,7 +288,7 @@ private List<BuildFileDependenciesGenerate.DependencyDeclaration> collectDepende
File sourceSetDir = sourceSet.getJava().getSrcDirs().iterator().next().getParentFile();
File whiteboxModuleInfoFile = new File(sourceSetDir, "java9/module-info.java");
if (whiteboxModuleInfoFile.exists()) {
moduleInfo = new ModuleInfo(project.getProviders().fileContents(project.getLayout().getProjectDirectory().file(whiteboxModuleInfoFile.getAbsolutePath())).getAsText().get(), whiteboxModuleInfoFile);
moduleInfo = new ModuleInfo(project.getProviders().fileContents(project.getLayout().getProjectDirectory().file(whiteboxModuleInfoFile.getAbsolutePath())).getAsText().get());
}
}
return moduleInfo.get(directive).stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ private void registerCatalogTask(Project project) {
moduleInfoFile = new File(srcDirSet, "java9/module-info.java");
}
if (moduleInfoFile.exists()) {
ModuleInfo moduleInfo = new ModuleInfo(project.getProviders().fileContents(project.getLayout().getProjectDirectory().file(moduleInfoFile.getAbsolutePath())).getAsText().get(), moduleInfoFile);
ModuleInfo moduleInfo = new ModuleInfo(project.getProviders().fileContents(project.getLayout().getProjectDirectory().file(moduleInfoFile.getAbsolutePath())).getAsText().get());
t.getEntries().addAll(collectCatalogEntriesFromModuleInfos(javaModuleDependencies, moduleInfo.get(REQUIRES_TRANSITIVE)));
t.getEntries().addAll(collectCatalogEntriesFromModuleInfos(javaModuleDependencies, moduleInfo.get(REQUIRES)));
t.getEntries().addAll(collectCatalogEntriesFromModuleInfos(javaModuleDependencies, moduleInfo.get(REQUIRES_STATIC_TRANSITIVE)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
package org.gradlex.javamodule.dependencies.internal.utils;

import javax.annotation.Nullable;
import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

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

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

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

public static final ModuleInfo EMPTY = new ModuleInfo("", new File(""));
public static final ModuleInfo EMPTY = new ModuleInfo("");

private String moduleName = "";
private final List<String> requires = new ArrayList<>();
Expand All @@ -52,12 +52,10 @@ public String literal() {
private final List<String> requiresStaticTransitive = new ArrayList<>();
private final List<String> requiresRuntime = new ArrayList<>();

private final File filePath;

public ModuleInfo(String moduleInfoFileContent, File filePath) {
this.filePath = filePath;
public ModuleInfo(String moduleInfoFileContent) {
boolean insideComment = false;
for(String line: moduleInfoFileContent.split("\n")) {
for (String line : moduleInfoFileContent.split("\n")) {
insideComment = parse(line, insideComment);
}
}
Expand Down Expand Up @@ -108,10 +106,6 @@ public String moduleNamePrefix(String projectName, String sourceSetName, boolean
return null;
}

public File getFilePath() {
return filePath;
}

/**
* @return true, if we are inside a multi-line comment after this line
*/
Expand Down Expand Up @@ -150,4 +144,29 @@ private boolean parse(String moduleLine, boolean insideComment) {
}
return moduleLine.lastIndexOf("/*") > moduleLine.lastIndexOf("*/");
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ModuleInfo that = (ModuleInfo) o;
return Objects.equals(moduleName, that.moduleName)
&& Objects.equals(requires, that.requires)
&& Objects.equals(requiresTransitive, that.requiresTransitive)
&& Objects.equals(requiresStatic, that.requiresStatic)
&& Objects.equals(requiresStaticTransitive, that.requiresStaticTransitive)
&& Objects.equals(requiresRuntime, that.requiresRuntime);
}

@Override
public int hashCode() {
return Objects.hash(
moduleName,
requires,
requiresTransitive,
requiresStatic,
requiresStaticTransitive,
requiresRuntime
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

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

import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.logging.Logger;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.Provider;
Expand Down Expand Up @@ -69,6 +68,15 @@ public ModuleInfo get(SourceSet sourceSet, ProviderFactory providers) {
return ModuleInfo.EMPTY;
}

public File getFolder(SourceSet sourceSet, ProviderFactory providers) {
for (File folder : sourceSet.getJava().getSrcDirs()) {
if (maybePutModuleInfo(folder, providers)) {
return folder;
}
}
return null;
}

/**
* @param projectRoot the project that should hold a Java module
* @return parsed module-info.java for the given project assuming a standard Java project layout
Expand Down Expand Up @@ -102,15 +110,17 @@ public String getCapability(String moduleName) {
}

private boolean maybePutModuleInfo(File folder, ProviderFactory providers) {
RegularFileProperty moduleInfoFile = getObjects().fileProperty();
moduleInfoFile.set(new File(folder, "module-info.java"));
Provider<String> moduleInfoContent = providers.fileContents(moduleInfoFile).getAsText();
if (moduleInfoContent.isPresent()) {
Provider<ModuleInfo> moduleInfoProvider = provideModuleInfo(folder, providers);
if (moduleInfoProvider.isPresent()) {
if (!moduleInfo.containsKey(folder)) {
moduleInfo.put(folder, new ModuleInfo(moduleInfoContent.get(), moduleInfoFile.get().getAsFile()));
moduleInfo.put(folder, moduleInfoProvider.get() );
}
return true;
}
return false;
}

private Provider<ModuleInfo> provideModuleInfo(File folder, ProviderFactory providers) {
return providers.of(ValueSourceModuleInfo.class, spec -> spec.parameters(param -> param.getDir().set(folder)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright the GradleX team.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.provider.ValueSource;
import org.gradle.api.provider.ValueSourceParameters;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public abstract class ValueSourceModuleInfo implements ValueSource<ModuleInfo, ValueSourceModuleInfo.Parameter> {

interface Parameter extends ValueSourceParameters {
DirectoryProperty getDir();
}

@Override
public @Nullable ModuleInfo obtain() {
Parameter parameters = getParameters();
File file = new File(parameters.getDir().get().getAsFile(), "module-info.java");
if (file.isFile()) {
try {
try (Scanner scan = new Scanner(file)) {
scan.useDelimiter("\\Z");
String content = scan.next();
return new ModuleInfo(content);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
public abstract class ModuleDirectivesOrderingCheck extends DefaultTask {

@Input
@Optional
public abstract Property<String> getModuleInfoPath();

@Input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void report() throws IOException {
if (file.exists()) {
try(Stream<String> lines = Files.lines(file.toPath())) {
String fileContent = lines.collect(Collectors.joining("\n"));
ownModuleNamesPrefix = new ModuleInfo(fileContent, file).moduleNamePrefix(projectName, main.getName(), false);
ownModuleNamesPrefix = new ModuleInfo(fileContent).moduleNamePrefix(projectName, main.getName(), false);
}
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1151,7 +1151,7 @@ com.github.gv2011.util.gcol=com.github.gv2011:util-gcol
com.github.gv2011.util.html.imp=com.github.gv2011:util-html
com.github.gv2011.util.image=com.github.gv2011:util-image
com.github.gv2011.util.json.imp=com.github.gv2011:util-json
com.github.gv2011.util.swing=com.github.gv2011:util-swing
com.github.gv2011.util.swing.imp=com.github.gv2011:util-swing
com.github.gv2011.util.tika=com.github.gv2011:util-tika
com.github.gw2toolbelt.gw2ml=com.github.gw2toolbelt.gw2ml:gw2ml
com.github.hanshsieh.pixivj=com.github.hanshsieh:pixivj
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ModuleInfoParseTest extends Specification {
// requires com.bla.blub;
requires transitive foo.bar.la;
}
''', new File(''))
''')

expect:
moduleInfo.moduleNamePrefix("thing", "main", false) == "some"
Expand All @@ -35,7 +35,7 @@ class ModuleInfoParseTest extends Specification {
module some.thing { // module some.thing.else
requires transitive foo.bar.la;
}
''', new File(''))
''')

expect:
moduleInfo.moduleNamePrefix("thing", "main", false) == "some"
Expand All @@ -55,7 +55,7 @@ class ModuleInfoParseTest extends Specification {
*/
requires static foo.bar.la;
}
''', new File(''))
''')

expect:
moduleInfo.get(REQUIRES) == []
Expand All @@ -75,7 +75,7 @@ class ModuleInfoParseTest extends Specification {
requires only.a.comment
*/
}
''', new File(''))
''')

expect:
moduleInfo.get(REQUIRES) == ["foo.bar.li"]
Expand All @@ -91,7 +91,7 @@ class ModuleInfoParseTest extends Specification {
module some.thing {
requires /*runtime*/ foo.bar.lo;
}
''', new File(''))
''')

expect:
moduleInfo.get(REQUIRES) == []
Expand Down
Loading