1.先创建一个模板项目,我这个项目是一个父子项目
/hera-archetype/pom.xml 的部分pom
<modules>
<module>server</module>
<module>common</module>
<module>client</module>
<module>datasource</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hera-archetype.version>1.0-SNAPSHOT</hera-archetype.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.hera.cloud.services</groupId>
<artifactId>hera-archetype-common</artifactId>
<version>${hera-archetype.version}</version>
</dependency>
<dependency>
<groupId>com.hera.cloud.services</groupId>
<artifactId>hera-archetype-client</artifactId>
<version>${hera-archetype.version}</version>
</dependency>
<dependency>
<groupId>com.hera.cloud.services</groupId>
<artifactId>hera-archetype-datasource</artifactId>
<version>${hera-archetype.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<distributionManagement>
<repository>
<id>release</id>
<name>release</name>
<url>http://10.247.22.100:8081/repository/release/</url>
<uniqueVersion>true</uniqueVersion>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>snapshots</name>
<url>http://10.247.22.100:8081/repository/snapshots/</url>
</snapshotRepository>
</distributionManagement>
<build>
<plugins>
<!--防止将.gitignore文件忽略-->
<!--http://maven.apache.org/plugins/maven-resources-plugin/resources-mojo.html-->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<addDefaultExcludes>false</addDefaultExcludes>
</configuration>
</plugin>
</plugins>
</build>
/hera-archetype/server/pom.xml 的部分pom
<build>
<finalName>hera-archetype-server</finalName>
<!--不添加此插件会造成打包失败-->
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2.在根目录下执行命令
mvn archetype:create-from-project
hera-archetype/target/generated-sources/archetype/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>com.hera.cloud.services</groupId>
<artifactId>hera-archetype</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>maven-archetype</packaging>
<name>hera-archetype</name>
<distributionManagement>
<repository>
<id>release</id>
<name>release</name>
<url>http://10.247.22.100:8081/repository/release/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>snapshots</name>
<url>http://10.247.22.100:8081/repository/snapshots/</url>
</snapshotRepository>
</distributionManagement>
<build>
<extensions>
<extension>
<groupId>org.apache.maven.archetype</groupId>
<artifactId>archetype-packaging</artifactId>
<version>3.1.2</version>
</extension>
</extensions>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-archetype-plugin</artifactId>
<version>3.1.2</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<addDefaultExcludes>**/.gitignore</addDefaultExcludes>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
/hera-archetype/target/generated-sources/archetype/src/main/resources/META-INF/maven/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="hera-archetype"
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="uArtifactId">
<defaultValue>${artifactId.replaceAll("^a|-a", "A").replaceAll("^b|-b", "B").replaceAll("^c|-c", "C").replaceAll("^d|-d", "D").replaceAll("^e|-e", "E").replaceAll("^f|-f", "F").replaceAll("^g|-g", "G").replaceAll("^h|-h", "H").replaceAll("^i|-i", "I").replaceAll("^j|-j", "J").replaceAll("^k|-k", "K").replaceAll("^l|-l", "L").replaceAll("^m|-m", "M").replaceAll("^n|-n", "N").replaceAll("^o|-o", "O").replaceAll("^p|-p", "P").replaceAll("^q|-q", "Q").replaceAll("^r|-r", "R").replaceAll("^s|-s", "S").replaceAll("^t|-t", "T").replaceAll("^u|-u", "U").replaceAll("^v|-v", "V").replaceAll("^w|-w", "W").replaceAll("^x|-x", "X").replaceAll("^y|-y", "Y").replaceAll("^z|-z", "Z")}</defaultValue>
</requiredProperty>
<requiredProperty key="upperArtifactId">
<defaultValue>${artifactId.substring(0).toUpperCase().replaceAll("-","")}</defaultValue>
</requiredProperty>
</requiredProperties>
<fileSets>
<fileSet encoding="UTF-8">
<directory></directory>
<includes>
<include>.gitignore</include>
<include>README.md</include>
</includes>
</fileSet>
</fileSets>
<modules>
<module id="${rootArtifactId}-server" dir="server" name="${rootArtifactId}-server">
<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>**/*.txt</include>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</fileSet>
</fileSets>
</module>
<module id="${rootArtifactId}-common" dir="common" name="${rootArtifactId}-common">
<fileSets>
<fileSet filtered="true" packaged="true" encoding="UTF-8">
<directory>src/main/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</fileSet>
</fileSets>
</module>
<module id="${rootArtifactId}-client" dir="client" name="${rootArtifactId}-client">
<fileSets>
<fileSet filtered="true" packaged="true" encoding="UTF-8">
<directory>src/main/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</fileSet>
</fileSets>
</module>
<module id="${rootArtifactId}-datasource" dir="datasource" name="${rootArtifactId}-datasource">
<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>
这个里面重新定义了两个requiredProperty 的 key后面会用到
/hera-archetype/target/generated-sources/archetype/src/main/resources/archetype-resources/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>
<parent>
<groupId>com.hera.cloud</groupId>
<artifactId>base</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${version}</version>
<packaging>pom</packaging>
<modules>
<module>server</module>
<module>common</module>
<module>client</module>
<module>datasource</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<${artifactId}.version>${version}</${artifactId}.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>${groupId}</groupId>
<artifactId>${rootArtifactId}-common</artifactId>
<version>${${artifactId}.version}</version>
</dependency>
<dependency>
<groupId>${groupId}</groupId>
<artifactId>${rootArtifactId}-client</artifactId>
<version>${${artifactId}.version}</version>
</dependency>
<dependency>
<groupId>${groupId}</groupId>
<artifactId>${rootArtifactId}-datasource</artifactId>
<version>${${artifactId}.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<distributionManagement>
<repository>
<id>release</id>
<name>release</name>
<url>http://10.247.22.100:8081/repository/release/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>snapshots</name>
<url>http://10.247.22.100:8081/repository/snapshots/</url>
</snapshotRepository>
</distributionManagement>
</project>
注意这里添加了modules模块
修改模板类
__uArtifactId__Application.java
#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package};
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
//@EnableScheduling //定时任务
//@EnableCaching //spring缓存
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
@MapperScan(basePackages = "${package}.dao.mapper") //或者是自己在每个mapper接口加上@Mapper
public class ${uArtifactId}Application {
public static void main(String[] args) {
SpringApplication.run( ${uArtifactId}Application.class, args);
}
}
其他的类与这个类似
生成骨架archetype-catalog.xml
/hera-archetype/target/generated-sources/archetype 下执行命令:
mvn clean install
/hera-archetype 下执行命令:
mvn archetype:crawl
本地的repository会生成
查看本地仓库中的archetype-catalog.xml可以看到我们刚刚的骨架
根据本地骨架archetype-catalog.xml生成项目
执行命令:
mvn archetype:generate -DarchetypeCatelog=/Users/yqx/.m2/repository/archetype-catalog.xml -DarchetypeGroupId=com.hera.cloud.services -DarchetypeArtifactId=hera-archetype -DarchetypeVersion=1.0-SNAPSHOT -DgroupId=com.hera.cloud.services -DartifactId=app-demo -Dpackage=com.hera.cloud.app.demo
只需要根据不同项目变换-DartifactId、Dpackage即可
将骨架上传到私服仓库中
/hera-archetype/target/generated-sources/archetype 下执行命令:
mvn clean deploy
根据私服仓库骨架生成项目
mvn archetype:generate -B -DarchetypeGroupId=com.hera.cloud.services -DarchetypeArtifactId=hera-archetype -DarchetypeVersion=1.0-SNAPSHOT -DgroupId=com.hera.cloud.services -DartifactId=app-demo -Dpackage=com.hera.cloud.app.demo
生成的项目和我们骨架是一模一样的,其他人就可以根据估计生成统一模板的项目了
将源码上传到maven私服
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
骨架的生成是一个消耗耐心的事,大家一定要在心情好的时候尝试
参考:
1.百度各种文档
2.https://maven.apache.org/archetype/archetype-models/archetype-descriptor/archetype-descriptor.html
3.http://maven.apache.org/plugins/maven-resources-plugin/resources-mojo.html