Java:One-jar Maven 插件资源

标签 java maven build resources onejar

我有一个 Maven 项目,我最终希望将其构建为可执行的 exe。该项目依赖于多个库。我发现this explanation 。当我“正常”构建项目时,一切正常。当我使用 onejar-maven-plugin 时,我遇到了一些问题。

在 UI 中,我使用位于 src\main\resources\UI\Icons\ 中的图像,例如logo.pnglogo.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 有关资源的信息吗?

<小时/>

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>

最佳答案

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

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

<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

关于Java:One-jar Maven 插件资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56480247/

相关文章:

java - Jasper Reports 中的空白子报表

java - Spring Boot 组件扫描不起作用

java - Maven 无法解析 C3P0 依赖项

c# - 不断计算 const 值

java - 如何使用maven构建包含引用的jar库的jar

java - 将整数的最后一位四舍五入

java - 时尚地使用 Return

java - 如何检查线程是否终止?

java - ava.lang.NullPointerException : null at org. apache.catalina.loader.WebappClassLoader.findResources(WebappClassLoader.java:1368)~[na:na]

android - 高效开发修改AOSP framework.jar