Maven多模块项目构建及模块打包

上一篇SpringCloud之天气预报微服务学习案例中,使用idea用maven进行项目管理,多模块构建公共api模块、微服务模块,开发过程中多有采坑,特别是微服务模块进行单独打jar包的时候开始始终没有成功,这里以天气预报微服务案例进行项目搭建,方便大家参考。

项目环境

  • IDEA IntelliJ IDEA 2017.1.3
  • Maven-3.5

构建步骤

  1. 创建父工程,步骤如下图顺序
    Maven多模块项目构建及模块打包_第1张图片
    Maven多模块项目构建及模块打包_第2张图片
    Maven多模块项目构建及模块打包_第3张图片
    Maven多模块项目构建及模块打包_第4张图片
父工程创建完成,进行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.0modelVersion>

    <groupId>com.weather.springcloudgroupId>
    <artifactId>springcloud_weatherartifactId>
    <packaging>pompackaging>
    <version>1.0-SNAPSHOTversion>

    <properties>
        
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        
        <maven.compiler.encoding>UTF-8maven.compiler.encoding>
        <java.version>1.8java.version>
        <maven.compiler.source>1.8maven.compiler.source>
        <maven.compiler.target>1.8maven.compiler.target>
        <junit.version>4.12junit.version>
    properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>Greenwich.SR2version>
                <type>pomtype>
                <scope>importscope>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-parentartifactId>
                <version>2.1.7.RELEASEversion>
                <type>pomtype>
                <scope>importscope>
            dependency>

            
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>springloadedartifactId>
                <version>1.2.8.RELEASEversion>
                
                <scope>providedscope>
            dependency>
            
        dependencies>
    dependencyManagement>

project>
  1. 创建Module
    Maven多模块项目构建及模块打包_第5张图片
    Maven多模块项目构建及模块打包_第6张图片
    Maven多模块项目构建及模块打包_第7张图片

Maven多模块项目构建及模块打包_第8张图片

按照上面步骤,依次创建所需模块

Maven多模块项目构建及模块打包_第9张图片

创建完成后,父工程pom.xml文件中会多出如下代码
   <modules>
        <module>weather_apimodule>
        <module>eureka_server_7201module>
        <module>weather_city_server_8001module>
        <module>weather_collection_server_8201module>
        <module>weather_data_server_8401module>
        <module>weather_report_server_8601module>
    modules>
打开Maven Projects效果

Maven多模块项目构建及模块打包_第10张图片

  1. weather_api为公共模块api,项目中引用公共模块
父工程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.0modelVersion>

    <groupId>com.weather.springcloudgroupId>
    <artifactId>springcloud_weatherartifactId>
    <packaging>pompackaging>
    <version>1.0-SNAPSHOTversion>

    <modules>
        <module>weather_apimodule>
        <module>eureka_server_7201module>
        <module>weather_city_server_8001module>
        <module>weather_collection_server_8201module>
        <module>weather_data_server_8401module>
        <module>weather_report_server_8601module>
    modules>

    <properties>
        
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        
        <maven.compiler.encoding>UTF-8maven.compiler.encoding>
        <java.version>1.8java.version>
        <maven.compiler.source>1.8maven.compiler.source>
        <maven.compiler.target>1.8maven.compiler.target>
        <junit.version>4.12junit.version>
    properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>Greenwich.SR2version>
                <type>pomtype>
                <scope>importscope>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-parentartifactId>
                <version>2.1.7.RELEASEversion>
                <type>pomtype>
                <scope>importscope>
            dependency>

            
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>springloadedartifactId>
                <version>1.2.8.RELEASEversion>
                
                <scope>providedscope>
            dependency>

            
            <dependency>
                <groupId>com.weather.springcloudgroupId>
                <artifactId>weather_apiartifactId>
                <version>${project.version}version>
            dependency>

        dependencies>
    dependencyManagement>


project>
其它模块pom文件引入,以weather_city_server_8001为例,其它都一样

<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_weatherartifactId>
        <groupId>com.weather.springcloudgroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>weather_city_server_8001artifactId>


    <dependencies>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
        dependency>


        
        <dependency>
            <groupId>com.weather.springcloudgroupId>
            <artifactId>weather_apiartifactId>
        dependency>


        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>springloadedartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
        dependency>
    dependencies>

project>
  1. 为需要打可执行jar包的模块pom文件增加build配置和插件
模块pom文件增加build配置,以weather_city_server_8001为例,其它都一样

<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_weatherartifactId>
        <groupId>com.weather.springcloudgroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>weather_city_server_8001artifactId>


    <dependencies>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
        dependency>


        
        <dependency>
            <groupId>com.weather.springcloudgroupId>
            <artifactId>weather_apiartifactId>
        dependency>


        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>springloadedartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
        dependency>
    dependencies>

    
    <build>
        
        <finalName>city_server_8001finalName>
        <resources>
            <resource>
                <directory>src/main/javadirectory>
                <includes>
                    <include>**/*.xmlinclude>
                includes>
            resource>
            <resource>
                <directory>src/main/resourcesdirectory>
                <includes>
                    <include>**.*include>
                    
                    <include>**/*.*include>
                    <include>**/*/*.*include>
                includes>
            resource>
            
            <resource>
                <directory>src/main/resourcesdirectory>
                <filtering>truefiltering>
            resource>
        resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <configuration>
                    
                    <fork>truefork>
                    
                    <mainClass>com.weather.springcloud.city.WeatherCityServer8001_AppmainClass>
                    <layout>ZIPlayout>
                configuration>
                <executions>
                    <execution>
                        <goals>
                            
                            <goal>repackagegoal>
                        goals>
                    execution>
                executions>
            plugin>
        plugins>
    build>

project>
  1. 编译工程,安装所有模块jar包至maven仓库
    Maven多模块项目构建及模块打包_第11张图片
    Maven多模块项目构建及模块打包_第12张图片
    Maven多模块项目构建及模块打包_第13张图片
    Maven多模块项目构建及模块打包_第14张图片
到此,所有工作步骤完成,即可运行所生成的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.

demo源码下载

你可能感兴趣的:(SpringCloud)