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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义Maven Archetype之 archetype:create-from-project相关的知识,希望对你有一定的参考价值。

前面讲过通过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-sourcesarchetypesrcmain esourcesMETA-INFmaven下的文件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的主要内容,如果未能解决你的问题,请参考以下文章

Maven自定义Archetype

Maven自定义Archetype(zz)

自定义Maven Archetype模板工程

自定义Maven Archetype模板工程

自定义Maven Archetype模板工程

自定义Maven Archetype模板工程