您的位置:首页 > 其它

使用nexus搭建maven私库并且使用archetype插件创建模板

2017-12-11 22:46 513 查看

使用nexus搭建maven私库并且使用archetype插件创建模板

maven私库搭建以及使用

下载nexus,nexus分为两个版本nexus OSS和nexus pro,nexus pro是收费版本并且提供了更多企业级的支持。下载地址

Java环境安装,nexus3.x 需要至少Java8及以上。

解压下载好的nexus压缩文件。测试运行,进入bin目录

linux:运行 ./nuxus run

windows :运行 nuxus.exe /run

当输出”Started Sonatype Nexus”证明运行成功,并且可以通过”http://localhost:8081访问,修改端口等其他信息可参见第五步。

注册为系统服务由于我是在windows下安装的下面我就只给出windows下的具体步骤,对于linux分为两种具体步骤参见这里。windows下具体步骤如下:

注册系统服务。

nexus.exe /install nexus(服务名称,可在控制面板->管理工具->服务中查看并设置启动规则)

启动服务。

nexus.exe /start nexus(刚刚注册的服务名称)

停止和卸载服务命令

nexus.exe /stop nexus

nexus.exe /uninstall nexus

端口等配置信息配置文件 %neusx_home%/etc/nexus.properties。jvm的一些参数配置文件 %neusx_home%/bin/nexus.vmoptions详细参见这里

在启动完成后访问http://localhost:8081,登录可对仓库进行管理默认用户名:admin 密码:admin123,登录完成后依次点击



可以看到nexus默认已经为我们创建了几个必须的repository,这些repository已经能够满足我们的需求无需自己创建,国内的朋友建议使用阿里云maven仓库修改方式如下





改为这个地址:http://maven.aliyun.com/nexus/content/groups/public/

使用搭建好的maven私库,修改本地maven配置文件。配置文件地址%maven_home%/conf/settings.xml或者User/.m/settings.xml

<servers>
标签之间添加如下内容,为创建项目原型准备如果只是使用则可以不添加此项配置。

<server>

<id>nexus</id>

<username>admin</username>

<password>admin123</password>

</server>


<mirrors>
标签之间添加如下内容,配置使用的远程镜像仓库。下面的id应该和上一步相同。

<mirror>

<id>nexus</id>

<mirrorOf>*</mirrorOf>

<url>http://192.168.1.7:8081/repository/maven-public/</url>

</mirror>


配置profiles。

<profiles>

<profile>

<id>nexus</id>

<repositories>

<repository>

<id>central</id>

<url>http://central</url>

<releases><enabled>true</enabled></releases>

<snapshots><enabled>true</enabled></snapshots>

</repository>

</repositories>

<pluginRepositories>

<pluginRepository>

<id>central</id>

<url>http://central</url>

<releases><enabled>true</enabled></releases>

<snapshots><enabled>true</enabled></snapshots>

</pluginRepository>

</pluginRepositories>

</profile>

</profiles>

<activeProfiles>

<activeProfile>nexus</activeProfile>

</activeProfiles>


将此配置文件共享给其他人,所有人都是可以使用该仓库。

使用archetype插件创建项目原型

使用idea创建一个普通maven项目,在这里我创建多个module利用module分层,如果你只是需要单个module则更简单。

创建子module,如果你想要module名根据不同项目改变就需要统一命名为
__rootArtifactId__
前后是两个英文下划线。下面是我简单创建的几个module。


`

修改最外层的pom.xml文件,将项目可能使用的所有jar包如下图格式配置。这种方式并非真的导入jar包,在子module中真正需要这个jar时再在相应层的pom.xml中添加依赖,在添加时并不需要指定jar包版本号子module默认继承自最外层的module这样可以做到版本的统一管理。



在最外层pom文件中添加archetype插件。

hljs xml">    <plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-archetype-plugin</artifactId>
<version>2.4</version>
</plugin>
</plugins>
</build>


使用archetype插件生成简易项目原型,进入项目根目录执行
mvn archetype:create-from-project
。在target目录会生成以下文件即一个简易的项目原型。



修改archetype目录下的pom文件,添加以下信息。id和maven插件中的sever id相同,URL即maven私库地址。

<distributionManagement>

<repository>

<id>nexus</id>

<name>maven-releases</name>

<url>http://192.168.1.7:8081/repository/maven-releases/</url>

</repository>

<snapshotRepository>

<id>nexus</id>

<name>maven-snapshots</name>

<url>http://192.168.1.7:8081/repository/maven-snapshots/</url>

</snapshotRepository>

</distributionManagement>


编辑archetype->src->main.resources->META-INF.maven->archetype-metadata.xml文件。在idea中默认会包含.idea目录,根据自己项目决定是否保留我建议保留。我的controller层是web层但由于我删除了.idea导致新创建的项目不知道找不到web.xml文件。

编辑module,下面给出一个例子。

<module id="${rootArtifactId}-dao" name="${rootArtifactId}-dao" dir="__rootArtifactId__-dao" >

<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>**/*.xml</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>**/*.xml</include> <
b0dd
br>
<include>**/*.properties</include>

</includes>

</fileSet>

<fileSet encoding="UTF-8">

<directory></directory>

<includes>

<include>release-notes</include>

<include>Readme</include>

</includes>

</fileSet>

</fileSets>

</module>


简单介绍一下这里面的元素,module标签属性id,name,dir既表示原型模板module的属性也表示利用此原型生成项目的module的id,name和dir;默认生成的属性的value不是通配符形式由于在生成项目时${}这种形式无法找到资源位置所以dir属性只能是这样,这也是使用这种奇怪方式命名module的原因,如果创建module是没有采用这种方式dir属性只能是一个和module名相同的字符串利用原型生成的项目module名也是固定的,没怎么说清楚等看完后面自然会有所理解。fileSet标签比较好理解就是指出那些文件是需要包含进原型的,默认如果文件夹中没有文件这不会保留但为了项目文件结构在这里进行配置是很好地解决方案。

发布项目原型到maven私库。
mvn clean deploy
如果maven插件中的
<server>
没用配置好会提示没有权限。

新建项目,添加原型在这里输入你原型的GroupId,ArtifactId,Version和Repostory,对于版本号要是在创建的时候没有改动都是”1.0-SNAPSHOT”; “SNAPSHOT”属于快照待项目稳定后应该改用正式版本号,Repostory是maven仓库地址是可选项。添加完成后就可以在中间的找到你创建的项目原型了,选中,next。



填写GroupId,ArtifactId,注意这的ArtifactId为了和原型区别我加了一个”1”,next next。





填写项目名称,前缀与ArtifactId相同



完成项目创建,可以看待项目名和子module前缀一致这样层次更加分明



以上演示的是在多个module的原型,如果你只需要单个module以分包的形式来区分各层则要简单许多,还有就是对于为什么要使用
__rootArtifactId__
作为前缀来创建子module如果你还是不理解可以参考一下这里
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  maven nexus 插件