一、archetype简介
Archetype是一个Maven项目的模板工具包,它定义了一类项目的基本架构。Archetype为开发人员提供了创建Maven项目的模板,同时它也可以根据已有的Maven项目生成参数化的模板。通过archetype,开发人员可以很方便地将一类项目的最佳实现应用到自己的项目中。在一个Maven项目中,开发者可以通过archetype提供的范例快速入门并了解该项目的结构与特点。
Maven Archetype由下面几个模块组成:
maven-archetype-plugin:Archetype插件。通过该插件,开发者可以在Maven中使用Archetype。
archetype-packaging:用于描述archetype的生命周期与构建项目软件包
archetype-models:用于描述类与引用
archetype-common:核心类
archetype-testing:用于测试Maven Archetype的内部组件
Archetype的生命周期:
Archetype插件有四个目标可以直接使用:
archetype:create(不推荐):从archetype 中创建一个Maven项目。
archetype:generate:从archetype 中创建一个Maven项目,需要开发人员在指定archetype,插件会从远程仓库中自动获取。
archetype:create-from-project:从已有的项目中生成archetype。
archetype:crawl:搜索并更新仓库中的archetype。
一个典型的archetype包含如下组成部分:
二、Archetype工程的组成
1、Archetype descriptor(archetype.xml),这个文件位于路径src/main/resources/META-INF/maven/,它列出了原型中包含的所有文件,并且给他们做了分类以便Archetype的生成机制可以正确的处理它们;
2、Archetype插件将要拷贝的原型文件,位于路径,src/main/resources/archetype-resources/
3、pom.xml原型文件,位于路径:src/main/resources/archetype-resources
4、这个工程本身自己的pom.xml
三、如何生成Archetype
有两种方法来创建Archetyp,第一种是通过已有的项目来创建,第二种是通过mvn archetype:generate创建一个简单的JAVA项目或WEB项目,然后进行改造。
1、先创建一个ssm框架
2、修改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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<groupId>org.example</groupId>
<artifactId>day06</artifactId>
<version>1.0-SNAPSHOT</version>
<url>http://maven.apache.org</url>
<!--初始化框架的版本号-->
<properties>
<spring.version>1.1.0 RELEASE</spring.version>
</properties>
<pluginRepositories>
<pluginRepository>
<id>alfresco-public</id>
<url>https://artifacts.alfresco.com/nexus/content/groups/public</url>
</pluginRepository>
<pluginRepository>
<id>alfresco-public-snapshots</id>
<url>https://artifacts.alfresco.com/nexus/content/groups/public-snapshots</url>
<snapshots>
<updatePolicy>daily</updatePolicy>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>beardedgeeks-releases</id>
<url>http://beardedgeeks.googlecode.com/svn/repository/releases</url>
</pluginRepository>
</pluginRepositories>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.4.RELEASE</version>
</dependency>
<!-- 事务 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.4.RELEASE</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.24</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>3.7.5</version>
</dependency>
<!--整合-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- mvc json -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.2</version>
</dependency>
<!--swagger2-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
<!--文件上传-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
<!--jsp相关-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.10</version>
</dependency>
</dependencies>
<build>
<finalName>czxy-SSM</finalName>
<plugins>
<!-- 加入maven-archetype-plugin插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-archetype-plugin</artifactId>
<version>2.0-alpha-4</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
可添加:
<pluginRepositories>
<pluginRepository>
<id>alfresco-public</id>
<url>https://artifacts.alfresco.com/nexus/content/groups/public</url>
</pluginRepository>
<pluginRepository>
<id>alfresco-public-snapshots</id>
<url>https://artifacts.alfresco.com/nexus/content/groups/public-snapshots</url>
<snapshots>
<updatePolicy>daily</updatePolicy>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>beardedgeeks-releases</id>
<url>http://beardedgeeks.googlecode.com/svn/repository/releases</url>
</pluginRepository>
</pluginRepositories>
可替换成自己所需的依赖:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.4.RELEASE</version>
</dependency>
<!-- 事务 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.4.RELEASE</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.24</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>3.7.5</version>
</dependency>
<!--整合-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- mvc json -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.2</version>
</dependency>
<!--swagger2-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
<!--文件上传-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
<!--jsp相关-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.10</version>
</dependency>
</dependencies>
往里面添加maven-archetype-plugin,Maven-resources-plugins,maven-compiler-plugin插件。所以一般可先创建所需的maven项目,然后利用archetype plugin的create-from-project将maven项目将该maven项目生成为archetype类型项目。生成maven项目后并确定定型后,执行:
mvn archetype:create-from-project
注意:
在执行上面命令之前最好,把这一些.setting, .class, .project的文件删除掉,因为这是多余的,所以我们在搭建好样板工程之后,在使用mvn archetype:create-from-project命令之前,要先把项目中不相关的工程文件、中间文件删除。
生成的archetype在此路径下:
target\generated-sources
此时会在该项目的target目录下发现如下文件:
---target
---generated-sources
---archetype
---our project
四、生成Archetype如何安装
在上文创建了archetype之后,进入到target\generate-sources\archetype目录,然后在命令行执行:
mvn clean install
安装该archetype项目到你的本地仓库,输出如下信息,特别是Build Success这段,表示就安装成功了。
安装之后主要做了一件事情:
1) 将文件安装到mvn缓存目录,在我本机就是G:\MavenRepository\com\fendo\ssm\fendo-SSM,该文件夹下的目录结构如下:
| maven-metadata-local.xml
|
\---0.0.1-SNAPSHOT
fendo-SSM-0.0.1-SNAPSHOT.jar
fendo-SSM-0.0.1-SNAPSHOT.pom
maven-metadata-local.xml
_remote.repositories
在target\generate-sources\archetype目录中执行crawl命令,生成archetype-catalog.xml:
mvn archetype:crawl
执行成功后会在G:\MavenRepository\archetype-catalog.xml中多出来一个骨架配置文件:
<archetype>
<groupId>com.fendo.ssm</groupId>
<artifactId>fendo-SSM-archetype</artifactId>
<version>0.0.1-SNAPSHOT</version>
<description>fendo-SSM</description>
</archetype>
五、使用上自定义archetype
有2种使用方式:
1)通过命令行使用
找到一个空目录,然后执行如下命令:
mvn archetype:generate -DarchetypeCatalog=local
此时会给出上文安装的archetype,选择即可,如下:
输入编号,就可以了,这里就输入"2",选择SSM,然后输入GroupId,artifactId,就可以了
2)通过IDEA使用
按照G:\MavenRepository\archetype-catalog.xml里面的依次填入即可;最后一个Repository可以不填。
六、archetype命令
1.帮助命令:
archetype:help
2.爬取一个maven仓库,创建目录文件:
archetype:crawl
3.根据一个工程,创建一个新的archetype:
archetype:create-from-project
4.根据一个archetype,创建一个新的工程:
archetype:generate
5.根据当前的archetype工程,创建一个jar包:
archetype:jar
6.更新本地的maven目录:
archetype:update-local-catalog
参考地址: http://maven.apache.org/guides/mini/guide-creating-archetypes.html