读取外部应用程序属性中的POM值

huangapple go评论91阅读模式
英文:

Read POM values in external application properties

问题

我在pom文件中有以下属性:

<name>DemoApplication</name>
<description>Demo spring project</description>
<version>1.0.0-SNAPSHOT</version>

我有一个类从application.yml文件中读取属性。
但是,我没有使用位于src/main/resources下的application.yml,而是通过外部文件指定属性,如下所示:

java -jar DemoApplication-1.0.0-SNAPSHOT.jar --spring.config.location=application.yml

在这个外部的应用程序属性中,我有以下属性:

swagger:
    apiTitle: '@project.name@'
    apiDescription: '@project.description@'
    apiVersion: '@project.version@'

问题是,@project.name@和其他属性没有被预期替换,而是按原样读取。
应该如何解决这个问题?

英文:

I have the following properties in the pom file

&lt;name&gt;DemoApplication&lt;/name&gt;
&lt;description&gt;Demo spring project&lt;/description&gt;
&lt;version&gt;1.0.0-SNAPSHOT&lt;/version&gt;

And I have a class that reads the properties from application.yml
But instead of using the application.yml under src/main/resources I am specifying the properties through an external file as follows

java -jar DemoApplication-1.0.0-SNAPSHOT.jar --spring.config.location=application.yml

In this external application properties, I have the following attributes

swagger:
    apiTitle: &#39;@project.name@&#39;
    apiDescription: &#39;@project.description@&#39;
    apiVersion: &#39;@project.version@&#39;

The issue is that the @project.name@ and other properties are not being replaced as expected, but are read as-is.

How should the problem be approached?

答案1

得分: 2

根据Spring Boot v2官方文档的这一部分,你可以进行配置:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.7</version>
    <configuration>
        <delimiters>
            <delimiter>@</delimiter>
        </delimiters>
        <useDefaultDelimiters>false</useDefaultDelimiters>
    </configuration>
</plugin>

根据你的配置,将useDefaultDelimiters设置为falsetrue

该官方文档的其他部分将对你的用例有所帮助,特别是"77.5 使用YAML进行外部属性配置"。

如果什么都不起作用,为什么不加载自定义的属性文件呢?只需在启动程序时引用正确的路径,并在程序内部测试你的config.properties文件是否可用并包含你所需的内容。

当然,使用Maven加载资源文件的方式是最简单的方法,它也应该是一个简单的属性文件。我在我发布的管理配置的软件内就是按照这种方式来做的:

  1. 编写一个 app.properties 文件
  2. 在运行时使用Maven加载该文件,使用资源配置
  3. 使用经典语法 ${my.prop} 来扩展属性
  4. 使用Maven任务运行程序。

当然,当你将应用作为一个jar分发时,情况会有些不同。
也许你可以尝试在一个Maven目标中编写你的属性文件。

英文:

According that section of the official documentation of Spring Boot v2, you can configure it with :

&lt;plugin&gt;
	&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
	&lt;artifactId&gt;maven-resources-plugin&lt;/artifactId&gt;
	&lt;version&gt;2.7&lt;/version&gt;
	&lt;configuration&gt;
		&lt;delimiters&gt;
			&lt;delimiter&gt;@&lt;/delimiter&gt;
		&lt;/delimiters&gt;
		&lt;useDefaultDelimiters&gt;false&lt;/useDefaultDelimiters&gt;
	&lt;/configuration&gt;
&lt;/plugin&gt;

With useDefaultDelimiters set to false or to true depending on your configuration.

The others sections of that official documentation will be helpful for your use case, especially these one : "77.5 Use YAML for External Properties".

If nothing is working, why don't you are loading a custom Properties file ? It could be loaded as you need without any problem. Just reference it with the correct path when you are starting your program, and inside your program, test if your file config.properties is available and contains what you need to work with.

Of course, the Maven way of loading resources files is the best easy way to go, and it should be a simple Properties file too. I have done exactly that way inside the software I am released to manage my configuration :

  1. Writing a app.properties
  2. Loading that file with Maven at runtime with resource configuration
  3. Expanding properties with classical syntax ${my.prop}
  4. Run the program with a Maven task.

Of course, when you distribute your app as a jar, it is a bit different.
Maybe you can try to write your properties files within a Maven goal.

huangapple
  • 本文由 发表于 2020年8月17日 19:59:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63450348.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定