Maven中不同构建配置文件的不同依赖项

时间:2020-03-06 15:04:31  来源:igfitidea点击:

对于不同的配置文件,maven pom.xml文件中是否可能具有一组不同的依赖关系?

例如

mvn -P debug
mvn -P release

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

解决方案

在此引用maven文档:

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>

groupId,artifactId应该在配置文件中标记为属性,并且我们可以将依赖项移至通用部分。