maven内网库

1. 搭建 Maven 私有仓库

首先,需要在内网环境中搭建一个 Maven 仓库,常用的私有 Maven 仓库工具有:

  • Nexus Repository:Sonatype Nexus 是最流行的私有 Maven 仓库管理工具。
  • Artifactory:JFrog Artifactory 是另一种流行的构建管理工具,提供了私有仓库的支持。
  • Apache Archiva:Apache Archiva 也是一个支持 Maven 的仓库管理工具。

以下是搭建 Nexus Repository 的简单步骤:

1.1 安装 Nexus Repository

  • 下载 Nexus: 访问 Nexus Repository 下载页面 下载 Nexus OSS 版本。

  • 解压并启动: 解压下载的压缩包并启动 Nexus。

1
2
3

cd /opt/nexus/bin
./nexus start
  • 访问 Nexus UI: 打开浏览器,访问 Nexus 的默认地址:
1
http://localhost:8081

默认用户名为 admin,密码为 admin123,可以在首次登录后修改密码。

1.2 配置 Maven 仓库

在 Nexus UI 中,你可以创建一个新的 Maven 仓库。创建仓库后,你可以上传公司的内部依赖、插件和构建工件。

2. 配置 Maven 使用内网仓库

配置 Maven 使用内网仓库需要修改 settings.xml 文件。

2.1 修改 settings.xml

在 Maven 的 settings.xml 文件中,配置私有仓库的地址和认证信息。settings.xml 文件通常位于 ~/.m2/ 目录下(用户级别配置)或者 ${MAVEN_HOME}/conf/ 目录下(全局配置)。

以下是配置内网 Maven 仓库的示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

<mirrors>
<!-- 配置私有仓库镜像 -->
<mirror>
<id>nexus</id>
<mirrorOf>external:http://central</mirrorOf>
<url>http://your-nexus-server:8081/repository/maven-public/</url>
<blocked>false</blocked>
</mirror>
</mirrors>

<repositories>
<repository>
<id>internal-repo</id>
<url>http://your-nexus-server:8081/repository/maven-releases/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>internal-plugins</id>
<url>http://your-nexus-server:8081/repository/maven-plugins/</url>
</pluginRepository>
</pluginRepositories>

<servers>
<!-- 配置 Maven 仓库认证 -->
<server>
<id>nexus</id>
<username>your-nexus-username</username>
<password>your-nexus-password</password>
</server>
</servers>
</settings>

2.2 配置镜像和仓库

  • 镜像(Mirror):在 标签中配置私有仓库的 URL,将 Maven 的中央仓库或其他公共仓库的请求代理到私有仓库中。通过 mirrorOf 配置来选择代理哪些仓库(external:http://central 表示代理所有外部仓库)。

  • 仓库(Repository):在 标签中配置你的内网仓库的 URL。

  • 认证(Server Authentication):在 标签中配置内网仓库的认证信息(如果仓库需要认证)。

3. 配置项目使用内网仓库

在项目的 pom.xml 文件中,通常不需要额外配置仓库,因为 Maven 会使用 settings.xml 中配置的内网仓库。但是如果需要强制指定某个仓库,可以在 pom.xml 中配置 标签:

1
2
3
4
5
6
<repositories>
<repository>
<id>nexus-repo</id>
<url>http://your-nexus-server:8081/repository/maven-releases/</url>
</repository>
</repositories>

4. 上传和下载依赖

4.1 上传依赖到内网仓库
你可以通过 Maven 命令将本地构建的 JAR 文件上传到内网仓库。例如,将某个 JAR 上传到 Nexus:

1
2
3
4
5
6
7
8
mvn deploy:deploy-file \
-DgroupId=com.example \
-DartifactId=my-artifact \
-Dversion=1.0.0 \
-Dpackaging=jar \
-Dfile=path/to/your-artifact.jar \
-DrepositoryId=nexus \
-Durl=http://your-nexus-server:8081/repository/maven-releases/

4.2 从内网仓库下载依赖

配置好内网仓库后,Maven 会自动从内网仓库下载依赖。如果仓库中没有该依赖,Maven 会尝试从其他配置的仓库下载。

5. 使用私有仓库中的依赖

一旦仓库配置好,Maven 将会从配置的内网仓库下载依赖。你可以在项目的 pom.xml 中正常引用依赖,Maven 会自动从私有仓库中拉取。

1
2
3
4
5
6
7
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>my-artifact</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>

6. 配置镜像以提高构建速度(可选)

为了提高构建速度,你可以配置 settings.xml 来使用私有仓库作为 Maven 的默认镜像,确保所有的构建依赖都从私有仓库中拉取,避免每次访问外部仓库,降低构建时间。