1.简介
maven脚手架是为了创建一个项目模板,以后新建项目都能够复用该模板
maven中模板引擎使用的是velocity,在文件中可以使用它的语法获取变量等操作
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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>single-springboot-template</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>maven-archetype</packaging>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven-javadoc-plugin.version>3.2.0</maven-javadoc-plugin.version>
<maven-source-plugin.version>3.2.1</maven-source-plugin.version>
<maven-gpg-plugin.version>1.6</maven-gpg-plugin.version>
<maven-archetype-plugin.version>3.2.1</maven-archetype-plugin.version>
<maven-checksum-plugin.version>1.10</maven-checksum-plugin.version>
</properties>
<build>
<extensions>
<extension>
<groupId>org.apache.maven.archetype</groupId>
<artifactId>archetype-packaging</artifactId>
<version>${maven-archetype-plugin.version}</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>net.nicoulaj.maven.plugins</groupId>
<artifactId>checksum-maven-plugin</artifactId>
<version>${maven-checksum-plugin.version}</version>
<executions>
<execution>
<id>create-checksums</id>
<goals>
<goal>artifacts</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<configuration>
<encoding>UTF-8</encoding>
<aggregate>true</aggregate>
<charset>UTF-8</charset>
<docencoding>UTF-8</docencoding>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<additionalparam>-Xdoclint:none</additionalparam>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
<configuration>
<autoVersionSubmodules>true</autoVersionSubmodules>
<useReleaseProfile>false</useReleaseProfile>
<releaseProfiles>release</releaseProfiles>
<goals>deploy</goals>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<includeEmptyDirs>true</includeEmptyDirs>
<addDefaultExcludes>false</addDefaultExcludes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-archetype-plugin</artifactId>
<version>${maven-archetype-plugin.version}</version>
<configuration>
<!-- needed to include .gitignore -->
<useDefaultExcludes>false</useDefaultExcludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
archetype-metadata.xml
<?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="xfg-frame-archetype"
xmlns="https://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<fileSets>
<fileSet filtered="true" encoding="UTF-8">
<directory>docs</directory>
<includes>
<include>**/**</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<directory>script</directory>
<includes>
<include>**/**</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<directory>deploy</directory>
<includes>
<include>**/**</include>
</includes>
</fileSet>
<fileSet encoding="UTF-8">
<directory/>
<includes>
<include>README.md</include>
<include>.gitignore</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<directory>src/main/java</directory>
<includes>
<include>**/**</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<directory>src/main/resources</directory>
<includes>
<include>**/**</include>
</includes>
</fileSet>
<fileSet>
<directory>src/test/java</directory>
</fileSet>
</fileSets>
</archetype-descriptor>
#模板项目安装到本地
mvn claen install
#执行生成项目命令 静默方式不需要用户输入
mvn archetype:generate -DgroupId=com.wl -DartifactId=demo -Dpackagename=com.wl -DarchetypeGroupId=org.example -DarchetypeArtifactId=single-springboot-template -DarchetypeVersion=1.0-SNAPSHOT -DinteractiveMode=false
#执行生成项目命令
mvn archetype:generate -DgroupId=com.wl -DartifactId=demo -Dpackagename=com.wl -DarchetypeGroupId=org.example -DarchetypeArtifactId=single-springboot-template -DarchetypeVersion=1.0-SNAPSHOT
3.参考文档
- https://medium.com/@sybrenbolandit/maven-archetype-deed0160cdeb
- Velocity入门到精通(上篇)_velocity教程-CSDN博客