Linux內網環境下使用Nexus配置Maven私有倉庫


一、 安裝配置Nexus

1、 下載nexus

https://www.sonatype.com/download-oss-sonatype 

2、 解壓tar -zxf nexus-3.5.2-01-unix.tar.gz

3、 進入bin目錄啟動:./nexus run &

出現如下界面啟動成功

-------------------------------------------------

Started Sonatype Nexus OSS 3.5.2-01

-------------------------------------------------

4、 訪問http://192.168.0.127:8081/ 可以登錄

 

默認端口號:8081

默認賬號:admin

默認密碼:admin123

5、 配置修改

5.1、修改運行nexus3所使用的用戶:

   [root@bigdata1 bin]#vi nexus.rc

   run_as_user=”root”

5.2、可修改nexus3啟動所使用的jdk版本

   [root@bigdata1 bin]#vi nexus

          INSTALL4J_JAVA_HOME_OVERRIDE=/data/program/software/java8

5.3、可修改nexus3默認端口

    [root@bigdata1 etc]# vi nexus-default.properties

           application-port=8282

5.4、修改nexus3數據以及相關日志的存儲位置

           [root@bigdata1 etc]# vi nexus.vmoptions 

    -XX:LogFile=./sonatype-work/nexus3/log/jvm.log

    -Dkaraf.data=./sonatype-work/nexus3

    -Djava.io.tmpdir=./sonatype-work/nexus3/tmp

二、 修改settings.xml配置,使用nexus私有庫

<?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>${user.home}/.m2/repository</localRepository>

<interactiveMode>true</interactiveMode>

    <offline>false</offline>

    <pluginGroups>

        <pluginGroup>org.mortbay.jetty</pluginGroup>

        <pluginGroup>org.jenkins-ci.tools</pluginGroup>

    </pluginGroups>

 

  <servers>

  <server>    

      <id>nexus-releases</id>    

      <username>admin</username>    

      <password>admin123</password>    

    </server>    

    <server>    

      <id>nexus-snapshots</id>    

      <username>admin</username>    

      <password>admin123</password>    

    </server>    

  </servers>

這是Server的ID(不是登錄進來的user),與Maven想要連接上的repository/mirror中的id元素相匹配。username,password:這兩個元素成對出現,表示連接這個server需要驗證username和password。在nexus中,默認管理員用戶名為admin,密碼為admin123。這里使用兩個服務器配置,分別對應release和snapshot。

#snapshots:開發過程中的版本倉庫

#release:正式發布的版本倉庫

  <mirrors>     

    <mirror>     

      <id>nexus-releases</id>     

      <mirrorOf>*</mirrorOf>     

      <url>http://192.168.0.127:8081/repository/maven-public/</url>     

    </mirror>    

    <mirror>     

      <id>nexus-snapshots</id>     

      <mirrorOf>*</mirrorOf>     

      <url>http://192.168.0.127:8081/repository/maven-snapshots/</url>     

    </mirror>     

  </mirrors>

id,name:唯一的鏡像標識和用戶友好的鏡像名稱。id被用來區分mirror元素,並且當連接時候被用來獲得相應的證書。

mirrorOf:鏡像所包含的倉庫的Id。例如,指向Maven central倉庫的鏡像(http://repo1.maven.org/maven2/),設置這個元素為central。更多的高級映射例如repo1,repo2 或者*,!inhouse都是可以的。沒必要一定和mirror的id相匹配。在這里mirrorOf項當然應該使用*,以表明是所有倉庫都會被鏡像到指定的地址。

url:鏡像基本的URL,構建系統將使用這個URL來連接倉庫。這里應該添nexus倉庫的地址,地址可以在nexus倉庫頁面中找到。

<profiles>    

   <profile>    

      <id>nexus</id>    

      <repositories>    

        <repository>    

          <id>nexus-releases</id>    

          <url>http://nexus-releases</url>    

          <releases><enabled>true</enabled></releases>    

          <snapshots><enabled>true</enabled></snapshots>    

        </repository>    

        <repository>    

          <id>nexus-snapshots</id>    

          <url>http://nexus-snapshots</url>    

          <releases><enabled>true</enabled></releases>    

          <snapshots><enabled>true</enabled></snapshots>    

        </repository>    

      </repositories>    

      <pluginRepositories>    

         <pluginRepository>    

                <id>nexus-releases</id>    

                 <url>http://nexus-releases</url>    

                 <releases><enabled>true</enabled></releases>    

                 <snapshots><enabled>true</enabled></snapshots>    

               </pluginRepository>    

               <pluginRepository>    

                 <id>nexus-snapshots</id>    

                  <url>http://nexus-snapshots</url>    

                <releases><enabled>true</enabled></releases>    

                 <snapshots><enabled>true</enabled></snapshots>    

             </pluginRepository>    

         </pluginRepositories>    

    </profile>    

  </profiles>    

profile項代表maven的基本配置。按照maven的一貫尿性,很多xml的配置項都會有一個配置項的復數形式作為父節點,以保證該配置項可以配置多個。在profiles項中,當然也可以配置多個profile,不過在這里配一個就夠了。下面介紹profile項的各個子節點。

id:用來確定該profile的唯一標識。

repositories/repository:用以規定依賴包倉庫的相關信息。在下屬節點中,id就不用多說了;URL是指倉庫地址,這里使用偽造的地址,否則即使設置了mirror,maven也有可能會直接從中央倉庫下載包;releases和snapshots放在一塊說吧,這兩個節點下屬的enable節點用以規定對應的依賴包是否對當前策略有效,假如將snapshot的enable項設為disable,則不會下載snapshot包。

  <activeProfiles>    

      <activeProfile>nexus</activeProfile>    

  </activeProfiles>    

</settings>

用以規定當前啟用的配置,將對應profile的ID加入到這一項即可使profile生效。

 

三、 上傳jar到nexus

第一種方式:

mvn deploy:deploy-file -DgroupId=com.alibaba -DartifactId=dubbo -Dversion=2.8.4 -Dpackaging=jar -Dfile=/Users/zhangyong/Documents/software/dubbo-2.8.4.jar -Durl=http://192.168.0.127:8081/repository/maven-releases/ -DrepositoryId=nexus-releases

 

DrepositoryId和settings.xml里配置的id一樣

第二種方式:

代碼pom.xml中直接接入

<distributionManagement>  

        <repository>  
            <id>nexus-releases</id>  
            <name>maven-releases</name>  
           <url>http://192.168.0.127:8081/repository/maven-releases/</url>  

        </repository>  

</distributionManagement> 

 

mvn deploy

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM