溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

自定義Maven Archetype之 archetype:create-from-project

發(fā)布時間:2020-07-27 08:58:07 來源:網(wǎng)絡(luò) 閱讀:2720 作者:mybabe0312 欄目:軟件技術(shù)

前面講過通過Maven原型maven-archetype-archetype來創(chuàng)建自定義Archetype的方法,但是這種方法似乎不太方便驗證文件的正確性,這里將使用maven-archetype-plugin插件的create-from-project目標來從一個project中創(chuàng)建archetype

1,創(chuàng)建用于創(chuàng)建archetype的project

mvn archetype:generate -DgroupId=com.ultrapower.archetypes -DartifactId=ultra-template-archetype -DarchetypeArtifactId=maven-archetype-quickstart -Dversion=1.0 -DinteractiveMode=false

自定義Maven Archetype之 archetype:create-from-project

其中關(guān)于添加相關(guān)的依賴等這里忽略

2,打開cmd,在project的根目錄執(zhí)行以下命令

mvn archetype:create-from-project

通過該命令,會在target目錄下面生成generated-sources/archetype目錄,這個就是生成的 archetype。 (同時,generated-sources\archetype\src\main\resources\META-INF\maven下的文件archetype-metadata.xml可能需要調(diào)整)
自定義Maven Archetype之 archetype:create-from-project

先來看看archetype下的pom.xml都包含些什么內(nèi)容

<?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文件看看里面的內(nèi)容,并做可能的必要修改【可能默認包含了些你不需要的文件】

<?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>

在此簡單說下幾個屬性的含義(完整的說明參考官網(wǎng)archetype-metadata.xml詳解)
1)filtered :被選擇的文件是否當做 Velocity 模板來使用(如果是true則可以使用Velocity的表達式,例如用于動態(tài)設(shè)置包名等)
2)packaged:被拷貝或生成(通過Velocity生成)的文件是否在package屬性預(yù)先設(shè)置的目錄結(jié)構(gòu)下進行存放
3)encoding:設(shè)置文件的字符編碼

3,cd進入generated-sourced/archetype目錄并執(zhí)行以下命令

mvn install

通過以上操作便會將自定義的archetype安裝到maven倉庫中,同時會在倉庫根目錄下的archetype-catalog.xml文件中添加自定義archetype的信息(沒有會自動創(chuàng)建該文件)

4,使用自定義的archetype創(chuàng)建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便生成
自定義Maven Archetype之 archetype:create-from-project


重要:對于某些java和文本文件里面的配置可能需要動態(tài)修改,所以需要手動的調(diào)整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>
向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI