父母和子女之间的Maven2共享依赖关系(没有重新声明儿童的依赖关系) [英] Maven2 sharing dependencies across parent and children (without redeclaring dependencies in children)
问题描述
使用maven1,我使用扩展标签来告诉我的孩子项目使用他们的父配置。
With maven1 I was using the extend tag to tell my children project to use their parent configuration.
父中声明的所有依赖项可用于扩展(子级)项目。
All dependencies declared in the parent were available in extending (children) projects.
现在使用maven2我使用继承/组合功能,我必须重新声明我的依赖项(减去版本号)每个孩子项目。
(请参阅如何共享 - 共享 - 属性 - 几个maven项目)
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. (see how-to-share-common-properties-among-several-maven-projects)
有没有办法告诉maven我想在我所有的孩子中分享我的一些依赖项?
Is there a way to tell maven that I want to share some of my dependencies among all my children ?
推荐答案
现在用maven2我使用继承/组合功能,我必须重新声明我的每个子项目中的依赖关系(减去版本号)
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中声明的依赖关系是继承的。
No, you don't. Dependencies declared in the parent pom are inherited.
有没有办法告诉maven我想在我所有的孩子们?
Is there a way to tell maven that I want to share some of my dependencies among all my children ?
只需在子POM中声明< parent>
元素。例如,使用这个父POM:
Just declare the <parent>
element in child POMs. For example, with this parent 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:
And this POM for the child module:
<?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依赖项按预期继承:
The junit dependency gets inherited as expected:
$ mvn dependency:tree
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'dependency'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Demo - Child
[INFO] task-segment: [dependency:tree]
[INFO] ------------------------------------------------------------------------
[INFO] [dependency:tree {execution: default-cli}]
[INFO] my.group.id:child:jar:1.0-SNAPSHOT
[INFO] \- junit:junit:jar:3.8.1:test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
...
我怀疑你在< a href =http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Management =noreferrer> < dependencyManagement>
部分(其另一目的)。
I suspect that you are declaring dependencies in the <dependencyManagement>
section (which has another purpose).
这篇关于父母和子女之间的Maven2共享依赖关系(没有重新声明儿童的依赖关系)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!