简体   繁体   中英

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. And I would like these to be editable without having to re-compile the jar file. So what I would like is to have them in a folder called resources in the same folder as the jar file. 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 .

I've also added my resources directory as a resource (so that it works within eclipse) using

<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.

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

And attempted to exclude my resource from being compiled into the jar file using

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

they are still getting compiled into the jar file. And if I remove them from the jar file manually then I get a null pointer exception when attempting to access the resource.

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.

Any help or guidance would be appreciated.

You have missed tag 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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