1. 程式人生 > >Nexus私服搭建

Nexus私服搭建

    Maven搭建眾所周知, 是一個很好的專案管理工具,預設情況下,我們下載第三方元件都是從MAVEN官網下載的。但是並不是所有的三方元件都可以在MAVEN官網找到,比如我們專案組自己內部的一些元件,可能要放在一個區域網伺服器上,並且向結合MAVEN使用,這時候我們就要用到Nexus來搭建私有服務了。

1.下載安裝

首先。我們從nexus下載

下載解壓後,找到bin資料夾,如下圖

開啟CMD命令列,以windwos服務形式安裝nexus,切換到bin資料夾下執行如下命令nexus.bat install ,這時候就可以在windows服務中看到nexus服務了。

如下圖

輸入使用者名稱和密碼進行登入 admin/admin123,成功後如下圖所示

預設情況下,私服的埠號為8081,如果想修改埠號,找到nexus-2.12.0-01\conf資料夾,開啟nexus.properties檔案,修改application-port屬性值

2.nexus介紹

在上圖的介面中,我們可以看到有三種類型的倉庫

group 倉庫組  可以對其他倉庫進行整合 hosted  宿主 proxy   代理倉庫 virtual  映象倉庫 主要有以下幾個倉庫,當然我們可以新建倉庫                 Public Repositories:  倉庫組,公共倉庫,可以對其他倉庫進行整合                   3rd party: 無法從公共倉庫獲得的第三方釋出版本的構件倉庫                   Apache Snapshots: 用了代理ApacheMaven倉庫快照版本的構件倉庫                   Central: 用來代理maven中央倉庫中釋出版本構件的倉庫                   Central M1 shadow: 用於提供中央倉庫中M1格式的釋出版本的構件映象倉庫                   Releases: 用來部署管理內部的釋出版本構件的宿主型別倉庫                   Snapshots:用來部署管理內部的快照版本構件的宿主型別倉庫

3.配置專案

  首先我們要設定專案專案索引和配置本地倉庫路徑,如下圖 設定專案索引是為了在私服站點上可以使用元件查詢依賴功能,可以MAVEN官網的元件下載到私服伺服器上。在私服伺服器上方便查詢。 設定本地倉庫路徑是儲存第三方索引元件下載後在本地的儲存位置。   我們可以右鍵選擇central倉庫,選擇repair index下載專案索引,這時候你會發現本地倉庫資料夾,新增了許多從服務下載的資料夾

4.Esclipse專案配置

進行JAVA專案開發最常用的工具是Eclipse,我們看下怎麼結合Eclipse來使用Nexus私有伺服器。首先我們匯入Maven的配置檔案settings.xml, 開啟Eclipse, 選擇windows-Preferences,選擇settings.xml檔案
settings.xml內容如下
<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->

<!--
 | This is the configuration file for Maven. It can be specified at two levels:
 |
 |  1. User Level. This settings.xml file provides configuration for a single user, 
 |                 and is normally provided in ${user.home}/.m2/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -s /path/to/user/settings.xml
 |
 |  2. Global Level. This settings.xml file provides configuration for all Maven
 |                 users on a machine (assuming they're all using the same Maven
 |                 installation). It's normally provided in 
 |                 ${maven.home}/conf/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -gs /path/to/global/settings.xml
 |
 | The sections in this sample file are intended to give you a running start at
 | getting the most out of your Maven installation. Where appropriate, the default
 | values (values used when the setting is not specified) are provided.
 |
 |-->
<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
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ~/.m2/repository-->
  <localRepository>D:/demo/nexus/Repositories</localRepository>
  

  <!-- interactiveMode
   | This will determine whether maven prompts you when it needs input. If set to false,
   | maven will use a sensible default value, perhaps based on some other setting, for
   | the parameter in question.
   |
   | Default: true
  <interactiveMode>true</interactiveMode>
  -->

  <!-- offline
   | Determines whether maven should attempt to connect to the network when executing a build.
   | This will have an effect on artifact downloads, artifact deployment, and others.
   |
   | Default: false
  <offline>false</offline>
  -->

  <!-- pluginGroups
   | This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.
   | when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers
   | "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.
   |-->
  <pluginGroups>
    <!-- pluginGroup
     | Specifies a further group identifier to use for plugin lookup.
    <pluginGroup>com.your.plugins</pluginGroup>
    -->
  </pluginGroups>

  <!-- proxies
   | This is a list of proxies which can be used on this machine to connect to the network.
   | Unless otherwise specified (by system property or command-line switch), the first proxy
   | specification in this list marked as active will be used.
   |-->
  <proxies>
    <!-- proxy
     | Specification for one proxy, to be used in connecting to the network.
     |
    <proxy>
      <id>optional</id>
      <active>true</active>
      <protocol>http</protocol>
      <username>proxyuser</username>
      <password>proxypass</password>
      <host>proxy.host.net</host>
      <port>80</port>
      <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
    </proxy>
    -->
  </proxies>

  <!-- servers
   | This is a list of authentication profiles, keyed by the server-id used within the system.
   | Authentication profiles can be used whenever maven must make a connection to a remote server.
   |-->
 <servers>  
  <server>  
   <id>release</id>  
    <username>admin</username>  
    <password>admin123</password>  
  </server>  
  
  <server>  
   <id>snap</id>  
    <username>admin</username>  
    <password>admin123</password>  
  </server>  
 
</servers>

 


  <profiles>
  	<profile>
  		<id>nexus</id>
  		<repositories>
  			<repository>
  				<id>central</id>
  				<url>http://repo1.maven.org/maven2</url>
  				<releases><enabled>true</enabled></releases>
  				<snapshots><enabled>true</enabled></snapshots>
  			</repository>
			<repository>
					<id>nexus</id>
					<name>local private nexus</name>
					<url>http://localhost:8081/nexus/content/groups/public</url>
			</repository>
  		</repositories>
  		<pluginRepositories>
  			<pluginRepository>
  				<id>central</id>
  				<url>http://repo1.maven.org/maven2</url>
  				<releases><enabled>true</enabled></releases>
  				<snapshots><enabled>true</enabled></snapshots>
  			</pluginRepository>
			<pluginRepository>
					<id>nexus</id>
					<name>Maven China Mirror</name>
					<url>http://localhost:8081/nexus/content/groups/public</url>
					<releases>
						<enabled>true</enabled>
					</releases>
					<snapshots>
						<enabled>false</enabled>
					</snapshots>
				</pluginRepository>
  		</pluginRepositories>
		<properties>
			<releases.url>http://localhost:8081/nexus/content/groups/public</releases.url>
		</properties>
  	</profile>
  </profiles>
 
  <activeProfiles>
  	<activeProfile>nexus</activeProfile>
  </activeProfiles>
</settings>
首先我們看下profiles中的倉庫配置, 如果想使用某個私服倉庫,需要先配置私服倉庫地址
  <repositories>
         <repository>
          <id>nexus</id>
            <name>nexus</name>
            <url>http://localhost:8081/nexus/content/groups/public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
          </repository>
         </repositories>
其次配置對應的外掛倉庫地址
  <pluginRepositories>
            <pluginRepository>
         <id>nexus</id>
         <name>Team Nexus Repository</name>
           <url>http://localhost:8081/nexus/content/groups/public</url>
              <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
         </pluginRepository> 
         </pluginRepositories>

4.專案釋出 建立一個Esclipse專案,裡面包括兩個模組,把nexusdemo1的模組所生成的jar包需要釋出到私服上
首先在nexusdemo1的專案的pom.xml中新增如下配置。
<!-- 釋出倉庫 -->
<distributionManagement>
	<!-- 釋出release倉庫 -->
	<repository>
		<id>release</id>
		<name>re</name>
		<url>http://localhost:8081/nexus/content/repositories/releases/</url>
	</repository>
	
	<!-- 釋出快照版本 -->
	<snapshotRepository>
		<id>snap</id>
		<name>sn</name>
		<url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
	</snapshotRepository>

</distributionManagement>
然後在settings.xml新增許可權,如果不新增許可權,則不能釋出
<servers>  
  <server>  
   <id>release</id>  
    <username>admin</username>  
    <password>admin123</password>  
  </server>  
  
  <server>  
   <id>snap</id>  
    <username>admin</username>  
    <password>admin123</password>  
  </server>  
 
</servers>
注意 server中的ID一定要與distributionManagement釋出倉庫的ID一樣,然後執行 mvc clean deploy進行釋出
釋出成功後,我們在nexus的倉庫中就可以看到jar包了 注意:預設情況下,nexusdemo1中的版本資訊為  <version>0.0.1-SNAPSHOT</version>,所以釋出成功後,會發布到SNAPSHOT倉庫中, 如果想釋出到Releases倉庫中,把版本資訊修改為<version>0.0.1</version>. 我們在nexusdemo2中引用nexusdemo1的JAR包,新增依賴
 <dependency>
	  <groupId>com.nexus</groupId>
	  <artifactId>nexusdemo1</artifactId>
	  <version>0.0.1-SNAPSHOT</version>
	</dependency>
此時在nexusdemo2中就可以看到nexusdemo1的jar包了