Skip to content
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

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

Open
testforstephen opened this issue Sep 17, 2021 · 8 comments
Assignees
Labels

Comments

@testforstephen
Copy link
Collaborator

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.

@snjeza
Copy link
Contributor

snjeza commented Sep 17, 2021

Related issue - #2090 (comment)

@testforstephen testforstephen self-assigned this Sep 26, 2021
@brunoborges
Copy link

This is the core issue to look into: eclipse-m2e/m2e-core#173

@testforstephen
Copy link
Collaborator Author

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.

@brunoborges
Copy link

Thanks @testforstephen I'll give it a try!

@fbricon
Copy link
Collaborator

fbricon commented Sep 28, 2021

the profiles will be inherited by submodules (yeah Maven)

@arjenzhou
Copy link

I have the same issue with gradle. :(

@matihost
Copy link

The workaournd allows to operate VSCode, but it is not possible to run tests from VSCode directly - in case there are some test dependencies etc

PS. It is not a technical argument, but maybe a product argument:
https://youtrack.jetbrains.com/issue/IDEA-253066
is fixed in the competition.

@orboan
Copy link

orboan commented May 3, 2022

Any updates on this?
Is it possible to work with multiple module-info.java files in one Java project, in VSCode?

This is was is keeping me from using VSCode for my java courses (I am a teacher).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants