项目即将开始进行重构,准备从以前的单体应用重新重构为基于spring cloud的微服务应用。那么问题来了,如果将各种工具类都冗余到每个微服务中,后期维护就特别难受,想着把工具类、业务模块、数据库访问等抽出来成独立的模块,每个服务去引入即可。基于maven的架构,小编首先想到了nexus,今天就跟着小编一起来玩玩Nexus的搭建及简单实用吧!
Nexus其实是商业收费的,但是它其实也有开源的版本,我们下载Nexus Repository Manager OSS 3.x
一、Nexus下载及安装
- 1、下载地址:
https://www.sonatype.com/download-oss-sonatype
下载完成之后大概就是这个样子的,咱们以windows为例,如下图:
- 2、修改Nexus默认端口号
移步至etc/
目录下,会有一个叫default-nexus.properties
文件,修改其配置项的端口号即可,默认端口号是8081
,配置如下:
application-port=8084
- 3、安装Nexus
Nexus可以在命令行直接启动,也可以被安装成一个服务,cmd
移步至安装目录下,输入nexus.exe -help
,回车如下:
其中install
,uninstall
两个为服务安装与卸载命令,start
,stop
为服务启停命令,我们可以键入nexus.exe install
将nexus安装成为一个windows服务,然后nexus.exe start
将其启动即可。
- 4、打开首页进行配置
如果你的端口没有改动,则默认是8081端口,在浏览器输入http://ip:port/
即可进入首页。如下图:
点击右上角的sign in去登录,Nexus默认有一个账号是admin
,密码是admin123
,进入之后可以修改密码。
登录成功之后,进去这个页面,将中央仓库的远程代理地址改为阿里云镜像地址。
为什么要修改远程代理地址呢?因为默认的远程仓库地址是maven中央仓库,如果遇到网络不好会非常的慢,再者是因为我们搭建了私服,后面项目引入jar包的原理就变成:[项目]->[私服地址]->[阿里云镜像地址]
问题:是不是项目引入jar文件的时候都会走[阿里云镜像地址]
?答案是否定的,只有当你的私服不存在项目需要的jar文件时,私服才会去你配置的代理地址去下载项目需要jar文件。当然了,nexus会备份从远程仓库拉取的包,保存在自己的center仓库中。
二、项目maven配置
以前我们项目没有连接私服的时候,我们本地的settings.xml
文件可能会加上这个配置,提高速度。
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
复制代码
那么,如果配置了,现在请你把它删掉,换成下面的配置:
<mirror>
<id>public</id>
<mirrorOf>central</mirrorOf>
<name>central repository</name>
<url>http://192.168.1.116:8084/repository/maven-public/</url>
</mirror>
复制代码
这样表示会从私服拉取jar文件,当然啦,我们上面在私服配置了远程代理地址,本质上如果私服没有你想要的jar文件的话,还是会去阿里云上下载的。
接着在settings.xml
文件中增加下面配置:
<profile>
<id>nexusProfile</id>
<repositories>
<repository>
<id>nexus</id>
<name>nexus</name>
<url>http://192.168.1.116:8084/repository/maven-central/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
<activeProfiles>
<activeProfile>nexusProfile</activeProfile>
</activeProfiles>
<server>
<id>releases</id> <!-- 记为 id1-->
<username>admin</username>
<password>******</password>
</server>
<server>
<id>snapshots</id> <!-- 记为 id2-->
<username>admin</username>
<password>******</password>
</server>
复制代码
最后在项目的pom.xml
文件中,新增下面配置:
<distributionManagement>
<repository>
<id>releases</id>
<name>User Project Release</name>
<url>http://192.168.1.116:8084/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>User Project SNAPSHOTS</name>
<url>http://192.168.1.116:8084/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
复制代码
此处2个id要和上面的id1、id2保持一致。再运行mvn deploy -e
即可完成将包上传至私服。
三、maven打包插件
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<!-- 生成的jar中,包含pom.xml和pom.properties这两个文件 -->
<addMavenDescriptor>true</addMavenDescriptor>
<manifest>
<!-- 为依赖包添加路径, 这些路径会写在MANIFEST文件的Class-Path下 -->
<addClasspath>true</addClasspath>
<!-- 这个jar所依赖的jar包添加classPath的时候的前缀,如果这个 jar本身和依赖包在同一级目录,则不需要添加 -->
<classpathPrefix>lib/</classpathPrefix>
<!-- jar启动入口类 -->
<mainClass>com.dl.middleware.bocimmid.BocimApplication</mainClass>
</manifest>
</archive>
<!-- jar包的位置 -->
<outputDirectory>${project.build.directory}</outputDirectory>
<includes>
<!-- 打jar包时,只打包class文件 -->
<!-- 有时候可能需要一些其他文件,这边可以配置,包括剔除的文件等等 -->
<include>**/*.class</include>
</includes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>2.1</version>
<configuration>
<attach>true</attach>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
复制代码
四、技巧
- 1、maven子模块
<parent>
标签必须指定<version>
,那么每次子模块版本更新岂不是很麻烦,可以使用mvn versions:set -DnewVersion=0.0.2-SNAPSHOT
进行版本控制