首页 > 解决方案 > 如何通过命令行将参数传递给 maven settings.xml?

问题描述

我需要根据 Spring Boot Maven 应用程序中的环境细节激活配置文件。我在settings.xml中配置了配置文件

    <profiles>
        <profile>
            <id>dev</id>
        <activation>
                <activeByDefault>false</activeByDefault> </activation>
            <repositories>
                <repository>
                    <id>devRepo</id>
                    <url>https://customRepo/maven/v1</url>
                    <releases>
                        <enabled>true</enabled>
                        <updatePolicy>always</updatePolicy>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                        <updatePolicy>always</updatePolicy>
                    </snapshots>
                </repository>
            </repositories>

        </profile>
        <profile>
            <id>qa</id>
        <activation>
                <activeByDefault>false</activeByDefault> </activation>
            <repositories>
                <repository>
                    <id>qaRepo</id>
                    <url>https://customRepo2/maven/v1</url>
                    <releases>
                        <enabled>true</enabled>
                        <updatePolicy>always</updatePolicy>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                        <updatePolicy>always</updatePolicy>
                    </snapshots>
                </repository>
            </repositories>
        </profile>

    </profiles>

我尝试使用命令激活配置文件-Pdev

mvn spring-boot:run -s settings.xml -D"spring-boot.run.profiles"=dev help:active-profiles

但配置文件未正确激活。但它通过添加activeprofiles标签来工作。

    <activeProfiles>
        <activeProfile>dev</activeProfile>
    </activeProfiles>

但是我需要激活特定的配置文件环境,例如

    <activeProfiles>
        <activeProfile>$env</activeProfile>
    </activeProfiles>

我们可以通过任何方式将参数传递给activeProfilesettings.xml 文件吗?类似这样-Denv

参考:

Maven:如何从命令行激活配置文件?

通过 Maven 在 SpringBoot 中配置活动配置文件

https://mkyong.com/maven/maven-profiles-example/

http://maven.apache.org/settings.html

标签: javamaven-3settings

解决方案


请注意这行描述:“激活发生在所有指定条件都已满足时,但并非所有条件都需要一次。” 从配置文件部分中的http://maven.apache.org/settings.html 并考虑在您的项目中定义 application.properties 并使用此配置添加行: spring.profiles.active=@spring.profiles.active@ 我希望这个工作。


推荐阅读