linux 上 maven 和 nexus3 的詳細過程

背景介紹

之前因爲做新技術研究,短暫的玩過 nexus3 兩天,基於瞭解也寫過一篇 nexus3 搭建的簡述,本以爲後續要用的時候可以基於簡述文檔快速上手。結果等到實際工作中,我個人需要完整的設計和搭建一套可用的私服用於公司開發,回過頭看文檔才發現,之前的瞭解及文檔難以起到有效幫助,故在原文上重新編輯一份。同時也驚醒自己,做技術研究,不說吃透,但一定要有可見可使用的 DEMO。沒有達到實際的效果,一切研究都是泡沫。

爲何要自己要在 linux 上搭建 maven 和 nexus 好的文章

在 linux 上搭建 maven 個人暫時覺得沒有必要,因爲 maven 一般在本地開發環境使用。可能一些發佈集成平臺提供集成 maven 來共享 jar 包。
在 linux 上搭建 nexus 提供隸屬於自己的 私服倉庫。暫時主要用於管理 個人或組織的私有 jar 包。

前言強調

  • jar 包上傳到私服 和 從私服下載 jar 包是兩套互不干擾的動作。故分開學習和實現 demo 比較好
  • jar 包上傳和 下載 jar 包都可以在 maven 的 setting.xml 文件中或者工程的 pom.xml 文件中實現。考慮到集中修改共用的優點,我選擇在 maven setting.xml 中集中配置。 PS : jar 包上傳 pom.xml 必須涉及修改
  • setting.xml 文件中 涉及很多 id 的配置。我之前很糾結這些 id 是否一定和 nexus3 上倉庫的 id(name) 一一對應。後經過驗證。除了 鏡像路徑的URL 及 賬號 密碼要對應的上,其他都可以自己有設置。setting.xml 中 name 的設置就更不用說了,隨意。

工具介紹

  • idea
  • nexus3
  • maven

配置過程詳解

maven 父工程不可缺少

  1. maven 工程 pom.xml 標籤詳解
  2. maven 工程 依賴、繼承關係詳解
    之所以要定義父工程。
    其一是因爲一般 maven 工程架構設計,都會有
    <packaging>pom</packaging>
    的父工程。用於統一維護各 jar 包的版本。
    其二是可以把上傳 jar 的一些統一操作 build 或其他 plug 的配置寫在父工程,其子工程可以統一繼承。如下:
    <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.taotao</groupId>
    <artifactId>taotao-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    
    <!-- 集中定義依賴版本號 -->
    <properties>
        <junit.version>4.12</junit.version>
        <spring.version>4.1.3.RELEASE</spring.version>
        <mybatis.version>3.2.8</mybatis.version>
        <mybatis.spring.version>1.2.2</mybatis.spring.version>
        <mybatis.paginator.version>1.2.15</mybatis.paginator.version>
        <mysql.version>5.1.32</mysql.version>
        <slf4j.version>1.6.4</slf4j.version>
        <jackson.version>2.4.2</jackson.version>
        <druid.version>1.0.9</druid.version>
        <httpclient.version>4.3.5</httpclient.version>
        <jstl.version>1.2</jstl.version>
        <servlet-api.version>2.5</servlet-api.version>
        <jsp-api.version>2.0</jsp-api.version>
        <joda-time.version>2.5</joda-time.version>
        <commons-lang3.version>3.3.2</commons-lang3.version>
        <commons-io.version>1.3.2</commons-io.version>
        <commons-net.version>3.3</commons-net.version>
        <pagehelper.version>3.4.2</pagehelper.version>
        <!--		<pagehelper.version>5.1.11</pagehelper.version>-->
        <jsqlparser.version>0.9.1</jsqlparser.version>
        <commons-fileupload.version>1.3.1</commons-fileupload.version>
        <jedis.version>2.7.2</jedis.version>
        <solrj.version>4.10.3</solrj.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <!-- 時間操作組件 -->
            <dependency>
                <groupId>joda-time</groupId>
                <artifactId>joda-time</artifactId>
                <version>${joda-time.version}</version>
            </dependency>
            <!-- Apache工具組件 -->
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
                <version>${commons-lang3.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-io</artifactId>
                <version>${commons-io.version}</version>
            </dependency>
            <dependency>
                <groupId>commons-net</groupId>
                <artifactId>commons-net</artifactId>
                <version>${commons-net.version}</version>
            </dependency>
            <!-- Jackson Json處理工具包 -->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>${jackson.version}</version>
            </dependency>
            <!-- httpclient -->
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>${httpclient.version}</version>
            </dependency>
            <!-- 單元測試 -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
            </dependency>
            <!-- 日誌處理 -->
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
            <!-- Mybatis -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>${mybatis.version}</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>${mybatis.spring.version}</version>
            </dependency>
            <dependency>
                <groupId>com.github.miemiedev</groupId>
                <artifactId>mybatis-paginator</artifactId>
                <version>${mybatis.paginator.version}</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper</artifactId>
                <version>${pagehelper.version}</version>
            </dependency>
            <!-- MySql -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
            </dependency>
            <!-- 連接池 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>${druid.version}</version>
            </dependency>
            <!-- Spring -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <!-- JSP相關 -->
            <dependency>
                <groupId>jstl</groupId>
                <artifactId>jstl</artifactId>
                <version>${jstl.version}</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>servlet-api</artifactId>
                <version>${servlet-api.version}</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jsp-api</artifactId>
                <version>${jsp-api.version}</version>
                <scope>provided</scope>
            </dependency>
            <!-- 文件上傳組件 -->
            <dependency>
                <groupId>commons-fileupload</groupId>
                <artifactId>commons-fileupload</artifactId>
                <version>${commons-fileupload.version}</version>
            </dependency>
            <!-- Redis客戶端 -->
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>${jedis.version}</version>
            </dependency>
            <!--			 solr客戶端-->
            <dependency>
                <groupId>org.apache.solr</groupId>
                <artifactId>solr-solrj</artifactId>
                <version>${solrj.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <!-- 資源文件拷貝插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <!-- java編譯插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
        <pluginManagement>
            <plugins>
                <!-- 配置Tomcat插件 -->
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
    
    <distributionManagement>
        <repository>
            <id>releases</id>
            <name>User Project Release</name>
            <url>http://47.99.69.84:8081/repository/maven-releases/</url>
        </repository>
    
        <snapshotRepository>
            <id>snapshots</id>
            <name>User Project SNAPSHOTS</name>
            <url>http://47.99.69.84:8081/repository/maven-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>
    

nexus 私服搭建

上傳 jar 包到私服

標題鏈接的文章,上傳 jar 包到私服教程還是非常好的。
pom.xml 的配置建議寫在父工程,這樣子工程可以直接繼承使用

從 私服上拉取 jar 包。先了解 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">
  <localRepository>E:\localRepository</localRepository>

  <pluginGroups>

  </pluginGroups>


  <proxies>

  </proxies>


  <servers>
  	<server>  
      <id>maven-XX</id>  
      <username>admin</username>  
      <password>admin123</password>  
    </server> 
	<server>  
      <id>releases</id>  
      <username>admin</username>  
      <password>admin123</password>  
    </server>  
    <server>  
      <id>snapshots</id>  
      <username>admin</username>  
      <password>admin123</password>  
    </server>  
  </servers>

  <mirrors>
	<mirror> 
		<id>maven-XX</id> 
		<name>maven-public</name> 
		<url>http://47.99.69.84:8081/repository/maven-public/</url> 
		<mirrorOf>*</mirrorOf> 
	</mirror> 
  </mirrors>


  <profiles>

    <profile>
		<id>yiebang-maven-central</id>
		<repositories>
			<repository>
				<id>maven-XX</id>
				<name>maven-public</name>
				<url>http://nexus-releases</url>
				<layout>default</layout>
				<releases>  
					<enabled>true</enabled>  
				</releases>  
				<snapshots>  
					<enabled>true</enabled>  
				</snapshots>  
			</repository>	
		</repositories>
		   <!-- 插件倉庫列表 -->
		<pluginRepositories>
			<pluginRepository>
				<id>maven-XX</id>
				<name>maven-public</name>
				<url>http://nexus-releases</url>
				<snapshots>
					<enabled>true</enabled>
				</snapshots>
				<releases>
					<enabled>true</enabled>
				</releases>
			</pluginRepository>
		</pluginRepositories>
    </profile>

  </profiles>


  <activeProfiles>
    <activeProfile>yiebang-maven-central</activeProfile>
  </activeProfiles>

</settings>

  • 再次回顧強調點。setting.xml 文件中 涉及很多 id 的配置。我之前很糾結這些 id 是否一定和 nexus3 上倉庫的 id(name) 一一對應。後經過驗證。除了 鏡像路徑的URL 及 賬號 密碼要對應的上,其他都可以自己有設置。setting.xml 中 name 的設置就更不用說了,隨意。
  1. 當前網上比較流行的配置都是 nexus2 。存在一定的誤導性,很多 配置的 URL 於 nexus3 不一樣,但是並未說明。如:
  • setting.xml 文件配置

 <servers>
    <server>
    // 這類 id 是可以隨意命名的 但是 工程裏面的 pom.xml 文件中配置上傳驅動時,要與這裏一一對應
	  <id>releases</id>
	  <username>admin</username>
	  <password>admin123</password>
	</server>
	<server>
	  <id>snapshots</id>
	  <username>admin</username>
	  <password>admin123</password>
	</server>
  </servers>
<mirrors> 
	<mirror>
      <!--This sends everything else to /public -->
      <id>nexus</id>
	  <name>nexus repository</name> 
      <mirrorOf>*</mirrorOf>
      // 此處的地址就是 下列圖示地址
      <url>http://47.99.69.84:8081/repository/maven-public/</url>
    </mirror>
  </mirrors>

在這裏插入圖片描述

  • pom.xml 文件配置
  • 其他的配置都無所謂 ,只有這個最重要。 id 與 setting.xml 中一一對應。URL 取自上方圖片
	<distributionManagement>  
        <repository>  
            <id>releases</id>
            <name>User Project Release</name>  
            <url>http://47.99.69.84:8081/repository/maven-releases/</url>
        </repository>  
  
        <snapshotRepository>  
            <id>snapshots</id>
            <name>User Project SNAPSHOTS</name>  
            <url>http://47.99.69.84:8081/repository/maven-snapshots/</url>
        </snapshotRepository>  
</distributionManagement>  

測試

<?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.demo</groupId>
 <artifactId>demo</artifactId>
 <version>1.0</version>
 <packaging>jar</packaging>

 <name>demo Maven Webapp</name>
 <!-- FIXME change it to the project's website -->
 <url>http://www.example.com</url>

 <properties>
   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   <maven.compiler.source>1.7</maven.compiler.source>
   <maven.compiler.target>1.7</maven.compiler.target>
 </properties>

 <dependencies>
   <dependency>
     <groupId>junit</groupId>
     <artifactId>junit</artifactId>
     <version>4.11</version>
     <scope>test</scope>
   </dependency>
 </dependencies>

 <build>
   <finalName>demo</finalName>
 </build>

 <distributionManagement>
   <repository>
     <id>releases</id>
     <name>User Project Release</name>
     <url>http://47.99.69.84:8081/repository/maven-releases/</url>
   </repository>

   <snapshotRepository>
     <id>snapshots</id>
     <name>User Project SNAPSHOTS</name>
     <url>http://47.99.69.84:8081/repository/maven-snapshots/</url>
   </snapshotRepository>
 </distributionManagement>
</project>

  • 按照步驟上傳 jar 包
  • 觀察 nexus 服務器 是否上傳成功。SNAPSHOTS 版本會自動加時間戳,沒有任何影響
    在這裏插入圖片描述
  • 用idea 再建一個 maven 工程
    配置 pom.xml 拉取 demo 。 不同工程 ( pom / war / jar ) 拉取配置不一樣。主要在於 type 不一樣。
    父工程自動拉取。
    war 包 則 配置
     <type>war</type>

默認拉取 jar 包


<?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.getdemo</groupId>
    <artifactId>getdemo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>getdemo Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.demo</groupId>
            <artifactId>demo</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>com.taotao</groupId>
            <artifactId>taotao-common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>bantu-framework</groupId>
            <artifactId>bantu-im</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <type>war</type>
        </dependency>
    </dependencies>


    <build>
        <finalName>getdemo</finalName>
    </build>
</project>

  • 觀察拉取效果
    在這裏插入圖片描述
    jar 包的拉取效果
    在這裏插入圖片描述

war 包的拉取效果

在這裏插入圖片描述

pom 父工程包的拉取效果

在這裏插入圖片描述
PS :拉取的時候,我中間糾結了很久,最後發現是 maven setting.xml 文件中的標籤寫錯了,導致沒有拉取成功,但是不影響上傳。所以,如果看這篇文章沒有拉取成功,可以研讀 maven setting.xml 的標籤含義及拼寫,對應檢查即可。

資源下載

安裝和調試

注意點

成果截圖說明

  1. 整體效果
    在這裏插入圖片描述

  2. 增加阿里雲代理配置
    在這裏插入圖片描述

  3. 對 public 進行排序

在這裏插入圖片描述

  1. 手動上傳 jar

在這裏插入圖片描述

錯誤碼

  • 401 賬號密碼錯誤
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章