阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Linux下使用Nexus搭建Maven私服详解

176次阅读
没有评论

共计 6401 个字符,预计需要花费 17 分钟才能阅读完成。

有个 Maven 私服可以很方便地管理我们的 jar 包和发布构建到远程仓库,本文就介绍了如何在 Linux 下一步步使用 Nexus 搭建 Maven 私服。

下载安装

最新 nexus 下载地址:http://www.sonatype.org/nexus/go

解压后会在同级目录中,出现两个文件夹:nexus-oss-webapp-1.8.0sonatype-work,前者包含了 nexus 的运行环境和应用程序,后者包含了你自己的配置和数据。

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

启动 nexus

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

查看控制台:

1
$ ./nexus console

显示未启动成功,报错如下:

12
3
45
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
$ ./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 会支持.

Nexus 所有版本下载地址:http://www.sonatype.org/nexus/archived

下载 Nexus2.4 重来

12
3
45
6
7
8
9
10
11
12
13
$ 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 日志:

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

配置 nexus

访问网址:http://yourhostname:8081/nexus

Linux 下使用 Nexus 搭建 Maven 私服详解

右上角以 admin 登陆,默认用户名 / 密码:admin/admin123。

Linux 下使用 Nexus 搭建 Maven 私服详解

3rd party、Snapshots、Releases 这三个,分别用来保存第三方 jar、项目组内部的快照、项目组内部的发布版.

手动添加第三方 jar

将第三方的 jar 上传到 nexus 上面:

Linux 下使用 Nexus 搭建 Maven 私服详解

Linux 下使用 Nexus 搭建 Maven 私服详解

点击 Upload Artifact(s)按钮提交后即上传。

查看上传的 jar 包如下:

Linux 下使用 Nexus 搭建 Maven 私服详解

在项目中使用私服的 jar 包配置 pom.xml 如下:

12
3
45
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<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 包都会从私服中下载到本地并关联上项目,私服中没有就会从网络上下载到私服,本地再从私服下载。

Linux 下使用 Nexus 搭建 Maven 私服详解

自动发布构件到远程仓库

在工程的 pom.xml 中添加:

12
3
45
6
7
8
9
10
<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 配置文件中的语句块中添加如下所示:

12
3
45
6
7
8
9
10
11
12
<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 命令行,在工程所在目录下执行

1
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 后的发布版。

Maven 权威指南_中文完整版清晰 PDF  http://www.linuxidc.com/Linux/2014-06/103690.htm

Maven 3.1.0 发布,项目构建工具 http://www.linuxidc.com/Linux/2013-07/87403.htm

Linux 安装 Maven http://www.linuxidc.com/Linux/2013-05/84489.htm

Maven3.0 配置和简单使用 http://www.linuxidc.com/Linux/2013-04/82939.htm

Ubuntu 下搭建 sun-jdk 和 Maven2 http://www.linuxidc.com/Linux/2012-12/76531.htm

Maven 使用入门 http://www.linuxidc.com/Linux/2012-11/74354.htm

Ubuntu 下 搭建 Nexus Maven 私服中央仓库  http://www.linuxidc.com/Linux/2016-08/133936.htm

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-08/134630.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计6401字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中