1. 程式人生 > >maven中搭建nexus-3.14私服

maven中搭建nexus-3.14私服

一、試驗環境

1、作業系統:Windows 10 
2、nexus版本:nexus-3.14.0-04-win64

Nexus常用功能就是:指定私服的中央地址、將自己的Maven專案指定到私服地址、從私服下載中央庫的專案索引、從私服倉庫下載依賴元件、將第三方專案jar上傳到私服供其他專案組使用。

二、安裝

1、下載地址:http://www.sonatype.com/download-oss-sonatype  
2、我們下載 
nexus-3.14.0-04-win64 後,使用cmd 命令安裝

   進入bin目錄:D:\nexus-3.14.0-04-win64\nexus-3.14.0-04\bin

   安裝:nexus.exe/install  (直接安裝到本機服務中)

   啟動:nexus.exe/start  (或者在管理,服務中,直接啟動,)

 安裝過程報錯: Could not open SCManager.  以管理員身份執行cmd (C:\Windows\System32-搜尋cmd.exe,右鍵用管理員身份開啟)

開啟Nexus服務後訪問url地址http://localhost:8081

使用者名稱密碼分別是:admin/admin123.

三、使用

安裝成功後有兩個預設賬號admin、anonymous,其中admin具有全部許可權預設密碼admin123;anonymous作為匿名使用者,只具有檢視許可權。 

pepositories說明

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


本地maven庫配置settings.xml

<settings>

  <pluginGroups>
    <pluginGroup>org.sonatype.plugins</pluginGroup>
  </pluginGroups>

 <servers>
    <server>
      <id>nexus</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
  </servers>

<mirrors>
    <mirror>
      <id>nexus</id>
      <mirrorOf>*</mirrorOf>
      <url>http://localhost:8081/repository/maven-public/</url>
    </mirror>
    <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>
      <id>nexus</id>
      <repositories>
        <repository>
          <id>central</id>
          <url>http://central</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
      </repositories>
     <pluginRepositories>
        <pluginRepository>
          <id>central</id>
          <url>http://central</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>

  </profiles>
  <activeProfiles>
    <activeProfile>nexus</activeProfile>
  </activeProfiles>
</settings>

工程配置pox.xml

<distributionManagement>
  <repository>
      <id>nexus</id>
      <name>Releases</name>
      <url>http://localhost:8081/repository/maven-releases</url>
    </repository>
    <snapshotRepository>
      <id>nexus</id>
      <name>Snapshot</name>
      <url>http://localhost:8081/repository/maven-snapshots</url>
    </snapshotRepository>
  </distributionManagement>

<build>
    <defaultGoal>compile</defaultGoal>
    <finalName>page</finalName>
    <plugins>
        <plugin> 
            <groupId>org.apache.maven.plugins</groupId> 
            <artifactId>maven-surefire-plugin</artifactId>  
            <configuration>  
                <skip>true</skip>  
            </configuration> 
        </plugin>
        <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
    </plugins>
  </build>
 

編譯到maven私庫

deploy -e 
專案右單擊->Run As->Maven build.. 
進入如下介面 


快照編譯:pom.xml中版本設定

<version>0.0.1-SNAPSHOT</version>
1
編譯後在nexus中看到如下圖結果,快照已經編譯到nexus中Components-> maven-snapshots。 


發行版編譯:pom.xml中版本設定

<version>0.0.1-RELEASE</version>
1
編譯後在nexus中看到如下圖結果,發行版已經編譯到nexus中Components->maven-releases。