我想使用maven multi模块来分离我的项目,所以我开始制作一个公共依赖项。
但是当我尝试将父项目中定义的依赖包用于子模块maven项目import org.springframework.transaction.annotation.Transactional;
Eclipe说The import org.springframework cannot be resolved
家长 pom.xml
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ao.parent</groupId>
<artifactId>ao-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>ao-commons</module>
</modules>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
</dependencies>
</project>
儿童 pom.xml
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>ao.parent</groupId>
<artifactId>ao-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>ao-commons</artifactId>
<packaging>jar</packaging>
</project>
答案 0 :(得分:0)
你必须在这做两件事。
首先,在您的父pom中,在<dependencyManagement>
标记中添加所有依赖项。这将确保子项可以使用依赖项。所以现在这就是你的父母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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ao.parent</groupId>
<artifactId>ao-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>ao-commons</module>
</modules>
<dependencyManagement>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
<dependencies>
</dependencyManagement>
</project>
其次,在您的孩子pom中,您只需指定groupId
和artifactId
即可明确包含依赖项。由于版本将由父pom处理。所以现在你的孩子pom看起来像这样。
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>ao.parent</groupId>
<artifactId>ao-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>ao-commons</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
</dependencies>
</project>
你必须这样做的原因是因为当你有一个带有父pom和一个或多个子pom的多模块项目时,依赖项的版本将在父pom中处理,而子pom将简单地处理继承它。此外,当您在父pom中包含任何依赖项时,maven将简单地理解这些依赖项可供子项使用。但是,如果孩子必须使用任何这些依赖关系,那么孩子必须仅使用groupId
和artifactId
来明确指定依赖关系。