简体   繁体   English

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

[英]Managing resources that will be external to a Jar file using Maven (and Eclipse)

I feel like I'm going round in circles with this, so hopefully its something relatively easy to solve. 我觉得我会围着这个圈子走,所以希望它能够相对容易地解决。

For the application I'm developing I want to have a set of XML files which contain configuration information. 对于我正在开发的应用程序,我想要一组包含配置信息的XML文件。 And I would like these to be editable without having to re-compile the jar file. 我希望这些可以编辑,而无需重新编译jar文件。 So what I would like is to have them in a folder called resources in the same folder as the jar file. 所以我想要的是将它们放在与jar文件相同的文件夹中名为resources的文件夹中。 So my intended structure is something like 所以我想要的结构是这样的

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

I have managed to adjust my pom.xml so that it will copy the resources into my target folder using copy-resources within the maven-resource-plugin . 我已设法调整我的pom.xml,以便它使用maven-resource-plugin copy-resources到我的目标文件夹中。

I've also added my resources directory as a resource (so that it works within eclipse) using 我还添加了我的资源目录作为资源(以便它在eclipse中工作)使用

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

so that I can access them using 这样我就可以使用它了

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

And included my resources directory on the classpath of the manifest of the jar (I think) using manifestEntries in the maven-jar-plugin. 并使用maven-jar-plugin中的manifestEntries将我的资源目录包含在jar清单的类路径中(我认为)。

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

And attempted to exclude my resource from being compiled into the jar file using 并试图将我的资源从编译到jar文件中排除

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

they are still getting compiled into the jar file. 它们仍然被编译到jar文件中。 And if I remove them from the jar file manually then I get a null pointer exception when attempting to access the resource. 如果我手动从jar文件中删除它们,那么在尝试访问资源时我会得到一个空指针异常。

So what I'm trying to achieve, and need help figuring out is a way to obtain the structure I've roughly outlined above for external resources which are not compiled into the jar file but which are are accessible (via the classpath) by code within a jar in the same root directory and which still functions in eclipse. 所以我想要实现的,并且需要帮助搞清楚是一种获取我上面大致概述的外部资源的结构的方法,这些外部资源没有编译到jar文件中但是可以通过代码访问(通过类路径)在同一根目录下的一个jar中,它仍然在eclipse中运行。

Any help or guidance would be appreciated. 任何帮助或指导将不胜感激。

You have missed tag excludes 您错过了标签excludes

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

You can complicatedly exclude all folder like this : 您可以复杂地排除所有文件夹,如下所示:

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

or just all xml files from config 或者只是来自config的所有xml文件

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM