Docker使用Nexus部署maven私有倉庫

docker創建一個nexus私服

1.獲取nexus3鏡像

docker pull sonatype/nexus3

1.jpg

2.創建啓動nexus3容器

docker run -dit -p 8081:8081 --name nexus -v /root/nexus-data:/var/nexus-data --restart=always sonatype/nexus3

參數說明

  • -dit:在容器中打開一個僞終端進行交互操作,並在後臺運行
  • -v:把宿主機的/root/nexus-data目錄掛載到容器/var/nexus-data目錄,來實現數據的持久化
  • -p:映射端口,訪問宿主機的8081端口就訪問到nexus容器的服務了
  • --restart=always:這是重啓的策略,假如這個容器異常退出會自動重啓容器
  • --name nexus:創建容器命名爲nexus,你可以隨便命名
  • sonatype/nexus3:pull下來的鏡像

3.訪問nexus
在瀏覽器輸入:http://ip:8081即可看到以下頁面:(ip爲遠程主機的ip地址)
1.jpg

用初始密碼admin/admin123登錄,發現報出:Your admin user password is located in /nexus-data/admin.password on the server.

意思是admin用戶密碼在/nexus-data/admin.password 文件中,那麼接下來我就需要找到這個文件問題就解決了。

進入nexus容器

docker exec -it nexus /bin/bash

使用這個命令找到這個文件地址

find / -name 'admin.password'

1.jpg

拿着上圖這個初始密碼,點擊右上方的Sign in進行登錄
1.jpg

nexus私服簡介

可以看到默認情況下Nexus會幫我們創建了幾個倉庫,仔細觀察紅色框住的地方,裏面有幾種倉庫的類型,解釋如下:

  • proxy 遠程倉庫的代理,比如說nexus配置了一個central repository的proxy,當用戶向這個proxy請求一個artifact的時候,會現在本地查找,如果找不到,則會從遠程倉庫下載,然後返回給用戶。
  • hosted 宿主倉庫,用戶可以把自己的一些倉庫deploy到這個倉庫中
  • group 倉庫組,是nexus特有的概念,目的是將多個倉庫整合,對用戶暴露統一的地址,這樣就不需要配置多個倉庫地址。

下面我們仔細看一下里面的一些倉庫,點擊maven-central倉庫:
1.jpg

可以看到是一個proxy類型的倉庫,默認代理的遠程倉庫地址是https://repo1.maven.org/maven2/,這裏把它改爲阿里雲http://maven.aliyun.com/nexus...

進入maven-public查看
2.jpg
可以看到這是一個group類型的倉庫,裏面包含了maven-releases/maven-snapshots/maven-central倉庫,意思是我們只需要在本地添加這個倉庫,則可以依賴到上述3個倉庫中的庫了。

實現本地上傳代碼庫

1.創建一個新的倉庫(也可以選用已經存在的倉庫)
創建倉庫,點擊Create repository,然後選擇maven2(hosted)然後輸入倉庫名稱(test-release)。在version policy中選擇這個倉庫的類型,這裏選擇release,在Deployment policy中選擇Allow redeploy(這個很重要)
1.jpg

2.修改本地D:Program Files (x86)apache-maven-3.2.3conf目錄下的settings.xml

  <servers>
    <!-- server
     | Specifies the authentication information to use when connecting to a particular server, identified by
     | a unique name within the system (referred to by the 'id' attribute below).
     | 
     | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are 
     |       used together.
     |
    <server>
      <id>deploymentRepo</id>
      <username>repouser</username>
      <password>repopwd</password>
    </server>
    -->
    
    <!-- Another sample, using keys to authenticate.
    <server>
      <id>siteServer</id>
      <privateKey>/path/to/private/key</privateKey>
      <passphrase>optional; leave empty if not used.</passphrase>
    </server>
    -->
    
     <!-- 指定私庫的賬號密碼 -->
     <server>
        <id>releases</id>
        <username>admin</username>
        <password>123456</password>
      </server> 
      
  </servers>

3.用IDEA搭建springboot項目,pom.xml文件內容爲

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>docker</artifactId>
    <name>docker</name>
    <description>Demo project for Spring Boot</description>
    <!--注意限定版本一定爲RELEASE,因爲上傳的對應倉庫的存儲類型爲RELEASE-->
    <version>1.0-RELEASE</version>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <!--指定倉庫地址-->
    <distributionManagement>
        <repository>
            <!--此名稱要和settings.xml中設置的ID一致-->
            <id>releases</id>
            <url>http://139.9.40.41:8081/repository/test-release/</url>
        </repository>
    </distributionManagement>

    <build>
        <plugins>
            <!--發佈代碼Jar插件-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.7</version>
            </plugin>
            <!--發佈源碼插件-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

打開終端,輸入mvn deploy,若報出mvn不是內部命令,可參考
https://blog.csdn.net/sz15732...,最後用管理職身份再次運行IDEA。

上傳成功,回到Nexus的網頁中查看結果
1.jpg

引用依賴

用IDEA新建一個springboot工程,pom.xml使用依賴

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>test</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.example</groupId>
            <artifactId>docker</artifactId>
            <version>1.0-RELEASE</version>
        </dependency>

    </dependencies>

    <repositories>
        <repository>
            <id>releases</id>
            <url>http://139.9.40.41:8081/repository/test-release/</url>
        </repository>
    </repositories>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

最後發現jar依賴成功
1.jpg

參考文獻:https://blog.csdn.net/u012943...

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章