不同构建配置文件的不同依赖关系

问题描述 投票:100回答:2

是否可以在maven pom.xml文件中为不同的配置文件使用不同的依赖项集?

EG

mvn -P debug
mvn -P release

我想在一个配置文件中选择一个不同的依赖jar文件,该配置文件具有相同的类名和相同接口的不同实现。

java maven-2 build-process dependencies
2个回答
155
投票

引用Maven documentation on this

A profile element contains both an optional activation (a profile trigger) and the set of changes to be made to the POM if that profile has been activated. For example, a project built for a test environment may point to a different database than that of the final deployment. Or dependencies may be pulled from different repositories based upon the JDK version used.

(重点是我的)

只需将release配置文件的依赖项放在配置文件声明本身中,并对debug执行相同操作。

<profiles>
    <profile>
        <id>debug</id>
        …
        <dependencies>
            <dependency>…</dependency>
        </dependencies>
        …
    </profile>
    <profile>
        <id>release</id>
        …
        <dependencies>
            <dependency>…</dependency>
        </dependencies>
        …
    </profile>
</profiles>

5
投票

您的groupId,artifactId应该在配置文件中标记为属性,您可以将依赖项移动到通用部分。

© www.soinside.com 2019 - 2024. All rights reserved.