1. 程式人生 > >微服務框架(十)Maven Archetype製作Dubbo專案原型

微服務框架(十)Maven Archetype製作Dubbo專案原型

  此係列文章將會描述Java框架Spring Boot、服務治理框架Dubbo、應用容器引擎Docker,及使用Spring Boot整合Dubbo、Mybatis等開源框架,其中穿插著Spring Boot中日誌切面等技術的實現,然後通過gitlab-CI以持續整合為Docker映象。
  本文為Maven Archetype的製作及使用,使用archetype外掛製作Dubbo專案原型

本系列文章中所使用的框架版本為Spring Boot 2.0.3-RELEASE,Spring 5.0.7-RELEASE,Dubbo 2.6.2。

Maven Archetype

原型(Archetypes)打包在JAR中,它們包含描述原型內容的原型元資料,以及構成原型專案的一組敏捷開發模板。

Archetypes are packaged up in a JAR and they consist of the archetype metadata which describes the contents of archetype, and a set of Velocity templates which make up the prototype project.

原型專案的開發分為以下步驟:

  1. 配置Maven archetype外掛
  2. 配置原型描述檔案
  3. 配置相關原型中的檔案
  4. 構建至本地或推送至私有倉庫

POM配置

配置Maven archetype外掛

<pluginManagement>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-archetype-plugin</artifactId>
            <version>3.0.0</version>
        </plugin>
        <plugin>
<groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> <configuration> <encoding>UTF-8</encoding> <includeEmptyDirs>true</includeEmptyDirs> </configuration> </plugin> </plugins> </pluginManagement>

若需推送到私有倉庫,還需配置對應的私有倉庫部署配置

    <properties>
        <nexus.url>ip:port</nexus.url>
    </properties>


    <distributionManagement>
        <repository>
            <id>maven-releases</id>
            <name>Nexus Release Repository</name>
            <url>http://${nexus.url}/repository/maven-releases/</url>
        </repository>
        <snapshotRepository>
            <id>maven-snapshots</id>
            <name>Nexus Snapshot Repository</name>
            <url>http://${nexus.url}/repository/maven-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>

archetype-descriptor

原型描述檔案需命名為archetype-metadata.xml,必須設在src/main/resources/META-INF/maven/.

<?xml version="1.0" encoding="UTF-8"?>

<archetype-descriptor name="dubbo-common-archetype">
    <fileSets>
        <fileSet filtered="true" encoding="UTF-8">
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.**</include>
            </includes>
        </fileSet>
        <fileSet filtered="true" encoding="UTF-8">
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.xml</include>
                <include>**/**</include>
            </includes>
        </fileSet>
        <fileSet filtered="true" encoding="UTF-8">
            <directory>src/test/java</directory>
            <includes>
                <include>**/*.**</include>
            </includes>
        </fileSet>
        <fileSet filtered="true" encoding="UTF-8">
            <directory></directory>
            <includes>
                <include>.**</include>
            </includes>
        </fileSet>
    </fileSets>
</archetype-descriptor>

原型專案路徑結構:

__packageInPathFormat__會自動適配生成Maven專案時設定的path

archetype
|-- pom.xml
`-- src
    `-- main
        `-- resources
            |-- META-INF
            |   `-- maven
            |       `--archetype.xml
            `-- archetype-resources
                |-- pom.xml
                `-- src
                    |-- main
                    |   |-- java
                    |   |   `-- __packageInPathFormat__
                    |   |       `-- handler
                    |   |       `-- model
                    |   |       `-- provider
                    |   |       `-- resources
                    |   |       `-- starter
                    |   |       `-- utils
                    |   |-- resources
                    |       `-- config
                    |       `-- mapper
                    |       `-- docker
                    |       `-- application.properties
                    |       `-- log4j2.xml
                    `-- test
                        `-- java

在Java檔案頭中配置以下引數,則會在生成Maven專案時自動適配相應的包路徑

#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.handler;

專案結構如下圖

這裡寫圖片描述

本地使用

本地使用archetype只需install後使用相應archetype

mvn install
mvn archetype:generate                                  \
  -DarchetypeGroupId=<archetype-groupId>                \
  -DarchetypeArtifactId=<archetype-artifactId>          \
  -DarchetypeVersion=<archetype-version>                \
  -DgroupId=<my.groupid>                                \
  -DartifactId=<my-artifactId>

在eclipse或idea中則需在install後重新整理目錄update-local-catalog

mvn rchetype:update-local-catalog

私有倉庫使用

推送至私有倉庫後,只需在eclipse或idea中建立Maven專案,新增對應archetype即可

這裡寫圖片描述

archetype命令

幫助命令

mvn archetype:help

爬取倉庫以建立目錄

mvn archetype:crawl

建立archetype

mvn archetype:create-from-project

根據archetype建立工程

mvn archetype:generate

根據當前的archetype工程建立jar

mvn archetype:jar

更新本地的maven目錄

mvn archetype:update-local-catalog

參考資料: