【问题标题】:maven list active profiles in filteringmaven 列出过滤中的活动配置文件
【发布时间】:2023-03-23 15:56:02
【问题描述】:

我正在尝试获取 maven 中所有活动配置文件的列表,并将它们用作资源过滤中的变量。

特别是当我运行类似的东西时

mvn package -Pprofile1,profile2

我想在资源过滤中将字符串“profile1,profile2”作为变量公开。

例如,在我的 pom.xml 文件中,我将 maven war 插件配置如下,以允许对 src/main/resources/META-INF 目录中的所有内容进行资源过滤,并且我定义了一些配置文件:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
            </manifest>
        </archive>
        <webResources>
            <resource>
                <directory>src/main/resources</directory>
                <!-- Only include the META-INF directory -->
                <includes>
                    <include>META-INF/*</include>
                </includes>
                <!-- replace placeholder values -->
                <filtering>true</filtering>
                    </resource>
                </webResources>
    </configuration>
</plugin>
...
<profiles>
    <profile>
        <id>profile1</id>
        <properties>
            <environment>profile1</environment>
        </properties>
    </profile>
    <profile>
        <id>profile2</id>
        <properties>
            <environment>profile2</environment>
        </properties>
    </profile>

在META-INF目录下,我有一个context.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/platform">
    <Environment name="spring.profiles.active" value="${environment}" type="java.lang.String" />
</Context>

我尝试在每个配置文件下设置属性“环境”,但生成的 context.xml 文件仅使用最后一个“环境”变量(例如 value=profile2)。

我也尝试过使用这个:value=${project.activeProfiles},但这会返回某种对象列表。如果我可以遍历这个列表并从配置文件中提取 id 来构建字符串,那就太好了,但是我在 maven 资源插件中找不到任何关于这样做的文档。

我最终要做的是通过 context.xml 文件将 spring.profiles.active 变量设置为环境变量,该变量与 maven 构建配置文件相同。

【问题讨论】:

  • 想想你的 pom 文件。为什么将 src/main/resources 定义为 webResources 原因 src/main/resources 是在资源阶段复制的常用资源,如果您想过滤它,请像这样定义它...
  • 我这样做是为了将 context.xml 文件放入 META-INF 文件夹中,我不确定这是否是正确的方法,但它确实对我有用。
  • 嘿,这有什么好运气吗?我也在寻找一种方法来“我可以遍历这个列表并从配置文件中提取 id 来构建字符串”。

标签: spring maven


【解决方案1】:

您可以通过以下方式列出活动的个人资料:

mvn help:active-profiles

【讨论】:

  • 谢谢,但我不想列出活动配置文件(我已经知道它们是明确设置的),而是让 Maven 使用资源过滤将占位符变量替换为所有活动配置文件。抱歉,如果我的问题不清楚。
猜你喜欢
  • 2017-07-12
  • 2021-01-08
  • 2015-11-21
  • 2014-10-22
  • 2014-10-14
  • 2019-08-03
  • 2014-07-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多