1. 程式人生 > >Docker安裝Nexus搭建Maven私服、部署引用jar包

Docker安裝Nexus搭建Maven私服、部署引用jar包

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

注意點:若出現許可權問題

chmod 777 nexus-data

記憶體佔用情況:image.png

1、歡迎頁(預設賬號:admin 預設密碼:admin123)

image.png

2、倉庫介紹

image.png

proxy:

是遠端倉庫的代理。比如說在nexus中配置了一個central repository的proxy,當用戶向這個proxy請求一個artifact,這個proxy就會先在本地查詢,如果找不到的話,就會從遠端倉庫下載,然後返回給使用者,相當於起到一箇中轉的作用

Hosted:

是宿主倉庫,使用者可以把自己的一些構件,deploy到hosted中,也可以手工上傳構件到hosted裡。比如說oracle的驅動程式,ojdbc6.jar,在central repository是獲取不到的,就需要手工上傳到hosted裡

Group:

是倉庫組,在maven裡沒有這個概念,是nexus特有的。目的是將上述多個倉庫聚合,對使用者暴露統一的地址,這樣使用者就不需要在pom中配置多個地址。

maven-central:maven中央庫,預設從[https://repo1.maven.org/maven2/](https://repo1.maven.org/maven2/)拉取jar 
maven-releases:私庫發行版jar 
maven-snapshots:私庫快照(除錯版本)jar 
maven-public:倉庫分組,把上面三個倉庫組合在一起對外提供服務,在本地maven基礎配置settings.xml中使用。

3、部署jar包到私服

在setting.xml新增對應的使用者名稱密碼

 <servers>
    <server>
      <id>nexus-snapshots</id>    
      <username>username</username>
      <password>password</password>
    </server>
  </servers>

在pom檔案中新增

    <distributionManagement>
        <snapshotRepository>
            <!--此名稱要和settings.xml中設定的ID一致-->
            <id>nexus-snapshots</id>
            <name>nexus-snapshots-name</name>
            <url>http://ip:8081/repository/maven-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>

4、從私服中引用自己部署上傳的jar包

在setting.xml檔案中指定私服映象

<mirrors>

     <mirror>
        <!--該映象的唯一識別符號。id用來區分不同的mirror元素。  -->
        <id>my-nexus</id>
        <!--此處配置所有的構建均從私有倉庫中下載 *代表所有,也可以寫central -->
        <mirrorOf>*</mirrorOf>
        <name>central repository</name>
        <!--該映象的URL。構建系統會優先考慮使用該URL,而非使用預設的伺服器URL。  -->
        <url>http://ip:8081/repository/maven-public/</url>
    </mirror>
		<mirror>
			<id>alimaven</id>
			<name>aliyun maven</name>
			<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
			<mirrorOf>central</mirrorOf>
		</mirror>
  </mirrors>

配置倉庫

<profiles>
        <profile>
            <id>nexus</id>
            <!--遠端倉庫列表,它是Maven用來填充構建系統本地倉庫所使用的一組遠端專案。  -->
            <repositories>
                <!--釋出版本倉庫-->
                <repository>
                    <id>nexus</id>
                    <!--name隨便-->
                    <name>Nexus Release Snapshot Repository</name>
                    <!--地址是nexus中repository(Releases/Snapshots)中對應的地址-->
                    <url>http://47.105.49.228:8081/repository/maven-releases/</url>
                    <!--true或者false表示該倉庫是否為下載某種型別構件(釋出版,快照版)開啟。 -->
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
            <!--發現外掛的遠端倉庫列表。倉庫是兩種主要構件的家。第一種構件被用作其它構件的依賴。這是中央倉庫中儲存的大部分構件型別。另外一種構件型別是外掛。-->
            <!--各節點的含義和repository是一樣的-->
            <pluginRepositories>
                <pluginRepository>
                    <id>nexus</id>
                    <name>Nexus Release Snapshot Repository</name>
                    <url>http://47.105.49.228:8081/repository/maven-snapshots/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </pluginRepository>
            </pluginRepositories>
        </profile>
    <!--設定maven編譯器級別-->
        <profile>
            <id>jdk18</id>
            <activation>
                <!--profile預設是否啟用的標識 -->
                <activeByDefault>true</activeByDefault>
                <!--activation有一個內建的java版本檢測,如果檢測到jdk版本與期待的一樣,profile被啟用。 -->
                <jdk>1.8</jdk>
            </activation>
            <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>
        </profile>
  </profiles>

啟用配置

 <!--啟用配置-->
    <activeProfiles>
        <!--profile下的id-->
        <activeProfile>nexus</activeProfile>
    </activeProfiles>