本文将详细讲解如何通过repo.osgeo.org和maven.aliyun.com两个平台,搭建一个Maven私服。
一、注册repo.osgeo.org账号
repo.osgeo.org是OSGeo的官方Maven仓库。在搭建私服之前,需要先注册一个账号。
注册链接:http://repo.osgeo.org/webstart/register/register.war
<distributionManagement> <repository> <id>osgeo</id> <url>http://repo.osgeo.org/repository/osgeo/content/</url> </repository></distributionManagement>
二、配置Maven的settings.xml文件
在配置Maven私服之前,需要先配置好Maven的settings.xml文件。下面是一个示例:
<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.0http://maven.apache.org/xsd/settings-1.0.0.xsd"> <servers> <server> <id>osgeo</id> <username>your_osgeo_username</username> <password>your_osgeo_password</password> </server> <server> <id>aliyun</id> <username>your_aliyun_username</username> <password>your_aliyun_password</password> </server></servers> <mirrors> <mirror> <id>aliyun</id> <name>maven.aliyun.com</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>*</mirrorOf> </mirror></mirrors> <profiles> <profile> <id>osgeo</id> <repositories> <repository> <id>osgeo</id> <name>OSGEO Repository</name> <url>http://repo.osgeo.org/repository/osgeo/content/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> </profile> <profile> <id>aliyun</id> <repositories> <repository> <id>central</id> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </profile></profiles><activeProfiles> <activeProfile>aliyun</activeProfile></activeProfiles> </settings>
三、搭建Maven私服
先从maven.aliyun.com下载Maven的二进制安装包。
$ wget https://maven.aliyun.com/repository/public/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.tar.gz
解压tar包:
$ tar -xzvf apache-maven-3.6.3-bin.tar.gz
移动到/usr/local/目录下:
$ mv apache-maven-3.6.3 /usr/local
在/etc/profile文件中添加以下配置:
export MAVEN_HOME=/usr/local/apache-maven-3.6.3export PATH=$PATH:$MAVEN_HOME/bin
重载配置文件:
$ source /etc/profile
创建Maven的本地仓库,例如创建在/home/maven/repo目录下:
$ mkdir -p /home/maven/repo
接下来,我们要使用Nexus搭建Maven私服。在这里,我使用Sonatype Nexus 3版本。
首先下载Sonatype Nexus 3,下载地址:https://www.sonatype.com/download-nexus-repo-oss
解压安装包,并运行bin/nexus run启动。Nexus默认使用8081端口。
访问http://localhost:8081,如果能正常打开网页,则说明Nexus已经启动成功。
Nexus默认用户名和密码均为admin。
接下来,我们要在Nexus中创建一个仓库。
依次点击左侧的“Repositories”->“Create repository”
选择“Maven2 (hosted)”类型的仓库,填写名称和存储路径等信息。
例如名称为“my-maven”,存储路径为“/home/maven/private”。
保存后,我们就创建了一个Maven私服仓库。
四、配置Maven项目的pom.xml文件
在Maven项目的pom.xml文件中,需要配置distributionManagement和repositories等信息。
例如:
<distributionManagement> <repository> <id>my-maven</id> <url>http://localhost:8081/repository/my-maven/</url> </repository></distributionManagement><repositories> <repository> <id>central</id> <url>http://repo.maven.apache.org/maven2</url> </repository> <repository> <id>my-maven</id> <url>http://localhost:8081/repository/my-maven/</url> </repository><repositories>
五、将本地的Maven仓库与私服同步
在本地的Maven仓库中,我们需要同步一些原始的依赖包到私服仓库中。
首先在本地使用Maven构建一个项目,并在仓库中生成相应的依赖包:
$ mvn clean package -Dmaven.test.skip=true
然后使用以下命令将本地的依赖包上传到私服仓库中:
$ mvn deploy:deploy-file -DgroupId=com.groupid \ -DartifactId=com.artifactid \ -Dversion=com.version \ -Dpackaging=jar -Dfile=path-to-jar \ -Durl=http://localhost:8081/repository/my-maven/ \ -DrepositoryId=my-maven
六、使用Maven从私服下载依赖
如果要从私服中下载依赖,需要在Maven项目的pom.xml文件中,添加以下配置:
<repositories> <repository> <id>my-maven</id> <url>http://localhost:8081/repository/my-maven/</url> </repository></repositories>
七、小结
在本文中,我们讲解了如何使用repo.osgeo.org和maven.aliyun.com来搭建Maven私服。通过私服的使用,可以大幅度提高Maven项目的构建速度和部署效率。
原创文章,作者:JKJUY,如若转载,请注明出处:https://www.506064.com/n/373515.html