1

I have a Maven project that I ultimately want to build into an executable exe. This project is dependent on multiple libraries. I found this explanation. When I build the project "normally", everything works fine. I get some issues as soon as I use the onejar-maven-plugin.

In the UI I use images located in src\main\resources\UI\Icons\, e.g. logo.png and logo.ico. If I build the code the normal way, all resources are found. In case I build with one-jar I get

File UI/Icons/logo.png not found!

in the creation of my main frame. The image is loaded using

public void test(){

    String logo = "/UI/Icons/logo.png";

    try {
        Image image = getImageResource(this.getClass(), logo);
        if (image != null) {this.setIconImage(image);}
    } catch (IOException ex) {
        System.err.println("File " + logo + " not found!");
    }
}

public static Image getImageResource( Class base, String name ) throws java.io.IOException {
    Image result = null;

    URL imageURL = base.getClassLoader().getResource( name );

    if ( imageURL != null ) {
        Toolkit tk = Toolkit.getDefaultToolkit();
        result = tk.createImage( (ImageProducer) imageURL.getContent() );
    }
    return result;
}

Is there something I have to tell the onejar-maven-plugin regarding resources?


pom.xml

relevant parts

<?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>

    <!-- TOOL INFO -->
    <groupId>org.test</groupId>
    <artifactId>project</artifactId>
    <version>0.0.1</version>
    <packaging>jar</packaging>

    <!-- PROPERTIES -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- System -->
        <java.version>1.8</java.version>
        <junit.jupiter.version>5.3.1</junit.jupiter.version>
        <junit.platform.version>1.0.0</junit.platform.version>
        <maven.compiler.source>12</maven.compiler.source>
        <maven.compiler.target>12</maven.compiler.target>
        <maven.model.version>3.0</maven.model.version>
        <maven.plugin.compiler.version>3.8.1</maven.plugin.compiler.version>
        <maven.plugin.dependency.version>3.1.1</maven.plugin.dependency.version>
        <maven.plugin.enforcer.version>3.0.0-M2</maven.plugin.enforcer.version>
        <maven.plugin.jar.version>3.1.2</maven.plugin.jar.version>
        <maven.plugin.javadoc.version>3.1.0</maven.plugin.javadoc.version>
        <maven.plugin.onejar.version>1.4.4</maven.plugin.onejar.version>
        <maven.plugin.release.version>2.5.3</maven.plugin.release.version>
        <maven.plugin.surefire.version>3.0.0-M3</maven.plugin.surefire.version>
        <!-- 3rd party -->
        ...
        <!-- Own -->
        ...
    </properties>

    <!-- DEPENDENCIES -->
    <dependencies>
        <!-- Own -->
        ...
        <!-- Own Testing -->
        ...
        <!-- System -->
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-model</artifactId>
            <version>${maven.model.version}</version>
        </dependency>
        <!-- Testing -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <!-- BUILD -->
    <build>

        <!-- THIS IS THE FINAL NAME OF THE JAR -->
        <finalName>${project.artifactId}-${project.version}</finalName>

        <!-- RESOURCES -->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>

        <!-- PLUGINS -->
        <plugins>

            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.plugin.compiler.version}</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-enforcer-plugin</artifactId>
                <version>${maven.plugin.enforcer.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>enforce</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <rules>
                        <requireJavaVersion>
                            <version>${java.version}</version>
                        </requireJavaVersion>
                    </rules>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-release-plugin</artifactId>
                <version>${maven.plugin.release.version}</version>
                <configuration>
                    <localCheckout>true</localCheckout>
                    <pushChanges>false</pushChanges>
                </configuration>
            </plugin>

            <!-- copy all required dependencies into the folder -->
            <!-- https://www.baeldung.com/executable-jar-with-maven -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>${maven.plugin.dependency.version}</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>
                                ${project.build.directory}/lib
                            </outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>${maven.plugin.jar.version}</version>
                <configuration>
                    <archive>                   
                        <manifest>
                            <!-- Build an executable JAR -->
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>org.test.project</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

            <!-- Create one single jar with libraries using onejar -->
            <plugin>
                <groupId>com.jolira</groupId>
                <artifactId>onejar-maven-plugin</artifactId>
                <version>${maven.plugin.onejar.version}</version>
                <executions>
                    <execution>
                        <configuration>
                            <mainClass>${mainClass}</mainClass>
                            <attachToBuild>false</attachToBuild>
                            <classifier>onejar</classifier>
                        </configuration>
                        <goals>
                            <goal>one-jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>

    </build>
</project>
raedma
  • 293
  • 1
  • 12

1 Answers1

0

You have to add the below as part of resources. I provide below the code.

You have to add the below as part of resources. I provide below the code.

<build>
    ...
    <resources>
      <resource>
        <directory>src/resources/UI/Icons</directory>
        <includes>
          <include>**/*.png</include>
        </includes>
      </resource>
      ...
    </resources>
    ...
  </build>

For more details, refer below the link.

https://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html

Sambit
  • 7,625
  • 7
  • 34
  • 65
  • I edited my original question. I had `src/main/resources` in my resources and deleted it by mistake for the question. Is this non-recursive? I also tried adding a second `resource` as you proposed, `src/main/resources/UI/Icons. However, I still get the same error. – raedma Jun 06 '19 at 15:29
  • I followed the link, it states `... if we want to include all ... files under our src/my-resources directory and in all its subdirectories ...`, so I guess it should be recursive – raedma Jun 06 '19 at 15:39
  • Can you open the jar file using any zip utility and check whether it contains any image files inside the desired directory ? – Sambit Jun 06 '19 at 15:41
  • Ok, so for the original build I have `project-0.0.1.jar!UI/Icons/` and the necessary files are in there. In the `project-0.0.1.one-jar.jar` I do not have that as it is a bundled jar from the `onejar-maven-plugin` – raedma Jun 06 '19 at 15:45
  • Check this link about maven jar. https://maven.apache.org/plugins/maven-jar-plugin/examples/include-exclude.html – Sambit Jun 06 '19 at 15:48
  • That wasn't it. However, I found a workaround. Creating a `BufferedImage` using `result = ImageIO.read(imageURL)` works. Would be interested to know why this works and `Toolkit.getDefaultToolkit().createImage( (ImageProducer) imageURL.getContent() );` doesn't. – raedma Jun 06 '19 at 15:53
  • So it means, it is not related to maven issue, it is more about accessing the image from the code. – Sambit Jun 06 '19 at 15:58
  • I never implied that its a `maven` issue. I was just wondering why the resource is there for the "normal" build and not there for the `one-jar` build. I now have a workaround but not an explanation. – raedma Jun 06 '19 at 16:06
  • 1
    If you are developing eclipse plugin using Swt, you have to following maven tycho to build. If it is different type project, instead of one-jar, it is better to use maven shade plugin. – Sambit Jun 06 '19 at 16:08
  • Thanks for the hint. I moved to shade – raedma Jun 07 '19 at 09:47