'Maven Packaging Resources

I'm trying to create a jar with maven but I don't know how to package resources where I have an icon.

public Image icon() {
        if (Files.exists(Paths.get("src/main/resources/icon.png"))) {
            log.info("Icon provided");
            return new ImageIcon("src/main/resources/icon.png").getImage();
        }
        log.info("No Icon provided");
        return null;
    }

This is the method where I specify the path of my icon. When I run the jar it doesn't work and returns null.

<?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>com.xdavide9</groupId>
    <artifactId>BetterNotePad</artifactId>
    <version>1.2</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.formdev</groupId>
            <artifactId>flatlaf</artifactId>
            <version>2.2</version>
        </dependency>

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.11</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.24</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>com.xdavide9.BetterNotePad</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <filters>
                        <filter>
                            <artifact>*:*</artifact>
                            <excludes>
                                <exclude>module-info.class</exclude>
                            </excludes>
                        </filter>
                        <filter>
                            <artifact>com.formdev:*</artifact>
                            <excludes>
                                <exclude>META-INF/*.MF</exclude>
                            </excludes>
                        </filter>
                        <filter>
                            <artifact>org.projectlombok:*</artifact>
                            <excludes>
                                <exclude>META-INF/*.MF</exclude>
                            </excludes>
                        </filter>
                        <filter>
                            <artifact>org.slf4j:*</artifact>
                            <excludes>
                                <exclude>META-INF/*.MF</exclude>
                            </excludes>
                        </filter>
                        <filter>
                            <artifact>ch.qos.logback:*</artifact>
                            <excludes>
                                <exclude>META-INF/*.MF</exclude>
                            </excludes>
                        </filter>
                    </filters>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

This is my pom.xml.

[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ BetterNotePad ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\david\Desktop\IdeaProjects\BetterNotePad\src\test\resources

These are logs about resources when I run mvn package.

Could somebody please instruct me on how to configure my pom to include resources and what should I put in the method as a path? I tried different solutions but I keep getting stuck and return back to this situation.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source