Java: ресурсы одного плагина maven - PullRequest
1 голос
/ 06 июня 2019

У меня есть Maven проект, который я в конечном итоге хочу встроить в исполняемый файл exe. Этот проект зависит от нескольких библиотек. Я нашел это объяснение . Когда я строю проект "нормально", все работает нормально. Я получаю некоторые проблемы, как только я использую 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 относительно ресурсов?


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>

1 Ответ

0 голосов
/ 06 июня 2019

Вы должны добавить ниже как часть ресурсов. Я приведу ниже код.

Вы должны добавить ниже как часть ресурсов. Я приведу ниже код.

<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

...