Maven的Archetype

Maven的Archetype( /ˈɑːrkitaɪp/ )

简介

Archetype插件准许用户从已有的archetype模板创建maven工程,也准许从已有的工程创建archetype,要求JDK版本在7及以上 image-20220520105513837

integration-test(集成测试是干什么?这个需要自己再去查下东西)

目标综述

archetype插件直接使用的几个命令:

  • archetype:generate 从一个archetype创建一个maven工程:询问用户在archetype目录选择一个archetype,然后从远程仓库获取它,获取到后就会着手创建一个初步的maven工程。

  • archetype:create-from-project 从已有的工程创建一个archetype

  • archetype:crawl 为archetypes搜索一个仓库,更新目录(archetypes有一个目录,id+archetype,使用id来指定archetype) 下面这三个命令通过maven-archetype packing 跟默认的生命周期绑定

  • archetype:jar (跟package阶段绑定)用来构建archetype的jar包

  • archetype:integration-test(跟integration-test阶段绑定)通过从刚刚构建的archetype来创建同样的工程来执行archetype集成测试

  • archetype:update-local-catalog(跟install阶段绑定) 用来更新本地的archetype目录

使用示例

下面的这些示例通过进阶的用户示例展示了如何使用archetype

从archetype构建工程的基本操作

简易操作

mvn archetype:generate 根据提示,输入一些参数,确认好后,就会生成一个工程了

进阶操作

从已有的工程创建一个archetype来创建工程 调用mvn archetype:create-from-project(Maven仓库如果不是默认位置的话,还需要手动设置,使用参数-s maven仓库地址),这个插件首先会通过确定工程的文件来处理包,之后它会在target/generated-sources/archetype下创建archetype的文件树,然后进入到该文件目录下,调用install将archetype安装到本地仓库(同时会在catalog.xml文件中新增或者更新一个archetype),最后进入一个新的文件目录下,使用archetype:generate,根据提示选择arthetype的id(自己安装的在最后),根据提示指定groupId,artifactId,pakageName即可。这个就是直接使用maven命令行进行操作。

系统配置准许用户定义groupId,artifactId,已创建的archetype的版本。他们也准许重写已经定好的包名去创造一个特殊作用的archetype 当被使用到的时候,配置文件优先级高于系统配置,这也是在archetype中定义额外配置的唯一方法 如果用户想对创造的工程定制化甚至更多的话,可以选择在src/main/resources/META-INF/.路径下添加一个groovy script(不过一般在元数据文件中进行一些标签的增减,属性的变化即可)。像所有的system.getProperties(),和archetype创造者具体化的配置文件一样使用archetypeGenerationRequest对象。

批处理模式创建工程

通过设置interactive属性为false或者使用-B标志可以不进行Maven Archetype的参数交互(指在命令行中根据提示输入一些参数等),但是接下来2组重要的配置是需要的

  • The archetypeGroupId, archetypeArtifactId and archetypeVersione

  • The groupId, artifactId, version and package are the main properties to be set. 一些archetype还定义了其他参数,如果需要的话,可以参考赌赢的额archetype文档

    在这个地方我使用powershell的话会报没有pom文件的错误,但是使用cmd的话就没问题了

    这是使用power shell的(Build Failure)

    image-20220520141444692

这是使用cmd的(Build Success)

image-20220520142014512

配置可选的archetype目录创建工程

  • 通过定义archetypeCatalog属性使用可选的目录,下面的都可以作为其中之一

    • internal 只使用archetype内部的catalog(目前就10种,官方demo一般会提示我们使用7的quickstart)

      image-20220520151012699

    • local 只使用本地的catalog(自己Install的)

      image-20220520151033563

    • remote 使用maven远程的catalog,没有catalog在当时就被提供(需要下载,非常多,就不上图了)

    • 默认的是remote,local。因此本地的会展示在远程的后面(这个地方我使用参数的时候-DarchetypeCatalog=X,X加逗号就会报错,不清楚是不是可以在命令行中指定多个可选目录)

从多模块工程创建archetype

从多模块工程创建archetype跟从单模块工程创建一样简单,只需要移动到多模块工程的根目录,执行mvn archetype:create-from-project,示例中的archetype有些文件不需要被过滤,通过在archetype.filterExtensions中指定一些archetype插件的参数来实现。在archetype中定义的这些参数都会使用artfactId来进行填充。

从配置文件创建archetype

通过定义一个包含那些会指引archetype构建的内容,并且通过命令行内容archetype.properties将调archetype 如你所见,额外的内容被作为默认值提供,当想要覆写这个值的时候,必须在第一次确认的时候回复N,然后你写的所有值会被提交 创建的工程树展示了唯一可被识别的语言是groovy,属性archetype.language给出了语言的列表。语言是那些在src/main跟src/test下的文件,且有空间包含packaged文件树的。这就意味着package的值被用来创建这个语言的新文件。 app.groovy跟App.java的内容表示唯一被过滤的文件是java文件。archetype.filteredExtensions的内容给出可以被过滤的文件拓展名的列表。示例中只有.java结尾的文件被过滤掉。

即使那些语言跟过滤拓展符没有任何意义,也会被使用。所有定义在配置文件中的内容都可只作为命令行属性被使用。当没有定义的时候,archetype.language跟archetype.filteredExtensions的内容将使用合理的默认值

  • archetype.filteredExtensions have: java, xml, txt, groovy, cs, mdo, aj, jsp, gsp, vm, html, xhtml, properties, .classpath, .project. Notice the dotted filtered extensions that contains the complete file names.

  • archetype.languages have: java, groovy, csharp, aspectj.

 

一些说明

1.一个archetype是什么?

其实就是一类maven工程的抽象,用于实例化出一个定制的maven工程。 它的JAR包结构(最精简版本) image-20220523142118326

image-20220523142143470

[1]元数据文件(archetype-metadata.xml)文件定义了 1.公共属性的默认值 2.额外属性的集合以及他们的默认值 3.archetype资源名的集合 [2]archetype的资源 [3]maven工程的pom文件 注:为了避免同名冲突,公共属性会被重名命名为target.groupId,等

2.archetype的元数据是如何进行存储的

元数据文件保存了额外的属性以及对应的默认值,也在文件集合中存储了工程需要创造的文件,也存储了archetyp的内部模块,使得用户可以使用单个的archetype就可以生成多模块工程。 元数据文件(最简易版本) image-20220523145035941

该文件意味着:archetyp的名字叫basic,fileset会取到在archetype-resource/src/main/java下匹配所有java文件,所有被选中的文件都会使用Velocity引擎来创建(filtered=true),被创建的工程会入JAR包文件的相同文件下一样在src/main/java下创建,而不是XXXbut with that directory prepended by the package property. 定义附加属性 image-20220523150337646

如图就表示有2个附加属性:property-with-dafault跟property-without-default

定义具体的文件集合 image-20220523152151431

如图就表示AllTest.java不放入新建的project中去

在元数据中定义多模块 image-20220523185603076

modules里面就可以放多个module,格子的module又有自己的filesets。单模块的直接使用fileSets即可

3.创建工程的时候做了什么

选择archetype,配置属性(groudId,artfactId以及一些额外的属性等等) 工程的创建: 1.创建的工程可以是单模块,也可以是多模块的 2.可以创建一部分工程,意味着可以在工程上添砖加瓦而不是重新创建一个 3.如果在一个已经存在的工程中调用的haul,使用完整的archetype可以在已存在的工程中创建子模块 4.从一个已有的工程创建archetype的时候,会在创建过程之前先看pom文件是否合理存在的这个先决条件 使用命令行直接进行工程的创建 Maven的repository根目录下的catalog.xml中会有当前本地仓库的install好的archetype 在使用m generate命令的时候,弹框进行如下提示 image-20220524093957853

1:表示Index,会提示你选择id internal表示是archetype插件的自带的archetype app-basic-jsf表示是这个archetype的名字 (......)表示的是对这个archetype的描述 本文对于变量(各个模块名的变量还不是特别地清晰,后续可能会继续天极爱内容)

元数据文件讲解

在archetype-descriptor标签中 存在2个属性 name(archetype的名字),partial(默认为false,表示是一个完全的maven工程还是一部分)

存在三种元素 requiredProperties/requiredProperty* 从该archetype创建工程需要的参数 filesets/fileset* 文件集合 module/mudule* 工程模块

requiredPropeties 属性 key 该属性的key 元素 defaultValue 默认值 validationRegex 校验正则

fileSet 如果在文件或者文件夹名称中携带[下划线属性下划线]形式,那么在生成工程的时候替换为对应的属性值 属性: filtered filesets可以被过滤,意味着被选中的文件将会被当做Velocity模板。也可以不过滤,意味被选中的文件会完全地复制 package filesets可以被打包,意味着被选中的文件会被复制到一个文件结构中(package属性预先准备的)。也可以不打包,意味着被选中的文件进行预处理就复制好 encoding 过滤内容时使用的编码 元素: directory 会被搜索的文件夹,也是工程中会被创造的文件位于的文件夹 include/include* Inclusion definition "à la" Ant. exclude/exclude* Exclusion definition "à la" Ant.

module 三个属性 id module的artifactId dir module的dirctory name module的name 两个元素 fileSets/fileSet* 文件集合定义 modules/module* 模块定义

以上官方文档中只说了filesets中的参数名替换,其实在modules中的module的id,name,dir属性都是可以进行替换的

示例 image-20220524111017701

<?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="fcbox-ib-vendlocker"
   xmlns="https://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.1.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

 <requiredProperties>
   <requiredProperty key="modulePrefix">
     <defaultValue>RockLeeArchetype</defaultValue>
   </requiredProperty>
 </requiredProperties>
 <fileSets>
   <fileSet filtered="true" encoding="UTF-8">
     <directory></directory>
     <includes>
       <include>.gitignore</include>
     </includes>
   </fileSet>
 </fileSets>
 <modules>
   <module id="${modulePrefix}-service" dir="__modulePrefix__-service" name="${modulePrefix}-service">
     <fileSets>
       <fileSet filtered="true" packaged="true" encoding="UTF-8">
         <directory>src/main/java</directory>
         <includes>
           <include>**/*.java</include>
         </includes>
       </fileSet>
       <fileSet filtered="true" encoding="UTF-8">
         <directory>src/main/resources</directory>
         <includes>
           <include>**/*.xml</include>
           <include>**/*.properties</include>
         </includes>
       </fileSet>
       <fileSet filtered="true" encoding="UTF-8">
         <directory>src/main/resources</directory>
         <includes>
           <include>**/*.</include>
           <include>**/*.yml</include>
           <include>**/*.sql</include>
         </includes>
       </fileSet>
     </fileSets>
   </module>
   <module id="${modulePrefix}-api" dir="__modulePrefix__-api" name="${modulePrefix}-api">
     <fileSets>
       <fileSet filtered="true" packaged="true"  encoding="UTF-8">
         <directory>src/main/java</directory>
         <includes>
           <include>**/*.java</include>
         </includes>
       </fileSet>
     </fileSets>
   </module>
   <module id="${modulePrefix}-common" dir="__modulePrefix__-common" name="${modulePrefix}-common">
     <fileSets>
       <fileSet filtered="true" packaged="true" encoding="UTF-8">
         <directory>src/main/java</directory>
         <includes>
           <include>**/*.java</include>
         </includes>
       </fileSet>
     </fileSets>
   </module>
 </modules>
</archetype-descriptor>

如图就表示,三个模块,每个模块都含有参数modulePrefix,可以在generate的时候进行指定

idea中的使用

我的整个操作过程其实还是通过命令行来进行,只有在建造新工程的时候,会使用到idea的界面操作,首先new project image-20220524112600542

选择maven,from archetype image-20220524113056162

如果没有自己之前安装的archetype的话,还需要店家add Archetype image-20220524113138718

填写你install的时候的信息即可 下一步,填写好project的名字,next,到如下界面,点击参数下方的加号则可以指定一些额外的参数(已经在元数据文件中定义好了的) image-20220524113353972

最后就大功告成咯

posted on 2022-05-24 13:47  wecantstop  阅读(2402)  评论(0)    收藏  举报