一、前言
在我们日常开发过程中,经常需要新建一些工程去处理相应模块的业务,同时也为了更好的区分模块边界,所以我们需要构建一些符合当前公司代码模块风格的自定义工程。下面是我供职于某互联网公司搭建脚手架的实践,若有错误之处,还请多多指教。
二、环境要求
- JDK1.8
- 现有的工程结构
- maven
- maven私服
三、操作步骤
先去到你的工程目录,如下是我的工程目录所在
如上图所示:在命令终端执行命令
mvn archetype:create-from-project
如下图所示
回车执行,输出日志如下图所示:
然后在上面的命令行输入:cd target/generated-sources/archetype ,如下图所示
接着还是在命令行执行如下命令:
mvn install
查看控制台输出日志,如果有如下图所示情况,则表示成功
同时运行命令将其推到私服上面:
mvn deploy
注意:以上需要maven配置私服地址、才能推得成功
那么在本地你可以运行一下如下命令:
mvn -U archetype:generate
-DarchetypeGroupId=com.tngee.crm \
-DarchetypeArtifactId=crm-demo-archetype \
-DarchetypeVersion=1.0.0-SNAPSHOT \
-Dversion=1.0.0-SNAPSHOT \
-DgroupId=com.tngee.crm \
-DartifactId=mongdb-demo \
-DappName=Mongdb-demo
温馨提示:以上命令请在git bash上面执行,若是在windows shell 请将 \ 去掉,将命令转成一行再执行,请注意保留参数与参数之间得空格。
以上命令解释:DgroupId 需要填你自己得项目包名,DartifactId需要填写你实际得微服务名称。
在bash命令行,按照指令输入即可生成工程。
四、知识点
像这种多模块的脚手架工程构建,需要明白以下几个知识点、如下
上图所示,可以看到archetype-metadata.xml文件,而这个文件是创建脚手架必须的,主要是对骨架的过滤
看到上面:
-
rootArtifactId 占位符会被parent项目的artifactId替换,也就是 上述中的crm-demo
-
${rootArtifactId}也会被parent项目中的artifactId替换。
-
src/main/resources/archetype-resources里必须要有一个顶级pom文件(如果是单工程就是工程pom文件),同时子文件夹代表了模块定义
看看模板工程定义描述文件: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="crm-demo"
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>.idea/inspectionProfiles</directory>
<includes>
<include>**/*.xml</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<directory>.idea</directory>
<includes>
<include>**/*.xml</include>
</includes>
</fileSet>
<fileSet encoding="UTF-8">
<directory>.idea</directory>
<includes>
<include>**/*.gitignore</include>
</includes>
</fileSet>
</fileSets>
<modules>
<module id="${rootArtifactId}-spi" dir="__rootArtifactId__-spi" name="${rootArtifactId}-spi">
<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}-svc" dir="__rootArtifactId__-svc" name="${rootArtifactId}-svc">
<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>
<fileSet encoding="UTF-8">
<directory>src/main/resources</directory>
<includes>
<include>**/*.yaml</include>
</includes>
</fileSet>
<fileSet filtered="true" packaged="true" encoding="UTF-8">
<directory>src/test/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<directory>src/test/resources</directory>
<includes>
<include>**/*.properties</include>
</includes>
</fileSet>
</fileSets>
</module>
</modules>
</archetype-descriptor>
子模块pom.xml定义如下(以crm-demo-svc模块为例)
<?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>${groupId}</groupId>
<artifactId>${rootArtifactId}</artifactId>
<version>${version}</version>
</parent>
<artifactId>${artifactId}</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.tungee.crm</groupId>
<artifactId>crm-common-swagger</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.tungee.crm</groupId>
<artifactId>crm-common-mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.tungee.crm</groupId>
<artifactId>crm-common-job</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>${groupId}</groupId>
<artifactId>${rootArtifactId}-spi</artifactId>
<version>${version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<!-- 将构件信息加入到/actuator/info -->
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
其中${rootArtifactId}就代表父项目的artifactId.
总结
在工作中,我们通常要有“偷懒”意识,通过摸索来开发出类似项目脚手架一样的工具来提升自己工作的效率。不能整天都是浑浑噩噩的过去,而且表面上看上去很难的东西实际上并不见得一定就如想象中的难。程序猿还是要有所追求,哈哈~