How to properly setup Maven with tests being a separate module

199 views Asked by At

My folder structure

|   pom.xml
|
+---.idea
|       .gitignore
|       compiler.xml
|       jarRepositories.xml
|       misc.xml
|       vcs.xml
|       workspace.xml
|
+---mysource
|       Main.java
|       pom.xml
|
\---tests
        MyTestClass.java
        pom.xml

Contents of the root pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>my-project</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>mysource</module>
        <module>tests</module>
    </modules>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>
</project>

Contents of mysource/pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.example</groupId>
        <artifactId>my-project</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <groupId>org.example</groupId>
    <artifactId>my-project-mysource</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.openjdk.jmh</groupId>
            <artifactId>jmh-core</artifactId>
            <version>1.34</version>
        </dependency>
        <dependency>
            <groupId>org.openjdk.jmh</groupId>
            <artifactId>jmh-generator-annprocess</artifactId>
            <version>1.34</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

</project>

Contents of tests/pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.example</groupId>
        <artifactId>my-project</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <groupId>org.example</groupId>
    <artifactId>my-project-tests</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.8.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.8.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>5.8.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.8.2</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
</project>

IntelliJ is showing "Java file outside of source root" for all the java files. I think there is something wrong with one or more of the pom.xml files.

NOTE: I want to keep the folder structure as it is.

1

There are 1 answers

0
Geoffrey De Smet On

By default, a pom.xml expects that you use the following structure:

|   pom.xml
+---.idea
|       ...
\---src
  +---main
  | \---java
  |     Main.java
  \---test
    \---java
        MyTestClass.java
        

Now, you might indeed want to split that up into 2 modules (to isolate your JMH tests in a separate module for example):

|   pom.xml // has <modules> entry
+---.idea
|       ...
+---my-main
| +---pom.xml
| \---src
|   \---main
|     \---java
|       Main.java
\---my-jmh-tests
  +---pom.xml
  \---src
    \---test
      \---java
        MyTestClass.java

And you can get rid of the "src/main/java" and "src/test/java" folders by customizing the <build> section in the pom.xml files.