`
leonzhx
  • 浏览: 769404 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

第十八章 Archetype

阅读更多

1.   Archetype Maven 项目的模板,它能生成项目的基本结构及 POM 文件。

 

2.   使用 Archetype 的一般步骤:

  1) m vn archetype:generate

  2)  选择要使用的 archetype 的编号(候选列表来自 archetype-catalog.xml

  3)  输入基本参数: groupId artifactId version package (想要创建项目的默认 Java 包名),其他额外参数。(前四项参数是 Archetype 插件内置的)

 

3.   当不希望以交互方式使用 archetype 的时候可以:

mvn archetype:generate –B –DarchetypeGroupId = org.apache.maven.archetypes –DarchetypeArtifactId=maven-archetype-quickstart –DarchetypeVersion=1.0 –DgroupId=com.juvenxu.mvnbook –DartifactId=archetype-test –Dversion=1.0-SNAPSHOT –Dpackage=com.juvenxu.mvnbook

 

4.   Maven 中央仓库的 archetype 列表:

http://repo1.maven.org/maven2/archetype-catalog.xml

 

5.   maven-archetype-quickstart 是最常用的 archetype ,也是 maven-archetype-plugin 提示用户选择 archetype 的时候的默认值。它包含:

  1)   包含 JUnit 依赖声明的 pom.xml

  2)   src/main/java 主代码目录及该目录下名为 App 的输出 ”Hello World!” 的类。

  3)   src/test/java 测试代码目录及该目录下名为 AppTest JUnit 测试用例。

 

6.   maven-archetype-webapp 是一个最简单的 Maven war 项目模板,它包含:

  1) 一个 packaging war 且带有 JUnit 依赖声明的 pom.xml

  2) src/main/webapp/ 目录

  3) src/main/webapp/index.jsp 文件

  4) src/main/webapp/WEB-INF/web.xml 文件

 

7.   AppFuse 是一个集成了很多开源工具的项目,它的核心就是一个项目的骨架,是包含了持久层、业务层及展现层的一个基本结构,它集成了大量流行的开源工具,如: Spring Struts 2 JPA JSF Tapestry 等。 AppFuse 提供了大量 archetype ,针对各种展现层框架分别为:

  1) appfuse-*-jsf :基于 JSF Archetype

  2) appfuse-*-spring :基本 Spring MVC Archetype

  3) appfuse-*-struts :基本 Struts 2 Archetype

  4) appfuse-*-tapestry :基本 Tapestry Archetype

每一种展现层框架都有 3 Archetype ,分别为 light basic modular light 包含最简单的骨架, basic 则包含了一些用户管理及安全方面的特性, modular 会生成多模块项目,其中的 core 模块包含了持久层及业务层代码, web 模块包含了展现层代码。

 

8.   关于 AppFuse Archetype 的具体信息可以参考:

http://appfuse.org/display/apf/appfuse+quickstart

 

9.   要编写一个自己的 Archetype ,需要创建一个 Archetype Maven 项目,包含如下部分:

  1)   pom.xml Archetype 自身的 POM

  2)   src/main/resources/archetype-resources/pom.xml :基于该 Archetype 生成的项目的 POM 原型

  3)   src/main/resources/META-INF/maven/archetype-metadata.xml Archetype 的描述符文件。

  4)   src/main/resources/archetype-resources/** :其他需要包含在 Archetype 中的内容。

 

10.   虽然 Archetype 是一种特殊的 Maven 项目,但 maven-archetype-plugin 并没有要求 Archetype 项目使用特殊的打包类型,一般为 jar Archetype 项目的 POM 也没有什么特别之处,只需要的声明项目基本信息就行了。

 

11.   POM 原型中,可以使用可配置的属性,如 ${groupId} ${artifactId} ${version} ${package} 是内置的四个属性。

 

12.   Archetype 描述符文件是 Archetype 最核心的部分,它主要描述两件事:

  1) 声明哪些目录及文件应该包含在 Archetype 文件中( <fileSets>

  2) Archetype 使用哪些属性参数( <requiredProperties>

 

13.   一个 Archetype 描述符文件的例子:

<?xml version=”1.0” encoding=”UTF-8”?>

<archetype-descriptor name=”sample”>

  <fileSets>

    <fileSet filtered=”true” packaged=”true”>

      <directory>src/main/java</directory>

      <includes>

        <include>**/*.java</include>

      </include>

    </fileSet>

    <fileSet filtered=”true” packaged=”true”>

      <directory>src/test/java</directory>

      <includes>

        <include>**/*.java</include>

      </include>

    </fileSet>

    <fileSet filtered=”true” packaged=”false”>

      <directory>src/main/resources</directory>

      <includes>

        <include>**/*.properties</include>

      </include>

    </fileSet>

  <requiredProperties>

    <requiredProperty key=”port”/>

    <requiredProperty key=”groupId” >

      <defaultValue>com.juvenxu.mvnbook</defaultValue>

    </requiredProperty>

  </requiredProperties>

</archetype-descriptor>
 

fileSet 定义了要被放入生成项目中的文件。 directory 指定的目录是相对于 Archetype 项目的 src/main/resources/archetype-resources 目录的相对路径。 Filtered 表示该文件集中的文件是否应用属性替换。而 packaged 表示是否将目录下的内容放到生成项目的包路径下。如果 package (生成项目时用户必须输入的参数)值为 com.juvenxu.mvnbook ,则所有 src/main/resources/archetype-resources/src/main/java 下的 .java 文件夹会被放到生成项目的 src/main/java/com/juvenxu/mvnbook 下。而 requiredProperty 表示了 Archetype 要求的额外参数。

 

 

14.   Archetype 项目 mvn clean install 后就可以用 maven-archetype-plugin:generate 使用它了。

 

15.   当用户以不指定 Archetype 坐标的方式使用 maven-archetype-plugin 的时候,会得到一个 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 xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance >

  <archetypes>

     <archetype>

       <groupId>com.juvenxu.mvnbook.archetypes</groupId>

       <artifactId>mvnbook-archetype-sample</artifactId>

       <version>1.0-SNAPSHOT</version>

       <description>sample</description>

     </archetype>

     <archetype>

        <groupId>com.juvenxu.mvnbook.archetypes</groupId>

       <artifactId>mvnbook-archetype-quickstart</artifactId>

       <version>1.0 </version>

       <description>quickstart</description>

     </archetype>

  </archetypes>

</archetype-catalog> 
 

16.   maven-archetype-plugin 可以从以下位置读取 archetype-catalog.xml 文件:

  1) internal maven-archetype-plugin 内置的 Archetype Catalog

  2) local :~ /.m2/archetype-catalog.xml

  3) remote http://repo1.maven.org/maven2/archetype-Catalog.xml

  4) file://... :用户可以指定本机任何位置的 archetype-catalog.xml 文件

  5) http://... :用户可以指定远程任何位置的 archetype-catalog.xml 文件

当用户运行 mvn archetype:generate 命令的时候,可以使用 archetypeCatalog 参数指定插件使用的 Catalog ,可以用逗号分隔多个来源。 2.0-beta-4 之前,默认为 ”internal, local” ,之后,默认为 ”remote,local”

 

17.   maven-archetype-plugin:crawl 可以遍历本地的 Maven 仓库的内容,生成 archetype-catalog.xml 文件。默认 crawl 会遍历用户 settings.xml 中定义的 localRepository ,可以使用 repository 参数自己指定仓库路径,也可以使用 catalog 参数指定要更新的 catalog 文件。

 

18.   nexus-archetype-plugin 插件能够基于 Nexus 仓库索引实时生成 archetype-catalog.xml 文件。用户可以在

http://repository.sonatype.org/content/groups/forge/org/sonatype/nexus/plugins/nexus-archetype-plugin/

下载该插件。将 bundle.zip 解压到 Nexus 工作目录 sonatype-work/nexus/ 下的 plugin-repository/ 子目录中,然后重启 Nexus 就完成了安装。当用户浏览 Nexus 仓库内容时,就能在仓库的根目录下下载 archetype-catalog.xml 文件。

分享到:
评论

相关推荐

    Maven实战(高清版)

    第1章 maven简介1.1 何为maven1.1.1 何为构建1.1.2 maven是优秀的构建工具1.1.3 maven不仅仅是构建工具1.2 为什么...灵活的构建第15章 生成项目站点第16章 m2eclipse第17章 编写maven插件第18章 archetype

    archetype-catalog.xml

    archetype-catalog.xml

    archetype-catalog.zip

    Eclipse Maven 创建Web 项目报错 Could not resolve archetype org.apache.maven.archetypes:maven-archetype-webap Could not resolve archetype org.apache.maven.archetypes:maven-archetype-webapp:1.0 from any...

    archetype-catalog.xml-最新

    2019-11-30 最新archetype-catalog.xml文件,解决maven新建项目慢。将archetype-catalog.xml拷贝到本地仓库下。 解决Archetype not found in any catalog 解决No archetype found in remote catalog

    201806最新archetype-catalog.xml

    201806最新archetype-catalog.xml,maven创建项目archetype:generate时,执行到Generating project in Batch mode卡主不动,解决办法:将下载的该文件放置到复制到C:\Users\Administrator\.m2\repository\org\apache...

    maven_archetype

    maven_archetype maven的文件,有需要的就下載吧 壓縮格式為.7z檔

    2021最新的maven本地archetype-catalog.xml

    idea用maven骨架创建项目速度慢,添加archetype-catalog.xml到本地仓库的文件夹后,设置-DarchetypeCatalog=local,就能使用maven默认的archetype-catalog.xml,再不用远程下载。

    archetype-catalog.xml 文件

    在IDEA中通过maven项目管理工具创建javaweb项目经常缓慢 大多数一直卡在了Running C:\Users\Administrator\AppData\Local\Temp\archetype1tmp 这一步。所以这里提供archetype-catalog.xml 文件。

    maven项目搭建:archetype-catalog.xml

    2021-10-28官网下载archetype-catalog.xml。(maven项目快速搭建,缺少这个文件时,提示maven-archetype-webapp could not resolve archetpye)

    archetype MyEclipse风格的,允许自动部署

    maven的archetype,需要安装,安装之后可以很方便的创建 WEB应用. 同时支持MyEclipse自动部署和Maven构建 详见文件内文档

    最新archetype-catalog.xml

    最新maven archetype-catalog.xml,截止日期:2018年4月26日

Global site tag (gtag.js) - Google Analytics