|
| 1 | +<!-- |
| 2 | +Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +or more contributor license agreements. See the NOTICE file |
| 4 | +distributed with this work for additional information |
| 5 | +regarding copyright ownership. The ASF licenses this file |
| 6 | +to you under the Apache License, Version 2.0 (the |
| 7 | +"License"); you may not use this file except in compliance |
| 8 | +with the License. You may obtain a copy of the License at |
| 9 | +
|
| 10 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | +Unless required by applicable law or agreed to in writing, |
| 13 | +software distributed under the License is distributed on an |
| 14 | +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +KIND, either express or implied. See the License for the |
| 16 | +specific language governing permissions and limitations |
| 17 | +under the License. |
| 18 | +--> |
| 19 | + |
| 20 | +# Module-info patch |
| 21 | + |
| 22 | +For white box testing, it is necessary to use compiler options such as |
| 23 | +`--patch-module`, `--add-modules`, `--add-reads`, `--add-exports` and `--add-opens`. |
| 24 | +Writing these options inside the Maven `<compilerArgs>` XML element is tedious, redundant |
| 25 | +(the name of the module to patch is repeated in every occurrence of some options), error prone, |
| 26 | +and must be repeated in every plugins that depends on the tests (Surefire, Javadoc for test documentation, _etc._). |
| 27 | +An alternative is to put a `module-info.java` file in the tests which *replace* the `module-info.java` file of the main code. |
| 28 | +However, it forces the developer to repeat all the content of the main `module-info.java` |
| 29 | +into the test `module-info.java` before to add test-specific statements. |
| 30 | +This is tedious if the main `module-info.java` is large, and risky if the two files become out of sync. |
| 31 | + |
| 32 | +Instead of defining a `module-info.java` file in test, Maven projects can define a `module-info-patch.maven`. |
| 33 | +The content of `module-info-patch.maven` uses the same syntax as Java, C/C++, JavaScript, Groovy, _etc._ |
| 34 | +(comments between `/*` … `*/` or after `//`, blocks between `{` … `}`, statements ending with `;`) |
| 35 | +but is not Java, hence the `.maven` file suffix. |
| 36 | +The general principles are: |
| 37 | + |
| 38 | +* Everything that a developer would like to change in a `module-info.java` file for testing purposes is declared in `module-info-patch.maven`. |
| 39 | +* Everything that is not in `module-info.java` is not in `module-info-patch.maven` neither. |
| 40 | + In particular, everything that specify paths to JAR files or paths to source code stay in the `pom.xml` file. |
| 41 | +* All keywords except `patch-module`, `SUBPROJECT-MODULES` and `TEST-MODULE-PATH` |
| 42 | + map directly to Java compiler or Java launcher options. |
| 43 | + |
| 44 | +Compared to declaring options in `<compilerArgs>` XML elements, the `module-info-patch.maven` file is more readable, |
| 45 | +keep the options in separated files for each module on which the options are applied, is less redundant as it avoids |
| 46 | +the need to repeat the module name in every `--add-reads`, `--add-exports` and `--add-opens` options, |
| 47 | +and is more flexibly as it is translated in slightly different options for compilation and test executions |
| 48 | +(e.g. `TEST-MODULE-PATH` means modules having `test` and `test-only` Maven's scope at compilation time, |
| 49 | +but means modules having `test` and `test-runtime` Maven's scope at execution time). |
| 50 | + |
| 51 | + |
| 52 | +## Syntax |
| 53 | +The syntax is: |
| 54 | + |
| 55 | +* The same styles of comment as Java (`/*` … `*/` and `//`) are accepted. |
| 56 | +* The first tokens, after comments, shall be `patch-module` followed by the name of the module to patch. |
| 57 | +* All keywords inside `patch-module` are Java compiler or Java launcher options without the leading `--` characters. |
| 58 | +* Each option value ends at the `;` character, which is mandatory. |
| 59 | + |
| 60 | +The accepted keywords are `add-modules`, `limit-modules`, `add-reads`, `add-exports` and `add-opens`. |
| 61 | +Note that they are options where the values are package or module names, not paths to source or binary files. |
| 62 | +Options with path values (`--module-path`, `--module-source-path`, `--patch-module`, _etc._) |
| 63 | +continue to be derived from the dependencies declared in the POM. |
| 64 | + |
| 65 | +### Options applying to all modules |
| 66 | +All options declared in a `module-info-patch.maven` file apply only to the module declared after the `patch-module` token, |
| 67 | +except the `--add-modules` and `--limit-modules` options. |
| 68 | +These two options apply to all modules in a multi-modules project, |
| 69 | +because these options given to `java` or `javac` expect no module name. |
| 70 | +Therefore, it is not necessary to repeat `add-modules TEST-MODULE-PATH` in all modules: |
| 71 | +declaring that particular option in only one module of a multi-modules project is sufficient. |
| 72 | +If the `--add-modules` or `--limit-modules` options are declared in many `module-info-patch.maven` files of a multi-modules project, |
| 73 | +then the effective value is the union of the values declared in each file, without duplicated values. |
| 74 | + |
| 75 | + |
| 76 | +### Special option values |
| 77 | +The following option values have special meanings: |
| 78 | + |
| 79 | +* `SUBPROJECT-MODULES`: all other modules in the current Maven (sub)project. |
| 80 | + * This is Maven-specific, not a standard value recognized by Java tools. |
| 81 | + * Allowed in: `add-exports`. |
| 82 | +* `TEST-MODULE-PATH`: all dependencies having a test scope in the build tools. |
| 83 | + * This is specific to this format, not a standard value recognized by Java tools. |
| 84 | + * Allowed in: `add-modules`, `add-reads` and `add-exports` options. |
| 85 | +* `ALL-MODULE-PATH`: everything on the module path, regardless if test or main. |
| 86 | + * This is a standard value accepted by the Java compiler. |
| 87 | + * Allowed in: `add-modules` option. |
| 88 | +* `ALL-UNNAMED`: all non-modular dependencies. |
| 89 | + * This is a standard value accepted by the Java compiler. |
| 90 | + * Allowed in: `add-exports` option. |
| 91 | + |
| 92 | + |
| 93 | +## Example |
| 94 | +Below is an example of a `module-info-patch.maven` file content |
| 95 | +for modifying the `module-info` of a module named `org.foo.bar`: |
| 96 | + |
| 97 | +```java |
| 98 | +/* |
| 99 | + * The same comments as in Java are allowed. |
| 100 | + */ |
| 101 | +patch-module org.foo.bar { // Put here the name of the module to patch. |
| 102 | + add-modules TEST-MODULE-PATH; // Recommended value in the majority of cases. |
| 103 | + |
| 104 | + add-reads org.junit.jupiter.api, // Frequently used dependency for tests. |
| 105 | + my.product.test.fixture; // Put here any other dependency needed for tests. |
| 106 | + |
| 107 | + add-exports org.foo.bar.internal // Name of a package which is normally not exported. |
| 108 | + to org.junit.jupiter.api, // Any module that need access to above package for testing. |
| 109 | + org.something.else; // Can export to many modules, as a coma-separated list. |
| 110 | + |
| 111 | + add-exports org.foo.bar.fixtures // Another package to export. It may be a package defined in the tests. |
| 112 | + to org.foo.bar.other; // Another module of this project which may want to reuse test fixtures. |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +### How module info patches are compiled |
| 117 | +`module-info-patch.maven` are compiled into a file of options in the following ways: |
| 118 | + |
| 119 | +* `add-modules org.foo, org.bar;` is translated to `--add-modules org.foo,org.bar`. |
| 120 | + * Note: spaces between `org.foo` and `org.bar` are removed for interpreting the option values as a single argument. |
| 121 | +* `limit-modules org.foo, org.bar;` is translated to `--limit-modules org.foo,org.bar`. |
| 122 | + * Note: idem regarding spaces removal. |
| 123 | +* `add-reads org.foo, org.bar;` is translated to `--add-reads org.patched=org.foo,org.bar` |
| 124 | + where `org.patched` is the module name declared in the first statement of the `module-info-patch` file. |
| 125 | +* `add-exports com.biz to org.foo, org.bar;` is translated to `--add-exports org.patched/com.biz=org.foo,org.bar` |
| 126 | + where `org.patched` is as above. |
| 127 | +* `add-opens com.biz to org.foo, org.bar;` is translated to `--add-opens org.patched/com.biz=org.foo,org.bar` |
| 128 | + like above but only for runtime execution, not for compilation. |
| 129 | + |
| 130 | +There is a separated `module-info-patch.maven` file for each module, |
| 131 | +and the Maven compiler plugin merges them in a single set of options for `java` and `javac`. |
| 132 | +While this format does not require the use of module source hierarchy, it fits nicely in that hierarchy. |
| 133 | + |
| 134 | +The results of the translation to compiler options can be seen in the `target/javac.args` and `target/javac-test.args` files. |
| 135 | +Those files are produced when the build failed or when Maven was executed with the `--verbose` command-line option. |
| 136 | +In addition, a slightly different set of options, suitable for tests execution, is written in the |
| 137 | +`target/test-classes/META-INF/maven/module-info-patch.args` file. |
0 commit comments