question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Handle module projects with separate module-info.java for main and test sources

See original GitHub issue

Here is a Java 11 maven module project: https://github.com/microsoft/gctoolkit. The project is a multi-module maven project. One submodule is “api”, see its file structure as follows. It describes the module-info.java file in the main sources and the test sources respectively.

api
├───src
│   ├───main
│   │   ├───java
│   │   │   │   module-info.java
│   │   │   └───com.microsoft.gctoolkit
│   │   │           GCToolKit.java
│   └───test
│       ├───java
|       |   |   module-info.java
│       │   └───com.microsoft.gctoolkit.test
│       │           Test.java

However, when opening this project in VS Code, it doesn’t work. It reports a compile error: “The project was not built due to “Build path contains duplicate entry: ‘module-info.java’ for project ‘api’”. Fix the problem, then try refreshing this project and building it since it may be inconsistent”. It doesn’t support multiple module-info.java files in one Java project.

image

The same project works in maven command line. There should be some gap in the JDT compiler that needs to be fixed.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
testforstephencommented, Sep 28, 2021

The problem is that there is no standard way at the JEP level to tell the tooling how to handle test sources. Each tool has to tweak it in their own way. The upstream JDT issues https://bugs.eclipse.org/bugs/show_bug.cgi?id=520667 and https://bugs.eclipse.org/bugs/show_bug.cgi?id=559601 are still under discussion, waiting for a spec or standard to deal with it.

@brunoborges There is a workaround to unlock your use of VS Code for gctoolkit. Add a profile as follows to your root pom.xml to exclude the test source module-info.java from the compiler configuration.

   <profiles>
        <profile>
            <id>m2e</id>
            <activation>
                <property>
                <name>m2e.version</name>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <version>${maven.compiler-plugin.version}</version>
                        <configuration>
                            <compilerArgs>
                                <arg>--module-version=${project.version}</arg>
                                <arg>-Xlint:unchecked</arg>
                            </compilerArgs>
                            <testExcludes>
                                <testExclude>**/module-info.java</testExclude>
                            </testExcludes>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
  </profiles>

This can bypass the duplicate module-info.java check, and the Java tooling will use the classpath way to handle test files. This profile only takes effect in VS Code environment, and not affect your maven command line. If you want, i can submit a PR in gctoolkit project.

Read more comments on GitHub >

github_iconTop Results From Across the Web

520713 – [9] Handle module projects with main and test ...
A common source structure is that tests are in separate source directory with separate bin-directory in the same project(=module for java 9) and...
Read more >
How can I define a Java 9 module inside both a source and ...
In one of the projects, there is a source and a separate source-test folder. I've defined a module definition in the source folder....
Read more >
Multi-Module Maven Application with Java Modules - Baeldung
Learn to create a multi-module Maven application using the Java Platform Module System (JPMS).
Read more >
Java Modules - Jenkov.com
Running a Java Module From a JAR With a Main Class Set ... that if your Java project has a source root directory...
Read more >
Building Java Modules Sample - Gradle User Manual
Typically, in a project with Java Modules like this one, the main source set of ... traditional way by not adding a module-info.java...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found