在上一章节中,重点介绍了如何搭建nexus,搭建成功之后关键在于如何运用到项目中,下面将结合实际项目做简单介绍。
windows下搭建nexus仓库:http://blog.csdn.net/hh12211221/article/details/74010609
在pom文件中配置nexus仓库
- <repositories>
- <!-- 配置nexus远程仓库 -->
- <repository>
- <id>nexus</id>
- <name>Nexus Snapshot Repository</name>
- <url>http://127.0.0.1:8088/nexus/content/groups/public/</url>
- <releases>
- <enabled>true</enabled>
- </releases>
- <snapshots>
- <enabled>false</enabled>
- </snapshots>
- </repository>
- </repositories>
- <!-- 配置从哪个仓库中下载构件,即jar包 -->
- <pluginRepositories>
- <pluginRepository>
- <id>nexus</id>
- <name>Nexus Snapshot Repository</name>
- <url>http://127.0.0.1:8088/nexus/content/groups/public/</url>
- <releases>
- <enabled>true</enabled>
- </releases>
- <snapshots>
- <enabled>false</enabled>
- </snapshots>
- </pluginRepository>
- </pluginRepositories>
配置setting.xml文件
setting.xml文件的位置:../apache-maven-3.3.3/conf/setting.xml。
1、在setting.xml文件中配置nexus仓库,如下所示:
- <profile>
- <id>development</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>
注:
在配置远程仓库时,中央仓库的id为central,远程url地址为http://repo.maven.apache.org/maven2。
在setting.xml中配置nexus仓库主要考虑到这样的情侣:若在一个公司内部,同时进行多个项目,随之这几个项目都相继结束,而越来越多的项目会开始,为避免在每一个项目中都配置相同的配置文件,所以我们提供一个统一的仓库,供所有项目使用,这样就可以提高代码的复用性,避免做没有必要的无用功!
2、激活profile
- <activeProfiles>
- <activeProfile>development</activeProfile>
- </activeProfiles>
3、配置镜像mirror
- <mirrors>
- <mirror>
- <id>nexus</id>
- <mirrorOf>*</mirrorOf>
- <name>Nexus Mirror</name>
- <url>http://127.0.0.1:8088/content/groups/public/</url>
- </mirror>
- </mirrors>
部署构件到nexus仓库
1、在pom文件中添加如下配置
- <distributionManagement>
- <repository>
- <id>archiva.releases</id>
- <name>Internal Releases</name>
- <url>http://127.0.0.1:8088/nexus/content/repositories/releases/</url>
- </repository>
- <snapshotRepository>
- <id>development</id>
- <name>Internal Snapshots</name>
- <url>http://127.0.0.1:8088/nexus/content/repositories/snapshots/</url>
- </snapshotRepository>
- </distributionManagement>
- <server>
- <id>development</id>
- <username>admin</username>
- <password>admin23</password>
- </server>
- <server>
- <id> archiva.releases </id>
- <username> admin </username>
- <password>admin23</password>
- </server>
nexus仓库默认用户名:admin;密码:admin123
3、执行部署
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>1.2.14</version>
- </dependency>
如果之前没用执行过该命令,maven会自动到中央仓库中下载部署所需的插件。最后在命令行中看到如下所示就代表构件已经部署成功。
到nexus的releases仓库中即可查看刚刚部署好的构件信息。