Maven:如何在控制台上打印当前配置文件?

Ale*_*o C 3 maven maven-profiles maven-antrun-plugin

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

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

我尝试了以下属性:

${project.activeProfiles[0].id}

${project.profiles[0].id}
Run Code Online (Sandbox Code Playgroud)

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

这是我的测试:

<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>
Run Code Online (Sandbox Code Playgroud)

但这是我获得的结果:

main:
 [echo] current active profile: ${project.activeProfiles[0].id}
Run Code Online (Sandbox Code Playgroud)

任何建议将不胜感激.

谢谢.

FrV*_*aBe 8

Maven的帮助下,插件提供你所需要的.它有一个活跃的配置文件目标.

您可以将它添加到您的pom中,甚至可以从命令行调用它(将其包含在您的maven构建调用中).该如何判断哪些配置文件实际上是在构建过程?一节的Maven的个人资料简介页面将告诉您如何.简而言之:

mvn help:active-profiles
Run Code Online (Sandbox Code Playgroud)

因为这对你不起作用(见评论),这是另一种解决方案:

我认为活动配置文件(可能有多个!)不会作为可用变量传播- 但属性是.

因此,在配置文件部分设置自定义属性并使用它,如

<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>
Run Code Online (Sandbox Code Playgroud)