对于 maven1,我使用的是 延长 标签告诉我的子项目使用他们的父配置。
在父项中声明的所有依赖项都可用于扩展(子)项目。
现在使用 maven2 我正在使用继承/组合功能,我必须在每个子项目中重新声明我的依赖项(减去版本号)。
(见 how-to-share-common-properties-among-several-maven-projects)
有没有办法告诉 maven 我想在所有 child 之间分享我的一些依赖关系?
最佳答案
Now with maven2 I'm using the inheritance/composition feature and I have to redeclare my dependencies (minus the version number) in every child project
不,你没有。在父 pom 中声明的依赖是继承的。
Is there a way to tell maven that I want to share some of my dependencies among all my children ?
只需声明
<parent>
子 POM 中的元素。例如,对于这个父 POM:<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>my.group.id</groupId>
<artifactId>parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>Demo - Parent</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<modules>
<module>child</module>
</modules>
</project>
而这个子模块的POM:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>my.group.id</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<name>Demo - Child</name>
<artifactId>child</artifactId>
<packaging>jar</packaging>
</project>
junit 依赖项按预期继承:
$ mvn 依赖:树
[INFO] 正在扫描项目...
[INFO] 搜索带有前缀的插件的存储库:“依赖”。
[信息] --------------------------------------------- -------------------------
[INFO] 建筑演示 - child
[INFO] 任务段:[依赖:树]
[信息] --------------------------------------------- -------------------------
[信息] [依赖:树{执行:默认-cli}]
[信息] my.group.id:child:jar:1.0-SNAPSHOT
[信息]\- junit:junit:jar:3.8.1:test
[信息] --------------------------------------------- -------------------------
[信息] 构建成功
[信息] --------------------------------------------- -------------------------
...
我怀疑您在 the
<dependencyManagement>
section 中声明了依赖关系(有另一个目的)。
关于maven-2 - Maven2 跨父子共享依赖(不重新声明子依赖),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3211643/