Maven:添加外部资源 [英] Maven : add external resources

查看:319
本文介绍了Maven:添加外部资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用maven构建一个可执行的jar文件,这意味着你用java -jar file.jar运行它。

I am building an executable jar file with maven, meaning that you run it with "java -jar file.jar".

我想依靠用户定义属性(只是一个包含键/值的文件),在开发阶段我将我的user.properties文件放在maven / src / main / resources /文件夹中。

I want to rely on user defined properties (just a file containing keys/values), during developpement phase I was putting my "user.properties" file in maven /src/main/resources/ folder.

我的属性文件加载:

final Properties p = new Properties();
final InputStream resource = IOParametres.class.getResourceAsStream("/user.properties");
p.load(resource);

现在,我想将该文件保留在JAR之外并且具有以下内容:

Now, I want to keep that file outside of the JAR and have something like this :

- execution_folder
   |_ file.jar
   |_ config
      |_ user.properties

我用maven插件尝试了很多东西,比如maven-jar-plugin,maven-surefire-plugin和maven-resources -plugin但我无法让它运作...

I tried many things with maven plugins like maven-jar-plugin, maven-surefire-plugin and maven-resources-plugin but I can't get it working...

提前感谢您的帮助!

推荐答案

我只使用maven配置找到了我需要的东西。

I found what I needed using only maven configuration.

首先我将config文件夹添加到类路径中:

First I add config folder to the classpath:

<build>
<plugins>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.3.1</version>
    <configuration>
        <archive>
            <manifestEntries>
                <Class-Path>config/</Class-Path>
            </manifestEntries>
        </archive>
    </configuration>
</plugin>
</plugins>
</build>

我以与以前相同的方式加载资源:

I load resources the same way as before:

final InputStream resource = IOParametres.class.getResourceAsStream("/user.properties");
p.load(resource);

如果您想将示例资源文件保存在您的仓库中并从构建中删除它们: / p>

And if you want to keep your example resource files in your repo and remove them from your build:

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <excludes>
                <exclude>user.properties</exclude>
                <exclude>conf/hibernate.cfg.xml</exclude>
            </excludes>
        </resource>
    </resources>
</build>

在jar文件旁边,我添加了一个包含我需要的所有资源文件的配置文件夹。

Next to the jar file, I add a config folder holding all the resource files I need.

结果是:


  • 可以使用 getResourceAsStream加载user.properties

  • 依赖于特定资源的其他图书馆(我不会争辩,但我发现......不是那么好)可以毫无问题地加载资源。

感谢您的帮助,我希望有一天它可以帮助某人!

Thanks for the help, and I hope it may help somebody someday!

这篇关于Maven:添加外部资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆