java Maven:添加外部资源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26672221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:22:22  来源:igfitidea点击:

Maven : add external resources

javamavenjarmaven-resources-pluginmaven-jar-plugin

提问by GournaySylvain

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

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

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.

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

My property file is loaded with:

我的属性文件加载了:

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

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

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

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

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 插件尝试了很多东西,比如 maven-jar-plugin、maven-surefire-plugin 和 maven-resources-plugin,但我无法让它工作......

Thanks in advance for your help!

在此先感谢您的帮助!

回答by GournaySylvain

I found what I needed using only maven configuration.

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

First I add config folder to the classpath:

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

<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);

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>

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

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

The result is:

结果是:

  • user.properties can be loaded using getResourceAsStream
  • other libraries relying on specific resources (I won't argue, but I find it... not that good) can load their resources without any issue.
  • 可以使用getResourceAsStream加载 user.properties
  • 其他依赖特定资源的库(我不会争论,但我发现它......不是那么好)可以毫无问题地加载他们的资源。

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

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

回答by Deltharis

As I mentioned in the comment - it looks like you want to use user.propertiesfile simply as a text file that lies besides your jar. If that's the case, than using it is rather simple - directory containing your jar file is the current directory when checked during runtime. That means that all you need is:

正如我在评论中提到的 - 看起来您只想将user.properties文件用作位于 jar 之外的文本文件。如果是这种情况,那么使用它就相当简单 - 在运行时检查时,包含 jar 文件的目录是当前目录。这意味着您只需要:

properties.load(new FileInputStream("config/user.properties"));

without trying to put in on the project classpath.

无需尝试放入项目类路径。

And if anything else is there to be done, it would just by copying your properties from resources directory to target to avoid the hussle of doing it by hand. That can be achieved by maven-antrun-plugin:

如果还有其他事情要做,只需将您的属性从资源目录复制到目标即可,以避免手工操作的麻烦。这可以通过 maven-antrun-plugin 来实现:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <tasks>
                            <mkdir dir="${project.build.directory}" />
                            <copy file="${basedir}/src/main/resources/user.properties" tofile="${project.build.directory}/config/user.properties" />
                        </tasks>
                    </configuration>
                </execution>
            </executions>
        </plugin>