Java第四十五天,Maven高级篇(三),私服

一、安装私服

1.下载 Nexus

方法一:到Nexus 官网下载Nexus

https://www.sonatype.com/

方法二:我的分享

https://download.csdn.net/download/ITlanyue/12736638

2.解压到指定目录(无中文、特殊字符)

3.安装

(1)以管理员身份运行 cmd ,进入 nexus 软件的 bin 目录

(2)运行以下命令

nexus.bat install

(3)检查服务是否自动

4.启动与关闭

  • nexus.bat start ===> 启动服务
  • nexus.bat stop ===> 关闭服务

5.卸载

nexus.bat uninstall

6.修改 nexus 服务端口号

通过修改软件 /conf/nexus.properties 配置文件的 application-port 键值修改

7.浏览器访问 nexus UI 界面

浏览器通过以下路径访问(端口默认为 8081 )

http://127.0.0.1:8081/nexus/#welcome

用户名和密码默认如下

  • 用户名:admin
  • 密码:admin123

8.nexus 仓库种类

二、本地仓库与私服的上传下载

1.利用 Maven 项目登录私服

在 maven 软件的 settings.xml 配置文件中的 <servers/> 标签内部配置如下标签

<servers>
    ......
    <server>
        <id>releases</id>
        <username>admin</username>
        <password>admin123</password>
    </server>
    <server>
        <id>snapshots</id>
        <username>admin</username>
        <password>admin123</password>
    </server>
</servers>

2.上传本地项目或模块到私服

(1)在需要上传的子模块的 pom.xml 配置文件中添加如下标签

<distributionManagement>
    <repository>
        <!--id 和 url 需要核对准确-->
        <id>releases</id>
        <url>http://localhost:8081/nexus/content/repositories/releases/</url>
    </repository>
    <snapshotRepository>
        <!--id 和 url 需要核对准确-->
        <id>snapshots</id>
        <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
    </snapshotRepository>
</distributionManagement>

(2)在需要上传的子模块中运行生命周期中的 deploy 生命周期命令

3.从私服下载本地所需的工程或模块

在 maven 的 settings.xml 配置文件中的 <profiles> 标签中添加如下标签

<!-- 下载jar包配置 -->
<profile>
    <!--profile的id -->
    <id>dev</id>
    <repositories>
        <repository> <!--仓库id,repositories可以配置多个仓库,保证id不重复 -->
            <id>nexus</id> <!--仓库地址,即nexus仓库组的地址 -->
            <url>http://localhost:8081/nexus/content/groups/public/</url> <!--是否下载releases构件 -->
            <releases>
                <enabled>true</enabled>
            </releases> <!--是否下载snapshots构件 -->
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories> <!-- 插件仓库,maven的运行依赖插件,也需要从私服下载插件 -->
        <pluginRepository> <!-- 插件仓库的id不允许重复,如果重复后边配置会覆盖前边 -->
            <id>public</id>
            <name>Public Repositories</name>
            <url>http://localhost:8081/nexus/content/groups/public/</url>
        </pluginRepository>
    </pluginRepositories>
</profile>


<!--激活下载-->
<activeProfiles>
    <!--id 需要与上面的 id 对应-->
    <activeProfile>dev</activeProfile>
</activeProfiles>

三、上传第三方 jar 包到私服

1.安装第三方 jar 包到本地仓库

需要提前知道 第三方 jar包 的坐标

// 安装第三方jar包到本地仓库

// 方法一:进入jar包所在目录运行
mvn install:install-file -DgroupId=项目组织名 -DartifactId=项目名 -Dversion=版本号 -Dfile=第三方jar包文件名.jar -Dpackaging=jar

// 方法二:打开cmd直接运行
mvn install:install-file -DgroupId=项目组织名 -DartifactId=项目名 -Dversion=版本号 -Dpackaging=jar -Dfile=第三方jar包路径+文件名.jar

2.安装第三方 jar 包到私服

在 Maven 软件的 settings.xml 配置文件中的 <servers/> 标签中添加如下代码

<server>
    <!--自己私服的第三方包id-->
    <id>thirdparty</id>
    <!--用户名;默认为 admin-->
    <username>admin</username>
    <!--密码;默认为 admin123-->
    <password>admin123</password>
</server>

使用如下命令上传第三方 jar包 到私服

// 进入jar包所在目录运行
mvn deploy:deploy-file -DgroupId=第三方jar包项目组织名 -DartifactId=第三方jar包项目名 -Dversion=第三方jar包项目版本号 -Dpackaging=jar -Dfile=第三方jar包文件名.jar -Durl=http://localhost:8081/nexus/content/repositories/thirdparty/ -DrepositoryId=thirdparty
        
// 打开cmd直接运行
mvn deploy:deploy-file -DgroupId=第三方jar包项目组织名 -DartifactId=第三方jar包项目名 -Dversion=第三方jar包项目版本号 -Dpackaging=jar -Dfile=第三方jar包路径+文件名.jar -Durl=http://localhost:8081/nexus/content/repositories/thirdparty/ -DrepositoryId=thirdparty
原文地址:https://www.cnblogs.com/viplanyue/p/13573664.html