-
Notifications
You must be signed in to change notification settings - Fork 181
Add documentation for multi-release and multi-module projects #976
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
<!-- | ||
Licensed to the Apache Software Foundation (ASF) under one | ||
or more contributor license agreements. See the NOTICE file | ||
distributed with this work for additional information | ||
regarding copyright ownership. The ASF licenses this file | ||
to you 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. | ||
--> | ||
|
||
# Module-info patch | ||
|
||
For white box testing, it is necessary to use compiler options such as | ||
`--patch-module`, `--add-modules`, `--add-reads`, `--add-exports` and `--add-opens`. | ||
Writing these options inside the Maven `<compilerArgs>` XML element is tedious, redundant | ||
(the name of the module to patch is repeated in every occurrence of some options), error prone, | ||
and must be repeated in every plugins that depends on the tests (Surefire, Javadoc for test documentation, _etc._). | ||
An alternative is to put a `module-info.java` file in the tests which *replace* the `module-info.java` file of the main code. | ||
However, it forces the developer to repeat all the content of the main `module-info.java` | ||
into the test `module-info.java` before to add test-specific statements. | ||
This is tedious if the main `module-info.java` is large, and risky if the two files become out of sync. | ||
|
||
Instead of defining a `module-info.java` file in test, Maven projects can define a `module-info-patch.maven`. | ||
The content of `module-info-patch.maven` uses the same syntax as Java, C/C++, JavaScript, Groovy, _etc._ | ||
(comments between `/*` … `*/` or after `//`, blocks between `{` … `}`, statements ending with `;`) | ||
but is not Java, hence the `.maven` file suffix. | ||
The general principles are: | ||
|
||
* Everything that a developer would like to change in a `module-info.java` file for testing purposes is declared in `module-info-patch.maven`. | ||
* Everything that is not in `module-info.java` is not in `module-info-patch.maven` neither. | ||
In particular, everything that specify paths to JAR files or paths to source code stay in the `pom.xml` file. | ||
* All keywords except `patch-module`, `SUBPROJECT-MODULES` and `TEST-MODULE-PATH` | ||
map directly to Java compiler or Java launcher options. | ||
|
||
Compared to declaring options in `<compilerArgs>` XML elements, the `module-info-patch.maven` file is more readable, | ||
keep the options in separated files for each module on which the options are applied, is less redundant as it avoids | ||
the need to repeat the module name in every `--add-reads`, `--add-exports` and `--add-opens` options, | ||
and is more flexibly as it is translated in slightly different options for compilation and test executions | ||
(e.g. `TEST-MODULE-PATH` means modules having `test` and `test-only` Maven's scope at compilation time, | ||
but means modules having `test` and `test-runtime` Maven's scope at execution time). | ||
|
||
|
||
## Syntax | ||
|
||
The syntax is: | ||
desruisseaux marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
* The same styles of comment as Java (`/*` … `*/` and `//`) are accepted. | ||
* The first tokens, after comments, shall be `patch-module` followed by the name of the module to patch. | ||
* All keywords inside `patch-module` are Java compiler or Java launcher options without the leading `--` characters. | ||
* Each option value ends at the `;` character, which is mandatory. | ||
|
||
The accepted keywords are `add-modules`, `limit-modules`, `add-reads`, `add-exports` and `add-opens`. | ||
Note that they are options where the values are package or module names, not paths to source or binary files. | ||
Options with path values (`--module-path`, `--module-source-path`, `--patch-module`, _etc._) | ||
continue to be derived from the dependencies declared in the POM. | ||
|
||
|
||
### Options applying to all modules | ||
desruisseaux marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
All options declared in a `module-info-patch.maven` file apply only to the module declared after the `patch-module` token, | ||
except the `--add-modules` and `--limit-modules` options. | ||
These two options apply to all modules in a multi-modules project, | ||
because these options given to `java` or `javac` expect no module name. | ||
Therefore, it is not necessary to repeat `add-modules TEST-MODULE-PATH` in all modules: | ||
declaring that particular option in only one module of a multi-modules project is sufficient. | ||
If the `--add-modules` or `--limit-modules` options are declared in many `module-info-patch.maven` files of a multi-modules project, | ||
then the effective value is the union of the values declared in each file, without duplicated values. | ||
|
||
|
||
### Special option values | ||
desruisseaux marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
The following option values have special meanings: | ||
|
||
* `SUBPROJECT-MODULES`: all other modules in the current Maven (sub)project. | ||
* This is Maven-specific, not a standard value recognized by Java tools. | ||
* Allowed in: `add-exports`. | ||
* `TEST-MODULE-PATH`: all dependencies having a test scope in the build tools. | ||
* This is specific to this format, not a standard value recognized by Java tools. | ||
* Allowed in: `add-modules`, `add-reads` and `add-exports` options. | ||
* `ALL-MODULE-PATH`: everything on the module path, regardless if test or main. | ||
* This is a standard value accepted by the Java compiler. | ||
* Allowed in: `add-modules` option. | ||
* `ALL-UNNAMED`: all non-modular dependencies. | ||
* This is a standard value accepted by the Java compiler. | ||
* Allowed in: `add-exports` option. | ||
|
||
|
||
## Example | ||
desruisseaux marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Below is an example of a `module-info-patch.maven` file content | ||
for modifying the `module-info` of a module named `org.foo.bar`: | ||
|
||
```java | ||
/* | ||
* The same comments as in Java are allowed. | ||
*/ | ||
patch-module org.foo.bar { // Put here the name of the module to patch. | ||
add-modules TEST-MODULE-PATH; // Recommended value in the majority of cases. | ||
|
||
add-reads org.junit.jupiter.api, // Frequently used dependency for tests. | ||
my.product.test.fixture; // Put here any other dependency needed for tests. | ||
|
||
add-exports org.foo.bar.internal // Name of a package which is normally not exported. | ||
to org.junit.jupiter.api, // Any module that need access to above package for testing. | ||
org.something.else; // Can export to many modules, as a coma-separated list. | ||
|
||
add-exports org.foo.bar.fixtures // Another package to export. It may be a package defined in the tests. | ||
to org.foo.bar.other; // Another module of this project which may want to reuse test fixtures. | ||
} | ||
``` | ||
|
||
|
||
### How module info patches are compiled | ||
desruisseaux marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
`module-info-patch.maven` are compiled into a file of options in the following ways: | ||
|
||
* `add-modules org.foo, org.bar;` is translated to `--add-modules org.foo,org.bar`. | ||
* Note: spaces between `org.foo` and `org.bar` are removed for interpreting the option values as a single argument. | ||
* `limit-modules org.foo, org.bar;` is translated to `--limit-modules org.foo,org.bar`. | ||
* Note: idem regarding spaces removal. | ||
* `add-reads org.foo, org.bar;` is translated to `--add-reads org.patched=org.foo,org.bar` | ||
where `org.patched` is the module name declared in the first statement of the `module-info-patch` file. | ||
* `add-exports com.biz to org.foo, org.bar;` is translated to `--add-exports org.patched/com.biz=org.foo,org.bar` | ||
where `org.patched` is as above. | ||
* `add-opens com.biz to org.foo, org.bar;` is translated to `--add-opens org.patched/com.biz=org.foo,org.bar` | ||
like above but only for runtime execution, not for compilation. | ||
|
||
There is a separated `module-info-patch.maven` file for each module, | ||
and the Maven compiler plugin merges them in a single set of options for `java` and `javac`. | ||
While this format does not require the use of module source hierarchy, it fits nicely in that hierarchy. | ||
|
||
The results of the translation to compiler options can be seen in the `target/javac.args` and `target/javac-test.args` files. | ||
Those files are produced when the build failed or when Maven was executed with the `--verbose` command-line option. | ||
In addition, a slightly different set of options, suitable for tests execution, is written in the | ||
`target/test-classes/META-INF/maven/module-info-patch.args` file. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
<!-- | ||
Licensed to the Apache Software Foundation (ASF) under one | ||
or more contributor license agreements. See the NOTICE file | ||
distributed with this work for additional information | ||
regarding copyright ownership. The ASF licenses this file | ||
to you 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. | ||
--> | ||
|
||
# Modular projects | ||
|
||
The Maven 3 way to make a modular project is to put a `module-info.java` file in the root directory of Java source files. | ||
Because the compilation and execution of tests usually require an amended version of module information, | ||
Maven 3 allows to overwrite that file with another `module-info.java` file placed in the test source directory. | ||
While this approach is still supported in Maven 4 for compatibility reasons, | ||
it is deprecated and may no longer be supported in a future version. | ||
Developers are encouraged to migrate to the approach described below. | ||
|
||
|
||
## Maven 3 | ||
|
||
The directory layout of a modular project in Maven 3 was as below: | ||
|
||
``` | ||
src | ||
├─ main | ||
│ └─ java | ||
│ ├─ module-info.java | ||
│ └─ org/foo/bar/*.java | ||
├─ test | ||
│ └─ java | ||
│ ├─ module-info.java (optional) | ||
│ └─ org/foo/bar/*.java | ||
└─ target | ||
└─ classes | ||
└─ org/foo/bar/*.class | ||
``` | ||
|
||
An alternative to the `test/java/module-info.java` file is to declare compiler arguments | ||
such as `--add-reads` in the `<testCompilerArgs>` element of the plugin configuration. | ||
|
||
|
||
## Maven 4 with package hierarchy | ||
|
||
Maven 4 allows the same directory layout as Maven 3. | ||
However, the `module-info.java` file in the test directory *should* be | ||
replaced by a `module-info-patch.maven` file in the same directory. | ||
|
||
``` | ||
src | ||
├─ main | ||
│ └─ java | ||
│ ├─ module-info.java | ||
│ └─ org/foo/bar/*.java | ||
├─ test | ||
│ └─ java | ||
│ ├─ module-info-patch.maven (optional) | ||
│ └─ org/foo/bar/*.java | ||
└─ target | ||
└─ classes | ||
└─ org/foo/bar/*.class | ||
``` | ||
|
||
The Maven compiler automatically adds `--patch-module`, `--add-modules` and `--add-reads` arguments for compiling the tests. | ||
If more `--add-reads` arguments are needed, or if `--add-modules`, `--add-exports` or `--add-opens` arguments are also needed, | ||
then a `module-info-patch.maven` file (syntax described below) can be placed in the `test/java` directory. | ||
This Maven file is preferred to a `module-info.java` file in the test directory because the Maven file | ||
*completes* the main `module-info.class` (using compiler arguments) instead of *replacing* it. | ||
|
||
|
||
### Limitation | ||
desruisseaux marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
When using the package hierarchy, problems may occur if the module name is a single name without `.` separator | ||
(for example, `foo` or `bar` but not `foo.bar`) and that name is identical to a package name. | ||
In such case, the hack implemented in the Maven compiler plugin for Maven 3 compatibility | ||
become confused about whether a directory named `foo` represents the module or the package. | ||
For avoiding ambiguity, use module names containing at least one `.` character | ||
(as it should be when using the reverse domain name convention) | ||
or use the module source hierarchy described below. | ||
|
||
|
||
## Maven 4 with module source hierarchy | ||
|
||
The [module source hierarchy](https://docs.oracle.com/en/java/javase/17/docs/specs/man/javac.html#directory-hierarchies) | ||
introduces one additional directory level in the paths to source Java files and to compiled classes. | ||
The name of this directory is the Java module name, | ||
and the directory is always present even in projects containing only one module. | ||
More than one Java module can be present in the same Maven sub-project. | ||
Such multi-module projects have advantages such as resolving compiler warnings | ||
in forward references to dependent modules and easier sharing of test code between modules. | ||
For example, a Maven project for a single Java module named `org.foo.bar` would have the following directory layout: | ||
|
||
``` | ||
src | ||
├─ org.foo.bar | ||
│ ├─ main | ||
│ │ └─ java | ||
│ │ ├─ module-info.java | ||
│ │ └─ org/foo/bar/*.java | ||
│ └─ test | ||
│ └─ java | ||
│ ├─ module-info-patch.maven (optional) | ||
│ └─ org/foo/bar/*.java | ||
└─ target | ||
└─ classes | ||
└─ org.foo.bar | ||
└─ org/foo/bar/*.class | ||
``` | ||
|
||
Note that the output directory also contains an `org.foo.bar` directory level. | ||
That directory level is generated by `javac`, this is not a convention invented by Maven. | ||
|
||
Above layout can be declared with the following fragment in the `pom.xml` file. | ||
Since this example uses the default directory layout for modular projects, | ||
the `<directory>` elements do not need to be specified. | ||
|
||
```xml | ||
<build> | ||
<sources> | ||
<source> | ||
<module>org.foo.bar</module> | ||
</source> | ||
<source> | ||
<scope>test</scope> | ||
<module>org.foo.bar</module> | ||
</source> | ||
</sources> | ||
</build> | ||
``` | ||
|
||
## Black Box testing | ||
|
||
"Black Box testing" refers to tests executed without access to the internal code of the project to test. | ||
Internal codes include package-private classes, interfaces, methods and fields, and also all non-exported packages. | ||
Because the module source hierarchy allows any number of Java modules in the same Maven sub-project, | ||
it is easy to add an `org.foo.bar.test` module which will test the `org.foo.bar` module as if it was | ||
an ordinary client application. | ||
|
||
|
||
## White Box testing | ||
|
||
"White Box testing" refers to tests which have an access to the internal classes of the project to test. | ||
For any `<source>` element with the `test` scope, all Java code placed in the directory managed by that | ||
element is automatically white box testing for the module declared in the `<module>` child element. | ||
Access to package-private types and members is granted by placing the code in the same package as the code to test. | ||
Access to non-exported modules is implicit, but only for the module where the tests belong. | ||
|
||
|
||
## Reusing test fixtures of another module | ||
|
||
The Maven 3 way (`test-jar`) is still supported in Maven 4. | ||
However, when using module source hierarchy, it is easier to place test fixtures | ||
in the test code of any module which is required by all modules that need these fixtures. | ||
The test fixtures can be in any package, not necessarily a package that exists in the main code. | ||
Then, the `module-info-patch.maven` file can export that package to the other modules. | ||
For example if the test fixtures are placed in the `org.foo.bar.test` package of the `org.foo.bar` module: | ||
|
||
```java | ||
patch-module org.foo.bar { // Put here the name of the module to patch. | ||
add-modules TEST-MODULE-PATH; // Recommended value in the majority of cases. | ||
add-reads TEST-MODULE-PATH; | ||
|
||
add-exports org.foo.bar.test // The package that contains the test fixtures. | ||
to SUBPROJECT-MODULES; // The other modules which want to use those test fixtures. | ||
} | ||
``` | ||
|
||
`SUBPROJECT-MODULES` is a Maven-specific keyword for exporting to all other Java modules | ||
in the Maven (sub)project being compiled. It can be replaced by an explicit list of modules. | ||
That's all, no need to deploy or install a test JAR. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.