spring cloud 多模块打包部署解决坑
前置条件
笔者使用Intellij IDEA进行Spring Cloud项目创建和部署
Intellij IDEA 版本 :IntelliJ IDEA 2019.1.3 (Ultimate Edition)
Spring Cloud 版本:Spring Cloud Hoxton.SR1,该版本基于 Spring Boot 2.2.2.Release
maven 版本:3.6.3,其中setting.xml文件为原生文件
主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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.albedo</groupId> <artifactId>sbe1</artifactId> <version>1.0-SNAPSHOT</version> <name>sbe1</name> <description>Demo project for Spring Boot</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>Utf-8</project.reporting.outputEncoding> <java.version>11</java.version> <spring-cloud.version>Hoxton.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependecies</artifactId> <version>${spring-cloud.version}</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>
第一个坑,项目可以通过run进行启动,但是通过 mvn clean命令,报如下错误
[INFO] Scanning for projects... [ERROR] [ERROR] Some problems were encountered while processing the POMs: [ERROR] Non-resolvable import POM: Failure to find org.springframework.cloud:spring-cloud-dependecies:pom:Hoxton.SR1 in https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced @ line 44, column 25 @ [ERROR] The build could not read 1 project -> [Help 1] [ERROR] [ERROR] The project com.albedo:sbe1:1.0-SNAPSHOT (/Users/wangzhangxiong/workspace/jproject/sbe1/pom.xml) has 1 error [ERROR] Non-resolvable import POM: Failure to find org.springframework.cloud:spring-cloud-dependecies:pom:Hoxton.SR1 in https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced @ line 44, column 25 -> [Help 2] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectBuildingException [ERROR] [Help 2] http://cwiki.apache.org/confluence/display/MAVEN/UnresolvableModelException
该错表面是报https://repo.maven.apache.org/maven2无法加载 org.springframework.cloud:spring-cloud-dependecies:pom:Hoxton.SR1
到本地,笔者通过多种思路,其中包括,更改maven 库镜像为阿里云镜像,添加spring-milestones repository到主pom等方法,也试过降低spring cloud版本,都无法达到有效解决,最后解决办法为将
<!--将该部分的type和scope删除-->
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependecies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
改为
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependecies</artifactId> <version>${spring-cloud.version}</version> </dependency> </dependencies> </dependencyManagement>
此问题可解。
此时如果使用mvn clean或者mvn install命令,则会报错
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.2.2.RELEASE:repackage (repackage) on project spring-cloud-eureka-cluster: Execution repackage of goal org.springframework.boot:spring-boot-maven-plugin:2.2.2.RELEASE:repackage failed: Unable to find main class -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException
这是plugin插件的问题,此时删除spring-boot-maven-plugin插件引入即可
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin>
多module一起打包问题
将主pom文件新增
<modules> <module>eureka-server</module> </modules>
同时新增配置
<packaging>pom</packaging>
最后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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.albedo</groupId> <artifactId>spring-cloud-eureka-cluster</artifactId> <version>1.0.0-SNAPSHOT</version> <name>spring-cloud-eureka-cluster</name> <packaging>pom</packaging> <description>Demo project for Spring Boot</description> <modules> <module>eureka-server</module> </modules> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>Utf-8</project.reporting.outputEncoding> <java.version>8</java.version> <spring-cloud.version>Hoxton.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependecies</artifactId> <version>${spring-cloud.version}</version> </dependency> </dependencies> </dependencyManagement> </project>