1. 程式人生 > >【Maven】---Nexus私服配置Setting和Pom

【Maven】---Nexus私服配置Setting和Pom

maven---nexus私服配置setting和pom

上一遍部落格已經在linux伺服器上,搭建好nexus私服了,部落格地址:Linux搭建Nexus3.X私服

現在就需要配置setting.xmlpom.xml來使nexus作為maven的私服。setting.xml檔案在conf下面,pom.xml是在你建立maven專案中的pom.xml中。

一、將jar傳送到nexus私伺服器

1、建立maven專案

建立一個最簡單的maven專案,然後新建一個工具類,用來測試當把它打成jar包放到私服後,其它專案是否能夠成功引用。

2、pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.jincou</groupId>
    <artifactId>xuxiaoxiao</artifactId>
    <!--SNAPSHOT代表是快照版本-->
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>xuxiaoxiao</name>
    <description>Demo project</description>

    <distributionManagement>
        <repository>
            <!--id的名字可以任意取,但是在setting檔案中的屬性<server>的ID與這裡一致-->
            <id>releases</id>
            <!--指向倉庫型別為host(宿主倉庫)的儲存型別為Release的倉庫-->
            <url>http://47.96.4.110:8081/repository/java-release/</url>
        </repository>
        <snapshotRepository>
            <id>snapshots</id>
            <!--指向倉庫型別為host(宿主倉庫)的儲存型別為Snapshot的倉庫-->
            <url>http://47.96.4.110:8081/repository/java-snapshot/</url>
        </snapshotRepository>
    </distributionManagement>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    
</project>

3、setting.xml配置

在這裡只要配置登陸nexus的使用者名稱密碼,不然沒有使用者名稱和密碼怎麼能將jar包傳送到私服呢。

<!--此處設定的使用者名稱和密碼都是nexus的登陸配置-->
 <servers>
     <server>
      <id>releases</id>  <!--對應pom.xml的id=releases的倉庫-->
      <username>xuxiaoxiao</username>
      <password>xuxiaoxiao123</password>
    </server>
     <server>
      <id>snapshots</id> <!--對應pom.xml中id=snapshots的倉庫-->
      <username>xuxiaoxiao</username>
      <password>xuxiaoxiao123</password>
    </server>
  </servers>

注意maven會判斷版本後面是否帶了-SNAPSHOT,如果帶了就釋出到snapshots倉庫,否則釋出到release倉庫。這裡我們可以在pom.xml檔案中

執行命令:mvn deploy

發現部署到nexus私服成功,我們到私服檢視下,因為這裡的版本是帶SNAPSHOT,所以會發布到snapshots倉庫中。

說明已經成功將jar包釋出到nexus私服中了。那麼下一步是如何引用私服中的jar包了。

二、從nexus引用第三方jar包

讓maven專案使用nexus作為遠端倉庫有兩種方式,第一種是在專案的pom.xml中進行更改,讓單個專案使用nexus倉庫;另一種是通過修改maven的配置檔案settings.xml進行更改,讓所有專案都使用nexus倉庫。我們這裡採取第二種,只需要setting.xml就可以了。還有就是拉取jar的私服倉庫地址只要寫一個java-group就可以了,因為在建立這個組的時候,裡面已經包含了其它三個倉庫。

1、setting.xml (完整版)

<?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">
    <pluginGroups>
  </pluginGroups>
  <proxies>
  </proxies>

 <servers>
    <!--第一個nexus-xu要和下面的mirror中的id一致,代表拉取是也需要進行身份校驗-->
    <server>
      <id>nexus-xu</id>
      <username>xuxiaoxiao</username>
      <password>xuxiaoxiao113</password>
    </server>
     <server>
      <!--這兩個前面講過,是jar上傳時候進行的驗證,id對應的是pom中id屬性的值-->
      <id>releases</id>
      <username>xuxiaoxiao</username>
      <password>xuxiaoxiao113</password>
    </server>
     <server>
      <id>snapshots</id>
      <username>xuxiaoxiao</username>
      <password>xuxiaoxiao113</password>
    </server>
  </servers>

  <mirrors>
    <mirror>
        <id>nexus-xu</id>
        <name>internal nexus repository</name>
        <!--映象採用配置好的組的地址-->
        <url>http://47.96.44.110:8081/repository/java-group/</url>
        <mirrorOf>!internal.repo,*</mirrorOf>
    </mirror>
  </mirrors>

  <profiles>
<profile>
  <!--ID用來確定該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>

    <profile>
  <id>nexus-pr</id>
   <!-- 遠端倉庫列表 -->
  <repositories>
    <repository>
      <id>nexus-xu</id>
      <name>Nexus Central</name>
     <!-- 虛擬的URL形式,指向映象的URL-->
      <url>http://47.96.44.110:8081/repository/java-group/</url>
      <layout>default</layout>
     <!-- 表示可以從這個倉庫下載releases版本的構件-->  
      <releases>
        <enabled>true</enabled>
      </releases>
     <!-- 表示可以從這個倉庫下載snapshot版本的構件 -->  
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    </repository>
  </repositories>
   <!-- 外掛倉庫列表 -->
  <pluginRepositories>
    <pluginRepository>
      <id>nexus-xu</id>
      <name>Nexus Central</name>
      <url>http://47.96.44.110:8081/repository/java-group/</url>
      <layout>default</layout>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
      <releases>
         <enabled>true</enabled>
      </releases>
    </pluginRepository>
  </pluginRepositories>
   </profile>
  </profiles>

  <activeProfiles>
     <!--需要啟用 <profile>中的ID才生效-->  
    <activeProfile>nexus-pr</activeProfile>
    <activeProfile>jdk-1.8</activeProfile>
  </activeProfiles>
</settings>

2、驗證

(1)新建專案新增pom依賴

    <dependencies>
        <dependency>
            <groupId>com.jincou</groupId>
            <artifactId>xuxiaoxiao</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

(2)看是否拉取到私服的jar包

並沒有報錯,表拉取成功

(3)寫測試類

引用成功

(4)看後臺輸出

輸出成功

從這裡將jar包傳送到私服和從私服拉取jar就成功了。

參考

1、maven釋出jar包到nexus
2、讓Maven專案使用Nexus作為遠端倉庫的settings.xml配置
3、Maven 全域性配置檔案settings.xml詳解

如果一個人充滿快樂,正面的思想,那麼好的人事物就會和他共鳴,而且被他吸引過來。同樣,一個人老帶悲傷,倒黴的事情也會跟過來。

                                                  ——在自己心情低落的時候,告誡自己不要把負能量帶給別人。(大校2)