Maven私服搭建並配置

目錄

 

私服簡介

私服的好處

使用docker搭建私服

添加阿里雲私服代理

配置settings.xml

配置pom.xml文件

測試


私服簡介

私服是一種特殊的遠程倉庫,它是架設在局域網內的倉庫服務,私服代理廣域網上的遠程倉庫,供局域網內的用戶使用。當Maven需要下載構件的時候,它從私服請求,如果 私服上不存在該構件,則從外部遠程倉庫下載,緩存在私服上之後,再爲Maven的下載請求提供服務。

私服的好處

a、節省自己的外網帶寬

b、加速Maven構建

c、部署自己內部的第三方構件

d、提高穩定性,增強控制

e、降低中央倉庫的負荷。

使用docker搭建私服

一  下載鏡像

docker pull sonatype/nexus3

二 創建數據目錄

mkdir -vp /opt/nexus/nexus-data && chown -R 200 /opt/nexus/nexus-data

三 啓動容器

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

訪問 http://ip:8081,可以看到如下頁面,用戶名admin,初始密碼再容器 /nexus-data/admin.password 裏面

可以使用docker exec -it nexus /bin/bash 進入容器,獲取登錄密碼。

登錄進去倉庫管理頁。我們可以看到maven2有三種倉庫類型。

1 proxy。proxy代理中央倉庫,它可以幫我們從遠程倉庫下載包到本地倉庫(可額外添加阿里雲maven私服作爲代理)。

2 hosted。本地倉庫,用來存儲包。有測試版和正式版

3 group。自定義倉庫組合,可以組合本地倉庫,代理倉庫。在配置文件中的鏡像來源可以配置此倉庫,這樣就相對於配置了本地倉庫和代理倉庫了。

因爲我們上傳的包都是在hosted倉庫,所以我們需要在hosted這兩個倉庫配置可以重新部署包的方式,這樣重新打包就不會報400錯誤

 

添加阿里雲私服代理

如果直接從maven中央倉庫拉取,由於網絡原因,會顯得非常慢,而且一些jar包中央倉庫也可能不全,所以我們可以添加國內的maven私服作爲代理。這裏,我們使用阿里雲maven私服添加額外的代理下載。

點擊 Create repository 添加倉庫

Remote storage 填寫阿里雲的私服倉庫地址,其它配置項跟中央倉庫配置一樣。

配置完成後,我們還要把此倉庫拉入倉庫組中(group),這樣就可以同時使用中央倉庫和阿里雲私服倉庫了。

 

配置settings.xml

將url改成自己私服的地址,密碼設置你改好的密碼

<?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">

  <activeProfiles>
    <activeProfile>xxxxProfile</activeProfile>
	<activeProfile>jdk-1.8</activeProfile>
  </activeProfiles>

  <localRepository>D:\maven_repository</localRepository>
  
  <servers>
	<!--maven私服賬號,用於上傳jar包到私服認證使用
		注意,項目的pom.xml文件的
		
    <distributionManagement>
        <repository>
            <id>releases</id>
            <name>releases Repository</name>
            <url>http://nexus.xxxx.info/repository/maven-releases/</url>
        </repository>

        <snapshotRepository>
            <id>snapshots</id>
            <url>http://nexus.xxxx.info/repository/maven-snapshots/</url>
            <uniqueVersion>true</uniqueVersion>
        </snapshotRepository>
    </distributionManagement>
	
	id必須於此保存一致,否則無法通過id找到對應的認證賬號信息
	-->
	<server>
		<id>releases</id>
		<username>admin</username>
		<password>xxxx</password>
	</server>
	<server>
		<id>snapshots</id>
		<username>admin</username>
		<password>xxxx</password>
	 </server>
  </servers>
  


  <mirrors>
	<mirror>
 	<id>releases</id>
	<mirrorOf>*</mirrorOf>
	<name>Nexus private</name>
        <!--配置倉庫爲group類型的私服倉庫地址-->
	<url>http://nexus.xxxx.info/repository/maven-public/</url>
	</mirror>
  </mirrors> 

  

  <profiles>
    <profile>
      <id>zhaodaoProfile</id>
      <properties>
        <encoding>UTF-8</encoding>
        <project.build.sourceEncoding>${encoding}</project.build.sourceEncoding>
        <project.reporting.outputEncoding>${encoding}</project.reporting.outputEncoding>
      </properties>
      <repositories>
		<repository>
			<id>nexus</id>
			<name>private nexus</name>
                        <!--配置倉庫爲group類型的私服倉庫地址-->
			<url>http://nexus.xxxx.info/repository/maven-public/</url>
			<releases>
				<enabled>true</enabled>
			</releases>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>  
			<id>nexus</id>
			<name>local private nexus</name>
                        <!--配置倉庫爲group類型的私服倉庫地址-->
			<url>http://nexus.xxxx.info/repository/maven-public/</url>
			<releases>
				<enabled>true</enabled>
			</releases>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</pluginRepository>
      </pluginRepositories>
    </profile>
	<profile>
		<id>jdk-1.8</id>
		<activation>
			<activeByDefault>true</activeByDefault>
			<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>


</settings>

配置pom.xml文件

在項目的pom.xml文件中,配置如下

    <distributionManagement>
        <repository>
            <id>releases</id>
            <name>releases Repository</name>
            <url>http://nexus.xxxx.info/repository/maven-releases/</url>
        </repository>

        <snapshotRepository>
            <id>snapshots</id>
            <url>http://nexus.xxxx.info/repository/maven-snapshots/</url>
            <name>snapshots Repository</name>
        </snapshotRepository>
    </distributionManagement>

 我們可以通過idea測試一下,是否可以打包成功。

測試

通過idea自帶的插件,依次點擊 clean、deploy。就可把本地的包上傳到我們的私服上去

 可以看到,我們的包已經上傳到私服了。

 

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