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

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

问题描述

是否可以有多个 application.properties 文件?(编辑:注意这个问题演变成标题上的问题.)

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

我尝试有 2 个文件.

I tried to have 2 files.

  • 第一个位于应用程序 Jar 的根文件夹中.
  • 第二个位于类路径中指定的目录中.

2 个文件都被命名为application.properties".

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:可以合并"内容.昨天好像第一个被忽略了,但似乎是因为当时有什么东西坏了.现在它运行良好.

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.

更新 2:它又回来了!同样,仅应用了两个文件之一.很奇怪......它是在我使用Spring Tool Suite构建应用程序jar文件之后启动的.并且似乎 Jar 版本总是忽略第二个(在类路径中),而在 STS 上运行的扩展版本的行为各不相同.我可以从哪里开始调查?

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?

更新 3:

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

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.

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

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

更新 4:

我在使用 -jar 选项时设法在外部路径中加载了一个 application.properties 文件.

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

关键是 PropertiesLauncher.

The key was PropertiesLauncher.

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

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>

为此,我参考了以下 StackOverflow 问题:spring boot properties launcher cannot使用.顺便说一句,在 Spring Boot Maven 插件文档中(http://docs.spring.io/spring-boot/docs/1.1.7.RELEASE/maven-plugin/repackage-mojo.html),没有提到指定ZIP触发PropertiesLauncher是用过的.(也许在另一个文件中?)

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

jar 文件构建完成后,我可以通过检查 jar 中 META-INF/MENIFEST.MF 中的 Main-Class 属性看到使用 PropertiesLauncher.

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.

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

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

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

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

现在 C:MyExternalDirconfig 中的 application.properties 文件已加载.

Now an application.properties file in C:MyExternalDirconfig is loaded.

作为奖励,该目录中的任何文件(例如,静态 html 文件)也可以被 jar 访问,因为它位于加载程序路径中.

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.

对于UPDATE 2中提到的非jar(扩展)版本,可能是classpath顺序问题.

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

推荐答案

我设法在使用 -jar 选项时在外部路径中加载了 application.properties 文件.

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

关键是 PropertiesLauncher.

The key was PropertiesLauncher.

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

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>

为此,我参考了以下 StackOverflow 问题:spring boot properties launcher cannot使用.顺便说一句,在 Spring Boot Maven 插件文档中(http://docs.spring.io/spring-boot/docs/1.1.7.RELEASE/maven-plugin/repackage-mojo.html),没有提到指定ZIP触发PropertiesLauncher是用过的.(也许在另一个文件中?)

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

jar 文件构建完成后,我可以通过检查 jar 中 META-INF/MENIFEST.MF 中的 Main-Class 属性看到使用 PropertiesLauncher.

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.

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

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

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

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

现在 C:MyExternalDirconfig 中的 application.properties 文件已加载.

Now an application.properties file in C:MyExternalDirconfig is loaded.

作为奖励,该目录中的任何文件(例如,静态 html 文件)也可以被 jar 访问,因为它位于加载程序路径中.

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.

对于UPDATE 2中提到的非jar(扩展)版本,可能是classpath顺序问题.

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

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

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