ArchLinux 下安装 Nexus

安装

下载地址:nexus-3.16.0-01-unix.tar.gz

解压:

1
2
3
tar -zxvf nexus-3.16.0-01-unix.tar.gz -C /home/maxzhao/soft/
mv nexus-3.16.0-01 nexus
cd nexus

查看配置文件

1
cat etc/nexus-default.properties 
1
2
3
4
5
6
7
8
9
10
# 端口配置
application-port=8081
application-host=0.0.0.0
nexus-args=${jetty.etc}/jetty.xml,${jetty.etc}/jetty-http.xml,${jetty.etc}/jetty-requestlog.xml
# 项目名配置
nexus-context-path=/
# 这部分不用管
nexus-edition=nexus-pro-edition
nexus-features=\
nexus-pro-feature

启动之前要清楚的目录

1
cat bin/nexus.vmoptions

可以进行详细的配置存储路径:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
-Xms1200M
-Xmx1200M
-XX:MaxDirectMemorySize=2G
-XX:+UnlockDiagnosticVMOptions
-XX:+UnsyncloadClass
-XX:+LogVMOutput
# 日志目录 ./就是 nexus3/目录
-XX:LogFile=./sonatype-work/nexus3/log/jvm.log
-XX:-OmitStackTraceInFastThrow
-Djava.net.preferIPv4Stack=true
-Dkaraf.home=.
-Dkaraf.base=.
-Dkaraf.etc=etc/karaf
-Djava.util.logging.config.file=etc/karaf/java.util.logging.properties
# 所有的work,包括nexus.properties
-Dkaraf.data=./sonatype-work/nexus3
# 所有的tmp
-Djava.io.tmpdir=./sonatype-work/nexus3/tmp
-Dkaraf.startLocalConsole=false

配置路径权限

1
2
# xxx 是当前 sonatype-work 所在的路径
sudo chown -R 账号.账号组 /xxx/sonatype-work

启动

1
sh bin/nexus start

访问:127.0.0.1:8081

原始账号密码为 admin / admin123

20210606 更新:原始秘密在 nexus3/etc/admin.password文件中。

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链接,在右边的面板中,选择create,接着在下拉菜单中选择maven2 (hosted),就会看到如下图的配置界面:

2019-04-16 15-24-29 的屏幕截图.png

  • Repository Name 仓库名称
  • Repository Policy 读者可以根据自己的需要来配置该仓库是发布版本构件仓库还是快照版本构件仓库。
  • Storage 表示该仓库的存储目录,左边列表Blob Stores就是本地的存储目录创建,default是默认地址,都存储在/xxx/nexus3/sonatype-work/nexus3/blobs/下 。

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 这时看到如下图:

2019-04-16 15-29-50 的屏幕截图.png

对于代理仓库来说,最重要的是远程仓库地址即

  • 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仓库组

创建仓库组同其他的一样步骤是在选择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文件配置 代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

<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>
</project>

这样的配置只对当前的Maven项目有效,实际应用中,我们往往想要通过一次配置就完成能让本机所有的Maven项目都使用自己的Maven私服。这时配置本地仓库setting.xml 代码如下:

1
2
3
4
5
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
43
44
45
46
47
48
49
50
51
52
53
54
55

<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配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

<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 中配置认证信息代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

<servers>

<server>
<id>releases</id>
<username>admin</username>
<password>iapppay</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>iapppay</password>
</server>
</servers>

mvn 命令上传jar

setting.xml配置 server 信息

1
2
3
4
5
6
7
<servers>
<server>
<id>maxzhao</id>
<username>admin</username>
<password>maxzhao</password>
</server>
</servers>

pom.xml配置

1
2
3
4
5
6
7
8
9
10
11
12
13

<distributionManagement>
<repository>
<!-- 这里的ID要和setting的id一致 -->
<id>maxzhao</id>
<url>http://127.0.0.1:8081/repository/maxzhao/</url>
</repository>
<!--这是打成快照版本的配置 -->
<snapshotRepository>
<id>maxzhao</id>
<url>http://127.0.0.1:8081/repository/maxzhao/</url>
</snapshotRepository>
</distributionManagement>

执行mvn脚本

1
mvn deploy:deploy-file -DgroupId=gt.maxzhao -DartifactId=maxzhao-table -Dversion=1.0 -Dpackaging=jar -DrepositoryId=maxzhao -Dfile=/run/media/maxzhao/study/mycode-java/maxzhao/maxzhao-table/target/maxzhao-table-1.0-SNAPSHOT.jar  -Durl=http://127.0.0.1:8081/repository/maxzhao/
  • -DgroupId=jar包的组名
  • -DartifactId=jar包名称
  • -Dversion=jar包版本
  • -Dfile=jar包绝对路径
  • -DrepositoryId=yang

本文地址:Manjaro下安装Nexus

本文地址: https://github.com/maxzhao-it/blog/post/12074/