繁体   English   中英

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

[英]Multiple Maven profiles activate multiple Spring profiles

我想在 Maven 中构建一个环境,我想在其中根据 Maven 处于活动状态的配置文件累积激活多个 spring 配置文件。

目前我的 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>

我的问题是,当我激活ldap-authentication配置文件时,只有相关的 spring 配置文件将处于活动状态,而不考虑任何 *-db 配置文件。

我想这样做,以便我可以激活 ldap-authentication 和 local-db maven 配置文件,然后应该激活两个相关的 spring 配置文件,或者当我激活时,让我们说 ldap-authentication 和 live-db in maven,然后那些2 spring 配置文件将处于活动状态。

我还没有真正找到连接 spring 配置文件的方法,所以如果您知道得更多,请告诉我。

提前致谢

我遇到了类似的问题,我不得不使用基于 spring 配置文件的 Artemis 或 RabbitMQ 组件,但还想包含其他配置文件(例如 dev、prod)。

我在我的 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>

这在我的 application.yml 中

spring:
  profiles.include: @includeSpringProfile@

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

prod 和 rabbitmq 配置文件都被激活:

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

参考: Spring boot 添加活动配置文件

好的,我找到了一个解决方案:您需要一个 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>

此脚本从活动配置文件中获取所有 <springProfiles> 属性,并将它们连接成一个字符串。 然后在主 <properties> 标签中设置 <spring.active.profiles> 属性。

我将配置文件中的 <spring.profiles.active> 标签切换为 <springProfiles>,以便它们相互冲突和覆盖的可能性为 0。

根据我的经验,您还需要在项目 <properties> 标签中定义一个空的 <spring.profiles.active> 标签。

我通过使用不同的变量并将它们设置在 applications.yml 文件的列表中来解决多个配置文件选择。

@activeEnv@ - 设置环境 @activeClient@ - 设置客户端

这是我的 application.yml 文件的示例:

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

在 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>

此设置会产生以下结果:

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

注意:如果您使用 IntelliJ,请记住在选择所需的配置文件后单击刷新按钮

暂无
暂无

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

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