目錄
一、簡介
二、具體步驟
三、 vscode通過模板創建項目
?四、通過IDEA創建
一、簡介
? ? ? ? 有時候MAVEN自帶的模板庫并不能滿足我們創建項目的需求,為了能夠快速創建項目,免去每次復雜的配置,所以我們需要自定義模板庫,本次操作基于vscode的環境。
? ? ? ? 在MAVEN的倉庫里,有個archetype-catalog.xml文件里面記載了模板的坐標信息,可以方便IDEA通過坐標添加模板,而vscode可以自動掃描得到自定義的模板庫,無需手動添加。
二、具體步驟
1、自定義一個項目,用來當作模板,我創建的是一個springmvc的模板
?2、在pom.xml文件中添加代碼。
????????以下代碼指定了一個插件maven-archetype-plugin和一個擴展maven-archetype-plugin.
? ? ? ? maven-archetype-plugin插件:通過該插件,可以創建包含特定目錄結構、配置文件和基本代碼框架的項目模板。
????????
archetype-packaging
?擴展:可以定義項目原型的結構和組織方式,涉及archetype-metadata.xml的生成。
<plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-archetype-plugin</artifactId><version>3.2.1</version></plugin></plugins><extensions><extension><groupId>org.apache.maven.archetype</groupId><artifactId>archetype-packaging</artifactId><version>3.0.1</version></extension></extensions>
最終的pom.xml代碼:
<?xml version="1.0" encoding="UTF-8"?><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.sunlan</groupId><artifactId>springmvc</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><name>springmvc Maven Webapp</name><!-- FIXME change it to the project's website --><url>http://www.sunlan.com</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.7</maven.compiler.source><maven.compiler.target>1.7</maven.compiler.target></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.30</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.3.30</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.0.1</version><scope>provided</scope></dependency><!-- 添加jstl依賴,使其支持<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>jstl語法 --><!-- jstl標簽庫相關 --><dependency><groupId>jstl</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.taglibs/taglibs-standard-impl --><dependency><groupId>org.apache.taglibs</groupId><artifactId>taglibs-standard-jstlel</artifactId><version>1.2.5</version></dependency></dependencies><build><finalName>springmvc</finalName><pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --><plugins><plugin><artifactId>maven-clean-plugin</artifactId><version>3.1.0</version></plugin><!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --><plugin><artifactId>maven-resources-plugin</artifactId><version>3.0.2</version></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.8.0</version></plugin><plugin><artifactId>maven-surefire-plugin</artifactId><version>2.22.1</version></plugin><plugin><artifactId>maven-war-plugin</artifactId><version>3.2.2</version></plugin><plugin><artifactId>maven-install-plugin</artifactId><version>2.5.2</version></plugin><plugin><artifactId>maven-deploy-plugin</artifactId><version>2.8.2</version></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-archetype-plugin</artifactId><version>3.2.1</version></plugin></plugins></pluginManagement><extensions><extension><groupId>org.apache.maven.archetype</groupId><artifactId>archetype-packaging</artifactId><version>3.0.1</version></extension></extensions></build>
</project>
?3、創建archetype骨架
在項目根目錄下運行以下代碼:
mvn clean archetype:create-from-project
運行完成后項目會在target文件夾下生成以下目錄并將默認的值替換成變量?:
以下是archetype骨架自帶的默認變量對應的默認值
其中在archetype-metadata.xml文件中可以用來定義模板的結構。
其中在archetype-resources文件中可以查看具體的模板結構和代碼 。
<?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="springmvc"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="false" packaged="false" encoding="UTF-8"><directory>src/main/java</directory><includes><include>**/*.java</include></includes></fileSet><fileSet filtered="false" packaged="false" encoding="UTF-8"><directory>src/main/resources/lib</directory><includes><include>**/*.jar</include></includes></fileSet><fileSet filtered="false" packaged="false" encoding="UTF-8"><directory>src/main/webapp</directory><includes><include>**/*.jsp</include><include>**/*.xml</include></includes></fileSet></fileSets>
</archetype-descriptor>
fileSet代表文件集,用來定義模板庫包含的文件和文件夾。
? ? ? ? 屬性filtered=true,可以替換引用的變量。如果將filtered設置為false,則不會替換引用的變量,而是保持引用的原樣。
? ? ? ? 屬性packaged=false則不會自動生成包類型的文件夾,反之則會自動生成。
? ? ? ? 屬性include用來表示要模板包含的文件,其中**表示匹配任意子目錄,*表示
。
?4、安裝archetype到本地
?進入target/generated-sources/archetype目錄下,執行指令:
mvn clean install
運行成功后可以在info指示的目錄下找到骨架的jar包。
?5、在本地倉庫生成骨架坐標信息
執行以下命令,在archetype-catalog.xml內生成坐標信息:?
mvn archetype:crawl
三、 vscode通過模板創建項目
?第一步:
??第二步:
?然后根據模板創建即可。
?四、通過IDEA創建
然后就跟創建webapps的模板庫一樣了。?
五、參考資料
maven-archetype自定義模板創建,并在本地和遠程倉庫生成模板項目_maven-archetype-plugin 版本-CSDN博客