1

I have the following folder structure in a Java project:

main-module (Java module)
   sub-module-to-import (Java module)
      ImportedClass.java
   sub-module (Java module)
      fx-sub-module (JavaFX module)
         - pom.xml and classes.

Whenever I import sub-module-to-import into fx-sub-module from maven, I can access ImportedClass.java after importing it in fx-sub-module's classes (instantiate the class, access its methods etc. in the IDE), but whenever I try to compile the module using mvn clean compile, I get a compilation error of type "cannot find symbol", where the symbol is a method of ImportedClass. Whenever I reload the maven project after adding the module as a dependency, everything is okay, with no XML errors or warnings, I can even ctrl + left click on sub-module-to-import's name and get sent to its pom.xml without issues. The project is a modular one, but I have removed the module-info file in order to not have to use modularity. I am using IntelliJ IDEA.

Here is the pom.xml file:

<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/maven-v4_0_0.xsd">
    <parent>
        <artifactId>sub-module</artifactId>
        <groupId>com.project</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>fx-sub-module</artifactId>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>16</maven.compiler.source>
        <maven.compiler.target>16</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.project</groupId>
            <artifactId>sub-module-to-import</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>13</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>13</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <release>16</release>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.6</version>
                <executions>
                    <execution>
                        <id>default-cli</id>
                        <configuration>
                            <mainClass>com.project.sub-module.fx-sub-module.MainClass</mainClass>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

After reading a lot about it on the internet and not managing to find a solution, I tried simple things like adding sub-module-to-import to the parent pom.xml of fx-sub-module (which is the pom.xml file of sub-module), but that didn't change anything. I also tried changing plugin/dependency versions, but this also did not help. I would really appreciate any help and will be quick to answer for further clarification on the question. Thanks in advance.

Yoan
  • 15
  • 5
  • My bad, the dependency was added in the parent pom.xml, but I added it here too now. Doesn't change anything though. – Yoan Feb 20 '23 at 07:20
  • I have to use this structure for the project, I cannot simplify it more that this. – Yoan Feb 20 '23 at 07:20
  • That seems unlikely, but you know your requirements better than me. Even something super complex like the [spring framework](https://github.com/spring-projects/spring-framework) only seems to nest build modules one level deep. – jewelsea Feb 20 '23 at 07:21
  • 1
    After recompiling sub-module-to-import everything is working fine. The reason the problem was occurring was that I added more methods to the class in that module without re-running mvn clean install on it. Thanks a lot for the help! – Yoan Feb 20 '23 at 07:23
  • 1
    Well for the project I have the following structure (it's an educational project): main-module sub-module1 ... Every sub-module corresponds to one topic, for example Threads, Collections, etc. and has different problem solutions in it. For the current topic I have to create gui apps and for that I need a Java-FX module for each task, that's why I have all that nesting. – Yoan Feb 20 '23 at 07:26
  • 1
    That’s fine, every project has different requirements. Thanks for taking the time to provide context and explain, it helps give meaning to the question. – jewelsea Feb 20 '23 at 07:28

1 Answers1

2

Ensure:

  1. mvn install has been run on the sub-module-to-import maven module.
  2. The Idea project has been synchronized with the maven project after that.
jewelsea
  • 150,031
  • 14
  • 366
  • 406