若是一个maven project 创建多modules ,pom文件应该是这样:java
<groupId>cn.ifengkou</groupId> <artifactId>spring-cloud-test</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>spring-boot-test</module> <module>spring-cloud-config</module> </modules>
module 的pom文件应该是这样:git
<parent> <artifactId>spring-cloud-test</artifactId> <groupId>cn.ifengkou</groupId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>spring-boot-test</artifactId>
你们在一开始学spring-boot时,都应该看过 官网的quick start项目,里面pom文件也有一个parentgithub
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent>
这样,modules 项目中的 要继承父项目,而spring boot 又要继承spring-boot-starter-parent,一个pom文件不能存在两个parent,这个问题怎么解决呢?web
网上提供了一种方式,经过在子module 的parent 中,加
#project(parent) pom <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.3.RELEASE</version> </parent> <modules> <module>dao</module> </modules> # module pom <parent> <artifactId>parent</artifactId> <groupId>com.luyh.projectv1</groupId> <version>1.0-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent>
其实spring-boot-starter-parent 只是一种快速开始spring boot 的方式,不必非要用它,它所作的只是提供依赖管理和plugin管理(dependency management and plugin management),咱们也能够本身作这些,固然若是想省事,spring 也提供了 spring-boot-dependencies 这个依赖(等价于 parent) 去管理依赖项,要实现一样的效果,你只须要用 scope=import, 像下面例子:apache
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring.boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
完整pom.xml,或者查看githubspring-cloud-testmaven
#project(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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.ifengkou</groupId> <artifactId>spring-cloud-test</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>spring-boot-test</module> <module>spring-cloud-config</module> </modules> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <spring.boot.version>1.5.2.RELEASE</spring.boot.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring.boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project> # module 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"> <parent> <artifactId>spring-cloud-test</artifactId> <groupId>cn.ifengkou</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>spring-boot-test</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>