使用nexus構建maven私服


使用Nexus創建私服

部署環境是windowsxp

首先從 http://nexus.sonatype.rog/downloads 下載最新版的Nexus

我們可以根據個人的需求下載。裏面有nexus-webapp-1.9.2.3-bundle.zip等其他系統運行的壓縮文件 或者war包nexus-webapp-1.9.2.3.war。

nexus-webapp-1.9.2.3-bundle.zip 包裏面自帶了一個Jetty容器,這樣我們不用另找容器,可以直接啓動運行。

Bundle 安裝方式:

首頁解壓Bundle文件,這時回得到兩個子目錄:

nexus-oss-webapp-1.9.2.3 該目錄包含了Nexus 運行所需要的文件,如啓動腳本、依賴jar包等。

sonatype-work   該目錄包含Nexus生成的配置、日誌文件、倉庫文件等。其中,第一個目錄是運行Nexus所必須的,而且所有相同版本Nexus實力所包含的該目錄內容都是一樣的。而第二個目錄不是必須的,Nexus會在運行的時候動態創建該目錄,不過他的內容對於各個Nexus實例不一樣的,因爲不同用戶在不同機器上使用的Nexus會有不同的配置和倉庫內容。當用戶需要本分Nexus的時候,默認備份sonatype-work/目錄,因爲該目錄包含了用戶特定的內容,而nexus-webapp1.9.2.3目錄下的內容是可以從安裝包直接獲取的。

如果要啓動nexus需要在nexus-oss-webapp-1.9.2.3\bin\jsw\ 下面選擇對應的系統版本,我的環境是:windows-x86-32,所以選擇這個目錄這個目錄裏面有一些腳本如下:
Installnexus.bat 將Nexus安裝成windows服務

Uninstallnexus.bat 卸載Nexus Windows服務

Startnexus.bat 啓動Nexus Windows 服務

Stopnexus.bat 停止Nexus windos 服務

Pausenexus.bat 暫停Nexus windows 服務

Resumenexus.bat 恢復暫停的 Nexus Windos服務

如果不想每次啓動都使用這些腳本,我可以可以配置成widnwos服務,可以開機自動啓動

 

在Linux 系統上啓動Nexus也非常方便,例如Unbuntu 32 位系統,那麼只需要進入到nexus-oss-webapp-1.9.2.3\bin\jsw\linux-x86-32 然後運行如下的命令:

$./nexus console

這時可以看到啓動的命令行輸出,可以使用Ctril+c停止Nexus

除了console之外,nexus的linux腳本還提供瞭如下命令:

./nexus start: 在後臺啓動Nexus服務

./nexus stop: 停止後臺的Nexus服務

./nexus status : 查看後臺Nexus服務狀態

./nexus restart: 重新啓動後臺的Nexus服務

 

Nexus Bundle 默認使用的端口號是:8081,如果啓動服務可以訪問:

http://localhost:8081/nexus

啓動Bundle時可能會出現端口衝突,或者想跟換端口時可以的打開:nexus-oss-webapp-1.9.2.3\conf\plexus.properties 文件裏面找到屬性:application-port 按自己的需要改成相應的端口 我的改成4040

這時在訪問的時候地址應該是這樣的:http://localhost:4040/nexus

WAR的方式安裝Nexus

該war包支持主流的web容器,如tomcat 、Classfish、Jetty和Resin

比如tomcat ,把下載好的war放在webapps目錄下 然後運行tomcat就行了,具體看看tomcat資料

 

登錄Nexus

Nexus默認管理員用戶名和密碼爲:admin/admin123

Nexus 內置倉庫介紹

Maven Gentral:該殘酷代理Maven中央倉庫,其策略爲Release,因此只會下載和緩存中央倉庫中的發佈版本構件。

Release:這是一個策略爲Release的宿主類型倉庫,用來部署組織內部的發佈版本構件。

Snapshots:這是一個策略爲Snapshot的宿主類型倉庫,用來部署組織內部的快照版本構件。

3rd party:這是一個策略爲Release的宿主類型倉庫,用來部署無法從公共倉庫獲得的第三方發佈版本構件。

Apache Snapshots: 這是一個策略爲Snapshot的代理倉庫,用來代理Apache Maven倉庫的快照版本構件。

Codehaus Snapshots: 這是一個策略爲Snapshot的代理倉庫,用來代理Codehaus Maven倉庫的快照版本構件。

Google code: 這是一個策略爲Release的代理倉庫,用來代理Google Code Maven倉庫發佈版本構件。

java.net Maven2:這是一個策略爲Release的代理倉庫,用來代理java.net Maven倉庫的發佈版本構件

Public Repositories:該倉庫組將上述所有策略爲Release的倉庫聚合並通過一致的地址提供服務

Public Snapshot Repositories: 該倉庫組將上述所有的策略爲Snapshot的倉庫聚合並通過一致的地址提供服務。(在nexus-oss-webapp-1.9.2.3 這版本中沒有此項)

Nexus倉庫分類概念

Maven可以直接從宿主倉庫下載構件;maven也可以從代理倉庫下載構件,而代理倉庫會間接地從遠程倉庫下載並緩存構件;最後,爲了方便,Maven可以從倉庫組下載構件,而倉庫組沒有實際內容,它會轉向其包含的宿主倉庫或者代理倉庫獲得市級構件的內容。

創建Nexus宿主倉庫

創建一個宿主倉庫首先單擊界面左邊導航欄中的Repositories鏈接,在右邊的面板中,選擇add,接着在下拉菜單中選擇Hosted Repository,就會看到如下圖的配置界面:

填寫倉庫ID Repostory ID  和倉庫名稱 Repository Name 以及倉庫類型Respository Type  倉庫的格式 Provider 選擇默認的Maven2 Repository,然後是Repository Policy 讀者可以根據自己的需要來配置該倉庫是發佈版本構件倉庫還是快照版本構件倉庫。Defualt Local Storage Location 表示該倉庫的沒哦人存儲目錄,圖中該字段的值爲空,待倉庫創建好之後,該值就會成爲基於sonatype-work的一個文件路徑,如:E:\nexus-oss-webapp-1.9.2.3-bundle\sonatype-work\nexus\storage

默認的地址倉庫存儲目錄地址。

Access Setting 小組中

  Deployment Policy用來配置該倉庫的部署策略,選項有隻讀(禁止部署)、關閉重新部署(同一構件只能部署一次)以及允許重新部署。

  Allow file Browsing 表示是否允許瀏覽倉庫內容

  Include in Search 表示是否對該倉庫進行索引並提供搜索

  Publish URL 用來控制是否通過URL提供服務,如果選擇false當訪問倉庫的地址時,會得到HTTP404 Not Found 錯誤

  Not Found Cache TTL 表示當一個文件沒有找到後,緩存這一不存在的信息的時間。以默認值1440分鐘爲例,如果某文件不存在,那麼在之後的1440分鐘內,如果Nexus再次得到該文件的請求,它將直接返回不存在的信息,而不會查找位呢間系統。

 

創建Nexus代理倉庫


操作和創建宿主倉庫類似,主要Repository Type 的值改爲proxy 這時看到如下圖:

對於代理倉庫來說,最重要的是遠程倉庫地址即

Remote Storage Location,用戶必須輸入有效的值

Download Remote Indexes 表示是否下載遠程倉庫的索引

Checksum Policy 配置校驗出錯時的策略,用戶可以選擇忽略、記錄警告信息或者拒絕下載。

Authentication 當遠程倉庫需要認證的時候這裏的時候,這裏的Authentication 配置就能派上用處。

Artifact Max Age 構件緩存最長的時間,對於快照版本來說 Artifact Max Age 默認值爲 -1,表示構件混存後就一直保存着,不在重新下載,對於快照版來說默認值爲1440分鐘表示每隔

Metadata Max Age 倉庫元數據文件緩存的最長時間

Http Request Setting 和 Override HTTP proxy Setting 其中前者用來配置Nexus訪問遠程倉庫時HTTP請求參數,後者用來配置HTTP代理

創建Nexus倉庫組   

創建倉庫組同其他的一樣步驟是在選擇add時選擇下拉框中的Repository Group 就會看到如下的:

配置中的信息同其他的一樣,倉庫組中沒有Release 和Snapshot ,這不同於宿主倉庫和代理倉庫。在配置界面中可以選擇Nexus中的倉庫,將其聚合成一個虛擬的倉庫組,注意,倉庫組所包含的倉庫的順序決定了倉庫組便利其所含倉庫的次序,因此最好將常用的倉庫放在前面,當用戶從倉庫組下載構件的時候,就能經快的訪問到包含構件的倉庫。

Nexus的索引與構件搜索

需要搜索Maven 中央庫,首先需要設置Nexus中的Maven Central 代理倉庫下載遠程索引 如下圖:

Download Remote Indexes 屬性設置爲true 默認爲false

true是開啓,false是關閉 由於中央倉庫內容比較多,因此其索引文件比較大,需要查看下載如何了,我們可以單擊界面左邊導航欄中的 Scheduled Tasks 鏈接後,就可以看到系統調度的任務其狀態爲 runing,在說哦因下載完畢之後,該任務就會消失。

Scheduled Tasks 界面:

配置Maven 從Nexus下載構件

當需要爲項目添加Nexus私服上的public倉庫時,可以在項目pom.xml文件配置  代碼如下:

複製代碼
<project>
     
      <repositories>
        <repository>
          <id>nexus</id>
          <url>http://http://localhost:4040/nexus/content/groups/public/</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
      </repositories>
     <pluginRepositories>
        <pluginRepository>
          <id>nexus</id>
          <url>http://localhost:4040/nexus/content/groups/public/</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>
    
  </project>
複製代碼

這樣的配置只對當前的Maven項目有效,實際應用中,我們往往想要通過一次配置就完成能讓本機所有的Maven項目都使用自己的Maven私服。這時配置本地倉庫setting.xml 代碼如下:

複製代碼
<settings>
  
    <mirrors>
   
     <mirror>
      <id>central</id>
      <mirrorOf>*</mirrorOf>
      <url>http://localhost:4040/content/groups/public/</url>
    </mirror>
  </mirrors>  

    <profiles>
     <profile>
        <id>JDK1.6</id>
        <activation>
            <activeByDefault>true</activeByDefault>
            <jdk>1.6</jdk>
        </activation>
        <properties>
            <maven.compiler.source>1.6</maven.compiler.source>
            <maven.compiler.target>1.6</maven.compiler.target>
            <maven.compiler.compilerVersion>1.6</maven.compiler.compilerVersion>
        </properties>
    </profile>  
      <profile>
      <id>central</id>
      <repositories>
        <repository>
          <id>central</id>
          <url>http://central</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
      </repositories>
     <pluginRepositories>
        <pluginRepository>
          <id>central</id>
          <url>http://central</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>
  </profiles>
 <activeProfiles>
    <activeProfile>central</activeProfile>
     <activeProfile>JDK1.6</activeProfile>
  </activeProfiles>
</settings>
複製代碼


使用Maven 部署構件至Nexus

日常開發生成的快照版本構件可以直接部署到Nexus中策略爲Snapshot的宿主倉庫中,項目正式發佈的構件則應該部署到Nexus中策略爲Release的宿主倉庫中。POM.XML配置如下:

複製代碼
<project>
    <distributionManagement>
         <repository>
             <id>nexus-releases</id>
            <name>Nexus Releases Repository</name>
           <url>http://localhost:4040/nexus/content/repositories/releases</url> 
        </repository>
        <snapshotRepository>
              <id>nexus-snapshots</id>
              <name>Nexus Snapshots Repository</name>
             <url>http://localhost:4040/nexus/content/repositories/snapshots</url>
        </snapshotRepository>
   </distributionManagement>
</project>
複製代碼


Nexus 的倉庫對於黎明用戶是隻讀的爲了能夠部署構件,還需要在setting.xml 中配置認證信息代碼如下:

複製代碼
<servers>
   
    <server>
      <id>releases</id>
      <username>admin</username>
      <password>iapppay</password>
    </server>
    <server>
      <id>snapshots</id>
      <username>admin</username>
      <password>iapppay</password>
    </server>
</servers>
複製代碼



經過實踐檢驗的setting.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>/path/to/local/repo</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
     | Specifies the authentication information to use when connecting to a particular server, identified by
     | a unique name within the system (referred to by the 'id' attribute below).
     | 
     | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are 
     |       used together.
     |
    <server>
      <id>deploymentRepo</id>
      <username>repouser</username>
      <password>repopwd</password>
    </server>
    -->
    
    <!-- Another sample, using keys to authenticate.
    <server>
      <id>siteServer</id>
      <privateKey>/path/to/private/key</privateKey>
      <passphrase>optional; leave empty if not used.</passphrase>
    </server>
    -->
  </servers>

  <!-- mirrors
   | This is a list of mirrors to be used in downloading artifacts from remote repositories.
   | 
   | It works like this: a POM may declare a repository to use in resolving certain artifacts.
   | However, this repository may have problems with heavy traffic at times, so people have mirrored
   | it to several places.
   |
   | That repository definition will have a unique id, so we can create a mirror reference for that
   | repository, to be used as an alternate download site. The mirror site will be the preferred 
   | server for that repository.
   |-->
  <mirrors>
    <!-- mirror
     | Specifies a repository mirror site to use instead of a given repository. The repository that
     | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
     | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
     |
    <mirror>
      <id>mirrorId</id>
      <mirrorOf>repositoryId</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://my.repository.com/repo/path</url>
    </mirror>
     -->
    <mirror>

	   <id>nexus</id>

	   <mirrorOf>*</mirrorOf>

	   <name>Nexus Mirror</name>

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

   </mirror>

  </mirrors>
  
  <!-- profiles
   | This is a list of profiles which can be activated in a variety of ways, and which can modify
   | the build process. Profiles provided in the settings.xml are intended to provide local machine-
   | specific paths and repository locations which allow the build to work in the local environment.
   |
   | For example, if you have an integration testing plugin - like cactus - that needs to know where
   | your Tomcat instance is installed, you can provide a variable here such that the variable is 
   | dereferenced during the build process to configure the cactus plugin.
   |
   | As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles
   | section of this document (settings.xml) - will be discussed later. Another way essentially
   | relies on the detection of a system property, either matching a particular value for the property,
   | or merely testing its existence. Profiles can also be activated by JDK version prefix, where a 
   | value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'.
   | Finally, the list of active profiles can be specified directly from the command line.
   |
   | NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact
   |       repositories, plugin repositories, and free-form properties to be used as configuration
   |       variables for plugins in the POM.
   |
   |-->
  <profiles>
    <!-- profile
     | Specifies a set of introductions to the build process, to be activated using one or more of the
     | mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
     | or the command line, profiles have to have an ID that is unique.
     |
     | An encouraged best practice for profile identification is to use a consistent naming convention
     | for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
     | This will make it more intuitive to understand what the set of introduced profiles is attempting
     | to accomplish, particularly when you only have a list of profile id's for debug.
     |
     | This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.
    <profile>
      <id>jdk-1.4</id>

      <activation>
        <jdk>1.4</jdk>
      </activation>

      <repositories>
        <repository>
          <id>jdk14</id>
          <name>Repository for JDK 1.4 builds</name>
          <url>http://www.myhost.com/maven/jdk14</url>
          <layout>default</layout>
          <snapshotPolicy>always</snapshotPolicy>
        </repository>
      </repositories>
    </profile>
    -->

    <!--
     | Here is another profile, activated by the system property 'target-env' with a value of 'dev',
     | which provides a specific path to the Tomcat instance. To use this, your plugin configuration
     | might hypothetically look like:
     |
     | ...
     | <plugin>
     |   <groupId>org.myco.myplugins</groupId>
     |   <artifactId>myplugin</artifactId>
     |   
     |   <configuration>
     |     <tomcatLocation>${tomcatPath}</tomcatLocation>
     |   </configuration>
     | </plugin>
     | ...
     |
     | NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
     |       anything, you could just leave off the <value/> inside the activation-property.
     |
    <profile>
      <id>env-dev</id>

      <activation>
        <property>
          <name>target-env</name>
          <value>dev</value>
        </property>
      </activation>

      <properties>
        <tomcatPath>/path/to/tomcat/instance</tomcatPath>
      </properties>
    </profile>
    -->
	 <profile>

  <id>nexus</id>

  <repositories>

    <repository>

      <id>nexus</id>

      <name>Nexus</name>

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

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

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

    </repository>

  </repositories>

  <pluginRepositories>

    <pluginRepository>

      <id>nexus</id>

      <name>Nexus</name>

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

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

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

    </pluginRepository>      

  </pluginRepositories>

</profile>
  </profiles>

  <!-- activeProfiles
   | List of profiles that are active for all builds.
   |
  <activeProfiles>
    <activeProfile>alwaysActiveProfile</activeProfile>
    <activeProfile>anotherAlwaysActiveProfile</activeProfile>
  </activeProfiles>
  -->
  <activeProfiles>

    <activeProfile>nexus</activeProfile>

  </activeProfiles>
</settings>


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