Maven archetype快速搭建脚手架工程并部署到私有nexus

前言

通过maven的archetype配置一套通用的开发脚手架工程,然后分别部署到本地和公司nexus私有仓库

创建archetype

假设目前已有一个maven的模板工程,对该模板工程创建一个archetype模板

maven工程树如下:

image

在项目的根目录下(和最下面的pom文件同级),运行


mvn archetype:create-from-project

此时,会在根目录下生成target文件夹,如图:

image

生成archetype模板

创建了archetype之后,还需要生成该模板

进入到archetype的根目录, 并部署到本地仓库


cd ./target/generated-sources/archetype

mvn clean install

如果需要部署到私有nexus服务器上,需要在target文件夹下的pom.xml里配置私有仓库地址,如


maven-releases

http://company-private-maven.url/repository/maven-releases/

false

maven-snapshots

http://company-private-maven.url/repository/maven-snapshots/

false

然后运行:


mvn deploy

如果需要查看archetype的骨架配置文件,在该目录下运行:


mvn archetype:crawl

会在本地仓库的根目录下生成一个archetype-catalog.xml文件,即为骨架配置文件,如:


<archetype-catalog xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0 http://maven.apache.org/xsd/archetype-catalog-1.0.0.xsd"

xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

org.apache.maven.archetypes

maven-archetype-quickstart

1.1

quickstart

com.aa.bb

aa-bb-cc-archetype

0.0.1-RELEASE

aa-bb-cc

使用archetype模板

有两种使用方法,一种是通过命令行生成maven项目,一种是直接通过IDEA生成

通过命令行生成maven项目

运行:


mvn archetype:generate -DarchetypeCatalog=local

选择2,ezia-connectivity-common-archetype,接着根据交互提示,输入groupId, artifactId, version以及package。

【避坑】这里在创建项目的时候,会去指定的archetype-catalog.xml文件中根据groupId,version等参数,依照优先级:本地maven仓库>settings.xml里profile指定的repo>pom文件中repo>settings.xml中的mirror,依次寻找对应的jar包。

如果,在根据archetype创建工程失败,很有可能的原因:

  • 1,本地没有对应的jar包,

  • 2,settings.xml配置的nexus仓库地址不正确,

  • 3,寻包优先级配置不对。

本地settings.xml文件优先级设置,该配置作用:如果本地maven仓库没有找到jar包,就从profile里的http://company-private-maven.url/repository/maven-releases/http://company-private-maven.url/repository/maven-snapshots/的私有仓库里去找对应的jar包,如果还是没有找到,那么会从项目的pom文件里配置nexus地址找,最后会通过mirror里aliyun的maven仓库的central里面找。

【避坑】 mirrorOf字段不能填*,否则mirror的仓库地址优先级最高且唯一。


...

alimaven

central

aliyun maven

http://maven.aliyun.com/repository/central/

ma-nexus

maven-releases

xx-nexus-sonatype-nexus-releases

http://company-private-maven.url/repository/maven-releases/

maven-snapshots

xx-nexus-sonatype-nexus-snapshots

http://company-private-maven.url/repository/maven-snapshots/

ma-nexus

...

image

通过IDEA生成maven项目

file->new,然后如图操作:

image
image
image

参考文档

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。