微服务框架(十)Maven Archetype制作Dubbo项目原型

简介:   此系列文章将会描述Java框架Spring Boot、服务治理框架Dubbo、应用容器引擎Docker,及使用Spring Boot集成Dubbo、Mybatis等开源框架,其中穿插着Spring Boot中日志切面等技术的实现,然后通过gitlab-CI以持续集成为Docker镜像。   本文为Maven Archetype的制作及使用,使用archetype插件制作Dubbo项目原型

  此系列文章将会描述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.

原型项目的开发分为以下步骤:

  1. 配置Maven archetype插件
  2. 配置原型描述文件
  3. 配置相关原型中的文件
  4. 构建至本地或推送至私有仓库

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

参考资料:

  1. 官方文档
  2. Maven自定义archetype生成项目骨架
相关文章
|
1月前
|
Java Maven
手把手教你搭建Maven项目
手把手教你搭建Maven项目
31 0
|
2月前
|
Java Maven
java修改当前项目的maven仓库地址为国内
修改当前项目的maven仓库地址为国内
|
1月前
|
微服务
jeecg微服务项目调用接口报错Token验证失效的解决方法
jeecg微服务项目调用接口报错Token验证失效的解决方法
31 0
|
1月前
|
Java Maven 开发工具
maven导入项目出现Unable to import maven project: See logs for details
maven导入项目出现Unable to import maven project: See logs for details
11 0
maven导入项目出现Unable to import maven project: See logs for details
|
1月前
|
SpringCloudAlibaba Java 持续交付
【构建一套Spring Cloud项目的大概步骤】&【Springcloud Alibaba微服务分布式架构学习资料】
【构建一套Spring Cloud项目的大概步骤】&【Springcloud Alibaba微服务分布式架构学习资料】
156 0
|
17天前
|
监控 数据可视化 安全
智慧工地SaaS可视化平台源码,PC端+APP端,支持二开,项目使用,微服务+Java++vue+mysql
环境实时数据、动态监测报警,实时监控施工环境状态,有针对性地预防施工过程中的环境污染问题,打造文明生态施工,创造绿色的生态环境。
14 0
智慧工地SaaS可视化平台源码,PC端+APP端,支持二开,项目使用,微服务+Java++vue+mysql
|
29天前
|
Java Maven
maven项目导出可执行jar
maven项目导出可执行jar
29 0
|
1月前
|
Java Maven
运行maven项目出现Error:java: JDK isn‘t specified for module ‘XXX‘
运行maven项目出现Error:java: JDK isn‘t specified for module ‘XXX‘
14 0
|
1月前
|
JavaScript Java 关系型数据库
实例!使用Idea创建SSM框架的Maven项目
实例!使用Idea创建SSM框架的Maven项目
38 0
|
1月前
|
Cloud Native Dubbo Java
如何确定微服务项目中Spring Boot、Spring Cloud、Spring Cloud Alibaba三者之间的版本
如何确定微服务项目中Spring Boot、Spring Cloud、Spring Cloud Alibaba三者之间的版本
31 0

热门文章

最新文章