简体   繁体   English

Maven和spring-boot:如何在spring-boot:repackage上指定配置文件

[英]Maven and spring-boot: how to specify profile(s) on spring-boot:repackage

With spring-boot, I know that I can have profiles and use different configuration files depending on the active profiles. 通过spring-boot,我知道我可以拥有配置文件,并根据活动配置文件使用不同的配置文件。 For instance the command: 例如,命令:

"mvn spring-boot:run -Drun.profiles=default,production" “ mvn spring-boot:run -Drun.profiles =默认,生产”

will run my spring-boot application using the settings defined in both the "application-default.properties" and "application-production.properties" with the settings on the second file overriding the same settings defined in the first file (for instance db connection settings). 将使用“ application-default.properties”和“ application-production.properties”中定义的设置运行我的spring-boot应用程序,第二个文件上的设置将覆盖第一个文件中定义的相同设置(例如db connection)设置)。 All this is currently running well. 目前,所有这些都运行良好。

However, I want to build my spring-boot application and generate a runnable jar using the following command: 但是,我想使用以下命令构建我的spring-boot应用程序并生成一个可运行的jar:

"mvn package spring-boot:repackage". “ MVN软件包spring-boot:repackage”。

This command does generate self-contained runnable jar perfectly well. 此命令确实可以很好地生成自包含的可运行jar。 The question is, ¿how do I specifiy active profiles with the former command? 问题是,¿如何使用前一个命令指定活动配置文件? I have used 我用过

"mvn package spring-boot:repackage -Drun.profiles=default,production" “ MVN软件包spring-boot:repackage -Drun.profiles = default,production”

but it is not working. 但它不起作用。

The spring profiles are targeted to application runtime. Spring配置文件针对应用程序运行时。 They don't operate at the time of packaging the application as the Maven ones do. 它们在打包应用程序时不像Maven那样运行。 So you have to use them when launching your application, not when packaging it. 因此,您必须在启动应用程序时使用它们,而不是在打包应用程序时使用它们。

However, if you want to generate different packages with some default profile each one, you could play with Maven resource filtering. 但是,如果要生成每个都有一个默认配置文件的不同软件包,则可以使用Maven资源过滤。 After all, the way to build a Spring Boot runnable jar with Maven is to follow the standard procedure, so you get the Spring Boot Maven plugin involved: 毕竟,使用Maven构建Spring Boot可运行jar的方法是遵循标准过程,因此您会涉及到Spring Boot Maven插件:

mvn clean install -PproductionMvnProfile

See also: 也可以看看:

I answered the same question in this thread: Pass Spring profile in maven build , but I will repeat the answer here again. 我在此线程中回答了相同的问题: 在maven build中传递Spring配置文件 ,但是我将在这里再次重复答案。

In case someone have the same situation, to run a spring boot runnable jar or war using specific profile you need to have the property spring.profiles.active present in your default application.properties file, to change its value dynamically while generating the artifact, you can do this: 如果有人遇到相同的情况,要使用特定配置文件运行spring boot runnable jar或war,则需要在默认application.properties文件中提供spring.profiles.active属性,以便在生成工件时动态更改其值,你可以这样做:

First in spring properties or yaml file, add the spring.profiles.active with it's value as placeholder: 首先在spring属性或yaml文件中,将spring.profiles.active及其值添加为占位符:

spring.profiles.active=@active.profile@

Second, pass the value with maven: 其次,使用maven传递值:

mvn clean package spring-boot:repackage -Dactive.profile=dev

or if the spring-boot plugin is already presented in your pom like the following: 或者如果您的pom中已经出现了spring-boot插件,如下所示:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

You can run instead the following command: 您可以改为运行以下命令:

mvn clean package -Dactive.profile=dev

When the jar/war packaged, the value will be set to dev. 打包jar / war后,该值将设置为dev。

you can also leverage the use of maven profiles: 您还可以利用Maven配置文件:

<profiles>
        <profile>
            <id>dev</id>
            <properties>
                <active.profile>dev</active.profile>
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <active.profile>prod</active.profile>
            </properties>
        </profile>
    </profiles>

Then run: 然后运行:

mvn clean install -Pdev

You don't need to pass the 2 properties files (the default and dev/prod), by default the variables in application.properties will be executed first. 您不需要传递2个属性文件(默认文件和dev / prod文件),默认情况下,将首先执行application.properties中的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM