您好,登錄后才能下訂單哦!
前面講過通過Maven原型maven-archetype-archetype來創建自定義Archetype的方法,但是這種方法似乎不太方便驗證文件的正確性,這里將使用maven-archetype-plugin插件的create-from-project目標來從一個project中創建archetype
mvn archetype:generate -DgroupId=com.ultrapower.archetypes -DartifactId=ultra-template-archetype -DarchetypeArtifactId=maven-archetype-quickstart -Dversion=1.0 -DinteractiveMode=false
其中關于添加相關的依賴等這里忽略
mvn archetype:create-from-project
通過該命令,會在target目錄下面生成generated-sources/archetype目錄,這個就是生成的 archetype。 (同時,generated-sources\archetype\src\main\resources\META-INF\maven下的文件archetype-metadata.xml可能需要調整)
先來看看archetype下的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.ultrapower.archetypes</groupId>
<artifactId>ultra-template-archetype-archetype</artifactId>
<version>1.0</version>
<packaging>maven-archetype</packaging>
<name>ultra-template-archetype-archetype</name>
<build>
<extensions>
<extension>
<groupId>org.apache.maven.archetype</groupId>
<artifactId>archetype-packaging</artifactId>
<version>3.0.1</version>
</extension>
</extensions>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-archetype-plugin</artifactId>
<version>3.0.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<description>Parent pom providing dependency and plugin management for applications
built with Maven</description>
<url>http://maven.apache.org</url>
<developers>
<developer>
<name>Pivotal</name>
<email>info@pivotal.io</email>
<organization>Pivotal Software, Inc.</organization>
<organizationUrl>http://www.spring.io</organizationUrl>
</developer>
</developers>
<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0</url>
</license>
</licenses>
<scm>
<url>https://github.com/spring-projects/spring-boot/spring-boot-starter-parent/ultra-template-archetype</url>
</scm>
</project>
我們再打開archetype-metadata.xml文件看看里面的內容,并做可能的必要修改【可能默認包含了些你不需要的文件】
<?xml version="1.0" encoding="UTF-8"?>
<archetype-descriptor 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" name="ultra-template-archetype"
xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<fileSets>
<fileSet filtered="true" packaged="true" encoding="UTF-8">
<directory>src/main/java</directory>
<includes>
<include>**/*.java</include>
<include>**/*.xml</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.html</include>
</includes>
</fileSet>
<fileSet encoding="UTF-8">
<directory>src/main/resources</directory>
<includes>
<include>**/*.yml</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>lib</directory>
<includes>
<include>**/*.jar</include>
</includes>
</fileSet>
<fileSet encoding="UTF-8">
<directory>boot</directory>
<includes>
<include>**/*.bat</include>
<include>**/*.sh</include>
</includes>
</fileSet>
<fileSet encoding="UTF-8">
<directory>.settings</directory>
<includes>
<include>**/*.prefs</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<directory></directory>
<includes>
<include>.classpath</include>
<include>.project</include>
</includes>
</fileSet>
</fileSets>
</archetype-descriptor>
將最后的兩項去掉即可滿足需要了
<fileSet encoding="UTF-8">
<directory>.settings</directory>
<includes>
<include>**/*.prefs</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<directory></directory>
<includes>
<include>.classpath</include>
<include>.project</include>
</includes>
</fileSet>
在此簡單說下幾個屬性的含義(完整的說明參考官網archetype-metadata.xml詳解)
1)filtered :被選擇的文件是否當做 Velocity 模板來使用(如果是true則可以使用Velocity的表達式,例如用于動態設置包名等)
2)packaged:被拷貝或生成(通過Velocity生成)的文件是否在package屬性預先設置的目錄結構下進行存放
3)encoding:設置文件的字符編碼
mvn install
通過以上操作便會將自定義的archetype安裝到maven倉庫中,同時會在倉庫根目錄下的archetype-catalog.xml文件中添加自定義archetype的信息(沒有會自動創建該文件)
mvn archetype:generate -DarchetypeGroupId=com.ultrapower.archetypes -DarchetypeArtifactId=ultra-template-archetype-archetype -DarchetypeVersion=1.0 -DgroupId=com.ultrapower.ioss -DartifactId=ultra-template-archetype-test -X
至此,一個新的project便生成
重要:對于某些java和文本文件里面的配置可能需要動態修改,所以需要手動的調整archetype下的文件,在此不再贅述
在安裝完成后,在本地倉庫的根路徑下會生成一個文件“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>com.ultrapower.archetypes</groupId>
<artifactId>ultra-template-archetype-archetype</artifactId>
<version>1.0</version>
<description>Parent pom providing dependency and plugin management for applications
built with Maven</description>
</archetype>
</archetypes>
</archetype-catalog>
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。