有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用Maven(和Eclipse)管理Jar文件外部的资源

我觉得我在绕圈子,所以希望这是一个相对容易解决的问题

对于我正在开发的应用程序,我希望有一组包含配置信息的XML文件。我希望这些文件可以编辑,而无需重新编译jar文件。所以我希望将它们放在一个名为resources的文件夹中,与jar文件位于同一个文件夹中。 所以我想要的结构是

root
|
- app.jar
|
- resources
  |
  - config
    |
    - config1.xml
    - config2.xml

我已经设法调整了我的pom。xml,以便它使用maven-resource-plugin中的copy-resources将资源复制到我的目标文件夹中

我还添加了我的资源目录作为一个资源(以便它在eclipse中工作),使用

<resources>
    <resource>
        <directory>resources</directory>
    </resource>
</resources>

这样我就可以使用

getClass().getResource("/config/xml/config1.xml");

并使用maven jar插件中的manifestEntries将我的资源目录包含在jar清单的类路径上(我想)

<manifestEntries>
    <Class-Path>. resources</Class-Path&>
</manifestEntries>

并试图使用

<configuration>
    <exclude>resources/**/*.xml</exclude>
</configuration>

它们仍在被编译到jar文件中。如果我手动将它们从jar文件中删除,那么在尝试访问资源时会出现空指针异常

所以我想要实现的,需要帮助的是 这是一种获得我在上面大致概述的外部资源结构的方法,这些外部资源没有编译到jar文件中,但是可以通过jar中相同根目录中的代码访问(通过类路径),并且仍然在eclipse中运行

任何帮助或指导都将不胜感激


共 (2) 个答案

  1. # 1 楼答案

    你错过了标签excludes

    <configuration>
        <excludes>
           <exclude>resources/**/*.xml</exclude>
        </excludes>
    </configuration>
    
  2. # 2 楼答案

    您可以像这样复杂地排除所有文件夹:

    <excludes>
            <exclude>config/</exclude>
    </excludes>
    

    或者只是配置文件中的所有xml文件

     <excludes>
         <exclude>**/config/*.xml</exclude>
     </excludes>