Skip to content

Commit 7b1a4c1

Browse files
authored
Merge pull request #118 from gradlex-org/settings-plugin
Settings plugin to configure module locations and identity
2 parents b182130 + 19bfbb3 commit 7b1a4c1

File tree

20 files changed

+907
-128
lines changed

20 files changed

+907
-128
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.7
4+
* [#112](https://github.com/gradlex-org/java-module-dependencies/issues/114) Settings plugin to configure module locations and identity
5+
36
## Version 1.6.6
47
* [#113](https://github.com/gradlex-org/java-module-dependencies/issues/113) Fix: Do not fail for duplicated project names (Thanks [TheGoesen](https://github.com/TheGoesen))
58
* [#111](https://github.com/gradlex-org/java-module-dependencies/issues/111) Fix: Do not use 'MapProperty.unset' (Thanks [TheGoesen](https://github.com/TheGoesen))

README.MD

Lines changed: 108 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
[![Build Status](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Fgradlex-org%2Fjava-module-dependencies%2Fbadge%3Fref%3Dmain&style=flat)](https://actions-badge.atrox.dev/gradlex-org/java-module-dependencies/goto?ref=main)
44
[![Gradle Plugin Portal](https://img.shields.io/maven-metadata/v?label=Plugin%20Portal&metadataUrl=https%3A%2F%2Fplugins.gradle.org%2Fm2%2Forg%2Fgradlex%2Fjava-module-dependencies%2Forg.gradlex.java-module-dependencies.gradle.plugin%2Fmaven-metadata.xml)](https://plugins.gradle.org/plugin/org.gradlex.java-module-dependencies)
55

6-
A Gradle 7.4+ plugin to make Gradle use dependencies from _module-info.java_ files automatically.
6+
A Gradle plugin to make Gradle use dependencies from _module-info.java_ files automatically.
77
If you have a project that fully uses Java Modules, you do **not** need to declare dependencies in the `dependencies { }` block anymore.
88
Gradle will use the information from your `module-info.java` directly.
99

10-
To manage the versions of Java Modules, the plugin conveniently integrates with
10+
Minimal required Gradle version:
11+
- **Gradle 7.4** if you **not** use the plugin in `settings.gradle.kts`
12+
- **Gradle 8.8** to use the plugin in `settings.gradle.kts` and the [additional functionality](#project-structure-definition-when-using-this-plugin-as-settings-plugin) that comes with it.
13+
14+
To manage the versions of Java Modules, the plugin integrates with
1115
[Platform Projects](https://docs.gradle.org/current/userguide/java_platform_plugin.html#sec:java_platform_usage) and
1216
[Dependency Versions Constraints](https://docs.gradle.org/current/userguide/dependency_constraints.html#sec:adding-constraints-transitive-deps) in general
1317
as well as [Version Catalogs](https://docs.gradle.org/current/userguide/platforms.html#sub:version-catalog).
@@ -22,36 +26,28 @@ There is a [CHANGELOG.md](CHANGELOG.md).
2226

2327
# Java Modules with Gradle
2428

25-
If you plan to build Java Modules with Gradle, you should consider using these plugins on top of Gradle core:
29+
If you build Java Modules with Gradle, you should consider using these plugins on top of Gradle core:
2630

27-
- [`id("org.gradlex.java-module-dependencies")`](https://github.com/gradlex-org/java-module-dependencies)
31+
- [`id("org.gradlex.java-module-dependencies")`](https://github.com/gradlex-org/java-module-dependencies) (this plugin)
2832
Avoid duplicated dependency definitions and get your Module Path under control
2933
- [`id("org.gradlex.java-module-testing")`](https://github.com/gradlex-org/java-module-testing)
3034
Proper test setup for Java Modules
3135
- [`id("org.gradlex.extra-java-module-info")`](https://github.com/gradlex-org/extra-java-module-info)
32-
Only if your (existing) project cannot avoid using non-module legacy Jars
33-
34-
[Here is a sample](https://github.com/gradlex-org/java-module-testing/tree/main/samples/use-all-java-module-plugins)
35-
that shows all plugins in combination.
36+
Only if you cannot avoid using non-module legacy Jars
3637

3738
[In episodes 31, 32, 33 of Understanding Gradle](https://github.com/jjohannes/understanding-gradle) I explain what these plugins do and why they are needed.
3839
[<img src="https://onepiecesoftware.github.io/img/videos/31.png" width="260">](https://www.youtube.com/watch?v=X9u1taDwLSA&list=PLWQK2ZdV4Yl2k2OmC_gsjDpdIBTN0qqkE)
3940
[<img src="https://onepiecesoftware.github.io/img/videos/32.png" width="260">](https://www.youtube.com/watch?v=T9U0BOlVc-c&list=PLWQK2ZdV4Yl2k2OmC_gsjDpdIBTN0qqkE)
4041
[<img src="https://onepiecesoftware.github.io/img/videos/33.png" width="260">](https://www.youtube.com/watch?v=6rFEDcP8Noc&list=PLWQK2ZdV4Yl2k2OmC_gsjDpdIBTN0qqkE)
4142

42-
[Full Java Module System Project Setup](https://github.com/jjohannes/gradle-project-setup-howto/tree/java_module_system) is a full-fledged Java Module System project setup using these plugins.
43-
[<img src="https://onepiecesoftware.github.io/img/videos/15-3.png" width="260">](https://www.youtube.com/watch?v=uRieSnovlVc&list=PLWQK2ZdV4Yl2k2OmC_gsjDpdIBTN0qqkE)
44-
4543
# How to use?
4644

47-
For a quick start, you can find some samples here:
48-
* [samples/versions-in-platform](samples/versions-in-platform)
49-
* [samples/versions-in-catalog](samples/versions-in-catalog)
50-
* [samples/module-info-dsl](samples/module-info-dsl)
51-
* [samples/module-info-dsl-no-platform](samples/module-info-dsl-no-platform)
52-
* [samples/kotlin](samples/kotlin)
45+
Working (example) projects to inspect:
46+
- [java-module-system](https://github.com/jjohannes/java-module-system) contains a compact sample and further documentation
47+
- [gradle-project-setup-howto](https://github.com/jjohannes/gradle-project-setup-howto/tree/java_module_system) is a full-fledged Java Module System project setup
48+
- [hedera-services](https://github.com/hashgraph/hedera-services) is an open-source Java project using this plugin large scale
5349

54-
For general information about how to structure Gradle builds and apply community plugins like this one to all subprojects
50+
For general information about how to structure Gradle builds and apply community plugins like this one
5551
you can check out my [Understanding Gradle video series](https://www.youtube.com/playlist?list=PLWQK2ZdV4Yl2k2OmC_gsjDpdIBTN0qqkE).
5652

5753
## Plugin dependency
@@ -61,7 +57,7 @@ Add this to the build file of your convention plugin's build
6157

6258
```
6359
dependencies {
64-
implementation("org.gradlex:java-module-dependencies:1.6.6")
60+
implementation("org.gradlex:java-module-dependencies:1.7")
6561
}
6662
```
6763

@@ -72,12 +68,14 @@ dependencies {
7268
7369
## Apply the plugin
7470
75-
In your convention plugin, apply the plugin.
71+
The plugin can be used in two ways:
72+
73+
1. As _Settings Plugin_ in `settings.gradle(.kts)` file **(recommended)**
74+
2. As _Project Plugin_ in `build.gradle(.kts)` files (sometimes easier to add to existing setups)
7675
7776
```
7877
plugins {
79-
...
80-
id("org.gradlex.java-module-dependencies")
78+
id("org.gradlex.java-module-dependencies")
8179
}
8280
```
8381
@@ -87,15 +85,54 @@ Once the plugin is applied, dependencies are automatically determined based on t
8785
8886
```
8987
module org.example.mymodule {
90-
requires com.fasterxml.jackson.core; // -> implementation("com.fasterxml.jackson.core:jackson-core")
91-
requires transitive org.slf4j; // -> api("org.slf4j:slf4j-api")
92-
requires static jakarta.servlet; // -> compileOnly("jakarta.servlet:jakarta.servlet-api")
93-
requires /*runtime*/ org.slf4j.simple; // -> runtimeOnly("org.slf4j:slf4j-simple")
88+
requires com.fasterxml.jackson.core; // -> implementation("com.fasterxml.jackson.core:jackson-core")
89+
requires transitive org.slf4j; // -> api("org.slf4j:slf4j-api")
90+
requires static jakarta.servlet; // -> compileOnly("jakarta.servlet:jakarta.servlet-api")
91+
requires /*runtime*/ org.slf4j.simple; // -> runtimeOnly("org.slf4j:slf4j-simple")
9492
}
9593
```
9694
9795
Note that `requires /*runtime*/` is a directive specifically supported by this plugin to allow the specification of _runtime only_ dependencies.
9896
97+
## Project structure definition when using this plugin as Settings Plugin
98+
99+
The plugin offers a Gradle DSL extension to configure the location of _Java Modules_ in the project structure to be used
100+
in the `settings.gradle(.kts)` file. It is an alternative to Gradle's native `include(...)` statement to configure
101+
subprojects. The advantage of using this is that it is more compact than Gradle's `include(...)` and allows the plugin
102+
to pick up more information during the [initialization phase](https://docs.gradle.org/current/userguide/build_lifecycle.html#sec:initialization).
103+
By this, the plugin is later able to establish dependencies between your own modules without making assumptions about
104+
how they need to be named (which is different when you use the plugin as
105+
[Project Plugin](#project-structure-definition-when-using-this-plugin-as-project-plugin)).
106+
107+
```
108+
// settings.gradle(.kts)
109+
javaModules { // use instead of 'include(...)'
110+
module("module-a") // Module in directory, discovers 'src/*/java/module-info.java' files
111+
112+
module("module-b") {
113+
group = "org.example" // define group early so that all subprojects know all groups
114+
artifact = "lib-x" // Gradle subproject name (if differnt than directory)
115+
plugin("java-library") // apply plugin to the Module's subproject to omit 'build.gradle'
116+
}
117+
118+
directory("modules") { // Auto-include all Modules in subfolders of 'modules'
119+
group = "org.example" // group for all Modules
120+
plugin("java-library") // apply plugin to all Modules' subprojects
121+
module("app") { ... } // individualise Module (only if needed)
122+
}
123+
124+
versions("gradle/versions") // subproject configured as Platform Project
125+
}
126+
```
127+
128+
## Project structure definition when using this plugin as Project Plugin
129+
130+
In this setup, subprojects with Java Modules are configured as in any traditional Gradle build: by using the
131+
`include(...)` statement in `settings.gradle(.kts)`. The plugin is then applied in all subprojects with Java Modules,
132+
ideally through a convention plugin. If you use the plugin like this, it needs to [make some assumption](#naming-patterns-for-modules-in-the-build-if-used-as-project-plugin)
133+
due to missing information and thus, for example, requires you to have the Gradle _project names_, _groups_ and _Java Module Names_ align.
134+
The preferred way to use the plugin is to use it as [Settings Plugin](#project-structure-definition-when-using-this-plugin-as-settings-plugin).
135+
99136
## Define additional module dependencies in build files
100137
101138
With this plugin you move dependency definitions into `module-info.java` files and no longer use the `dependencies {}` block in build files.
@@ -104,8 +141,8 @@ For this, the plugin offers an extension of Gradle's DSL to be used in `build.gr
104141
105142
```
106143
mainModuleInfo {
107-
runtimeOnly("org.slf4j.simple") // runtime only dependency for the 'main' module
108-
annotationProcessor("dagger.compiler") // annotation processor dependency for the 'main' module
144+
runtimeOnly("org.slf4j.simple") // runtime only dependency for the 'main' module
145+
annotationProcessor("dagger.compiler") // annotation processor dependency for the 'main' module
109146
}
110147
```
111148
@@ -116,9 +153,9 @@ The only case where this should be used is for whitebox testing activated via th
116153
117154
```
118155
testModuleInfo {
119-
requires("org.assertj.core")
120-
requires("org.hamcrest")
121-
requires("org.junit.jupiter.api")
156+
requires("org.assertj.core")
157+
requires("org.hamcrest")
158+
requires("org.junit.jupiter.api")
122159
}
123160
```
124161
@@ -142,18 +179,18 @@ org.apache.commons.lang3.test.fixtures=org.apache.commons:commons-lang3|test-fix
142179
143180
```
144181
javaModuleDependencies {
145-
// Module Name to Component GA Coordinates
146-
moduleNameToGA.put("org.apache.commons.lang3", "org.apache.commons:commons-lang3")
182+
// Module Name to Component GA Coordinates
183+
moduleNameToGA.put("org.apache.commons.lang3", "org.apache.commons:commons-lang3")
147184

148-
// Module Name to Component GA Coordinates & Capability GA Coordinates
149-
moduleNameToGA.put("org.apache.commons.lang3.test.fixtures", "org.apache.commons:commons-lang3|test-fixtures")
185+
// Module Name to Component GA Coordinates & Capability GA Coordinates
186+
moduleNameToGA.put("org.apache.commons.lang3.test.fixtures", "org.apache.commons:commons-lang3|test-fixtures")
150187
}
151188
```
152189
153190
There is also the option to register a mapping for all Modules that share a common _name prefix_ and _group_.
154191
For example: `moduleNamePrefixToGroup.put("com.example.product.module.", "com.example.product")`.
155192
156-
## Naming patterns for Modules in the build
193+
## Naming patterns for Modules in the build (if used as Project Plugin)
157194
158195
This plugin makes the following assumption about _Module Names_ of your own Modules in the build to establish dependencies between them:
159196
@@ -177,22 +214,22 @@ For libraries that consist of multiple components and have a BOM for version man
177214
178215
```
179216
plugins {
180-
id("java-platform")
181-
id("org.gradlex.java-module-versions")
217+
id("java-platform")
218+
id("org.gradlex.java-module-versions")
182219
}
183220

184221
// Define versions for Modules via the Module Name
185222
moduleInfo {
186-
version("org.apache.xmlbeans", "5.0.1")
187-
version("org.slf4j", "2.0.7")
188-
version("org.slf4j.simple", "2.0.7")
223+
version("org.apache.xmlbeans", "5.0.1")
224+
version("org.slf4j", "2.0.7")
225+
version("org.slf4j.simple", "2.0.7")
189226
}
190227

191228
// Use BOMs for Modules that are part of a library of multiple Modules
192229
javaPlatform.allowDependencies()
193230
dependencies {
194-
api(platform("com.fasterxml.jackson:jackson-bom:2.13.2"))
195-
api(platform("org.junit:junit-bom:5.8.2"))
231+
api(platform("com.fasterxml.jackson:jackson-bom:2.13.2"))
232+
api(platform("org.junit:junit-bom:5.8.2"))
196233
}
197234
```
198235
@@ -201,9 +238,9 @@ For example:
201238
202239
```
203240
dependencies {
204-
javaModuleDependencies {
205-
testRuntimeOnly(ga("org.junit.jupiter.engine"))
206-
}
241+
javaModuleDependencies {
242+
testRuntimeOnly(ga("org.junit.jupiter.engine"))
243+
}
207244
}
208245
```
209246
@@ -216,13 +253,13 @@ Alternatively, versions can be defined in the `[version]` block of a [version ca
216253
217254
```
218255
dependencyResolutionManagement {
219-
versionCatalogs.create("libs") {
220-
version("org.apache.xmlbeans", "5.0.1")
221-
version("com.fasterxml.jackson.databind", "2.12.5")
222-
version("org.slf4j", "2.0.7")
223-
224-
version("org.junit.jupiter.api", "5.8.2")
225-
}
256+
versionCatalogs.create("libs") {
257+
version("org.apache.xmlbeans", "5.0.1")
258+
version("com.fasterxml.jackson.databind", "2.12.5")
259+
version("org.slf4j", "2.0.7")
260+
261+
version("org.junit.jupiter.api", "5.8.2")
262+
}
226263
}
227264
```
228265
@@ -250,19 +287,19 @@ $ ./gradlew :app:recommendModuleVersions -q
250287
Latest Stable Versions of Java Modules - use in your platform project's build.gradle(.kts)
251288
==========================================================================================
252289
moduleInfo {
253-
version("com.fasterxml.jackson.annotation", "2.13.2")
254-
version("com.fasterxml.jackson.core", "2.13.2")
255-
version("com.fasterxml.jackson.databind", "2.13.2.2")
256-
version("org.apache.logging.log4j", "2.17.2")
257-
version("org.apache.xmlbeans", "5.0.3")
258-
version("org.junit.jupiter.api", "5.8.2")
259-
version("org.junit.jupiter.engine", "5.8.2")
260-
version("org.junit.platform.commons", "1.8.2")
261-
version("org.junit.platform.engine", "1.8.2")
262-
version("org.junit.platform.launcher", "1.8.2")
263-
version("org.opentest4j", "1.2.0")
264-
version("org.slf4j", "1.7.36")
265-
version("org.slf4j.simple", "1.7.36")
290+
version("com.fasterxml.jackson.annotation", "2.13.2")
291+
version("com.fasterxml.jackson.core", "2.13.2")
292+
version("com.fasterxml.jackson.databind", "2.13.2.2")
293+
version("org.apache.logging.log4j", "2.17.2")
294+
version("org.apache.xmlbeans", "5.0.3")
295+
version("org.junit.jupiter.api", "5.8.2")
296+
version("org.junit.jupiter.engine", "5.8.2")
297+
version("org.junit.platform.commons", "1.8.2")
298+
version("org.junit.platform.engine", "1.8.2")
299+
version("org.junit.platform.launcher", "1.8.2")
300+
version("org.opentest4j", "1.2.0")
301+
version("org.slf4j", "1.7.36")
302+
version("org.slf4j.simple", "1.7.36")
266303
}
267304
```
268305
@@ -391,14 +428,14 @@ Module Name mappings for Jars that were patched with extra module info will be a
391428
392429
```
393430
plugins {
394-
id("org.gradlex.extra-java-module-info")
395-
id("org.gradlex.java-module-dependencies")
431+
id("org.gradlex.extra-java-module-info")
432+
id("org.gradlex.java-module-dependencies")
396433
}
397434

398435
extraJavaModuleInfo {
399-
automaticModule("org.apache.commons:commons-math3", "commons.math3")
400-
// Module Dependencies plugin automatically knows that
401-
// 'commons.math3' now maps to 'org.apache.commons:commons-math3'
436+
automaticModule("org.apache.commons:commons-math3", "commons.math3")
437+
// Module Dependencies plugin automatically knows that
438+
// 'commons.math3' now maps to 'org.apache.commons:commons-math3'
402439
}
403440
```
404441

build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55
}
66

77
group = "org.gradlex"
8-
version = "1.6.6"
8+
version = "1.7"
99

1010
tasks.withType<JavaCompile>().configureEach {
1111
options.release = 8
@@ -73,6 +73,7 @@ testing.suites.named<JvmTestSuite>("test") {
7373
description = "Runs tests against Gradle $gradleVersionUnderTest"
7474
systemProperty("gradleVersionUnderTest", gradleVersionUnderTest)
7575
exclude("**/*SamplesTest.class") // Not yet cross-version ready
76+
exclude("**/initialization/**") // Settings plugin only for Gradle 8.8+
7677
}
7778
}
7879
}

samples/configuration-cache/build-logic/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ plugins {
33
}
44

55
dependencies {
6-
implementation("org.gradlex:java-module-dependencies:1.6.6")
6+
implementation("org.gradlex:java-module-dependencies:1.7")
77
}

samples/kotlin/build-logic/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ plugins {
33
}
44

55
dependencies {
6-
implementation("org.gradlex:java-module-dependencies:1.6.6")
6+
implementation("org.gradlex:java-module-dependencies:1.7")
77
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.21")
88
}

samples/module-info-dsl-no-platform/gradle/plugins/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ plugins {
44

55
dependencies {
66
implementation("com.autonomousapps:dependency-analysis-gradle-plugin:1.21.0")
7-
implementation("org.gradlex:java-module-dependencies:1.6.6")
7+
implementation("org.gradlex:java-module-dependencies:1.7")
88
implementation("org.gradlex:java-module-testing:1.2.1")
99
}

samples/module-info-dsl/gradle/plugins/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ plugins {
44

55
dependencies {
66
implementation("com.autonomousapps:dependency-analysis-gradle-plugin:1.21.0")
7-
implementation("org.gradlex:java-module-dependencies:1.6.6")
7+
implementation("org.gradlex:java-module-dependencies:1.7")
88
implementation("org.gradlex:java-module-testing:1.2.1")
99
}

samples/versions-in-catalog/build-logic/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ plugins {
33
}
44

55
dependencies {
6-
implementation("org.gradlex:java-module-dependencies:1.6.6")
6+
implementation("org.gradlex:java-module-dependencies:1.7")
77
}

samples/versions-in-platform/build-logic/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ plugins {
33
}
44

55
dependencies {
6-
implementation("org.gradlex:java-module-dependencies:1.6.6")
6+
implementation("org.gradlex:java-module-dependencies:1.7")
77
}

0 commit comments

Comments
 (0)