上一篇SpringCloud之天气预报微服务学习案例中,使用idea用maven进行项目管理,多模块构建公共api模块、微服务模块,开发过程中多有采坑,特别是微服务模块进行单独打jar包的时候开始始终没有成功,这里以天气预报微服务案例进行项目搭建,方便大家参考。
项目环境
- IDEA IntelliJ IDEA 2017.1.3
- Maven-3.5
构建步骤
- 创建父工程,步骤如下图顺序
父工程创建完成,进行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>com.weather.springcloud</groupId>
<artifactId>springcloud_weather</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<properties>
<!-- 文件拷贝时的编码 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- 编译时的编码 -->
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>4.12</junit.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- 修改后立即生效,热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.8.RELEASE</version>
<!-- 开发环境下使用,部署到服务器时候就被踢出 -->
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
- 创建Module
按照上面步骤,依次创建所需模块
创建完成后,父工程pom.xml文件中会多出如下代码
<modules>
<module>weather_api</module>
<module>eureka_server_7201</module>
<module>weather_city_server_8001</module>
<module>weather_collection_server_8201</module>
<module>weather_data_server_8401</module>
<module>weather_report_server_8601</module>
</modules>
打开Maven Projects效果
- weather_api为公共模块api,项目中引用公共模块
父工程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>com.weather.springcloud</groupId>
<artifactId>springcloud_weather</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>weather_api</module>
<module>eureka_server_7201</module>
<module>weather_city_server_8001</module>
<module>weather_collection_server_8201</module>
<module>weather_data_server_8401</module>
<module>weather_report_server_8601</module>
</modules>
<properties>
<!-- 文件拷贝时的编码 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- 编译时的编码 -->
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>4.12</junit.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- 修改后立即生效,热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.8.RELEASE</version>
<!-- 开发环境下使用,部署到服务器时候就被踢出 -->
<scope>provided</scope>
</dependency>
<!-- 引入自己定义的api通用包 -->
<dependency>
<groupId>com.weather.springcloud</groupId>
<artifactId>weather_api</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
其它模块pom文件引入,以weather_city_server_8001为例,其它都一样
<?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>springcloud_weather</artifactId>
<groupId>com.weather.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>weather_city_server_8001</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- 引入自己定义的api通用包 -->
<dependency>
<groupId>com.weather.springcloud</groupId>
<artifactId>weather_api</artifactId>
</dependency>
<!-- 修改后立即生效,热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>
- 为需要打可执行jar包的模块pom文件增加build配置和插件
模块pom文件增加build配置,以weather_city_server_8001为例,其它都一样
<?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>springcloud_weather</artifactId>
<groupId>com.weather.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>weather_city_server_8001</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- 引入自己定义的api通用包 -->
<dependency>
<groupId>com.weather.springcloud</groupId>
<artifactId>weather_api</artifactId>
</dependency>
<!-- 修改后立即生效,热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
<!-- 打成可执行jar包 -->
<build>
<!-- 包名 -->
<finalName>city_server_8001</finalName>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**.*</include>
<!-- i18n能读取到 -->
<include>**/*.*</include>
<include>**/*/*.*</include>
</includes>
</resource>
<!-- 允许访问工程resources下资源内容 -->
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 如果没有该配置,devtools不会生效 -->
<fork>true</fork>
<!-- 指定该Main Class为全局的唯一入口 -->
<mainClass>com.weather.springcloud.city.WeatherCityServer8001_App</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<!-- 可以把依赖的包都打包到生成的Jar包中 -->
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
- 编译工程,安装所有模块jar包至maven仓库
到此,所有工作步骤完成,即可运行所生成的jar包,然后单一模块想打 jar 包都不会有问题,如果不进行最后 5 这个流程,在打单一 jar 时候很可能会报如下异常,我之前做项目就是卡在此处,只能在idea进行运行,无法把模块进行打jar包出来进行运行。异常信息如下:
[ERROR] Failed to execute goal on project weather_collection_server_8001: Could not resolve dependencies for project com.weather.springcloud:weather_collection_server_8001:jar:1.0-SNAPSHOT: Failed to collect dependencies at com.weather.springcloud:weather_api:jar:1.0-SNAPSHOT: Failed to read artifact descriptor for com.weather.springcloud:weather_api:jar:1.0-SNAPSHOT: Could not find artifact com.weather.springcloud:springcloud_weather:pom:1.0-SNAPSHOT -> [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.