002-maven開發Java腳手架archrtype【如無定製開發,請直接看3.3使用】

1、概述

  項目基礎構建須要:項目結構,spring框架,orm,鏈接池,數據庫,單元測試等等。html

  上述即便複用:001-腳手架發展,基礎代碼結構+mybatis代碼生成,基礎代碼結構,也須要修改爲本身單位或者我的所屬包,以及鏈接池,版本控制等。同時各個項目的結構迥異,不利於團隊管理。java

  下述主要使用maven項目原型,搭建了一個團隊共享的腳手架。maven自定義archetype(腳手架)mysql

1.一、什麼是archetype

  模板工具類,經過archetype咱們能夠快速的生成項目的基本架構。好比咱們使用idea建立一個maven web項目時,經常會選擇maven-archetype-webapp模板來初始化項目,使用maven-archetype-webapp生成的項目中包括webapp目錄,裏面包含web的配置文件git

  

1.二、archetype由四部分組成

prototype files 原型文件github

  位於src/main/resources/archetype-resource目錄下。prototype files 原型文件能夠理解爲多模塊中的子模塊或是單模塊工程中的源文件[即src文件]。這些原型文件在使用對應archetype生成項目時被生成web

archetype-metadata.xmlspring

  位於src/main/resources/META-INF/maven/目錄下。該配置文件中主要列出了原型文件以及使用archetype生成模板工程須要的參數sql

prototype pom數據庫

  位於src/main/resources/archetype-resources目錄下。這個pom文件會出如今archetype建立的模板工程中,若是是單模塊工程,則是對整個項目的依賴管理;若是是多模塊工程,該pom是總pom文件,該文件中會定義項目的子模塊以及對子模塊的依賴進行管理等,子模塊pom定義在子模塊下,子模塊pom文件只管理子模塊的依賴。apache

archetype pom

  位於自定義archetype工程的根目錄下。這是archetype工程項目的pom文件,裏面通常沒什麼東西,不會出如今archetype建立的模板工程中

1.三、爲何要自定義archetype

maven也內置了不少的archetype供用戶選擇使用什麼樣的骨架去建立一個項目,好比:

  • maven-archetype-webapp

  • maven-archetype-quickstart

在建立一個maven項目的時候會列出不少archetype供選擇,maven默認的archetype是maven-archetype-webapp。

  團隊作開發的過程當中,僅僅依靠maven預先提供的archetyp多是不夠的,團隊之間協做有本身的定義方式,每一個人的結構定義風格也不盡相同,在這樣的背景下咱們有必要去定義一個統一的代碼骨架供團隊使用,這樣作的好處還有在新人加入團隊的初期可以快速的理解項目。

2、插件開發

參數說明、Archetype的一些built-in參數

Variable Meaning
__rootArtifactId__ 作文件夾名替換用,例如__rootArtifactId__-dal, 佔位符來動態獲取父工程的ArtifactId
${rootArtifactId}

it holds the value entered by the user as the project name (the value that maven ask as the artifactId: in the prompt when the user runs the archetype)

它保存用戶輸入的值做爲項目名(maven在用戶運行原型時在提示符中詢問爲artifactid:的值)

${artifactId}

If your project is composed by one module, this variable will have the same value as ${rootArtifactId}, but if the project contains several modules, this variable will be replaced by the module name inside every module folder, for example: given a module named portlet-domain inside a project namedportlet, all the files inside this module folder that are to be filtered will have the value of the variable ${artifactId} replaced by portlet-domainwhereas the ${rootArtifactId} variable will be replaced by portlet

若是您的項目由一個模塊組成,則此變量的值將與${rootartifactid}相同,但若是項目包含多個模塊,則此變量將由每一個模塊文件夾中的模塊名替換,例如:給定一個名爲Portlet domain的模塊位於一個名爲Portlet的項目中,此模塊文件夾中要篩選的全部文件的變量${artifactid}的值將替換爲portlet域,而${rootartifactid}的變量將替換爲portlet

${package}

The user provided package for the project, also prompted by maven when the user runs the archetype

用戶爲項目提供的包,也在用戶運行原型時由maven提示

${packageInPathFormat}

The same value as ${package} variable but replacing '.' with the character'/', e.g:, for the package com.foo.bar this variable is com/foo/bar

與${package}變量的值相同,但將「.」替換爲字符「/」,例如:,對於包com.foo.bar,此變量爲com/foo/bar

${groupId}

The user supplied groupId for the project, prompted by maven when the user runs the archetype

用戶爲項目提供的groupid,在用戶運行原型時由maven提示

${version} The user supplied version for the project, prompted by maven when the user runs the archetype

  其中有些變量是系統支持的,有些變量是自定義的,例如rootArtifactId、project.version等都是系統支持的,有些是自動以的,例如上面用到的package

  ${rootArtifactId}也會被parent項目的artifactId替換

2.一、[自定義archetype]結構說明

  

   包含了archetype的四個組成部分,兩個pom文件,一個archtype-metadata文件和五個原型文件[__rootArtifactId__-*],其中__rootArtifactId__在生成模板工程時會被傳入的值替代。

  代碼參看地址:https://github.com/bjlhx15/java_base_architecture.git

配置說明

2.1.一、archtype-metadata.xml配置文件

一、定義使用archetype生成模板工程須要傳入的參數

    <!--須要輸入的屬性-->
    <requiredProperties>
        <requiredProperty key="groupId">
            <!--默認的groupId-->
            <defaultValue>com.github.bjlhx15.test</defaultValue>
        </requiredProperty>
        <requiredProperty key="artifactId">
            <!--默認的artifactId-->
            <defaultValue>demo</defaultValue>
        </requiredProperty>
        <requiredProperty key="package">
            <!--默認的包名和groupId同樣-->
            <defaultValue>${groupId}</defaultValue>
        </requiredProperty>
    </requiredProperties>

  ${}標識的變量都是經過maven中的命令行傳進來的

二、 定義原型文件

    <!--子模塊-->
    <modules>
        <module id="${rootArtifactId}-web" name="${rootArtifactId}-web" dir="__rootArtifactId__-web">
            <fileSets>
                <fileSet filtered="true" encoding="UTF-8" packaged="true">
                    <directory>src/main/java</directory>
                    <includes>
                        <include>**/*.*</include>
                    </includes>
                </fileSet>
                <fileSet filtered="true" encoding="UTF-8" packaged="true">
                    <directory>src/test/java</directory>
                    <includes>
                        <include>**/*.*</include>
                    </includes>
                </fileSet>
                <fileSet encoding="UTF-8">
                    <directory>src/main/resources</directory>
                    <includes>
                        <include>**/*.*</include>
                    </includes>
                </fileSet>
                <fileSet encoding="UTF-8">
                    <directory>src/test/resources</directory>
                    <includes>
                        <include>**/*.*</include>
                    </includes>
                </fileSet>
            </fileSets>
        </module>

  module屬性介紹: id:子模塊工程的artifactId dir:子模塊工程源文件在archetype-resources裏對應的directory name :子模塊的名字.

  可定製化本身的服務模塊。

  注意   

    src/main/resources/archetype-resources裏必需要有一個頂級pom文件(若是是單工程就是工程pom文件),同時子文件夾表明了模塊定義

2.1.二、prototype pom文件

一、定義了基礎子模塊

    <!--項目子模塊-->
    <modules>
        <module>${rootArtifactId}-common</module>
        <module>${rootArtifactId}-dao</module>
        <module>${rootArtifactId}-service</module>
        <module>${rootArtifactId}-web</module>
        <module>${rootArtifactId}-model</module>
    </modules> 

二、子模塊依賴版本統一管理

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
                <version>2.0.4.RELEASE</version>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <version>2.0.4.RELEASE</version>
                <scope>test</scope>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <!-- 去掉springboot默認配置 -->
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-logging</artifactId>
                    </exclusion>
                </exclusions>
                <version>2.0.4.RELEASE</version>
            </dependency>

            <!--mysql-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>6.0.6</version>
            </dependency>

            <!--mybatisplus-->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>3.1.2</version>
            </dependency>

            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-generator</artifactId>
                <version>3.1.2</version>
            </dependency>

            <dependency>
                <groupId>org.apache.velocity</groupId>
                <artifactId>velocity-engine-core</artifactId>
                <version>2.0</version>
            </dependency>

            <dependency>
                <groupId>org.freemarker</groupId>
                <artifactId>freemarker</artifactId>
                <version>2.3.23</version>
            </dependency>

            <!-- log4j2 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-log4j2</artifactId>
                <version>2.1.1.RELEASE</version>
            </dependency>

            <dependency>
                <groupId>com.lmax</groupId>
                <artifactId>disruptor</artifactId>
                <version>3.4.2</version>
            </dependency>

            <!--aop-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-aop</artifactId>
                <version>2.0.5.RELEASE</version>
            </dependency>

            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.9.8</version>
            </dependency>

            <!--modules-->
            <dependency>
                <groupId>${groupId}</groupId>
                <artifactId>${rootArtifactId}-common</artifactId>
                <version>${version}</version>
            </dependency>

            <dependency>
                <groupId>${groupId}</groupId>
                <artifactId>${rootArtifactId}-dao</artifactId>
                <version>${version}</version>
            </dependency>

            <dependency>
                <groupId>${groupId}</groupId>
                <artifactId>${rootArtifactId}-service</artifactId>
                <version>${version}</version>
            </dependency>

            <dependency>
                <groupId>${groupId}</groupId>
                <artifactId>${rootArtifactId}-model</artifactId>
                <version>${version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
View Code

子模塊所需依賴都定義在該pom中,子模塊使用依賴時不須要<version>標籤,可是須要顯示引用

2.1.三、prototype files 原型文件

  以web模塊說明 就是一個簡單的maven工程,裏面寫了使用archetype生成模板項目的類

    

2.1.四、archetype pom文件

  主要打包用,遠端發佈,生成jar

3、主要技術框架以及應用

3.一、技術框架

  DB【mysql】、ORM【mybatisplus】、Framework【springboot】、

3.二、主要結合

  springboot與mybatisplus整合【代碼生成器、分頁、邏輯刪除】參看 代碼

  log4j2整合【定時刪除】參看 代碼

  統一響應【統一異常處理】

3.三、使用

3.3.一、下載maven pom

TODO:

3.3.二、使用自定義archetype初始化項目

mvn archetype:generate \
-DgroupId=com.aaa.test -DartifactId=test-demo -Dversion=1.0.0-SNAPSHOT \
-DarchetypeGroupId=com.github.bjlhx15 -DarchetypeArtifactId=maven-archetypes-webapp -DarchetypeVersion=0.0.1-SNAPSHOT \
-X -DarchetypeCatalog=local -DinteractiveMode=false

參數說明
-DgroupId:組ID,默認項目的包名的組ID相同,也能夠設置包名

-DartifactId:項目惟一標識符,即項目名稱,maven會根據這個名稱在當前目錄下新建一個名爲該名稱的目錄用於創建項目

-DarchetypeGroupId:腳手架maven-archetypes-webapp的組ID,值不須要進行修改,如 com.github.bjlhx15

-DarchetypeArtifactId:腳手架maven-archetypes-webapp的artifactId,值不須要進行改變,如 maven-archetypes-webapp

-DinteractiveMode=false  是否已交互模式進行,若是是false的話就會採用默認設置創建項目

3.3.三、使用生產的項目

一、修改參數配置

  配置resoueces:將XXXX-generator、以及XXXX-web等須要配置resources配置,步驟:resource文件件→右鍵→Make Directory as →Resources Root

    

  使用idea或其餘工具打開項目,修改配置文件:resource文件夾下的配置文件,該文件夾下有application.properties ,log4j2.component.properties,log4j2-spring.xml 等配置文件

  修改數據庫配置、mybatis包別名配置,日誌存儲配置等。

二、代碼生成

  運行 MybatisPlusGenerator 中main方法生產 各層代碼

三、啓動項目便可

  訪問地址:http://localhost:8081/test/hello

相關文章
相關標籤/搜索