天天看點

Maven多子產品項目建構及子產品打包

上一篇SpringCloud之天氣預報微服務學習案例中,使用idea用maven進行項目管理,多子產品建構公共api子產品、微服務子產品,開發過程中多有采坑,特别是微服務子產品進行單獨打jar包的時候開始始終沒有成功,這裡以天氣預報微服務案例進行項目搭建,友善大家參考。

項目環境

  • IDEA IntelliJ IDEA 2017.1.3
  • Maven-3.5

建構步驟

  1. 建立父工程,步驟如下圖順序
    Maven多子產品項目建構及子產品打包
    Maven多子產品項目建構及子產品打包
    Maven多子產品項目建構及子產品打包
    Maven多子產品項目建構及子產品打包

父工程建立完成,進行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>
           
  1. 建立Module
    Maven多子產品項目建構及子產品打包
    Maven多子產品項目建構及子產品打包
    Maven多子產品項目建構及子產品打包
Maven多子產品項目建構及子產品打包

按照上面步驟,依次建立所需子產品

Maven多子產品項目建構及子產品打包

建立完成後,父工程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效果

Maven多子產品項目建構及子產品打包
  1. 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>
           
  1. 為需要打可執行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>
           
  1. 編譯工程,安裝所有子產品jar包至maven倉庫
    Maven多子產品項目建構及子產品打包
    Maven多子產品項目建構及子產品打包
    Maven多子產品項目建構及子產品打包
    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.
           

demo源碼下載下傳

繼續閱讀