始终在Maven的编译阶段显示当前活动的配置文件

问题描述 投票:1回答:1

我有一个Spring Boot应用程序,它支持两个配置文件:dev和release。显然,在本地工作时使用dev配置文件,并且当在服务器上实际部署应用程序时,Jenkins将release配置文件用作CI / CD管道的一部分。

配置文件定义

<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <activatedProperties>dev</activatedProperties>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>

    <profile>
        <id>release</id>
        <properties>
            <activatedProperties>release</activatedProperties>
        </properties>
    </profile>
</profiles>

dev配置文件默认激活,因为这是开发人员花费大部分时间的地方:开发。我希望他们在将应用程序导出到其他位置时指定发布配置文件,但更好的是,将该任务委托给Jenkins。

我刚刚发现以下命令来查看哪些配置文件处于活动状态:

mvn help:active-profiles

所以我可以在我的Jenkins管道脚本中使用它

mvn clean compile -Prelease help:active-profiles

这样可行。但是,我想知道是否有一种方法可以在编译阶段始终运行help:active-profiles目标,因此所有开发人员都可以清楚地看到他们使用的是哪个配置文件。

谢谢

maven spring-boot
1个回答
2
投票

您可以添加Maven帮助插件,如下所示:

<plugin>
    <artifactId>maven-help-plugin</artifactId>
    <version>2.2</version>
    <executions>
        <execution>
            <id>print-profile</id>
            <phase>compile</phase>
            <goals>
                <goal>active-profiles</goal>
            </goals>
        </execution>
    </executions>
</plugin>

您可以通过以下消息获得日志:

[INFO] --- maven-help-plugin:2.2:active-profiles (print-profile) @ testProject ---
[INFO] 
Active Profiles for Project 'com.test.testProject:jar:1.0.0-SNAPSHOT': 

The following profiles are active:

 - sonar (source: external)
 - release (source: external)
 - dev (source: com.test.testProject:jar:1.0.0-SNAPSHOT)
© www.soinside.com 2019 - 2024. All rights reserved.