Java:One-jarmaven插件资源

Java:One-jarmaven插件资源,java,maven,build,resources,onejar,Java,Maven,Build,Resources,Onejar,我有一个Maven项目,我最终想将它构建到一个可执行的exe中。此项目依赖于多个库。我找到了。当我“正常”构建项目时,一切正常。我一使用onejarmaven插件就会遇到一些问题 在UI中,我使用位于src\main\resources\UI\Icons\中的图像,例如logo.png和logo.ico。如果我以正常方式构建代码,就会找到所有资源。如果我使用一个jar构建 File UI/Icons/logo.png not found! 在创建我的主框架时。图像是使用 public void

我有一个
Maven
项目,我最终想将它构建到一个可执行的
exe
中。此项目依赖于多个库。我找到了。当我“正常”构建项目时,一切正常。我一使用
onejarmaven插件
就会遇到一些问题

在UI中,我使用位于
src\main\resources\UI\Icons\
中的图像,例如
logo.png
logo.ico
。如果我以正常方式构建代码,就会找到所有资源。如果我使用
一个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插件的吗


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>

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

4.0.0
组织测试
项目
0.0.1
罐子
UTF-8
1.8
5.3.1
1.0.0
12
12
3
3.8.1
3.1.1
3.0.0-M2
3.1.2
3.1.0
1.4.4
2.5.3
3.0.0-M3
...
...
...
...
org.apache.maven
maven模型
${maven.model.version}
org.junit.jupiter
JUnitJupiter api
${junit.jupiter.version}
测试
org.junit.jupiter
朱尼特朱庇特酒店
${junit.jupiter.version}
测试
org.junit.jupiter
朱尼特木星发动机
${junit.jupiter.version}
测试
${project.artifactId}-${project.version}
src/main/resources
真的
maven编译器插件
${maven.plugin.compiler.version}
${java.version}
${java.version}
org.apache.maven.plugins
maven enforcer插件
${maven.plugin.enforcer.version}
执行
${java.version}
org.apache.maven.plugins
maven发布插件
${maven.plugin.release.version}
真的
假的
org.apache.maven.plugins
maven依赖插件
${maven.plugin.dependency.version}
复制依赖项
准备包装
复制依赖项
${project.build.directory}/lib
org.apache.maven.plugins
maven jar插件
${maven.plugin.jar.version}
真的
解放党/
org.test.project
com.joira
onejar maven插件
${maven.plugin.onejar.version}
${mainClass}
假的
奥内贾尔
一罐

您必须添加以下内容作为参考资料的一部分。我提供以下代码

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

您必须添加以下内容作为资源的一部分。我提供以下代码

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

...
src/resources/UI/Icons
**/*.png
...
...
有关更多详细信息,请参阅下面的链接


您必须添加以下内容作为参考资料的一部分。我提供以下代码

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

您必须添加以下内容作为资源的一部分。我提供以下代码

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

...
src/resources/UI/Icons
**/*.png
...
...
有关更多详细信息,请参阅下面的链接


我编辑了我的原始问题。我在我的参考资料中有
src/main/resources
,并且因为这个问题而错误地删除了它。这是非递归的吗?我还尝试添加了第二个
resource
,正如您建议的那样,`src/main/resources/UI/Icons。然而,我仍然得到相同的错误。我跟随链接,它声明
。。。如果我们想包括所有。。。我们的src/my resources目录下及其所有子目录下的文件…
,因此我想它应该是递归的。您可以使用任何zip实用程序打开jar文件,并检查它是否包含所需目录中的任何图像文件吗?好的,对于原始版本,我有
project-0.0.1.jar!UI/Icons/
和必要的文件都在那里。在
project-0.0.1.one-jar.jar
中,我没有它,因为它是
onejar-maven插件的捆绑jar
如果您使用Swt开发eclipse插件,您必须遵循maven