我有一个Maven
项目,我最终希望将其构建到可执行文件exe
中。该项目依赖于多个库。我找到了this explanation。当我“正常”构建项目时,一切正常。使用onejar-maven-plugin
后,我会遇到一些问题。
在用户界面中,我使用位于src\main\resources\UI\Icons\
中的图像,例如logo.png
和logo.ico
。如果我以正常方式构建代码,则会找到所有资源。如果我用one-jar
构建,我会得到
File UI/Icons/logo.png not found!
在创建我的主框架时。使用
加载图像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;
}
我需要告诉onejar-maven-plugin
有关资源的信息吗?
相关部分
<?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>
答案 0 :(得分:0)
您必须在资源中添加以下内容。我在下面提供了代码。
您必须在资源中添加以下内容。我在下面提供了代码。
<build>
...
<resources>
<resource>
<directory>src/resources/UI/Icons</directory>
<includes>
<include>**/*.png</include>
</includes>
</resource>
...
</resources>
...
</build>
有关更多详细信息,请参阅下面的链接。
https://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html