【问题标题】:Maven: How to print the current profile on the console?Maven:如何在控制台上打印当前配置文件?
【发布时间】:2016-12-13 09:10:29
【问题描述】:

我正在尝试打印正在运行 Maven 项目构建的当前配置文件。

我使用maven-antrun-plugin 结合引用当前配置文件的属性在控制台上打印消息。

我尝试了以下属性:

${project.activeProfiles[0].id}

${project.profiles[0].id}

但在这两种情况下,它都会在写入时打印“字符串”,而不解析变量。

这是我的测试:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <tasks>
                            <echo>current active profile: ${project.activeProfiles[0].id}</echo>
                        </tasks>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

但这是我得到的结果:

main:
 [echo] current active profile: ${project.activeProfiles[0].id}

任何建议将不胜感激。

谢谢。

【问题讨论】:

  • 为什么要具体做,这要看个人资料?

标签: maven maven-antrun-plugin maven-profiles


【解决方案1】:

maven-help-plugin 提供您所需的一切。它有一个active-profiles 目标。

您可以将它添加到您的 pom 中,甚至可以从命令行调用它(将它包含在您的 maven 构建调用中)。 Maven profile introduction page如何判断哪些配置文件在构建过程中有效? 部分将向您展示如何操作。简而言之:

mvn help:active-profiles

由于这对您不起作用(请参阅 cmets),因此这是另一种解决方案:

我认为活动配置文件(可能不止一个!)不会作为available variables 传播 - 但属性是。

所以在配置文件部分设置一个自定义属性并使用它,比如

<profiles>
    <profile>
        <id>default</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <myProfile>default</myProfile>
        </properties>
    </profile>
    <profile>
        <id>debug</id>
        <activation>
            <property>
                <name>debug</name>
            </property>
        </activation>
        <properties>
            <myProfile>debug</myProfile>
        </properties>
    </profile>
</profiles>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <tasks>
                            <echo>current active profile: ${myProfile}</echo>
                        </tasks>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

【讨论】:

  • 是的,但这不是我需要的。我想打印通过变量访问它的配置文件,因为我想根据配置文件做一些操作。
  • 没错,这就是我需要的!谢谢!
【解决方案2】:

您可以在 pom 中添加 maven-help-plugin 以始终显示活动配置文件

<build>
    <plugins>
        <!-- display active profile in compile phase -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-help-plugin</artifactId>
            <version>3.1.0</version>
            <executions>
                <execution>
                    <id>show-profiles</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>active-profiles</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>

来源:https://www.mkyong.com/maven/maven-profiles-example

【讨论】:

    猜你喜欢
    • 2010-12-03
    • 1970-01-01
    • 2019-12-15
    • 2012-03-09
    • 1970-01-01
    • 2011-10-24
    • 2021-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多