使用nexus发布和获取依赖包

背景

开发分布式服务, 各服务会根据命名规范定义各自的消息协议, 某一服务A调用指定服务B时, 服务A需要依赖服务B的消息协议, 使用构建工具(maven/sbt/gradle)+nexus私服可以满足多服务并行开发时, 服务间消息协议构件(artifact)最新版本的发布和获取.

搭建nexus私服

详见参考文章.

仓库

仓库类型

  • group:组仓库,用于方便开发人员自己设定仓库, 该类型可以引用多个已有仓库.
  • hosted: 内部项目的发布仓库
    • maven-releases: 内部发布的正式版本, 即版本不带-SNAPSHOT标识.
    • maven-snapshots: 内部发布的快照版本, 发布上去的构件会自带实践戳, 当有客户端获取时, nexus会取最近生成的进行返回,版本带-SNAPSHOT标识
  • proxy: 从远程仓库中寻找数据的仓库, 例如: 中央库

配置多仓库

创建类型为group的仓库(命名为xxx-central), 同时把需要的远程仓库纳入其中, 目前纳入的仓库有maven-central, maven-releases, maven-snapshots

下载构件

maven

pom.xml文件的project节点下加入如下内容,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!-- nexus仓库 -->
<repositories>
<repository>
<id>xxx-central</id>
<name>xxx-central</name>
<!-- xxx-central为创建的类型为group的组仓库 -->
<url>http://192.168.10.180:8081/repository/xxx-central/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>

sbt

build.sbt文件末尾增加如下内容,

1
2
// xxx-central为创建的类型为group的组仓库
resolvers += "xxx-central" at "http://192.168.10.180:8081/repository/xxx-central/"

发布构件

  • 正式版: x.x.x, 例如: 1.1.0, 该版本构件将发布至maven-releases仓库中
  • 快照版:x.x.x-SNAPSHOT, 例如: 1.1.0-SNAPSHOT, 该版本构件将发布至maven-snapshots仓库中

maven

发布构件需要进行认证, 在maven的setting.xml文件中servers节点下增加如下配置

1
2
3
4
5
6
7
8
9
10
11
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>

<server>
<id>release</id>
<username>admin</username>
<password>admin123</password>
</server>

pom.xml文件的project节点下加入如下内容,

1
2
3
4
5
6
7
8
9
10
11
12
13
<!-- 发布jar到nexus -->
<distributionManagement>
<repository>
<id>release</id>
<name>Nexus Release</name>
<url>http://192.168.10.180:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>Nexus Snapshot</name>
<url>http://192.168.10.180:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>

发布指令

maven clean deploy

sbt

build.sbt中增加如下配置,

1
2
3
4
5
6
7
8
publishTo := {
val nexus = "http://192.168.10.180:8081/"
if (isSnapshot.value) {
Some("maven-snapshot" at nexus + "repository/maven-snapshots/")
} else {
Some("maven-releases" at nexus + "repository/maven-releases/")
}
}

显式配置credential

build.sbt中增加如下配置,

1
2
3
4
5
6
credentials += Credentials(
"Sonatype Nexus Repository Manager",
"192.168.10.180:8081",
"admin",
"admin123"
)

隐式配置credential

新建sonatype.sbt文件, 并将其放置于sbt目录下, 内容如下

1
2
3
4
5
6
credentials += Credentials(
"Sonatype Nexus Repository Manager",
"192.168.10.180:8081",
"admin",
"admin123"
)

发布指令

  • 发布本地: sbt publish
  • 发布至远程: sbt publishSigned

详见: 发布Scala构件至Maven中央库

注意事项

以上配置针对nexus 3.x.x, 2.x.x3.x.x差异如下,

参考文章

转载

本文出自<<arccode>>, 欢迎转载, 转载请注明出处.