Nexus--Maven私服使用

Nexus安裝

基於docker安裝(內核低起不來)

  1. 下載一個nexus3的鏡像
    docker pull sonatype/nexus3
  2. 將容器內部/var/nexus-data掛載到主機/root/nexus-data目錄。
    docker run -d -p 8081:8081 --name nexus -v /root/nexus-data:/var/nexus-data --restart=always sonatype/nexus3

Nexus介紹

Nexus項目中使用

1.面臨的問題

(1)通過私服下載依賴
(2)如何把項目上傳到私服以提供其他項目的使用

2.正式使用私服之前的準備工作

(1)添加第三方倉庫,例如oschina,類型爲proxy,並且添加到maven-public倉庫組
(2)禁用匿名賬號
(3)將release、snapshot設置爲可重複提交

3.解決問題1——通過私服下載依賴

(1)第一種配置方式(修改maven的setting文件)

(2)第二種配置方式(修改maven的setting文件)

(3)直接配置maven項目的pom文件裏

(4)總結

第二種配置方式比第一種方式的優點:
第二種方式通過profiles配置, 首先從配置的私服倉庫下載,但是如果此時私服宕機,它仍然會從maven遠程倉庫繼續下載依賴。
(因爲第一種配置的是* 代表所以依賴都從私服下載)

4.解決問題2——上傳到私服


配置完後 執行maven的deploy命令

注意

當上傳快照版本(snapshots)時,私服上的jar包名稱後面會自動增加時間戳,其他工程引用該依賴的快照版本時候要注意,(最好直接上傳release版本)

Nexus忘記密碼

可以登入官網查看密碼重置步驟

我的配置及問題

當前maven配置如下:

<?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>E:\maven\repository-3.6.3</localRepository>
    <servers>
        <server>
            <id>baojia</id>
            <username>baojia</username>
            <password>baojia</password>
        </server>
    </servers>
	<mirrors>
        <mirror>
        <id>baojia</id>
        <mirrorOf>*</mirrorOf>
        <name>Nexus baojia</name>
        <url>http://172.16.6.222:8081/repository/maven-public/</url>;
        </mirror>
				<!-- 阿里雲倉庫 -->
		<mirror>
			<id>alimaven</id>
			<mirrorOf>central</mirrorOf>
			<name>aliyun maven</name>
			<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
		</mirror>
		<mirror>
          <id>alimaven</id>
          <name>aliyun maven</name>
          <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
          <mirrorOf>central</mirrorOf>        
        </mirror>
		<!-- 中央倉庫1 -->
		<mirror>
			<id>repo1</id>
			<mirrorOf>central</mirrorOf>
			<name>Human Readable Name for this Mirror.</name>
			<url>http://repo1.maven.org/maven2/</url>
		</mirror>


		<!-- 中央倉庫2 -->
		<mirror>
			<id>repo2</id>
			<mirrorOf>central</mirrorOf>
			<name>Human Readable Name for this Mirror.</name>
			<url>http://repo2.maven.org/maven2/</url>
		</mirror>
    </mirrors>
    <profiles>
        <profile>
            <!--profile的id-->
            <id>dev</id>
            <repositories>
                <repository>
                    <!--倉庫id,repositories可以配置多個倉庫,保證id不重複-->
                    <id>baojia</id>
                    <name>baojia's repository</name>
                    <!--倉庫地址,即nexus倉庫組的地址-->
                    <url>http://172.16.6.222:8081/repository/maven-public/</url>
                    <!--是否下載releases構件-->
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <!--是否下載snapshots構件-->
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
        </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>
<!-- 激活 只寫dev 就行了 jdk那個自動激活了-->
  <activeProfiles>
    <activeProfile></activeProfile>
	
  </activeProfiles>
</settings>

其中micros配置、或者profiles配置都能實現 下載依賴到本地,並且可以從私服上下載。
問題是:如果私服上沒有需要的依賴時候,會從中央倉庫或阿里雲倉庫下載到本地,但是沒有放到私服上。

問題解決:私服的maven-central倉庫默認是沒有聯網的,選上聯網就可以下載了。

下面是依賴上傳私服的pom配置:

	<!-- 配置發佈到私服 -->
	<distributionManagement>
		<repository>
			<id>baojia</id>
			<name>maven-releases</name>
			<url>http://172.16.6.222:8081/repository/maven-releases/</url>
		</repository>
		<snapshotRepository>
			<id>baojia</id>
			<name>maven-snapshots</name>
			<url>http://172.16.6.222:8081/repository/maven-snapshots/</url>
		</snapshotRepository>
	</distributionManagement>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章