官网地址:https://maven.apache.org/archetype/maven-archetype-plugin/index.html
generate:基于原型生成项目
https://maven.apache.org/archetype/maven-archetype-plugin/generate-mojo.html
https://maven.apache.org/archetype/maven-archetype-plugin/usage.html
create-from-project:创建原型
https://maven.apache.org/archetype/maven-archetype-plugin/create-from-project-mojo.html
https://maven.apache.org/archetype/maven-archetype-plugin/advanced-usage.html
crawl:获取本地仓库所有原型并生成
archetype-catalog.xml
文件。https://maven.apache.org/archetype/maven-archetype-plugin/crawl-mojo.html
配置私库地址及认证:https://maven.apache.org/archetype/maven-archetype-plugin/archetype-repository.html
简介
如果我们要创建一个spingboot
项目,可以访问spring
官方提供的https://start.spring.io
网站,或者其它公司如阿里提供的https://start.aliyun.com/bootstrap.html
网站,还可以使用编程工具idea
去创建等等。
maven
官方也有一个插件用来生成新项目,这个插件就是archetype
,它是基于一些创建好的模板去生成新项目的,它可以生成任意结构的项目。
maven
官方提供了很多基础模板,使用命令mvn archetype:generate
:
> mvn archetype:generate
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.2.1:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.2.1:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:3.2.1:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[WARNING] No archetype found in remote catalog. Defaulting to internal catalog
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Choose archetype:
1: internal -> org.apache.maven.archetypes:maven-archetype-archetype (An archetype which contains a sample archetype.)
2: internal -> org.apache.maven.archetypes:maven-archetype-j2ee-simple (An archetype which contains a simplifed sample J2EE application.)
3: internal -> org.apache.maven.archetypes:maven-archetype-plugin (An archetype which contains a sample Maven plugin.)
4: internal -> org.apache.maven.archetypes:maven-archetype-plugin-site (An archetype which contains a sample Maven plugin site.
This archetype can be layered upon an existing Maven plugin project.)
5: internal -> org.apache.maven.archetypes:maven-archetype-portlet (An archetype which contains a sample JSR-268 Portlet.)
6: internal -> org.apache.maven.archetypes:maven-archetype-profiles ()
7: internal -> org.apache.maven.archetypes:maven-archetype-quickstart (An archetype which contains a sample Maven project.)
8: internal -> org.apache.maven.archetypes:maven-archetype-site (An archetype which contains a sample Maven site which demonstrates
some of the supported document types like APT, XDoc, and FML and demonstrates how
to i18n your site. This archetype can be layered upon an existing Maven project.)
9: internal -> org.apache.maven.archetypes:maven-archetype-site-simple (An archetype which contains a sample Maven site.)
10: internal -> org.apache.maven.archetypes:maven-archetype-webapp (An archetype which contains a sample Maven Webapp project.)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 7:
......
上面列出的 1、2、3、4... 都是可以直接使用的模板。
当然,archetype
插件强大之处就在于,他支持使用者自定义模板。下面就讲解如何使用archetype
自定义模板以及基于模板生成新项目。
使用archetype
插件有几个步骤:
- 创建
archetype
原型项目。可以自己从零新建一个,也可以基于一个现成的项目生成。 - 基于
archetype
原型项目生成新的项目。
archetype:create-from-project 创建原型
从零新建一个archetype
创建的archetype
原型项目结构如下:
│ pom.xml
├─src
│ ├─main
│ │ └─resources
│ │ ├─archetype-resources
│ │ │ │ pom.xml
│ │ │ │ __gitignore__
│ │ │ │ README.md
│ │ │ │
│ │ │ └─core
│ │ │ │ pom.xml
│ │ │ │
│ │ │ └─src
│ │ │ └─main
│ │ │ ├─java
│ │ │ │ ├─com
│ │ │ │ │ └─demo
│ │ │ │ │ Application.java
│ │ │ │ │
│ │ │ │ └─controller
│ │ │ │ DemoController.java
│ │ │ │
│ │ │ └─resources
│ │ │ │ application.yml
│ │ │ │
│ │ │ └─i18n
│ │ │ messages.properties
│ │ │
│ │ └─META-INF
│ │ └─maven
│ │ archetype-metadata.xml
根pom.xml
:其实archetype
原型项目也是一个maven项目,因此它也需要一个pom
文件。
<?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.demo</groupId>
<artifactId>demo-archetype</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>maven-archetype</packaging>
<name>demo-archetype</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<!-- 私服配置,如果你不需要推送到私服,可以不配置 distributionManagement -->
<distributionManagement>
<repository>
<id>release-nexus</id>
<url>http://192.168.19.110:8081/repository/maven-releases/</url>
<name>nexus私服中宿主仓库->存放/下载稳定版本的构件</name>
</repository>
<snapshotRepository>
<id>snapshot-nexus</id>
<url>http://192.168.19.110:8081/repository/maven-snapshots/</url>
<name>nexus私服中宿主仓库->存放/下载快照版本的构件</name>
</snapshotRepository>
</distributionManagement>
<build>
<extensions>
<extension>
<groupId>org.apache.maven.archetype</groupId>
<artifactId>archetype-packaging</artifactId>
<version>3.2.1</version>
</extension>
</extensions>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-archetype-plugin</artifactId>
<version>3.2.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
archetype-resources
目录:这个目录及其子目录的内容,就是生成的新项目的内容,但是会基于archetype-metadata.xml
文件的配置动态变化。
archetype-metadata.xml
:元数据文件,配置生成新项目时的参数。
<?xml version="1.0" encoding="UTF-8"?>
<archetype-descriptor xmlns="https://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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="com.xxx">
<requiredProperties>
<requiredProperty key="version">
<defaultValue>1.0-SNAPSHOT</defaultValue>
</requiredProperty>
<requiredProperty key="gitignore">
<defaultValue>.gitignore</defaultValue>
</requiredProperty>
</requiredProperties>
<fileSets>
<fileSet encoding="UTF-8">
<directory></directory>
<includes>
<include>pom.xml</include>
<include>__gitignore__</include>
<include>README.md</include>
</includes>
</fileSet>
</fileSets>
<modules>
<module id="core" dir="core" name="core">
<fileSets>
<fileSet filtered="true" packaged="true" encoding="UTF-8">
<directory>src/main/java</directory>
<includes>
<include>**/*.java</include>
</includes>
<excludes>
<exclude>com/demo/Application.java</exclude>
</excludes>
</fileSet>
<fileSet filtered="true" packaged="false" encoding="UTF-8">
<directory>src/main/java</directory>
<includes>
<include>com/demo/Application.java</include>
</includes>
</fileSet>
<fileSet encoding="UTF-8">
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
</includes>
</fileSet>
<fileSet encoding="UTF-8">
<directory>src/main/resources</directory>
<includes>
<include>**/*.yml</include>
</includes>
</fileSet>
<fileSet encoding="UTF-8">
<directory></directory>
<includes>
<include>pom.xml</include>
</includes>
</fileSet>
</fileSets>
</module>
</modules>
</archetype-descriptor>
- archetype-descriptor --> name:可以理解为
archetype
的一个描述。 - requiredProperties:定义必须要输入的参数,即在使用
mvn archetype:generate
命令时的必需参数,可以设置默认值。 - modules:用于子模块。
- id:子模块的ID。
- dir:子模块的生成路径。
- name:子模块的名称。
- fileSets:文件集。主要用来配置生成的新项目内容。
- directory:匹配文件的目录。
- includes:包括的文件。
- excludes:排除的文件。
- filtered:是否动态渲染匹配到的文件。比如说替换文件里的参数
${package}
等等。 - packaged:匹配到的文件是否需要放在入参包下。
- encoding:复制文件使用的编码。
这里Application.java
的packaged
设置的是false
,是因为我想让它在生成时放在com.demo
这个包下。然后其它的*.java
文件,就会被放在入参的包下(使用mvn archetype:generate
命令时会要求输入参数package
的值,比如com.xxx
,到时候*.java
都会放在这个包下)。
同时你也可能注意到了,.gitignore
文件是使用变量替换来完成复制的,因为直接使用<include>.gitignore</include>
不会复制.gitignore
文件,不知道是这个版本的bug
还是需要配置其它参数。
Application.java
:
#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package com.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
/**
* 启动程序
*/
@ComponentScan({"${package}.**", "com.demo.**"})
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
System.out.println("------>>>>>> 程序启动成功 <<<<<<------");
}
}
DemoController.java
:
#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.controller;
import com.demo.system.domain.File;
import com.demo.system.service.ISysFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/api/public")
public class DemoController {
@Autowired
private ISysFileService sysFileService;
@GetMapping("/file")
public List<File> file() {
return sysFileService.list();
}
}
resources
目录下的文件基本保持不变,当然你可以根据自己的需要去改写archetype-metadata.xml
文件。
archetype-resources --> pom.xml
,生成的新项目的父pom
文件:
<?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>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${version}</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java.version>1.8</java.version>
</properties>
<!-- 依赖声明 -->
<dependencyManagement>
</dependencyManagement>
<!-- build -->
<build>
</build>
<modules>
<module>core</module>
</modules>
</project>
archetype-resources --> core --> pom.xml
,生成的新项目的子模块pom
文件:
<?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">
<parent>
<groupId>${groupId}</groupId>
<artifactId>${rootArtifactId}</artifactId>
<version>${version}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<artifactId>${artifactId}</artifactId>
<!-- 依赖声明 -->
<dependencies>
</dependencies>
<!-- build -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.1.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<finalName>${project.artifactId}</finalName>
</build>
</project>
以上两个pom
内容你可以自己去写,相信你已经注意到了,上面的groupId
、artifactId
、version
都是用的${}
形式出现的,这些都是在使用mvn archetype:generate
命令时当作参数传入的。
PS
这里还有一个技巧可以使用,那就是动态生成子模块的名称。
上面的目录archetype-resources --> core
中的core
可以改为__rootArtifactId__core
。
archetype-metadata.xml
文件中的<module id="core" dir="core" name="core">
可以改为<module id="__rootArtifactId__core" dir="__rootArtifactId__core" name="__rootArtifactId__core">
。
在创建项目时,__rootArtifactId__
会作为参数被动态替换掉。
到这里archetype
原型项目就创建好了,如果你不需要推送到私服,直接在根目录下运行mvn clean install
即可安装到本地仓库。
如果你需要推送到私服,那么还需要修改maven
的setting.xml
文件,在其中配置私服的账号密码,然后mvn clean deploy
即可:
<!-- 配置私服账号密码 -->
<servers>
<server>
<id>release-nexus</id>
<username>username</username>
<password>password</password>
</server>
<server>
<id>snapshot-nexus</id>
<username>username</username>
<password>password</password>
</server>
</servers>
使用install
或deploy
后,会在本地仓库生成原型jar
文件(就和普通jar
的安装推送一样)。
基于现成的项目生成archetype
这个相较于上面那种方法,就要简单很多。
- 直接在你现有的项目的根目录下执行
mvn archetype:create-from-project
即可。然后它会在target/generated-sources/archetype
目录下生成原型树。这个原型树,和我们上面从零新建一个archetype所创建的那个项目是一样的。 - 进入原型树目录
cd target/generated-sources/archetype
,按照自己的需要调整原型树结构和配置文件。 - 在原型树根目录下,执行
mvn install
或mvn deploy
安装到本地仓库或私服。
archetype:generate 生成新项目
如果你要使用私服上面的archetype
原型,那么需要配置私服的认证(当然如果没有私服,可跳过这个配置),修改maven
的setting.xml
文件:
<!-- 配置私服账号密码 -->
<servers>
<server>
<id>archetype</id>
<username>username</username>
<password>password</password>
</server>
</servers>
<!-- 使用profile配置私服仓库地址 -->
<profiles>
<profile>
<!-- 这个需要与server的id一致,具体叫什么无所谓 -->
<id>archetype</id>
<repositories>
<repository>
<!-- 这个固定为archetype -->
<id>archetype</id>
<url>http://192.168.19.110:8081/repository/maven-snapshots/</url>
<releases>
<enabled>true</enabled>
<checksumPolicy>fail</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
配置了profile
,那么在使用时就需要指定profile
,这个可以在使用maven
命令时通过-Parchetype
来指定,也可以直接配置在setting.xml
中永久生效:
<activeProfiles>
<activeProfile>archetype</activeProfile>
</activeProfiles> -->
基于原型生成新项目:
mvn archetype:generate -Parchetype -D archetypeGroupId=com.demo -D archetypeArtifactId=demo-archetype -D archetypeVersion=1.0-SNAPSHOT -D groupId=com.test -D artifactId=TestParent -D package=com.core
- archetypeGroupId:原型的groupId。
- archetypeArtifactId:原型的artifactId。
- archetypeVersion:原型的version。
- groupId:生成项目的groupId,会作为参数替换
${groupId}
。 - artifactId:生成项目的artifactId,会作为参数替换
${artifactId}
。 - version:生成项目的version,会作为参数替换
${version}
,当然,上面我们已经指定了默认值,所以可以不传这个参数。 - package:参数
${package}
。所有*.java
文件都会被放到这个包下。
archetype:crawl
这个命令是用来获取本地仓库所有原型并在仓库根目录下生成archetype-catalog.xml
文件,下次使用mvn archetype:generate
就会直接从这个文件检索原型。
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.demo</groupId>
<artifactId>demo-archetype</artifactId>
<version>1.0-SNAPSHOT</version>
<description>demo-archetype</description>
</archetype>
</archetypes>
</archetype-catalog>
-- end --