自定義Maven Archetype之 archetype:create-from-project
阿新 • • 發佈:2019-01-21
通過 pow pen 倉庫 develop yml archetype 詳解 ring 前面講過通過Maven原型maven-archetype-archetype來創建自定義Archetype的方法,但是這種方法似乎不太方便驗證文件的正確性,這裏將使用maven-archetype-plugin插件的create-from-project目標來從一個project中創建archetype
1,創建用於創建archetype的project
mvn archetype:generate -DgroupId=com.ultrapower.archetypes -DartifactId=ultra-template-archetype -DarchetypeArtifactId=maven-archetype-quickstart -Dversion=1.0 -DinteractiveMode=false
其中關於添加相關的依賴等這裏忽略
2,打開cmd,在project的根目錄執行以下命令
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>[email protected]</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:設置文件的字符編碼
3,cd進入generated-sourced/archetype目錄並執行以下命令
mvn install
通過以上操作便會將自定義的archetype安裝到maven倉庫中,同時會在倉庫根目錄下的archetype-catalog.xml文件中添加自定義archetype的信息(沒有會自動創建該文件)
4,使用自定義的archetype創建project
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下的文件,在此不再贅述
自定義Maven Archetype之 archetype:create-from-project