0

I have a Maven project with a submodule, developed in IntelliJ, using Java 11.

Unless the pom.xml file contains <packaging>pom</packaging>, there is a warning that

'packaging' with value 'jar' is invalid. Aggregator projects require 'pom' as packaging.

But when packaging is set to "pom", the resource file that I need can't be loaded; a null value is returned, and an exception is thrown. From the main() method:

    URL resource = getClass().getResource("/fx/gui.fxml");
    Objects.requireNonNull(resource);

On the other hand, sometimes the submodule is not found, unless I ask for pom packaging. What I do then is: Request pom packaging, start the program and watch it fail, remove the pom packaging statement from pom.xml, start again and the program works.

My resource file is in the standard location src/main/resources/fx/gui.fxml. This location is also given in the pom file:

<build>
    <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
    <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
    <resources>
        <resource>
            <directory>${project.basedir}/src/main/resources</directory>
        </resource>
    </resources>
</build>

Please help me understand what's going on. Do I need pom packaging, and how can resources be loaded with it?

Helge
  • 145
  • 9

2 Answers2

2

Looks like you have your source code in your parent pom.

A parent pom (with sub-modules) must have packaging as pom and cannot have java source code. See this question

You should move your code in a new sub-module.

Zinc
  • 1,064
  • 3
  • 16
1

The project or module which contains the source code, has to have the packaging as jar/war as per your requirement. It can not be packaging as pom. Generally pom packaging is used with parent module when you have multimodule project structure and the submodules would have packaging as jar/war. So in your case, if you have multimodule project structure, your parent packaging would be "pom" and all submodules(contain source code) must have jar/war. Note : Your parent module should not have source code, if it is then move your source code to submodule. Multimodule project structure is basically used where there are common dependencies and artifacts can be used in multiple submodules so that duplication can be removed. Like below.

parent 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.abc.test</groupId>
    <artifactId>testartifact</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <properties>
        <java.version>1.8</java.version>
    </properties>

     <modules>
        <module>rest-services</module>
     </modules>

</project>

Submodule pom.xml

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.abc.test</groupId>
        <artifactId>testartifact</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <artifactId>rest-services</artifactId>
    <name>rest-services</name>
 
    <dependencies>
        <dependency>
        </dependency>
    </dependencies>

 </project>
P.Sanjay
  • 303
  • 1
  • 7
  • Thanks for your explanation. I see that your code example uses Java 1.8. It seems that the packaging rule (pom for parent module, no code) has little or nothing to do with the Java modules of Java 9 or later. But usually, they are more or less the same, right? – Helge Dec 11 '20 at 13:48
  • yes, there is no difference with java version. I have just added properties tag for the example to mentioned that these kind of properties or some dependencies are used in parent pom which are common for used by submodule. Like think of if you have 10 projects and in pom.xml of all the project you have to add that common properties or dependency. Thats duplication of code. So get rid of that we use parent pom in multimodule project. – P.Sanjay Dec 11 '20 at 18:11