I can't figure out what's the best practice or just proper way for setting up a JPMS Maven project with test sources that have additional Java SE module dependencies to those of the main sources.

I have the following basic project structure:

    pom.xml
    src/
    ├─ main/
    │  └─ java/
    │     ├─ module-info.java
    │     └─ net/example/stanio/jpms/
    │        └─ HelloJPMS.java
    └─ test/
       └─ java/
          └─ net/example/stanio/jpms/
             └─ HelloJPMSTest.java

The `module-info.java` is:

    module net.example.stanio.jpms {
        exports net.example.stanio.jpms;
    }

That is the main sources don't have any dependencies other than `java.base`, but the tests use some JAXP (`java.xml`).

My `pom.xml` is like:

    <project>
        <modelVersion>4.0.0</modelVersion>

        <groupId>net.example.stanio</groupId>
        <artifactId>maven-jpms</artifactId>
        <version>0.0.1-SNAPSHOT</version>

        <name>maven-jpms</name>

        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.release>9</maven.compiler.release>
            
<maven.test.redirectTestOutputToFile>true</maven.test.redirectTestOutputToFile>
        </properties>

        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.13.2</version>
                <scope>test</scope>
            </dependency>
        </dependencies>

        <build>
            <plugins>
                <plugin>
                   <artifactId>maven-compiler-plugin</artifactId>
                   <version>3.10.1</version>
                </plugin>
            </plugins>
        </build>
    </project>

Running `mvn -V clean test` I'm getting:

```
Apache Maven 3.8.6 (84538c9988a25aec085021c365c560670ad80f63)
Maven home: C:\...
Java version: 11.0.12, vendor: Oracle Corporation, runtime: C:\...
Default locale: en_US, platform encoding: Cp1251
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
[INFO] Scanning for projects...
...
[INFO] --- maven-compiler-plugin:3.10.1:testCompile (default-testCompile) @ 
maven-jpms ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:\...\maven-jpms\target\test-classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] 
/C:/.../maven-jpms/src/test/java/net/example/stanio/jpms/HelloJPMSTest.java:[7,17]
 package javax.xml.transform is not visible
  (package javax.xml.transform is declared in module jdk.pack, but module 
net.example.stanio.jpms does not read it)
[ERROR] 
/C:/.../maven-jpms/src/test/java/net/example/stanio/jpms/HelloJPMSTest.java:[8,27]
 package javax.xml.transform.sax is not visible
  (package javax.xml.transform.sax is declared in module jdk.pack, but module 
net.example.stanio.jpms does not read it)
[ERROR] 
/C:/.../maven-jpms/src/test/java/net/example/stanio/jpms/HelloJPMSTest.java:[9,27]
 package javax.xml.transform.sax is not visible
  (package javax.xml.transform.sax is declared in module jdk.pack, but module 
net.example.stanio.jpms does not read it)
[ERROR] 
/C:/.../maven-jpms/src/test/java/net/example/stanio/jpms/HelloJPMSTest.java:[10,27]
 package javax.xml.transform.stream is not visible
  (package javax.xml.transform.stream is declared in module jdk.pack, but 
module net.example.stanio.jpms does not read it)
[INFO] 4 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.668 s
[INFO] Finished at: 2022-07-03T01:49:22+03:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal 
org.apache.maven.plugins:maven-compiler-plugin:3.10.1:testCompile 
(default-testCompile) on project maven-jpms: Compilation failure: Compilation 
failure:
...
```

So I've added `src/test/java/module-info.java` like:

    module net.example.stanio.jpms {
        requires java.xml;
        requires junit;
    }

that appears to have solved the test compilation failure.  Note, I had to re-declare the other test dependencies (junit), as well. I would really like to avoid that double dependency bookkeeping, and then having duplicated `module-info.java` completely breaks importing the project into Eclipse (m2e).

Is there an established approach to this (or similar) setup? Maybe I need to fiddle with the `default-testCompile` `<compilerArgs>` – haven't tried yet, but I've just seen this javac option:

      --add-modules <module>(,<module>)*
            Root modules to resolve in addition to the initial modules, or all 
modules
            on the module path if <module> is ALL-MODULE-PATH.

--
Stanimir



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@maven.apache.org
For additional commands, e-mail: users-h...@maven.apache.org

Reply via email to