简体   繁体   English

多个Maven配置文件激活多个Spring配置文件

[英]Multiple Maven profiles activate multiple Spring profiles

I want to put together an environment in Maven, where I want to activate multiple spring profiles cumulatively depending on what Maven profiles are active.我想在 Maven 中构建一个环境,我想在其中根据 Maven 处于活动状态的配置文件累积激活多个 spring 配置文件。

Currently the relevant part of my pom.xml looks like this:目前我的 pom.xml 的相关部分如下所示:

<profiles>
    <profile>
        <id>local-db-development</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>

        <dependencies>
            <dependency>
                <groupId>com.h2database</groupId>
                <artifactId>h2</artifactId>
                <scope>runtime</scope>
            </dependency>
        </dependencies>
        <properties>
            <spring.profiles.active>local-db</spring.profiles.active>
        </properties>
    </profile>
    <profile>
        <id>live-db-development</id>
        <dependencies>
            <dependency>
                <groupId>org.postgresql</groupId>
                <artifactId>postgresql</artifactId>
                <scope>runtime</scope>
            </dependency>
        </dependencies>
        <properties>
            <spring.profiles.active>live-db</spring.profiles.active>
        </properties>
    </profile>
    <profile>
        <!--
        If active, the user authentication will be through LDAP and AD,
        Database auth will be used otherwise
         -->
        <id>ldap-authentication</id>
        <properties>
            <spring.profiles.active>ldap-authentication</spring.profiles.active>
        </properties>

        <dependencies>
            <dependency>
                <groupId>org.springframework.security</groupId>
                <artifactId>spring-security-ldap</artifactId>
            </dependency>
            <dependency>
                <groupId>org.apache.directory.server</groupId>
                <artifactId>apacheds-server-jndi</artifactId>
                <version>1.5.5</version>
            </dependency>
        </dependencies>
    </profile>
</profiles>

My problem is that when I activate ldap-authentication profile, only the relevant spring profile will be active, not taking into account any of the *-db profiles.我的问题是,当我激活ldap-authentication配置文件时,只有相关的 spring 配置文件将处于活动状态,而不考虑任何 *-db 配置文件。

I'd like to make it so that I can activate the ldap-authentication and local-db maven profiles and then both relevant spring profiles should be activated, or when I activate let's say ldap-authentication and live-db in maven, then those 2 spring profiles will be active.我想这样做,以便我可以激活 ldap-authentication 和 local-db maven 配置文件,然后应该激活两个相关的 spring 配置文件,或者当我激活时,让我们说 ldap-authentication 和 live-db in maven,然后那些2 spring 配置文件将处于活动状态。

I haven't really found a way to concat the spring profiles, so if you know better, let me know.我还没有真正找到连接 spring 配置文件的方法,所以如果您知道得更多,请告诉我。

Thanks in advance提前致谢

I faced a similar problem where I had to use either Artemis or RabbitMQ component based on the spring profile but also wanted to include other profiles (eg dev, prod).我遇到了类似的问题,我不得不使用基于 spring 配置文件的 Artemis 或 RabbitMQ 组件,但还想包含其他配置文件(例如 dev、prod)。

I did this in my pom.xml:我在我的 pom.xml 中做了这个:

    <profiles>
    <profile>
        <id>artemis</id>
        <activation>
            <property>
                <name>messaging.service</name>
                <value>artemis</value>
            </property>
        </activation>

        <properties>
            <includeSpringProfile>artemis-profile</includeSpringProfile>
        </properties>
    </profile>

    <profile>
        <id>rabbitmq</id>
        <activation>
            <property>
                <name>!messaging.service</name>
            </property>
        </activation>

        <properties>
            <includeSpringProfile>rabbitmq-profile</includeSpringProfile>
        </properties>
    </profile>
</profiles>

And this in my application.yml这在我的 application.yml 中

spring:
  profiles.include: @includeSpringProfile@

Now when I run: mvn spring-boot:run -Dspring-boot.run.arguments=--spring.profiles.active=prod现在当我运行时: mvn spring-boot:run -Dspring-boot.run.arguments=--spring.profiles.active=prod

Both prod and rabbitmq profiles are activated: prod 和 rabbitmq 配置文件都被激活:

2019-12-11 18:46:52.296  INFO 837 --- [  restartedMain] c.l.tacocloud.TacoKitchenApplication     : The following profiles are active: rabbitmq-profile,prod

Reference: Spring boot adding active profiles参考: Spring boot 添加活动配置文件

Okay, I found one solution: You need a groovy plugin, which looks like this:好的,我找到了一个解决方案:您需要一个 groovy 插件,如下所示:

<plugin>
            <groupId>org.codehaus.gmavenplus</groupId>
            <artifactId>gmavenplus-plugin</artifactId>
            <version>1.6</version>
            <dependencies>
                <dependency>
                    <groupId>org.codehaus.groovy</groupId>
                    <artifactId>groovy-all</artifactId>
                    <version>2.4.9</version>
                    <scope>runtime</scope>
                </dependency>
            </dependencies>
            <executions>
                <execution>
                    <id>concatMavenProfiles</id>
                    <phase>initialize</phase>
                    <goals>
                        <goal>execute</goal>
                    </goals>
                    <configuration>
                        <scripts>
                            <script><![CDATA[
                                def value = ""
                                (project.activeProfiles).each{ profile -> value += profile.properties.springProfiles + "," }
                                project.properties.setProperty('spring.profiles.active', value.substring(0, value.length() - 1))
                            ]]>
                            </script>
                        </scripts>
                    </configuration>
                </execution>
            </executions>
        </plugin>

This scripts gets all <springProfiles> properties from the active profiles and concatenates them into one string.此脚本从活动配置文件中获取所有 <springProfiles> 属性,并将它们连接成一个字符串。 It then sets the <spring.active.profiles> property in the main <properties> tag.然后在主 <properties> 标签中设置 <spring.active.profiles> 属性。

I switched <spring.profiles.active> tag in the profiles to <springProfiles>, so that they have 0 chance of clashing and overwriteing one another.我将配置文件中的 <spring.profiles.active> 标签切换为 <springProfiles>,以便它们相互冲突和覆盖的可能性为 0。

In my experience, you also need to define an empty <spring.profiles.active> tag in the project <properties> tag.根据我的经验,您还需要在项目 <properties> 标签中定义一个空的 <spring.profiles.active> 标签。

I solved multiple profile selections by using different variables and setting them in a list in the applications.yml file.我通过使用不同的变量并将它们设置在 applications.yml 文件的列表中来解决多个配置文件选择。

@activeEnv@ - to set the environment @activeClient@ - to set the client @activeEnv@ - 设置环境 @activeClient@ - 设置客户端

This is an example of my application.yml file:这是我的 application.yml 文件的示例:

spring:
  profiles:
    active:
      - @activeEnv@
      - @activeClient@

In the pom.xml I set the profiles like this:在 pom.xml 中,我这样设置配置文件:

<profiles>
    <!-- Environment Profiles -->
    <profile>
        <id>production</id>
        <properties>
            <activeEnv>production</activeEnv>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>development</id>
        <properties>
            <activeEnv>development</activeEnv>
        </properties>
    </profile>
    <!-- Environment Profiles -->
    <!-- Tax Client Profiles -->
    <profile>
        <id>clientA</id>
        <properties>
            <activeTaxClient>ClientA</activeTaxClient>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>clientB</id>
        <properties>
            <activeTaxClient>ClientB</activeTaxClient>
        </properties>
    </profile>
    <!-- Client Profiles -->
</profiles>

This setting results in the following:此设置会产生以下结果:

org.springframework.boot.SpringApplication: The following 2 profiles are active: "production", "clientA"

Note : if you use IntelliJ remember to click the refresh button after selecting the desired profiles注意:如果您使用 IntelliJ,请记住在选择所需的配置文件后单击刷新按钮

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

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