1.为什么使用nexus
如果没有私服,我们所需的所有构件都需要通过maven的中央仓库和第三方的Maven仓库下载到本地,而一个团队中的所有人都重复的从maven仓库下载构件无疑加大了仓库的负载和浪费了外网带宽,如果网速慢的话,还会影响项目的进程。很多情况下项目的开发都是在内网进行的,连接不到maven仓库怎么办呢?开发的公共构件怎么让其它项目使用?这个时候我们不得不为自己的团队搭建属于自己的maven私服,这样既节省了网络带宽也会加速项目搭建的进程,当然前提条件就是你的私服中拥有项目所需的所有构件,其次稍微大型公司都有自己的内部架构组件,这些jar包不会被开放,放在nexus也是个不错的选择
2. nexus安装
2.1 安装环境
软件环境 | 简介 | 版本 | RAM |
---|---|---|---|
centos | Linux操作系统 | 7.8 | 8G |
docker | docker虚拟化环境 | 20.10.6 | |
docker-compose | docker容器编排 | 1.29.1 |
2.2 安装nexus应用
- 新建nexus目录
mkdir nexus
cd nexus
touch docker-compose.yaml
vim docker-compose.yaml
复制代码
- 配置docker-compose.yaml文件
version: "3.7"
services:
nexus:
image: sonatype/nexus3
container_name: nexus
environment:
- "INSTALL4J_ADD_VM_PARAMS=-Xms1g -Xmx1g -XX:MaxDirectMemorySize=1g"
ports:
- 8764:8081
volumes:
- ./data:/nexus-data
restart: always
复制代码
注意事项
- INSTALL4J_ADD_VM_PARAMS:设置nexus占用的内存,默认是2g
- 运行docker-compose文件
docker-compose up -d
复制代码
看见done,说明安装完成
2.3 连接nexus
访问http//:127.0.0.1:8764出现nexus可视化页面
默认账户是:admin 密码:admin
2.4设置maven私库连接
我们设置国内的阿里云maven源,通过走nexus进行代理阿里云maven源
- 设置阿里云仓库代理
- 新建仓库,选择maven2(proxy),进入新增仓库页面
- 设置名称,代理源地址,版本类型
阿里云maven仓库地址:maven.aliyun.com/nexus/conte…
- 新建完如图所示
- 按照上述步骤,设置maven仓库代理源
- 阿里云maven仓库和中央仓库源设置完成后,再继续设置默认的maven-public仓库
- 到此基本的nexus的仓库设置结束
3. 使用nexus仓库
3.1 修改maven目录中的settings.xml文件
如图所示
- 找到localRepository标签,设置你需要本地存储的maven仓库目录,也可以默认不设置
2. 找到servers节点标签,设置nexus的账户和密码,这边是需要设置,因为maven项目需要读取server节点的账户和密码进行nexus的数据连接
<servers>
<server>
<id>maven-releases</id>
<username>admin</username>
<password>123456</password>
</server>
<server>
<id>maven-snapshots</id>
<username>admin</username>
<password>123456</password>
</server>
</servers>
复制代码
- 找到mirrors节点标签,设置maven镜像源,这边就是设置maven仓库的镜像地址
<mirrors>
<mirror>
<id>nexus-public</id>
<mirrorOf>central</mirrorOf>
<name>central repository</name>
<url>http://27.0.0.1:8764/repository/maven-public/</url>
</mirror>
</mirrors>
复制代码
设置的依据看maven-public仓库连接地址:
3.2 设置maven项目的pom.xml文件
3.2.1 内部上传组件pom结构
- 添加distributionManagement节点
<distributionManagement>
<repository>
<id>maven-releases</id>
<url>http://127.0.0.1/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>maven-snapshots</id>
<url>http://127.0.0.1:8764/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
复制代码
id标签对应之前的server标签里面的id字段
2. 添加上传nexus配置
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- resources资源插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<delimiters>
<delimiter>@</delimiter>
</delimiters>
<useDefaultDelimiters>false</useDefaultDelimiters>
<encoding>UTF-8</encoding>
<!-- 后缀为pem、pfx的证书文件 -->
<nonFilteredFileExtensions>
<nonFilteredFileExtension>pem</nonFilteredFileExtension>
<nonFilteredFileExtension>pfx</nonFilteredFileExtension>
<nonFilteredFileExtension>p12</nonFilteredFileExtension>
<nonFilteredFileExtension>key</nonFilteredFileExtension>
<nonFilteredFileExtension>jks</nonFilteredFileExtension>
<nonFilteredFileExtension>db</nonFilteredFileExtension>
<nonFilteredFileExtension>txt</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
<!--配置生成源码包 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<!-- 编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<!-- 资源插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
</plugin>
<!--配置生成源码包 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<!-- 打包的 -P参数 -->
<id>release</id>
<build>
<plugins>
<!-- Source -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Javadoc -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
</plugins>
</build>
<distributionManagement>
<repository>
<id>maven-releases</id>
<url>http://127.0.0.1:8764/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>maven-snapshots</id>
<url>http://127.0.0.1:8764/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
复制代码
snapshotRepository和repository设置你需要上传的仓库地址:
- 到此,上传jar包到maven私库nexus的配置完成,点击deploy试试吧
3.2.2 使用nexus maven pom设置
新增repositories节点,连接nexus特定的仓库
<repositories>
<repository>
<id>maven-releases</id>
<url>http://127.0.0.1:8764/repository/maven-releases/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>maven-snapshots</id>
<url>http://127.0.0.1:8764/repository/maven-snapshots/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
复制代码
到此maven私库就可以正常使用了
4 总结
maven私库在日常开发中占据着很大的作用,希望我的这边文章能给小白们少一点踩坑,更好地提高工作效率,愉快的摸鱼~