Skip to content

Commit 4b66b5c

Browse files
committed
Support using Gradle's 'include' in settings plugin
This allows to configure project paths individually via the native include("...") statement while still using other functionality provided by the settings plugin.
1 parent a870abc commit 4b66b5c

File tree

5 files changed

+50
-19
lines changed

5 files changed

+50
-19
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Java Module Dependencies Gradle Plugin - Changelog
22

33
## Version 1.8
4+
* [#136](https://github.com/gradlex-org/java-module-dependencies/pull/136) Support hierarchical project paths in Settings DSL
45
* [#141](https://github.com/gradlex-org/java-module-dependencies/pull/141) Introduce `org.gradlex.java-module-dependencies.register-help-tasks` property
56
* [#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))
67
* [#128](https://github.com/gradlex-org/java-module-dependencies/issues/128) Less configuration cache misses when using Settings plugin (Thanks [TheGoesen](https://github.com/TheGoesen))

README.MD

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,19 @@ javaModules { // use instead of 'include(...)'
129129
}
130130
```
131131
132+
If you need more control over the properties of a Gradle subproject, in particular to define a nested project path,
133+
you can still use Gradle's `include(...)` and then register the subproject with this plugin.
134+
135+
```
136+
include(":project:with:custom:path")
137+
javaModules {
138+
module(project(":project:with:custom:path")) {
139+
group = "org.example" // define group early so that all subprojects know all groups
140+
plugin("java-library") // apply plugin to the Module's subproject to omit 'build.gradle'
141+
}
142+
}
143+
```
144+
132145
## Project structure definition when using this plugin as Project Plugin
133146
134147
In this setup, subprojects with Java Modules are configured as in any traditional Gradle build: by using the

src/main/java/org/gradlex/javamodule/dependencies/initialization/Directory.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import org.gradle.api.model.ObjectFactory;
2121
import org.gradle.api.provider.ListProperty;
2222
import org.gradle.api.provider.Property;
23-
import org.gradle.api.provider.Provider;
2423

2524
import javax.inject.Inject;
2625
import java.io.File;
@@ -80,8 +79,7 @@ public void module(String subDirectory, Action<Module> action) {
8079
}
8180

8281
Module addModule(String subDirectory) {
83-
Module module = getObjects().newInstance(Module.class, root);
84-
module.getDirectory().convention(subDirectory);
82+
Module module = getObjects().newInstance(Module.class, new File(root, subDirectory));
8583
module.getGroup().convention(getGroup());
8684
module.getPlugins().addAll(getPlugins());
8785
return module;

src/main/java/org/gradlex/javamodule/dependencies/initialization/JavaModulesExtension.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,27 @@ public void module(String directory) {
7272
* Register and configure Module located in the given folder, relative to the build root directory.
7373
*/
7474
public void module(String directory, Action<Module> action) {
75-
Module module = getObjects().newInstance(Module.class, settings.getRootDir());
76-
module.getDirectory().set(directory);
75+
Module module = getObjects().newInstance(Module.class, new File(settings.getRootDir(), directory));
7776
action.execute(module);
78-
includeModule(module, new File(settings.getRootDir(), module.getDirectory().get()));
77+
includeModule(module, new File(settings.getRootDir(), directory));
78+
}
79+
80+
/**
81+
* {@link JavaModulesExtension#module(ProjectDescriptor, Action)}
82+
*/
83+
public void module(ProjectDescriptor project) {
84+
module(project, m -> {});
85+
}
86+
87+
/**
88+
* Register and configure Module already registered as project by an 'include' statement.
89+
*/
90+
public void module(ProjectDescriptor project, Action<Module> action) {
91+
Module module = getObjects().newInstance(Module.class, project.getProjectDir());
92+
module.getArtifact().set(project.getName());
93+
module.getArtifact().finalizeValue(); // finalize, as the project name can no longer be changed
94+
action.execute(module);
95+
configureModule(module, project.getProjectDir());
7996
}
8097

8198
/**
@@ -94,7 +111,7 @@ public void directory(String directory, Action<Directory> action) {
94111
action.execute(moduleDirectory);
95112

96113
for (Module module : moduleDirectory.customizedModules.values()) {
97-
includeModule(module, new File(modulesDirectory, module.getDirectory().get()));
114+
includeModule(module, module.directory);
98115
}
99116
Provider<List<String>> listProvider = getProviders().of(ValueModuleDirectoryListing.class, spec -> {
100117
spec.getParameters().getExclusions().set(moduleDirectory.getExclusions());
@@ -128,6 +145,10 @@ private void includeModule(Module module, File projectDir) {
128145
ProjectDescriptor project = settings.project(":" + artifact);
129146
project.setProjectDir(projectDir);
130147

148+
configureModule(module, projectDir);
149+
}
150+
151+
private void configureModule(Module module, File projectDir) {
131152
String mainModuleName = null;
132153
for (String path : module.getModuleInfoPaths().get()) {
133154
ModuleInfo moduleInfo = moduleInfoCache.put(projectDir, path,
@@ -139,7 +160,7 @@ private void includeModule(Module module, File projectDir) {
139160

140161
String group = module.getGroup().getOrNull();
141162
List<String> plugins = module.getPlugins().get();
142-
moduleProjects.add(new ModuleProject(artifact, group, plugins, mainModuleName));
163+
moduleProjects.add(new ModuleProject(module.getArtifact().get(), group, plugins, mainModuleName));
143164
}
144165

145166
private static class ModuleProject {

src/main/java/org/gradlex/javamodule/dependencies/initialization/Module.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@
2828

2929
public abstract class Module {
3030

31-
/**
32-
* The directory, relative to the build root directory, in which the Module is located.
33-
*/
34-
public abstract Property<String> getDirectory();
35-
3631
/**
3732
* The 'artifact' name of the Module. This corresponds to the Gradle subproject name. If the Module is published
3833
* to a Maven repository, this is the 'artifact' in the 'group:artifact' identifier to address the published Jar.
@@ -58,14 +53,17 @@ public abstract class Module {
5853
*/
5954
public abstract ListProperty<String> getPlugins();
6055

56+
File directory;
57+
6158
@Inject
62-
public Module(File root) {
63-
getArtifact().convention(getDirectory().map(f -> Paths.get(f).getFileName().toString()));
64-
getModuleInfoPaths().convention(getDirectory().map(projectDir -> listChildren(root, projectDir + "/src")
59+
public Module(File directory) {
60+
this.directory = directory;
61+
getArtifact().convention(directory.getName());
62+
getModuleInfoPaths().convention(listSrcChildren()
6563
.map(srcDir -> new File(srcDir, "java/module-info.java"))
6664
.filter(File::exists)
6765
.map(moduleInfo -> "src/" + moduleInfo.getParentFile().getParentFile().getName() + "/java")
68-
.collect(Collectors.toList())));
66+
.collect(Collectors.toList()));
6967
}
7068

7169
/**
@@ -76,8 +74,8 @@ public void plugin(String id) {
7674
getPlugins().add(id);
7775
}
7876

79-
private Stream<File> listChildren(File root, String projectDir) {
80-
File[] children = new File(root, projectDir).listFiles();
77+
private Stream<File> listSrcChildren() {
78+
File[] children = new File(directory, "src").listFiles();
8179
return children == null ? Stream.empty() : Arrays.stream(children);
8280
}
8381
}

0 commit comments

Comments
 (0)