【问题标题】:Multiple Maven profiles activate multiple Spring profiles多个 Maven 配置文件激活多个 Spring 配置文件
【发布时间】:2019-12-12 01:52:46
【问题描述】:

我想在 Maven 中组合一个环境,我想根据激活的 Maven 配置文件累积激活多个弹簧配置文件。

目前我的 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 配置文件,或者当我激活时让我们说 maven 中的 ldap-authentication 和 live-db ,那么这 2 个弹簧配置文件将处于活动状态。

我还没有真正找到连接弹簧轮廓的方法,所以如果你知道得更好,请告诉我。

提前致谢

【问题讨论】:

  • “这样我就可以点击 ldap-authentication 和 local-db 然后”点击 ??请详细说明你的意思。您在应用程序范围内单击,而不是在运行诸如 mvn 之类的可执行文件时单击。
  • 已编辑,我希望现在更清楚了。 “点击”是指我在 IntelliJ 中激活 maven 配置文件时的操作(如点击复选框)
  • 你不应该那样做。您应该构建一个工件,而不是为不同的配置文件构建一个工件。 Maven 配置文件!= 弹簧配置文件。
  • 我知道,这是我的问题。我想这样做,以便每个活动的 Maven 配置文件激活相关的弹簧配置文件,而不会相互覆盖。
  • 您能否详细说明为什么我不应该这样做,或者有什么更好的方法来解决我的问题?哦,这个问题的重点是我想构建一个具有多个活动弹簧轮廓的工件

标签: java spring maven


【解决方案1】:

我遇到了类似的问题,我不得不使用基于 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 adding active profiles

【讨论】:

    【解决方案2】:

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

    此脚本从活动配置文件中获取所有 属性并将它们连接成一个字符串。 然后它在主 标记中设置 属性。

    我将配置文件中的 标记切换为 ,这样它们相互冲突和覆盖的机会为 0。

    根据我的经验,你还需要在项目的标签中定义一个空的标签。

    【讨论】:

      猜你喜欢
      • 2011-06-05
      • 2011-07-22
      • 1970-01-01
      • 2014-10-29
      • 2011-07-17
      • 1970-01-01
      • 1970-01-01
      • 2011-11-12
      • 1970-01-01
      相关资源
      最近更新 更多