使用idea新建的spring boot2.X工程使用命令行无法编译通过,查找网上经验为spring boot 2.X所需 jar包较新,可能有些maven仓库拉不到新jar包。在此特记录一下spring boot 2.X下的maven配置与pom配置。
maven 镜像地址配置
<mirrors>
<mirror>
<id>repo2</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://repo2.maven.org/maven2/</url>
</mirror>
</mirrors>
在需要依赖spring-cloud系列jar包的情况下,可以通过dependencyManagement节点指定spring cloud系列jar包的版本号:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
在命令行编译过程中,可能出现spring系列的jar包或者pom文件无法获取的情况,综合网上意见以及个人实践,spring boot 2.X项目在这种情况下需要在pom文件中添加以下节点:
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>repository.springframework.maven.release</id>
<name>Spring Framework Maven Release Repository</name>
<url>http://maven.springframework.org/milestone/</url>
</repository>
<repository>
<id>org.springframework</id>
<url>http://maven.springframework.org/snapshot</url>
</repository>
<repository>
<id>spring-milestone</id>
<name>Spring Maven MILESTONE Repository</name>
<url>http://repo.spring.io/libs-milestone</url>
</repository>
<repository>
<id>spring-release</id>
<name>Spring Maven RELEASE Repository</name>
<url>http://repo.spring.io/libs-release</url>
</repository>
</repositories>
如果觉得每个spring 2.X项目都得添加repositories比较麻烦,那么可以在所有项目的上层添加一个父pom来管理这些项目。其中父pom.xml 的packaging 必须为 pom 类型,此时可以把需管理的项目的共用的dependencies ,plugin 移动到这个pom.xml ,然后使用modules 添加到父pom.xml 中。
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>spring-cloud</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<name>spring boot root pom</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/>
</parent>
<modules>
<module>···</module>
<module>···</module>
</modules>
<repositories>
···
</repositories>
<dependencies>
···
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
修改子项目的pom.xml ,添加一个parent 元素。如下所示:
<parent>
<groupId>com.example</groupId>
<artifactId>spring-cloud</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath> <!--指定父pom路径-->
</parent>
网友评论