天天看點

做開發這麼久了,還不會搭建伺服器Maven私有倉庫?這也太Low了吧

作者:冰河001

大家好,我是冰河~~

最近不少小夥伴想在自己公司的内網搭建一套Maven私服環境,可自己搭建的過程中,或多過少的總會出現一些問題,問我可不可以出一篇如何搭建Maven私服的文章。這不,就有了這篇文章嘛。

好了,其他的不多說了,接下來,我們就一起來搭建Maven私服環境吧!

環境說明

  • 環境:CentOS 6.x~8.0、 JDK8、 Sonatype Nexus、 Maven
  • IP:192.168.50.131
  • root 使用者操作

安裝Nexus

前提:已安裝 JDK8 并配置好了環境變量,小夥伴們自行搭建JDK8環境,這裡我就不再贅述了。相信小夥伴們都能夠正确搭建JDK8環境。

下載下傳Nexus

下載下傳Nexus(這裡,我使用的是:nexus-2.11.2-03-bundle.tar.gz) ,下載下傳位址:http://www.sonatype.org/nexus/go/ ,我們也可以在伺服器的指令行輸入如下指令下載下傳nexus-2.11.2-03-bundle.tar.gz安裝檔案。

# wget https://sonatype-download.global.ssl.fastly.net/nexus/oss/nexus-2.11.2-03-bundle.tar.gz
           

也可以到連結:https://download.csdn.net/download/l1028386804/12523592 下載下傳

解壓Nexus

# mkdir nexus
# tar -zxvf nexus-2.11.2-03-bundle.tar.gz -C nexus
# cd nexus
# ls
nexus-2.11.2-03 sonatype-work
(一個 nexus 服務,一個私有庫目錄)
           

編輯 Nexus

編輯 Nexus 的 nexus.properties 檔案,配置端口和 work 目錄資訊(保留預設)

# cd nexus-2.11.2-03
# ls
bin conf lib LICENSE.txt logs nexus NOTICE.txt tmp
           

檢視目錄結構, jetty 運作

# cd conf
# vi nexus.properties
# Jetty section
application-port=8081
application-host=0.0.0.0
nexus-webapp=${bundleBasedir}/nexus
nexus-webapp-context-path=/nexus
# Nexus section
nexus-work=${bundleBasedir}/../sonatype-work/nexus
runtime=${bundleBasedir}/nexus/WEB-INF
           

編輯 nexus 腳本,配置 RUN_AS_USER 參數

# vi /usr/local/nexus/nexus-2.11.2-03/bin/nexus
#RUN_AS_USER=
           

改為:

RUN_AS_USER=root
           

防火牆中打開 8081 端口

# vi /etc/sysconfig/iptables
           

添加:

-A INPUT -m state --state NEW -m tcp -p tcp --dport 8081 -j ACCEPT
           

儲存好後重新開機防火牆

# service iptables restart
           

啟動 nexus

# /usr/local/nexus/nexus-2.11.2-03/bin/nexus start
****************************************
WARNING - NOT RECOMMENDED TO RUN AS usr/local
****************************************
Starting Nexus OSS...
Started Nexus OSS.
           

通路nexus

浏覽器中打開:http://192.168.50.131:8081/nexus/

做開發這麼久了,還不會搭建伺服器Maven私有倉庫?這也太Low了吧

登入nexus

預設使用者名admin,預設密碼admin123。

做開發這麼久了,還不會搭建伺服器Maven私有倉庫?這也太Low了吧
做開發這麼久了,還不會搭建伺服器Maven私有倉庫?這也太Low了吧
做開發這麼久了,還不會搭建伺服器Maven私有倉庫?這也太Low了吧

到此, Nexus 已安裝完成, 接下來是 Nexus 的配置

Nexus 配置(登入後)

設定管理者郵箱

菜單 Administration/Server 配置郵箱服務位址(如果忘記密碼,可以通過該郵箱找回密碼)

做開發這麼久了,還不會搭建伺服器Maven私有倉庫?這也太Low了吧

設定使用者郵箱

給使用者配置郵箱位址,友善忘記密碼時找回:

做開發這麼久了,還不會搭建伺服器Maven私有倉庫?這也太Low了吧

使用者修改密碼

做開發這麼久了,還不會搭建伺服器Maven私有倉庫?這也太Low了吧
做開發這麼久了,還不會搭建伺服器Maven私有倉庫?這也太Low了吧
做開發這麼久了,還不會搭建伺服器Maven私有倉庫?這也太Low了吧

倉庫類型

做開發這麼久了,還不會搭建伺服器Maven私有倉庫?這也太Low了吧
  • group 倉庫組:Nexus通過倉庫組的概念統一管理多個倉庫,這樣我們在項目中直接請求倉庫組即可請求到倉庫組管理的多個倉庫;
  • hosted 宿主倉庫:主要用于釋出内部項目構件或第三方的項目構件 (如購買商業的構件)以及無法從公共倉庫擷取的構件(如 oracle 的 JDBC 驅動)proxy 代理倉庫:代理公共的遠端倉庫;
  • virtual 虛拟倉庫:用于适配 Maven 1;

一般用到的倉庫種類是什麼 hosted、 proxy。

Hosted 倉庫常用類型說明:

  • releases 内部的子產品中 release 子產品的釋出倉庫
  • snapshots 釋出内部的 SNAPSHOT 子產品的倉庫
  • 3rd party 第三方依賴的倉庫,這個資料通常是由内部人員自行下載下傳之後釋出上去

如果建構的 Maven 項目本地倉庫沒有對應的依賴包,那麼就會去 Nexus 私服去下載下傳,如果Nexus私服也沒有此依賴包,就回去遠端中央倉庫下載下傳依賴,這些中央倉庫就是 proxy。Nexus 私服下載下傳成功後再下載下傳至本地 Maven 庫供項目引用。

設定 proxy 代理倉庫

設定 proxy 代理倉庫(Apache Snapshots/Central/Codehaus Snapshots)準許遠端下載下傳,如下所示。

做開發這麼久了,還不會搭建伺服器Maven私有倉庫?這也太Low了吧
做開發這麼久了,還不會搭建伺服器Maven私有倉庫?這也太Low了吧
做開發這麼久了,還不會搭建伺服器Maven私有倉庫?這也太Low了吧

Maven 本地庫的安裝與配置

下載下傳Maven

連結http://maven.apache.org/download.cgi 下載下傳Maven

配置Maven環境變量

vim /etc/profile

MAVEN_HOME=/usr/local/maven
JAVA_HOME=/usr/local/jdk
CLASS_PATH=$JAVA_HOME/lib
PATH=$JAVA_HOME/bin:$MAVEN_HOME/bin:$PATH
export JAVA_HOME MAVEN_HOME CLASS_PATH PATH

source /etc/profile
           

配置本地Maven

拷貝Maven的conf目錄下的配置檔案settings.xml,重命名為settings-lyz.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">
 <localRepository>D:/Maven_Repository/.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>deployment</username>
   <password>deployment123</password>
  </server>
  <server> 
   <id>nexus-snapshots</id>
   <username>deployment</username>
   <password>deployment123</password>
  </server>
 </servers>
 
    <mirrors>
 
    </mirrors>
 
 <profiles>
  <profile>
     <id>lyz</id>
       <activation>
                    <activeByDefault>false</activeByDefault>
                    <jdk>1.8</jdk>
                </activation>
       <repositories>
     <!-- 私有庫位址-->
        <repository>
      <id>nexus</id>
      <url>http://192.168.50.131:8081/nexus/content/groups/public/</url>
      <releases>
       <enabled>true</enabled>
      </releases>
      <snapshots>
       <enabled>true</enabled>
      </snapshots>
     </repository>
    </repositories>      
    <pluginRepositories>
     <!--插件庫位址-->
     <pluginRepository>
      <id>nexus</id>
      <url>http://192.168.50.131:8081/nexus/content/groups/public/</url>
      <releases>
       <enabled>true</enabled>
      </releases>
      <snapshots>
       <enabled>true</enabled>
        </snapshots>
     </pluginRepository>
    </pluginRepositories>
   </profile>
 </profiles>
 
 <!--激活profile-->
 <activeProfiles>
  <activeProfile>lyz</activeProfile>
 </activeProfiles>

</settings>
           

其中,配置檔案中的

<localRepository>D:/Maven_Repository/.m2/repository</localRepository>
           

說明本地倉庫位于D:/Maven_Repository/.m2/repository目錄下。

配置檔案中的如下配置項。

<url>http://192.168.50.131:8081/nexus/content/groups/public/</url>
           

與下圖中的連結一緻:

做開發這麼久了,還不會搭建伺服器Maven私有倉庫?這也太Low了吧

配置Eclipse Maven

做開發這麼久了,還不會搭建伺服器Maven私有倉庫?這也太Low了吧
做開發這麼久了,還不會搭建伺服器Maven私有倉庫?這也太Low了吧

配置IDEA Maven

做開發這麼久了,還不會搭建伺服器Maven私有倉庫?這也太Low了吧
做開發這麼久了,還不會搭建伺服器Maven私有倉庫?這也太Low了吧

項目的建構與釋出

首先我們要在項目的pom.xml檔案中加入如下内容,将項目建構成的Jar釋出到Maven私有倉庫

<distributionManagement>
    <repository>
        <id>nexus-releases</id>
        <name>Nexus Release Repository</name>
        <url>http://192.168.50.131:8081/nexus/content/repositories/releases/</url>
    </repository>
    <snapshotRepository>
        <id>nexus-snapshots</id>
        <name>Nexus Snapshot Repository</name>
        <url>http://192.168.50.131:8081/nexus/content/repositories/snapshots/</url>
    </snapshotRepository>
</distributionManagement>
           

配置說明

項目中的pom.xml檔案中,如果版本配置如下:

<version>0.0.1-SNAPSHOT</version>
           

則釋出到Maven私有倉庫後對應的目錄如下:

做開發這麼久了,還不會搭建伺服器Maven私有倉庫?這也太Low了吧

如果版本配置如下:

<version>0.0.1-RELEASE</version>
           

則釋出到Maven私有倉庫後對應的目錄如下。

做開發這麼久了,還不會搭建伺服器Maven私有倉庫?這也太Low了吧

完整pom.xml檔案的配置如下所示。

<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>common-utils-maven</groupId>
    <artifactId>com.chwl.common</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <distributionManagement>
        <repository>
            <id>nexus-releases</id>
            <name>Nexus Release Repository</name>
            <url>http://192.168.50.131:8081/nexus/content/repositories/releases/</url>
        </repository>
        <snapshotRepository>
            <id>nexus-snapshots</id>
            <name>Nexus Snapshot Repository</name>
            <url>http://192.168.50.131:8081/nexus/content/repositories/snapshots/</url>
        </snapshotRepository>
    </distributionManagement>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <jdk.version>1.8</jdk.version>
    </properties>
    <dependencies>
       此處省略....
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.1.2</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
           

具體釋出步驟如下:

右鍵pom.xml->Run as->Maven build->

做開發這麼久了,還不會搭建伺服器Maven私有倉庫?這也太Low了吧
做開發這麼久了,還不會搭建伺服器Maven私有倉庫?這也太Low了吧

上圖中的私有庫為空,我們右鍵pom.xml->Run as->Maven build(此時pom.xml檔案的version為0.0.1-SNAPSHOT)。

建構完畢後

做開發這麼久了,還不會搭建伺服器Maven私有倉庫?這也太Low了吧

說明已經将項目建構并釋出到了我們的Maven私有倉庫。

做開發這麼久了,還不會搭建伺服器Maven私有倉庫?這也太Low了吧

此時,上圖中的Release目錄為空,此時,我們修改pom.xml的version為0.0.1-RELEASE,再次右鍵pom.xml->Run as->Maven build,建構項目,此時釋出的目錄如下圖:

做開發這麼久了,還不會搭建伺服器Maven私有倉庫?這也太Low了吧

最後,我們添加第三方的Jar依賴到我們的Maven私有倉庫,具體操作如下:

如上圖,第三方依賴私有倉庫為空,我們按照以下步驟上傳第三方依賴到我們的Maven私有倉庫。

做開發這麼久了,還不會搭建伺服器Maven私有倉庫?這也太Low了吧
做開發這麼久了,還不會搭建伺服器Maven私有倉庫?這也太Low了吧
做開發這麼久了,還不會搭建伺服器Maven私有倉庫?這也太Low了吧
做開發這麼久了,還不會搭建伺服器Maven私有倉庫?這也太Low了吧

如上圖,第三方依賴已經上傳到我們的Maven私有倉庫。

至此,Maven 私有庫和本地庫的安裝與配置到此結束。

好了,今天就到這兒吧,我是冰河,我們下期見~~

繼續閱讀