需要自定義骨架的需求
有的時候吭历,我們項目中會有很多公共的依賴、公共的代碼擂橘、公共的配置文件
但是我們又不希望創(chuàng)建一個新項目之后重新從老項目拷貝晌区。所以我們能使用老的項目作為新建項目的maven archetype骨架。
如何自定義骨架
首先創(chuàng)建一個maven項目,這個項目是作為archetype骨架的項目
要想生成骨架朗若,我們的maven要加一個插件pom.xml的build節(jié)點下加入以下代碼
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-archetype-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
<extensions>
<extension>
<groupId>org.apache.maven.archetype</groupId>
<artifactId>archetype-packaging</artifactId>
<version>3.0.1</version>
</extension>
</extensions>
構建archetype
在項目pom.xml同級的目錄下運行以下命令
mvn clean archetype:create-from-project
注意:一定要clean恼五,不然使用骨架會把骨架的java項目結構也加到新項目中
看到build success后我們會發(fā)現(xiàn)生成了target文件夾,結構如下:
target目錄下會有generated-sources目錄哭懈,generated-sources/archetype/src/main.resource/META_INF.maven下會有一個archetype-metadata.xml文件灾馒,這里是可以配置那些資源會被包含在骨架中,那些不會包含在骨架中遣总。
安裝骨架到本地倉庫
到generated-sources/archetype 目錄下安裝骨架到本地
cd target/generated-sources/archetype/
mvn clean install
構建成功后會輸出一下信息:
[INFO] Installing E:\SpringIO\achetype-test\target\generated-sources\archetype\pom.xml to E:\MavenRepository\org\example\achetype-test-archetype\1.0-SNAPSHOT\achetype-test-archetype-1.0-SNAPSHOT.pom
在本地按照路徑找到achetype-test-archetype-1.0-SNAPSHOT.pom 打開睬罗,看看里面的信息:
<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>org.example</groupId>
<artifactId>achetype-test-archetype</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>maven-archetype</packaging>
<name>achetype-test-archetype</name>
<build>
<extensions>
<extension>
<groupId>org.apache.maven.archetype</groupId>
<artifactId>archetype-packaging</artifactId>
<version>2.2</version>
</extension>
</extensions>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-archetype-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
里面標識了jar包的類型是maven-archetype
在本地倉庫生成坐標信息
執(zhí)行一下命令:
mvn archetype:crawl
在指定的Maven庫中查找可以的模板,并更新模板目錄旭斥,這個時候在本地的maven倉庫中就會生成一個archetype-catalog.xml文件容达,里面有固件的坐標信息。
打開:
<?xml version="1.0" encoding="UTF-8"?>
<archetype-catalog xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0 http://maven.apache.org/xsd/archetype-catalog-1.0.0.xsd"
xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<archetypes>
<archetype>
<groupId>org.example</groupId>
<artifactId>achetype-test-archetype</artifactId>
<version>1.0-SNAPSHOT</version>
<description>achetype-test</description>
</archetype>
</archetypes>
</archetype-catalog>
根據(jù)模板坐標信息創(chuàng)建新項目
首先創(chuàng)建maven項目
然后把骨架的坐標信息輸入
輸入的坐標信息是archetype-catalog.xml里achetype-test-archetype骨架的坐標信息垂券,輸入完確認之后下面的archetype面板會出現(xiàn)以下骨架:這個信息會緩存在:
C:\Users/[用戶]\AppData\Local\JetBrains\IntelliJIdea2020.x.x\Maven\Indices/UserArchetypes.xml下
如果想要清楚花盐,打開,把你添加的骨架坐標信息刪掉再清楚IDEA緩存重啟就好
選擇然后下一步菇爪,輸入完項目的groupId和artifactId之后項目就創(chuàng)建成功了
創(chuàng)建的項目會有骨架項目的java代碼算芯、依賴配置、以及資源文件
上傳骨架到nexus
在項目target/generated-sources/archetype/pom.xml 中加入以下配置,指定nexus地址娄帖。
<distributionManagement>
<repository>
<id>nexus-releases</id>
<name>Micaicms Releases</name>
<url>http://localhost:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Micaicms Releases</name>
<url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
然后在target/generated-sources/archetype運行
mvn deploy
這樣就能把骨架上傳到nexus倉庫中了(這個nexus只是筆者自己搭建的本地nexus服務)
注意:上傳nexus私服需要密碼也祠。
使用遠程nexus倉庫的archetype創(chuàng)建項目
使用遠程nexus倉庫的archetype創(chuàng)建項目的時候,必須在自己的maven conf 下settings.xml加入以下配置:
因為Maven 3改變了原型存儲庫的集成方式近速。該-DarchetypeRepository
參數(shù)不再存在。相反堪旧,需要將archteype存儲庫添加到settings.xml
<profile>
<!-- the profile id is arbitrary 這個id是任意填的-->
<id>my_archetype</id>
<repositories>
<repository>
<!-- the repository id has to be named: archetype 這repository Id必須是archetype -->
<id>archetype</id>
<name>my archetypes</name>
<url>http://127.0.0.1:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
<checksumPolicy>fail</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
</repository>
</repositories>
</profile>
<activeProfiles>
<activeProfile>my_archetype</activeProfile> <!-- 這個id是上面的profile id -->
</activeProfiles>
注意:以上配置一定要加削葱!以上配置一定要加!淳梦!以上配置一定要加N鲈摇!爆袍!上面的配置是為了告訴maven archetype可以從哪里拿首繁,如果沒有上面的配置,使用遠程nexus 的archetype的時候會報The desired archetype does not exist