我有以下结构:
main/
--pom.xml
--common/
--pom.xml
--core/
--pom.xml
common和core都是主项目的模块,所以main / pom.xml是这样的:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>main</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>Main APP</name>
<url>http://maven.apache.org</url>
<modules>
<module>common</module>
<module>core</module>
</modules>
</project>
除此之外,common / pom.xml应该在所有未来的模块之间保留一些共同的依赖关系。这就是现在/ pom.xml的常见现象:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>common</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>mod1</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>com.mycompany.app</groupId>
<artifactId>main</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>common-dependency</groupId>
<artifactId>common</artifactId>
<version>3.8.1</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
最后,模块核心应该使用公共模块中定义的依赖项,所以这就是我在core / pom.xml中所做的:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>core</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>Core</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>com.mycompany.app</groupId>
<artifactId>main</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.mycompany.app</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
我想要的是当我在核心模块中运行mvn dependency:copy-dependencies
时,maven应该复制来自公共模块的所有依赖项(例如,common-dependency:common)。但实际上发生的是它不会复制任何依赖关系,所以就像核心模块根本没有任何依赖关系一样。
我在这里缺少什么?
答案 0 :(得分:1)
范围'import'用于dependencyManagement,它只在您实际使用它们时为您提供模块的版本和默认范围。
如果要获取依赖项,则不需要将它们包装在dependencyManagement中,只需使用传递依赖机制。
所以常见的/ pom.xml应该是
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>common</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>mod1</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>com.mycompany.app</groupId>
<artifactId>main</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>common-dependency</groupId>
<artifactId>common</artifactId>
<version>3.8.1</version>
</dependency>
</dependencies>
</project>
core / pom.xml应该是
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>core</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>Core</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>com.mycompany.app</groupId>
<artifactId>main</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>com.mycompany.app</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>