自定义idea archetype
編寫 archetype 元數(shù)據(jù)
通過plugin從現(xiàn)有項(xiàng)目中導(dǎo)出
1、引入plugin
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-archetype-plugin</artifactId><version>3.2.0</version></plugin>2、通過maven命令生成archetype元數(shù)據(jù)
- 在cmd中執(zhí)行
- 在idea中執(zhí)行
生成的archetype元數(shù)據(jù)在 目錄:target\generated-sources\archetype 下。
├─src │ ├─main │ │ └─resources │ │ ├─archetype-resources │ │ │ ├─.idea │ │ │ │ ├─inspectionProfiles │ │ │ │ └─sonarlint │ │ │ │ └─issuestore │ │ │ ├─frame-api │ │ │ │ └─src │ │ │ │ └─main │ │ │ │ └─java │ │ │ │ └─cloud │ │ │ │ └─frame │ │ │ │ ├─dto │ │ │ │ ├─feign │ │ │ │ └─vo │ │ │ └─frame-biz │ │ │ └─src │ │ │ └─main │ │ │ ├─java │ │ │ │ ├─cloud │ │ │ │ │ └─frame │ │ │ │ │ ├─config │ │ │ │ │ ├─constant │ │ │ │ │ ├─controller │ │ │ │ │ ├─exception │ │ │ │ │ ├─mapper │ │ │ │ │ ├─model │ │ │ │ │ ├─service │ │ │ │ │ │ └─impl │ │ │ │ │ ├─util │ │ │ │ │ └─vo │ │ │ │ ├─mapper │ │ │ │ ├─model │ │ │ │ └─service │ │ │ │ └─impl │ │ │ └─resources │ │ └─META-INF │ │ └─maven │ └─test │ └─resources │ └─projects │ └─basic注意,通過現(xiàn)有項(xiàng)目生成會(huì)出現(xiàn)以下問題:
- 如果源代碼中僅有package,而沒有類文件,則此package對(duì)應(yīng)的目錄會(huì)丟失。
- 有些文件例如:.gitignore會(huì)丟失
- 生成的模塊名稱是固定的
因此要對(duì)現(xiàn)有archetype進(jìn)行調(diào)整
手工編寫
手工編寫也是生成上面的archetype,但是可以進(jìn)行調(diào)整,主要包括:
增加一些變量,更精確的進(jìn)行控制
增加一些文件
生成 archetype
在目錄target\generated-sources\archetype 下執(zhí)行 以下命令
#本地安裝 mvn install #遠(yuǎn)程發(fā)布 mvn deploy在根目錄下執(zhí)行
#把本地倉庫中所有的可用archetype 展示出來 mvn archetype:crawl使用archetype
在Idea中使用
1、添加archetype
groupId=com.jurassic artifactId=jurassic-frame-archetype version=2.0.02、通過archetype創(chuàng)建項(xiàng)目
刪除自定義archetype
在 下面文件中有對(duì)應(yīng)記錄,刪除重啟IDEA即可
# C:\Users\{用戶}\.IntelliJIdea2018.2\system\Maven\Indices\<archetypes><archetype groupId="com.jurassic" artifactId="jurassic-frame-archetype" version="1.0.0" /> </archetypes>命令行
mvn archetype:generate -DarchetypeGroupId=com.jurassic -DarchetypeArtifactId=jurassic-frame-archetype -DarchetypeVersion=2.0.0 -DprojectName=test可以給每個(gè)屬性在命令行中設(shè)置參數(shù),如果沒有設(shè)置,則會(huì)提示輸入?yún)?shù)值,有默認(rèn)值的直接回車即可
交互
[INFO] Generating project in Interactive mode [WARNING] No archetype found in remote catalog. Defaulting to internal catalog [INFO] Archetype repository not defined. Using the one from [com.jurassic:jurassic-frame-archetype:2.0.0] found in catalog local [INFO] Using property: groupId = com.jurassic [INFO] Using property: projectName = Test Define value for property 'artifactId' cloud-Test: : Define value for property 'version' 1.0-SNAPSHOT: : 2.0.0 Define value for property 'package' com.jurassic.cloud.Test: : Confirm properties configuration: groupId: com.jurassic projectName: Test artifactId: cloud-Test version: 2.0.0 package: com.jurassic.cloud.TestY: : Y [INFO] ---------------------------------------------------------------------------- [INFO] Using following parameters for creating project from Archetype: jurassic-frame-archetype:2.0.0 [INFO] ---------------------------------------------------------------------------- [INFO] Parameter: groupId, Value: com.jurassic [INFO] Parameter: artifactId, Value: cloud-Test [INFO] Parameter: version, Value: 2.0.0 [INFO] Parameter: package, Value: com.jurassic.cloud.Test [INFO] Parameter: packageInPathFormat, Value: com/jurassic/cloud/Test [INFO] Parameter: package, Value: com.jurassic.cloud.Test [INFO] Parameter: version, Value: 2.0.0 [INFO] Parameter: projectName, Value: Test [INFO] Parameter: groupId, Value: com.jurassic [INFO] Parameter: artifactId, Value: cloud-Test [INFO] Parent element not overwritten in E:\codes\cloud-test\cloud-Test\cloud-Test-api\pom.xml [INFO] Parent element not overwritten in E:\codes\cloud-test\cloud-Test\cloud-Test-biz\pom.xml [INFO] Project created from Archetype in dir: E:\codes\cloud-test\cloud-Test [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 44.414 s [INFO] Finished at: 2021-01-07T15:22:33+08:00 [INFO] ------------------------------------------------------------------------ [WARNING] The requested profile "env-dev" could not be activated because it does not exist.如果不想用交互模式,則設(shè)置參數(shù): -DinteractiveMode=false
mvn archetype:generate -DinteractiveMode=false -DarchetypeGroupId=com.jurassic -DarchetypeArtifactId=jurassic-frame-archetype -DarchetypeVersion=2.0.0 -DprojectName=Testarchetype詳細(xì)介紹
文件詳解
- 根目錄下的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)。
- target\generated-sources\archetype\src\test\resources 目錄下為插件測試用的配置,如果有自定義參數(shù),需要修改projects\basic\archetype.properties 文件信息
根目錄下pom.xml
<groupId>com.jurassic</groupId><artifactId>jurassic-frame-archetype</artifactId><version>2.0.0</version><packaging>maven-archetype</packaging><name>jurassic-frame-archetype</name>/**/archetype-resources/pom.xml
<parent><groupId>com.jurassic</groupId><artifactId>jurassic-cloud</artifactId><version>2.0.0-RELEASE</version></parent><groupId>${groupId}</groupId><artifactId>${artifactId}</artifactId><version>${version}</version><packaging>pom</packaging>POM中 ${}標(biāo)識(shí)的變量都是通過maven中的命令行傳進(jìn)來的 ,idea操作會(huì)通過輸入框輸入。
mvn archetype:generate -DgroupId=com.jurassicarchetype-metadata.xml
<?xml version="1.0" encoding="UTF-8"?> <archetype-descriptor xsi:schemaLocation="https://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.1.0 http://maven.apache.org/xsd/archetype-descriptor-1.1.0.xsd" name="jurassic-frame"xmlns="https://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.1.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><fileSets><fileSet filtered="true" encoding="UTF-8"><directory>.idea/inspectionProfiles</directory><includes><include>**/*.xml</include></includes></fileSet><fileSet filtered="true" encoding="UTF-8"><directory>.idea</directory><includes><include>**/*.xml</include></includes></fileSet>... ... <fileSet encoding="UTF-8"><directory/><includes><include>.gitignore</include><include>jurassic-frame.iml</include><include>readme.md</include></includes></fileSet></fileSets><requiredProperties><requiredProperty key="groupId"><defaultValue>com.jurassic</defaultValue></requiredProperty><requiredProperty key="artifactId"><defaultValue>cloud</defaultValue></requiredProperty><requiredProperty key="package"><defaultValue>com.jurassic.cloud</defaultValue></requiredProperty></requiredProperties><modules><module id="${rootArtifactId}-api" dir="__rootArtifactId__-api" name="${rootArtifactId}-api">... ... </module><module id="${rootArtifactId}-biz" dir="__rootArtifactId__-biz" name="${rootArtifactId}-biz">... ... </module></modules> </archetype-descriptor>說明:
- packaged="true"標(biāo)識(shí)src/main/resources/archetype-resources/src/main/java中對(duì)應(yīng)的內(nèi)容是否要放入到package中,比如package為com.jurassic,那么如果該屬性為true,則對(duì)應(yīng)的java文件會(huì)放到com/jurassic文件夾下,也就是包路徑下。
- filtered="true"標(biāo)識(shí)下面提到的**${}是否要進(jìn)行替換**
module有三個(gè)屬性,解釋如下:
id :定義子模塊工程的artifactId.
dir :子模塊工程源文件在archetype-resources里對(duì)應(yīng)的directory.
name :子模塊的名字.
類文件
#set( $symbol_pound = '#' ) #set( $symbol_dollar = '$' ) #set( $symbol_escape = '\' ) package ${package}.constant;public class T1Constant { }${package}是包名變量。
變量/屬性
- “ __rootArtifactId__ ”占位符會(huì)被parent項(xiàng)目的***artifactId***替換,主要用于路徑中
- ${rootArtifactId}也會(huì)被parent項(xiàng)目的artifactId替換,主要用于文件中
- src/main/resources/archetype-resources里必須要有一個(gè)頂級(jí)pom文件(如果是單工程就是工程pom文件),同時(shí)子文件夾代表了模塊定義
這些屬性可以在資源元文件里的任意一個(gè)文件里通過${var}來引用,所以的元文件最終都可以選擇通過velocity引擎來執(zhí)行替換后生成。
默認(rèn)的屬性有:groupId,artifactId,packeage,version等
文件名或者路徑中包含 _property_ 則會(huì)被屬性值替換
注意:如果打包(install deploy等)時(shí),報(bào)錯(cuò)說:沒有在archetype.properties定義你定義的自定義屬性。Missing required properties in archetype.properties: ,是因?yàn)榧蓽y試模塊造成,請?jiān)趕rc/test/resources/projects/basic/archetype.properties中添加對(duì)應(yīng)的屬性即可解決。參考
http://maven.apache.org/archetype/maven-archetype-plugin/specification/archetype-metadata.html
http://maven.apache.org/archetype/archetype-models/archetype-descriptor/archetype-descriptor.html
https://blog.csdn.net/zp820705/article/details/103852642
.gitignore文件丟失
通過變量解決:
1.首先修改下你的.gitignore文件名為(注意是兩個(gè)_)
__gitignore__2.在你的archetype-metadata.xml中添加
<requiredProperties><requiredProperty key="gitignore"><defaultValue>.gitignore</defaultValue></requiredProperty> </requiredProperties> <fileSets><fileSet encoding="UTF-8"><directory></directory><includes><include>.editorconfig</include><include>__gitignore__</include></includes></fileSet> </fileSets>3.在對(duì)應(yīng)的test包中archetype.properties中添加變量默認(rèn)值
gitignore=.gitignore參考:https://blog.csdn.net/dj7858177/article/details/111948017
總結(jié)
以上是生活随笔為你收集整理的自定义idea archetype的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: nacos云环境集群部署
- 下一篇: Typora云图片