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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/166895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:59:24  来源:igfitidea点击:

Different dependencies for different build profiles

javamaven-2build-processdependencies

提问by izb

Is it possible to have a different set of dependencies in a maven pom.xml file for different profiles?

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

e.g.

例如

mvn -P debug
mvn -P release

I'd like to pick up a different dependency jar file in one profile that has the same class names and different implementations of the same interfaces.

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

采纳答案by Aleksandar Dimitrov

To quote the Maven documentation on this:

引用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.
配置文件元素包含一个可选的激活(配置文件触发器)和一组对 POM 进行的更改(如果该配置文件已被激活)。例如,为测试环境构建的项目可能指向与最终部署不同的数据库。或者可以根据所使用的 JDK 版本从不同的存储库中提取依赖项

(Emphasis is mine)

(重点是我的)

Just put the dependency for the releaseprofile inside the profile declaration itself and do the same for debug.

只需将release配置文件的依赖项放在配置文件声明本身中,然后对debug.

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

回答by Aleksandar Dimitrov

Your groupId, artifactId should be tokenized in your profiles as properties and you can move your dependencies to the generic section.

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