此系列文章将会描述Java框架Spring Boot、服务治理框架Dubbo、应用容器引擎Docker,及使用Spring Boot集成Dubbo、Mybatis等开源框架,其中穿插着Spring Boot中日志切面等技术的实现,然后通过gitlab-CI以持续集成为Docker镜像。
本文为Maven Archetype的制作及使用,使用archetype插件制作Dubbo项目原型
本系列文章中所使用的框架版本为Spring Boot 2.0.3-RELEASE,Spring 5.0.7-RELEASE,Dubbo 2.6.2。
Maven Archetype
原型(Archetypes)打包在JAR中,它们包含描述原型内容的原型元数据,以及构成原型项目的一组敏捷开发模板。
Archetypes are packaged up in a JAR and they consist of the archetype metadata which describes the contents of archetype, and a set of Velocity templates which make up the prototype project.
原型项目的开发分为以下步骤:
- 配置Maven archetype插件
- 配置原型描述文件
- 配置相关原型中的文件
- 构建至本地或推送至私有仓库
POM配置
配置Maven archetype插件
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-archetype-plugin</artifactId>
<version>3.0.0</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>
<includeEmptyDirs>true</includeEmptyDirs>
</configuration>
</plugin>
</plugins>
</pluginManagement>
若需推送到私有仓库,还需配置对应的私有仓库部署配置
<properties>
<nexus.url>ip:port</nexus.url>
</properties>
<distributionManagement>
<repository>
<id>maven-releases</id>
<name>Nexus Release Repository</name>
<url>http://${nexus.url}/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>maven-snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://${nexus.url}/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
archetype-descriptor
原型描述文件需命名为archetype-metadata.xml,必须设在src/main/resources/META-INF/maven/
.
<?xml version="1.0" encoding="UTF-8"?>
<archetype-descriptor name="dubbo-common-archetype">
<fileSets>
<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>**/*.xml</include>
<include>**/**</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<directory>src/test/java</directory>
<includes>
<include>**/*.**</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<directory></directory>
<includes>
<include>.**</include>
</includes>
</fileSet>
</fileSets>
</archetype-descriptor>
原型项目路径结构:
__packageInPathFormat__
会自动适配生成Maven项目时设置的path
archetype
|-- pom.xml
`-- src
`-- main
`-- resources
|-- META-INF
| `-- maven
| `--archetype.xml
`-- archetype-resources
|-- pom.xml
`-- src
|-- main
| |-- java
| | `-- __packageInPathFormat__
| | `-- handler
| | `-- model
| | `-- provider
| | `-- resources
| | `-- starter
| | `-- utils
| |-- resources
| `-- config
| `-- mapper
| `-- docker
| `-- application.properties
| `-- log4j2.xml
`-- test
`-- java
在Java文件头中配置以下参数,则会在生成Maven项目时自动适配相应的包路径
#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${
package}.handler;
项目结构如下图
本地使用
本地使用archetype只需install
后使用相应archetype
mvn install
mvn archetype:generate \
-DarchetypeGroupId=<archetype-groupId> \
-DarchetypeArtifactId=<archetype-artifactId> \
-DarchetypeVersion=<archetype-version> \
-DgroupId=<my.groupid> \
-DartifactId=<my-artifactId>
在eclipse或idea中则需在install
后刷新目录update-local-catalog
mvn rchetype:update-local-catalog
私有仓库使用
推送至私有仓库后,只需在eclipse或idea中创建Maven项目,添加对应archetype即可
archetype命令
帮助命令
mvn archetype:help
爬取仓库以创建目录
mvn archetype:crawl
创建archetype
mvn archetype:create-from-project
根据archetype创建工程
mvn archetype:generate
根据当前的archetype工程创建jar
mvn archetype:jar
更新本地的maven目录
mvn archetype:update-local-catalog
参考资料: