一文搭建Maven骨架

Maven骨架介绍

Maven archetype

Archetype是一个Maven项目模板工具包,通过Archetype我们可以快速搭建Maven项目,通俗理解就是项目的基础架构,项目初始化的过程。我们在使用Idea开发工具的时候会有如下图操作:
IDEA
在这里选择的Maven进行生成的项目都是利用Maven骨架来进行生成的,通过选择骨架生成项目结构的方式方便了我们手动去搭建相关项目内容。可以直接通过自带骨架或自己构建的骨架进行项目的快速搭建,节约成本。
我们构建一个完整的骨架整体的操作逻辑如下图所示:
Maven archetype
接下来将详细剖析每一步内容。

Maven骨架创建过程

使用archetype创建项目

  1. 第一步建立Maven的项目,其实就是我们想要生成骨架的项目结构。

  2. 在pom.xml中添加插件maven-archetype-plugin。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-archetype-plugin</artifactId>
    <version>2.2</version>
    </plugin>
    </plugins>
  3. 打开命令行进入到项目主目录中,运行如下命令:mvn archetype:create-from-project
    此时会在该项目的target目录下发现如下文件:
    maven骨架目录
    我们可以清晰的发现archetype目录下是我们的项目结构,项目结构下面src/main/resources/META-INF/包含archetype-metadata.xml,这个文件是用来自定义骨架内容的,接下内容中会对信息进行详细的说明。

  4. 进入到target/generated-sources/archetype目录下,使用mvn clean install进行打包到本地文件中。
    这时候你会在maven的本地仓库中多出一个文件archetype-catalog.xml
    archetype-catalog.xml
    打开文件发现这里面有我们添加的Maven骨架信息。
    maven骨架目录

注意:如果想要删除本地的内容就直接删除archetype节点即可。

使用本地Maven骨架

1
2
3
4
5
6
7
8
mvn archetype:generate  
-DinteractiveMode=false 禁止询问,如果不加命令行会询问你填写版本号等信息是否正确。
-DarchetypeCatalog=local 生成项目解析方式,local=本地骨架 internal=内部的 remote=这是Maven中央存储库或其镜像的捷径
-DgroupId=com.jtech.wpark.test 生成项目的groupId
-DartifactId=tetetetetet 生成项目的artifactId
-DarchetypeGroupId=com.jtech 骨架的groupId
-DarchetypeVersion=0.0.1-SNAPSHOT 骨架版本号
-DarchetypeArtifactId=yaoyuan-archetype 骨架的artifactId

archetypeCatalog如果想要来自不同仓库的骨架信息,可以在maven的Setting.xml中设置如下内容:

1
2
3
4
5
6
7
8
9
10
11
<repository>
<id>archetype</id>
<url>https://repository.domain.com/path/to/repo/</url>
</repository>

<!-- in case of a repository with authentication -->
<server>
<id>archetype</id> // 私服的Id
<username>xxxx</username>
<password>xxxx</password>
</server>

如果Maven中央存储库目录文件是空的,则使用内部目录。

Maven私服上传

使用mvn clean deploy对骨架上传到私服中,这里会存在一个问题,也就是上传的地址需要在pom.xml配置下,pom.xml文件路径为target/generated-sources/archetype/pom.xml文件。
配置上传路径
需要对该pomw文件添加distributionManagement节点,节点内容主要是私服地址信息,配置之后使用mvn clean deplod会将骨架上传到私服中去。

使用私服服务器骨架

1
2
3
4
5
6
7
8
9
10
mvn archetype:generate  
-DinteractiveMode=false
-DarchetypeCatalog=internal,remote 类型改变
-DarchetypeRepository=http://xxxxxx:8181/nexus/content/groups/public 私服地址
-DarchetypeGroupId=com.jtech
-DarchetypeArtifactId=yaoyuan-archetype
-DarchetypeVersion=0.0.1-SNAPSHOT
-DgroupId=com.jtech.wpark.test
-DartifactId=tetetetetet
-Dversion=0.1-SNAPSHOT

这里需要注意一下,我们发现修改了远程私服进行构建项目时,指定了Repository为什么没有下载下来反而会出现如下错误信息:
错误信息
解决方案是:
修改maven的setting.xml文件,将文件修改为如下:

  1. 添加repository

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    <profiles>
    <profile>
    <id>snapshot</id>
    <repositories>
    <repository>
    <snapshots />
    <id>my-snapshot</id>
    <name>my-snapshot</name>
    <url>http://127.0.0.1:8181/nexus/content/groups/public/</url>
    </repository>
    </repositories>
    </profile>
    </profiles>
    <activeProfiles>
    <activeProfile>snapshot</activeProfile>
    </activeProfiles>
  2. 添加server

    1
    2
    3
    4
    5
    <server>   
    <id>my-snapshot</id>
    <username>admin</username>
    <password>admin</password>
    </server>

需要添加对私服的repository,以及Server,这里的Server ID和对应的archetype repository的Id是一致的。这样才会在私服中下载下来相应的archetype文件。

自定义骨架内容

当我们按照上面步骤走完之后发现项目中多出了不需要的文件,这时候我们就需要对骨架的内容进行自定义。
多余内容
这里面.idea和yaoyuan.iml文件都不是我们想要的,这时候我们想到之前说过的一个文件archetype-metadata.xml,我们打开这个文件看一下。 (这里的xml指的是target生成的文件目录下的)
xml内容
包含了一堆没用的文件进来了,这时候我们将这些东西删除掉,在mvn install一下。
生成内容

注意:这个文件很重要,主要是如果文件夹为空的时候当我们生成骨架的时候是不会包含当前文件的,你需要对上面xml进行配置包含当前文件即可。

文章目录
  1. 1. Maven骨架介绍
    1. 1.1. Maven archetype
  2. 2. Maven骨架创建过程
    1. 2.1. 使用archetype创建项目
  3. 3. 使用本地Maven骨架
  4. 4. Maven私服上传
  5. 5. 使用私服服务器骨架
  6. 6. 自定义骨架内容
|
载入天数...载入时分秒...