0

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.

Youssef13
  • 3,836
  • 3
  • 24
  • 41
  • Unit tests belong to the same module where you have your production code... except you would like to write special integration tests or maybe testing on a java module level... – khmarbaise Jan 06 '22 at 12:09
  • Also I strongly recommend to use a dependencyManagement in your root pom instead of repeating everything... – khmarbaise Jan 06 '22 at 12:12
  • @khmarbaise Can I do that while keeping the folder structure? – Youssef13 Jan 06 '22 at 12:22
  • Based on the outline structure your whole setup is wrong. The source belongs to `src/main/java//` the unit tests belong to `src/test/java//XYZTest.java` etc. The source code does not belong to the root directory. The question is why? For what purpose do you want to change the convention? Also IDEA has told you to change it... Why? – khmarbaise Jan 06 '22 at 12:25
  • @khmarbaise Regardless of the reason, there should be a way to do it. Per [docs](https://maven.apache.org/guides/mini/guide-using-one-source-directory.html), "There is a common misconception that Maven can't build a project that doesn't conform to certain directory structures or build practices. This often isn't the case. However, it is true that some Maven features or plugins (especially by third parties) may not work or work completely." ---- So there should be a way to do it, I just want to know it :) – Youssef13 Jan 06 '22 at 12:32
  • The reasons are important... Just for curiosity? Or is there real requirement/reason behind that? Follow the conventions makes your life easier and all people who need to maintain that later on... If you don't have a real good reason follow the conventions. – khmarbaise Jan 06 '22 at 12:38
  • It's for a project that's going to be automatically graded, and the system that will grade it requires a specific folder structure. But again, the reason isn't really important. There should be a way to do that, and that's what I'm asking. – Youssef13 Jan 06 '22 at 12:41
  • Why does that project require a special folder structure? The reason is important... having an option doing something does not mean you should go that way. (Follow convention over configuration)... Technically you can: https://maven.apache.org/pom.html (search for: `sourceDirectory`) but strongly recommend not to do that.. – khmarbaise Jan 06 '22 at 12:46
  • @khmarbaise I know I shouldn't, but I *have* to. I have tried to add `/` to `mysource/pom.xml`, but looks like IntelliJ is still confused, and is now showing my whole "C:\" partition under the project. – Youssef13 Jan 06 '22 at 12:49
  • Also tried `${project.basedir}` but IntelliJ still not recognized java files. – Youssef13 Jan 06 '22 at 12:52
  • So you have exactly what is described in the docs (and the reason I strongly recommend not to do that)... some tools might fail...for example like IntelliJ...if you do something like this. Try to build first on plain command line if that's an issue with the tools you are using (for example your IDE)... Furthermore remove the whole project from IDEA intellij also remove the configuration directory completely from your project and try to import that project from scratch into IDEA... – khmarbaise Jan 06 '22 at 12:59
  • It worked. I think something bad was cached into idea configuration files. Thank you! And I certainly won't do that in a real/production project. It's only something as a college project. – Youssef13 Jan 06 '22 at 13:04
  • In particular if it's a college project you should go with the conventions because that's much more easier to understand for the students and to find help also as a a preparation for later on the job is much better. The questions states: What is the reason to go a wrong path... – khmarbaise Jan 06 '22 at 13:09

1 Answers1

0

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.

Geoffrey De Smet
  • 26,223
  • 11
  • 73
  • 120