手把手教你寫Maven的archetype項(xiàng)目腳手架
一、背景
maven是一個(gè)很好的代碼構(gòu)建工具,采用“約定優(yōu)先于配置”的原則進(jìn)行項(xiàng)目管理,相信很多的java開發(fā)者應(yīng)該都了解maven并可能在工作當(dāng)中都是通過maven來管理項(xiàng)目的,在創(chuàng)建的項(xiàng)目的時(shí)候,我們往往會(huì)使用maven內(nèi)置的項(xiàng)目骨架也就是archetype來快速生成項(xiàng)目結(jié)構(gòu)。但是在一個(gè)團(tuán)隊(duì)做開發(fā)的過程中,可能僅僅依靠maven預(yù)先提供的archetyp可能是不夠的,團(tuán)隊(duì)之間協(xié)作有自己的定義方式,每個(gè)人的結(jié)構(gòu)定義風(fēng)格也不盡相同,在這樣的背景下我們有必要去定義一個(gè)統(tǒng)一的代碼骨架供團(tuán)隊(duì)使用,這樣做的好處是當(dāng)團(tuán)隊(duì)需要開始一個(gè)新項(xiàng)目的時(shí)候,可以利用自定義的maven骨架一鍵生成項(xiàng)目。
archetype是在maven-archetype-plugin插件執(zhí)行g(shù)enerate目標(biāo)的時(shí)候進(jìn)行配置的,我們經(jīng)常使用到maven的內(nèi)嵌的骨架包括:maven-archetype-webapp、maven-archetype-quickstart。前者用來快速搭建一個(gè)web工程項(xiàng)目,后者用來快速搭建一個(gè)普通的java工程項(xiàng)目。
二、手寫普通單模塊項(xiàng)目的archetype
單模塊項(xiàng)目的archetype腳手架項(xiàng)目的結(jié)構(gòu)
上圖中的各個(gè)文件詳解:
- 根目錄beast-archetype下的pom.xml和一般的maven項(xiàng)目一樣主要定義archetype項(xiàng)目的坐標(biāo)等信息。
- 所有的項(xiàng)目骨架內(nèi)容都集中在src/main/resources/archetype-resources文件夾下。
- archetype-resources中的pom.xml定義了待生成項(xiàng)目的pom文件的內(nèi)容,/src/main/java、/src/test/java中分別定義了待生成項(xiàng)目中相應(yīng)目錄下的內(nèi)容
- /src/main/resources/META-INF/maven/archetype-metadata.xml中定義相關(guān)的元數(shù)據(jù)描述(其中該文件的位置固定為resources/META-INF/maven文件夾下,且名稱固定為archetype-metadata.xml)。
1.beast-archetype/pom.xml內(nèi)容如下
<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.thebeastshop</groupId> <artifactId>beast-archetype</artifactId> <version>1.1</version> <packaging>jar</packaging> <name>beast-archetype</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>beast-archetype</finalName> </build> </project>
2.src/main/resources/archetype-resources/pom.xml內(nèi)容如下:
<?xml version="1.0"?> <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>${groupId}</groupId> <artifactId>${artifactId}</artifactId> <version>${version}</version> <name>${artifactId}</name> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-deploy-plugin</artifactId> <configuration> <skip>true</skip> </configuration> </plugin> </plugins> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-archetype-plugin</artifactId> <version>2.2</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </pluginManagement> </build> </project>
其中:上面${}標(biāo)識的變量都是通過maven中的命令行傳進(jìn)來的,如:mvn archetype:generate -DgroupId=com.thebeastshop
3.src/main/resources/META-INF/maven/archetype-metadata.xml內(nèi)容如下:
<?xml version="1.0" encoding="UTF-8"?> <archetype-descriptor name="beast-archetype" xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0 http://maven.apache.org/xsd/archetype-descriptor-1.0.0.xsd"> <requiredProperties> <requiredProperty key="package-name" /> </requiredProperties> <fileSets> <fileSet filtered="true" packaged="true" encoding="UTF-8"> <directory>src/main/java</directory> <includes> <include>**/*.java</include> </includes> </fileSet> <fileSet filtered="true" encoding="UTF-8"> <directory>src/main/resources</directory> <includes> <include>**/*.*</include> </includes> </fileSet> <fileSet filtered="true" packaged="true" encoding="UTF-8"> <directory>src/test/java</directory> <includes> <include>**/*.java</include> </includes> </fileSet> <fileSet encoding="UTF-8"> <directory>src/test/resources</directory> <includes> <include>**/*.*</include> </includes> </fileSet> </fileSets> </archetype-descriptor>
說明:
- packaged="true"標(biāo)識src/main/resources/archetype-resources/src/main/java中對應(yīng)的內(nèi)容是否要放入到package中,比如package為com.thebeastshop,那么如果該屬性為true,則對應(yīng)的java文件會(huì)放到com/thebeastshop文件夾下,也就是包路徑下。
- filtered="true"標(biāo)識下面提到的${}是否要進(jìn)行替換
4.src/main/resources/archetype-resources/src/main/java/Demo.java內(nèi)容如下:
package ${package}; public class Demo{ public static void main( String[] args ) { System.out.println( "Hello My Archetype!" ); } }
5.這樣我們就手寫好了一個(gè)自定義的maven的archetype骨架項(xiàng)目,我們只需要通過mvn clean install 命令把該jar包安裝到本地倉庫,然后通過本地倉庫中的該jar包來生成一個(gè)項(xiàng)目看看效果,使用如下命令:
mvn archetype:generate -DgroupId=comthebeastshop -DartifactId=beast-test -Dpackage="com.thebeastshop.test" -DarchetypeGroupId=com.thebeastshop -DarchetypeArtifactId=beast-archetype -DarchetypeVersion=1.1 -X -DarchetypeCatalog=local
三、手寫maven多module的項(xiàng)目骨架archetype
1.多模塊骨架項(xiàng)目的項(xiàng)目結(jié)構(gòu)
這跟單模塊項(xiàng)目區(qū)別不大,但是有幾個(gè)概念需要說明:
- “__rootArtifactId__”占位符會(huì)被parent項(xiàng)目的artifactId替換
- ${rootArtifactId}也會(huì)被parent項(xiàng)目的artifactId替換
- src/main/resources/archetype-resources里必須要有一個(gè)頂級pom文件(如果是單工程就是工程pom文件),同時(shí)子文件夾代表了模塊定義
2.模板工程定義描述文件
META-INF/maven/archetype-metadata.xml
<?xml version="1.0" encoding="UTF-8"?> <archetype-descriptor name="beast-archetype" xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0 http://maven.apache.org/xsd/archetype-descriptor-1.0.0.xsd"> <requiredProperties> <requiredProperty key="groupId"> <defaultValue>com.thebeastshop</defaultValue> </requiredProperty> <requiredProperty key="artifactId"> <defaultValue>test</defaultValue> </requiredProperty> <requiredProperty key="package"> <defaultValue>com.thebeastshop.test</defaultValue> </requiredProperty> </requiredProperties> <modules> <module id="${rootArtifactId}-api" name="${rootArtifactId}-api" dir="__rootArtifactId__-api"> <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="${rootArtifactId}-core" name="${rootArtifactId}-core" dir="__rootArtifactId__-core"> <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 filtered="true" 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="${rootArtifactId}-dao" name="${rootArtifactId}-dao" dir="__rootArtifactId__-dao"> <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 filtered="true" encoding="UTF-8"> <directory>src/main/resources</directory> <includes> <include>**/*.*</include> <include>mapper</include> </includes> </fileSet> <fileSet encoding="UTF-8"> <directory>src/test/resources</directory> <includes> <include>**/*.*</include> </includes> </fileSet> </fileSets> </module> <module id="${rootArtifactId}-main" name="${rootArtifactId}-main" dir="__rootArtifactId__-main"> <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 filtered="true" 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> <fileSet encoding="UTF-8"> <directory>src/main/assembly</directory> <includes> <include>**/*.*</include> </includes> </fileSet> <fileSet encoding="UTF-8"> <directory>src/main/bin</directory> <includes> <include>**/*.*</include> </includes> </fileSet> </fileSets> </module> <module id="${rootArtifactId}-mybatisGen" name="${rootArtifactId}-mybatisGen" dir="__rootArtifactId__-mybatisGen"> <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> </modules> </archetype-descriptor>
屬性變量定義
<requiredProperties> <requiredProperty key="groupId"> <defaultValue>com.thebeastshop</defaultValue> </requiredProperty> <requiredProperty key="artifactId"> <defaultValue>test</defaultValue> </requiredProperty> <requiredProperty key="package"> <defaultValue>com.thebeastshop.test</defaultValue> </requiredProperty> </requiredProperties>
這些屬性可以在資源元文件里的任意一個(gè)文件里通過${var}來引用,所以的元文件最終都可以選擇通過velocity引擎來執(zhí)行替換后生成。
默認(rèn)的屬性有:groupId,artifactId,packeage,version等
項(xiàng)目子模塊定義
<modules> <module id="${rootArtifactId}-api" name="${rootArtifactId}-api" dir="__rootArtifactId__-api"> ... </module> <module id="${rootArtifactId}-core" name="${rootArtifactId}-core" dir="__rootArtifactId__-core"> ... </module> <module id="${rootArtifactId}-dao" name="${rootArtifactId}-dao" dir="__rootArtifactId__-dao"> ... </module> <module id="${rootArtifactId}-main" name="${rootArtifactId}-main" dir="__rootArtifactId__-main"> ... </module> <module id="${rootArtifactId}-mybatisGen" name="${rootArtifactId}-mybatisGen" dir="__rootArtifactId__-mybatisGen"> ... </module> </modules>
module有三個(gè)屬性,解釋如下:
- id :定義子模塊工程的artifactId.
- dir :子模塊工程源文件在archetype-resources里對應(yīng)的directory.
- name :子模塊的名字.
3.子模塊pom.xml定義如下(以core模塊為例):
<?xml version="1.0"?> <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> <parent> <groupId>com.thebeastshop</groupId> <artifactId>${rootArtifactId}</artifactId> <version>${version}</version> </parent> <artifactId>${artifactId}</artifactId> <name>${artifactId}</name> <dependencies> <dependency> <groupId>com.thebeastshop</groupId> <artifactId>${rootArtifactId}-api</artifactId> <version>${api.version}</version> </dependency> <dependency> <groupId>com.thebeastshop</groupId> <artifactId>${rootArtifactId}-dao</artifactId> <version>${project.parent.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-deploy-plugin</artifactId> <configuration> <skip>true</skip> </configuration> </plugin> </plugins> </build> </project>
其中${rootArtifactId}就代表父項(xiàng)目的artifactId.
4.我們和單模塊腳手架工程一樣,通過mvn clean install命令把該腳手架項(xiàng)目安裝到本地maven倉庫,然后就可以使用該項(xiàng)目來快速生成新項(xiàng)目結(jié)構(gòu)了,生成命令如下:
mvn archetype:generate -DgroupId=com.thebeastshop -DartifactId=ddd -Dversion=1.0.0-SNAPSHOT -DarchetypeGroupId=com.thebeastshop -DarchetypeArtifactId=beast-archetype -DarchetypeVersion=1.3-SNAPSHOT -X -DarchetypeCatalog=local
我們就會(huì)看到生成好的項(xiàng)目結(jié)構(gòu)如下:
多模塊項(xiàng)目腳手架源碼:https://github.com/hafizzhang/beast-archetype
四、總結(jié)
到此這篇關(guān)于手把手教你寫Maven的archetype項(xiàng)目腳手架的文章就介紹到這了,更多相關(guān)Maven archetype項(xiàng)目腳手架內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- eclipse maven maven-archetype-webapp 創(chuàng)建失敗問題解決
- 使用Maven Archetype插件構(gòu)建Maven工程原型模板的實(shí)例
- 使用maven-archetype-plugin現(xiàn)有項(xiàng)目生成腳手架的方法
- IDEA自定義Maven archetype的方法步驟
- 教你如何在IDEA?中添加?Maven?項(xiàng)目的?Archetype(解決添加不起作用的問題)
- idea中使用maven?archetype新建項(xiàng)目時(shí)卡住問題解決方案
- maven中自定義MavenArchetype的實(shí)現(xiàn)
相關(guān)文章
MyBatis SELECT基本查詢實(shí)現(xiàn)方法詳解
這篇文章主要介紹了MyBatis SELECT基本查詢實(shí)現(xiàn)方法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08詳解Spring Cloud微服務(wù)架構(gòu)下的WebSocket解決方案
這篇文章主要介紹了詳解Spring Cloud微服務(wù)架構(gòu)下的WebSocket解決方案,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12基于SpringBoot2.0默認(rèn)使用Redis連接池的配置操作
這篇文章主要介紹了基于SpringBoot2.0默認(rèn)使用Redis連接池的配置操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12Spring Batch輕量級批處理框架實(shí)戰(zhàn)
本文主要介紹了Spring Batch輕量級批處理框架實(shí)戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10SpringCloud Gateway動(dòng)態(tài)路由配置詳解
這篇文章主要為大家介紹了SpringCloud Gateway動(dòng)態(tài)路由配置詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03java int轉(zhuǎn)byte和long轉(zhuǎn)byte的方法
下面小編就為大家?guī)硪黄猨ava int轉(zhuǎn)byte和long轉(zhuǎn)byte的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10java中JsonObject與JsonArray轉(zhuǎn)換方法實(shí)例
在項(xiàng)目日常開發(fā)中常常會(huì)遇到JSONArray和JSONObject的轉(zhuǎn)換,很多公司剛?cè)肼毜男∶刃聲?huì)卡在這里,下面這篇文章主要給大家介紹了關(guān)于java中JsonObject與JsonArray轉(zhuǎn)換方法的相關(guān)資料,需要的朋友可以參考下2023-04-04