Search code examples
javamavenjavafxmaven-dependency

JavaFX with Maven fails to import module from the same project


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.


Solution

  • 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.