1. 程式人生 > >Linux下使用nexus搭建maven私服

Linux下使用nexus搭建maven私服

下載安裝

解壓後會在同級目錄中,出現兩個資料夾:nexus-oss-webapp-1.8.0sonatype-work,前者包含了nexus的執行環境和應用程式,後者包含了你自己的配置和資料。

$ mkdir nexus
$ tar xzvf /home/jili/nexus-2.7.0-05-bundle.tar.gz
$ ls
nexus-2.7.0-05  sonatype-work

啟動nexus

$ cd bin/
$ ls
jsw  nexus  nexus.bat
$ ./nexus
Usage: ./nexus { console | start | stop | restart |
status | dump }
$ ./nexus start Starting Nexus OSS... Started Nexus OSS.

檢視控制檯:

$ ./nexus console

顯示未啟動成功,報錯如下:

$ ./nexus console
Running Nexus OSS...
wrapper  | --> Wrapper Started as Console
wrapper  | Launching a JVM...
wrapper  | JVM exited while loading the application.
jvm 1    |
Exception in thread "main" java.lang.UnsupportedClassVersionError: org/sonatype/nexus/bootstrap/jsw/JswLauncher : Unsupported major.minor version 51.0
jvm 1 | at java.lang.ClassLoader.defineClass1(Native Method) jvm 1 | at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632) jvm 1 |
at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
jvm 1 | at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141) jvm 1 | at java.net.URLClassLoader.defineClass(URLClassLoader.java:283) jvm 1 | at java.net.URLClassLoader.access$000(URLClassLoader.java:58) jvm 1 | at java.net.URLClassLoader$1.run(URLClassLoader.java:197) jvm 1 | at java.security.AccessController.doPrivileged(Native Method) jvm 1 | at java.net.URLClassLoader.findClass(URLClassLoader.java:190) jvm 1 | at java.lang.ClassLoader.loadClass(ClassLoader.java:307) jvm 1 | at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) jvm 1 | at java.lang.ClassLoader.loadClass(ClassLoader.java:248) jvm 1 | Could not find the main class: org.sonatype.nexus.bootstrap.jsw.JswLauncher. Program will exit. wrapper | Reloading Wrapper configuration... wrapper | Launching a JVM... wrapper | JVM exited while loading the application. . . . jvm 5 | Exception in thread "main" java.lang.UnsupportedClassVersionError: org/sonatype/nexus/bootstrap/jsw/JswLauncher : Unsupported major.minor version 51.0 jvm 5 | at java.lang.ClassLoader.defineClass1(Native Method) jvm 5 | at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632) jvm 5 | at java.lang.ClassLoader.defineClass(ClassLoader.java:616) jvm 5 | at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141) jvm 5 | at java.net.URLClassLoader.defineClass(URLClassLoader.java:283) jvm 5 | at java.net.URLClassLoader.access$000(URLClassLoader.java:58) jvm 5 | at java.net.URLClassLoader$1.run(URLClassLoader.java:197) jvm 5 | at java.security.AccessController.doPrivileged(Native Method) jvm 5 | at java.net.URLClassLoader.findClass(URLClassLoader.java:190) jvm 5 | at java.lang.ClassLoader.loadClass(ClassLoader.java:307) jvm 5 | at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) jvm 5 | at java.lang.ClassLoader.loadClass(ClassLoader.java:248) jvm 5 | Could not find the main class: org.sonatype.nexus.bootstrap.jsw.JswLauncher. Program will exit. wrapper | There were 5 failed launches in a row, each lasting less than 300 seconds. Giving up. wrapper | There may be a configuration problem: please check the logs. wrapper | <-- Wrapper Stopped

原因:查詢原因是JDK版本過低造成的,升級到最新的JDK7或者使用nexus-2.4-bundle.tar.gz版本JDK6會支援.

下載Nexus2.4重來

$ ls
nexus-2.4.0-09  sonatype-work
$ cd nexus-2.4.0-09/bin/
$ ls
jsw  nexus  nexus.bat
$ ./nexus
Usage: ./nexus { console | start | stop | restart | status | dump }
$ ./nexus start
Starting Nexus OSS...
Started Nexus OSS.
$ ./nexus console
Running Nexus OSS...
Nexus OSS is already running.

控制檯顯示啟動成功。

檢視nexus日誌:

$ cd nexus-2.4.0-09/logs
$ ls
wrapper.log
$ tail -f wrapper.log

配置nexus

右上角以admin登陸,預設使用者名稱/密碼:admin/admin123。

3rd party、Snapshots、Releases這三個,分別用來儲存第三方jar、專案組內部的快照、專案組內部的釋出版.

手動新增第三方jar

將第三方的jar上傳到nexus上面:

點選Upload Artifact(s)按鈕提交後即上傳。

檢視上傳的jar包如下:

在專案中使用私服的jar包配置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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.vclk.mkt.crawler</groupId>
  <artifactId>MarketingCrawler</artifactId>
  <packaging>jar</packaging>
  <version>0.3</version>
  <name>MarketingCrawler</name>
  <url>http://maven.apache.org</url>
  <!-- 倉庫地址 -->
  <repositories>
      <repository>
          <id>nexus</id>
          <name>Team Nexus Repository</name>
          <url>http://yourhostname:8081/nexus/content/groups/public</url>
      </repository>
  </repositories>
  <!-- 外掛地址 -->
  <pluginRepositories>
      <pluginRepository>
          <id>nexus</id>
          <name>Team Nexus Repository</name>
          <url>http://yourhostname:8081/nexus/content/groups/public</url>
      </pluginRepository>
  </pluginRepositories>
  <!-- jar -->
  <dependencies>
      <dependency>
          <groupId>de.innosystec</groupId>
          <artifactId>java-unrar</artifactId>
          <version>0.5</version>
      </dependency>
  </dependencies>
</project>

Maven在專案根目錄下執行mvn eclipse:eclipse命令時,所依賴的jar包都會從私服中下載到本地並關聯上專案,私服中沒有就會從網路上下載到私服,本地再從私服下載。

自動釋出構件到遠端倉庫

在工程的pom.xml中新增:

<distributionManagement>
  <repository>
      <id>nexus-releases</id>
      <url>http://yourhostname:8081/nexus/content/repositories/releases/</url>
  </repository>
  <snapshotRepository>
      <id>nexus-snapshots</id>
      <url>http://yourhostname:8081/nexus/content/repositories/snapshots/</url>
  </snapshotRepository>
</distributionManagement>

進入maven的安裝目錄apache-maven-3.1.1\conf目錄下,向settings.xml配置檔案中的語句塊中新增如下所示:

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

進入windows命令列,在工程所在目錄下執行

mvn deploy

所部署的包就自動上傳到了nexus安裝目錄下的/maven/nexus/sonatype-work/nexus/storage/releases/com/vclk/mkt/crawler/MarketingCrawler/0.3目錄

nexus倉庫中各目錄介紹

專案中的各種jar包和專案快照等都放在/nexus/sonatype-work/nexus/storage/目錄下,在這個目錄下包括以下各種目錄和存放相應檔案。

/nexus/sonatype-work/nexus/storage/central - 用於放置maven從中央倉庫中下載下來的專案pom.xml中配置到的相關jar包;

/nexus/sonatype-work/nexus/storage/thirdparty - 用於放置自己手動上傳的第三方jar包;

/nexus/sonatype-work/nexus/storage/releases - 用於放置專案deploy後的釋出版。