使用Docker搭建Nexus(maven)私有仓库,适用于公司小团队开发

Docker的安装  可以看下面这篇文章

docker完全卸载与docker安装_敲代码的李白的博客-CSDN博客

nexus3的安装

拉取nexus3镜像啊

docker pull sonatype/nexus3

 创建容器与宿主机的共享文件夹

  1. mkdir -p /home/nexus/data

  2. chmod 777 -R /home/nexus/data

 启动镜像

docker run -d --name nexus3 -p 8081:8081 --restart always -v /home/nexus/data:/nexus-data sonatype/nexus3

安装完成后打开浏览器,访问 http://1.2.3.4:8080/

查看nexus3的默认密码

cat /home/nexus/data/admin.password

默认仓库说明

maven-central:maven中央库,默认从https://repo1.maven.org/maven2/拉取jar
maven-releases:私库发行版jar,初次安装请将Deployment policy设置为Allow redeploy
maven-snapshots:私库快照(调试版本)jar
maven-public:仓库分组,把上面三个仓库组合在一起对外提供服务,在本地maven基础配置settings.xml或项目pom.xml中使用

 Nexus仓库类型介绍 

hosted:本地仓库,通常我们会部署自己的构件到这一类型的仓库。比如公司的第二方库。
proxy:代理仓库,它们被用来代理远程的公共仓库,如maven中央仓库。
group:仓库组,用来合并多个hosted/proxy仓库,当你的项目希望在多个repository使用资源时就不需要多次引用了,只需要引用一个group即可。

Nexus仓库

 查看组内有哪些库来源

 创建proxy 代理仓库

 选择maven2(proxy),代理仓库

 

 远程仓库地址

  1. 阿里云的maven中央仓库地址:http://maven.aliyun.com/nexus/content/groups/public/

  2. apache的maven中央仓库地址:http://repo.maven.apache.org/maven2/

 

 

 创建hosted   托管仓库

Hosted有三种方式:Releases、Snapshot、Mixed
 
Releases: 一般是已经发布的Jar包
Snapshot: 未发布的版本
Mixed:混合的

 

 

创建group  组仓库

 

 Maven配置

Maven下的setting.xml文件和项目中的pom.xml文件的关系是:settting.xml文件是全局设置,优先使用  而pom.xml文件是局部设置。pom.xml文件中如果没有配置镜像地址的话,就按照settting.xml中定义的地址去查找。

修改本地maven的setting.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<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">
	<!-- 构建系统本地仓库的路径 -->
    <localRepository>D:\temp\apache-maven-3.8.5\repository</localRepository> 
   
   <pluginGroups>
    <!-- pluginGroup
     | Specifies a further group identifier to use for plugin lookup.
    <pluginGroup>com.your.plugins</pluginGroup>
    -->
  </pluginGroups>

<!-- proxies
   | This is a list of proxies which can be used on this machine to connect to the network.
   | Unless otherwise specified (by system property or command-line switch), the first proxy
   | specification in this list marked as active will be used.
   |-->
  <proxies>
 
  </proxies>
 
  <!-- servers
   | This is a list of authentication profiles, keyed by the server-id used within the system.
   | Authentication profiles can be used whenever maven must make a connection to a remote server.
   |-->
   
  <servers>
    <!-- 配置本地仓库访问私服的权限  nexus的 登录用户名密码 -->
      <server>
        <id>My_group</id>
        <username>admin</username>
        <password>123456</password>
      </server>

      <server>
        <id>My_hosted</id>
        <username>admin</username>
        <password>123456</password>
      </server>

      <server>
        <id>My_proxy</id>
        <username>admin</username>
        <password>123456</password>
      </server>
  </servers>
   
      

     <!-- 配置本地仓库资源来源 -->
     <mirrors>
        <mirror>
	      <!--配置仓库组的ID-->
          <id>My_group</id>
	      <!--*代表所有内容都从私服获取-->
          <mirrorOf>*</mirrorOf>
	      <!--私服仓库组maven-public的访问路径-->
          <url>http://1.2.3.4:8080/repository/My_group/</url>
        </mirror>
     </mirrors>
  
  
  
  <profiles>
      <!-- 属性列表配置 -->
      <profile>
        <id>maven_profile</id>
        <properties>
          <maven.compiler.source>1.8</maven.compiler.source>  
          <maven.compiler.target>1.8</maven.compiler.target>  
          <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
        </properties>

        <!-- 远程仓库列表 maven用来填充构建系统本地仓库所使用的一组远程仓库 -->
        <repositories>
          <repository>
            <id>My_proxy</id>
            <url>http://1.2.3.4:8080/repository/My_proxy/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
          </repository>
 
          <repository>
            <id>My_hosted</id>
            <url>http://1.2.3.4:8080/repository/My_hosted/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
          </repository>
 
      </repositories>
 
    <pluginRepositories>
      <pluginRepository>
        <id>My_group</id>
        <url>http://1.2.3.4:8080/repository/My_group/</url>
      </pluginRepository>
    </pluginRepositories>
    
    </profile>
  </profiles>

  
  
 <!-- 激活所使用的配置-->
  <activeProfiles>
   <activeProfile>maven_profile</activeProfile>     
  </activeProfiles>

   
</settings>

修改后可以重新编译项目(pom文件位置cmd),必须添加参数-U,(-U,--update-snapshots,强制更新releases、snapshots类型的插件或依赖库,否则maven一天只会更新一次snapshot依赖)。代理仓库会从远程中央仓库下载jar包

mvn clean compile -U
 

 如果编译失败  一般是项目所用jar包中央仓库没有  手动提交到My_hosted宿主仓库即可

mvn deploy:deploy-file -DgroupId=dm.jdbc.driver -DartifactId=DmDriver7-18 -Dversion=7.6.1.142 -Dpackaging=jar -DpomFile=D:\DmDialect7-18-5.0.pom  -Dfile=D:\DmDialect7-18-5.0.jar -Durl=http://1.2.3.4:8080/repository/My_hosted/ -DrepositoryId=My_hosted

命令解释:

本地仓库目录结构:repository\dm\jdbc\driver\DmDriver7-18\7.6.0.142

-DgroupId=dm.jdbc.driver            自定义   根据本地jar目录结构选择
-DartifactId=DmDriver7-18             自定义   jar包名称
-Dversion=7.6.1.142                自定义   版本号
-Dpackaging=jar               传的类型是jar类型
-Dfile=D:\DmDialect7-18-5.0.jar            jar包的本地磁盘位置

-DpomFile=""D:\DmDialect7-18-5.0.pom""             jar包的Pom文件本地磁盘位置
-Durl=http://1.2.3.4:8080/repository/My_hosted/  hosted资源库的地址
-DrepositoryId=My_hosted         需要和setting.xml文件中配置的ID一致

 上传后的结构

 再次编译就成功了

 

这个时候可以看到代理仓库已经从中央仓库下载了项目编译需要的jar包。同样地,在组仓库中也能看到所有的jar包,包括代理仓库和宿主仓库的。

 

 还有一种方法上传本地jar包到hosted 宿主仓库

 

 上传成功后,就可以看到hosted repository和group repository中已经有了刚上传的三方jar包 

到这里nexus作为私服仓库就配置完毕了  还有一点是deploy部署jar包到私服  这个根据每个人情况选择  目前我这边用不到  等后期大家需要我再做补充。        谢谢大家!  

                                                                                                             2023.3.22@敲代码的李白

猜你喜欢

转载自blog.csdn.net/qq_46264836/article/details/129671876