在Spring Boot插件中定义系统属性 [英] Defining System Properties in Spring Boot Plugin

查看:100
本文介绍了在Spring Boot插件中定义系统属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序中指定一些系统属性(在编译时确定).

我正在使用Spring Boot Maven插件进行编译

当前,根据以下问题:为Maven项目指定系统属性 我尝试了以下设置(但是对于其他插件,它不起作用)

    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>application.boot.AppStarter</mainClass>
                <systemProperties>
                    <systemProperty>
                        <name>application.version</name>
                        <value>${application.version}</value>
                    </systemProperty>
                    <systemProperty>
                        <name>release.date</name>
                        <value>${timestamp}</value>
                    </systemProperty>
                </systemProperties> 
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>

如何在此插件中指定属性?

解决方案

您添加的Java系统属性只能通过添加它们的过程来访问.因此,即使您在Maven构建过程中设法添加了一些系统属性,它构建完成后将不再在那里.

如果您将罐子分发给其他人,将会发生什么.您如何期望这些属性可用?

解决方案

请参阅帖子,了解如何在运行时访问artifactId和版本 您可以通过类似的方式将时间戳条目也添加到src/main/resources/project.properties

buildTimestamp=${timestamp}

timestamp不是像project.versionproject.artifactId这样的预定义属性,因此您必须设置从Maven属性${maven.build.timestamp}中提取时间戳,并将其设置为timestamp属性的值.在此问题中已经回答.. >

I would like to specify some system properties in my applicatio (deterined at compile time).

I am using the spring boot maven plugin to compile

Currently, according to this questions: Specify system property to Maven project I tried the following setup (however this does not work as it is for a different plugin)

    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>application.boot.AppStarter</mainClass>
                <systemProperties>
                    <systemProperty>
                        <name>application.version</name>
                        <value>${application.version}</value>
                    </systemProperty>
                    <systemProperty>
                        <name>release.date</name>
                        <value>${timestamp}</value>
                    </systemProperty>
                </systemProperties> 
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>

How can i specify the properties in this plugin?

解决方案

Java system properties which you add are only accessible by the process they are added to.So even if you manage to add some system properties during the Maven build, it will no longer be there when the build is done.

What will happen if you distribute your jar to someone else. How do you expect these properties to be available?

Solution

Refer this post to see how to access the artifactId and version at runtime In a similar fashion you can add the timestamp entry as well to the src/main/resources/project.properties

buildTimestamp=${timestamp}

timestamp is not a pre-defined property like project.version or project.artifactId.So you will have to set extract the timestamp from the Maven property ${maven.build.timestamp} and set it as value to your timestamp property. This is already answered in this question.

这篇关于在Spring Boot插件中定义系统属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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