Spring Boot:是否可以在任意目录中使用外部application.properties文件,使用fat jar? [英] Spring Boot: Is it possible to use external application.properties files in arbitrary directories with a fat jar?

查看:4344
本文介绍了Spring Boot:是否可以在任意目录中使用外部application.properties文件,使用fat jar?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有可能有多个application.properties文件吗? ( EDIT :请注意,此问题演变为标题上的问题。)



我试图有2个文件。




  • 第一个位于应用程序Jar中的根文件夹。

  • 第二个位于目录在类路径中指定。



2个文件都命名为application.properties。



是否可以合并这两个文件的内容? (和第二个属性值重写第一个)或者,如果我有一个文件,然后另一个文件被忽略?



UPDATE 1 :可以合并内容。昨天似乎第一个被忽略,但似乎是因为那个东西被打破了。现在效果很好。



UPDATE 2 :又回来了!再次只有两个文件中的一个正在应用。它奇怪...它开始之后,我使用Spring工具套件构建应用程序jar文件。看起来Jar版本总是忽略第二个(在类路径中),而在STS上运行的扩展版本的行为是不同的。



UPDATE 3

的Jar版本其实是正确的。它是java.exe的规范。
当指定-jar选项时,java.exe将忽略-classpath选项和CLASSPATH环境变量,类路径将仅包含jar文件。因此,类路径上的第二个application.properties文件被忽略。



现在,如何让类路径上的第二个application.properties被加载?



UPDATE 4



我管理在外部路径加载application.properties文件-jar选项。



键是PropertiesLauncher。



要使用PropertiesLauncher,必须更改pom.xml文件像这样:

 < build> 
< plugins>
< plugin>
< groupId> org.springframework.boot< / groupId>
< artifactId> spring-boot-maven-plugin< / artifactId>
< configuration> <! - added - >
< layout> ZIP< / layout> <! - 使用PropertiesLaunchar - >
< / configuration>
< / plugin>
< / plugins>
< / build>

为此,我引用了以下StackOverflow问题: spring启动属性启动器无法使用。 BTW,在Spring Boot Maven插件文档( http:/ /docs.spring.io/spring-boot/docs/1.1.7.RELEASE/maven-plugin/repackage-mojo.html ),没有提到指定使用PropertiesLauncher的ZIP触发器。 (也许在另一个文档中?)



在jar文件构建之后,我可以看到PropertiesLauncher被用于检查META-INF / MENIFEST中的Main-Class属性



现在,我可以运行该jar如下(在Windows中):

  java -Dloader.path = file:/// C:/My/External/Dir,MyApp-0.0.1-SNAPSHOT.jar -jar MyApp-0.0.1-SNAPSHOT.jar 

请注意,应用程序jar文件包含在loader.path中。



现在加载了C:\My\External\Dir\config中的application.properties文件。





对于非jar(扩展)文件(例如,静态html文件)也可以由jar访问,因为它位于加载器路径中。在UPDATE 2中提到的版本,也许有一个类路径顺序问题。



(BTW,我改变了问题的标题更具体到这个问题。

解决方案

我自己的UPDATE 4中的问题其实是答案。其他答案和评论有助于找到答案。



谢谢。


Is it possible to have multiple application.properties file? (EDIT: note that this question evolved to the one on the title.)

I tried to have 2 files.

  • The first one is on the root folder in the application Jar.
  • The second one is on the directory which is specified in classpath.

2 files are both named 'application.properties'.

Is it possible to 'merge' the contents of both files? (and the second one's property values override the first one's) Or, if I have one file then the other file is ignored?

UPDATE 1: it is possible to 'merge' the contents. Yesterday it seemed that the first one was ignored, but it seems that it's because that something was broken then. Now it works well.

UPDATE 2: It's back again! Again only one of two files is being applied. It's strange... It started after I built the app jar file using Spring Tool Suite. And it seems that the Jar version always ignores the second one (in classpath), while the behavior of the expanded version which runs on STS varies. From where can I start to investigate?

UPDATE 3:

The behavior of the Jar version was in fact correct. It's the specification of java.exe. When -jar option is specified, java.exe ignores both -classpath option and CLASSPATH environment variable, and the classpath will contain only the jar file. So, the second application.properties file on the classpath is ignored.

Now, how can I let the second application.properties on the classpath be loaded?

UPDATE 4:

I managed to load an application.properties file in external path while using -jar option.

The key was PropertiesLauncher.

To use PropertiesLauncher, pom.xml file must be changed like this:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>  <!-- added -->
                <layout>ZIP</layout> <!-- to use PropertiesLaunchar -->
            </configuration>
        </plugin>
    </plugins>
</build>

For this, I referenced the following StackOverflow question: spring boot properties launcher unable to use . BTW, In Spring Boot Maven Plugin document(http://docs.spring.io/spring-boot/docs/1.1.7.RELEASE/maven-plugin/repackage-mojo.html), there is no mention that specifying ZIP triggers that PropertiesLauncher is used. (Perhaps in another document?)

After the jar file had been built, I could see that the PropertiesLauncher is used by inspecting Main-Class property in META-INF/MENIFEST.MF in the jar.

Now, I can run the jar as follows(in Windows):

java -Dloader.path=file:///C:/My/External/Dir,MyApp-0.0.1-SNAPSHOT.jar -jar MyApp-0.0.1-SNAPSHOT.jar

Note that the application jar file is included in loader.path.

Now an application.properties file in C:\My\External\Dir\config is loaded.

As a bonus, any file (for example, static html file) in that directory can also be accessed by the jar since it's in the loader path.

As for the non-jar (expanded) version mentioned in UPDATE 2, maybe there was a classpath order problem.

(BTW, I changed the question's title to be more specific to this issue.)

解决方案

My own UPDATE 4 in the question is actually the answer. Other answers and comments were helpful to find the answer.

Thank you.

这篇关于Spring Boot:是否可以在任意目录中使用外部application.properties文件,使用fat jar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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