java - 无法将资源添加到maven中的jar中

标签 java swing maven jar pom.xml

这是我的问题:我有一个使用 Swing 库的 Maven 项目,它是一个 GUI 应用程序,我想在我的 jar 文件中包含资源(图像和一个 config.properties 文件)。

这是我的pom.xml代码:

  <build>
    <resources>

      <resource>

        <directory>${basedir}/resource</directory>

            <includes>
                <include>**/*.png</include>
            </includes>

        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>

      </resource>

      <resource>
        <directory>${basedir}/config</directory>

        <includes>
            <include>config.properties</include>
        </includes>

        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>

      <resource>
        <directory>.settings</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>


    </resources>



    <plugins>
      <plugin>
      <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <configuration>
         <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <mainClass>bis.debug.mode.ui.MainBISDebugMode</mainClass>
            </manifest>
         </archive>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>


  </build>
</project>

这是我的项目结构:

Project structure

我包含了在 pom.xml 中看到的目录和其中的文件,但仍然没有包含在 jar 文件中,并且在 GUI 应用程序上看不到图像。当我将程序作为 Java 应用程序运行时,一切都很好。

我的代码可能有问题吗?我该如何解决这个问题?

最佳答案

您只需将配置文件放入 src/main/resources 中,无需在 pom.xml 中添加资源,因为 src/main/resources 是maven的默认资源文件夹

关于java - 无法将资源添加到maven中的jar中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52072869/

相关文章:

java - 更改 JButton 渐变颜色,但仅针对一个按钮,而不是所有按钮

java - 如何使用 GhostDriver 和 PhantomJS 减少从 Web 获取 URL 页面的时间

java - 如何对 map 集合的层次键进行排序?

java - 如何在空布局中设置jpanel的大小

java - "kill -QUIT process_id"是如何工作的?

Java GUI/Swing、停止线程、在类之间传递整数

java - 调试 JBoss 源代码

maven - 如何丑化代码仅用于maven中的部署

java - 按子列表的值对 LIst 列表进行排序

Java ServersSocket 读取后关闭