JFrog Artifactory搭建Maven私有仓库

在本书技术架构中,我们选用了Gradle做为Java的依赖管理工具。

实际上,Gradle只提供了构建的前端,实际使用的还是Maven仓库。

出于安全性、速度等因素的考量,我们需要配置私有的Maven仓库。

本节,我们将基于JFrog Artifactory,搭建私有的Maven仓库。

运行

启动脚本如下:

  1. #!/bin/bash
  2. NAME="artifactory"
  3. PUID="1000"
  4. PGID="1000"
  5. VOLUME="$HOME/docker_data/artifactory"
  6. mkdir -p $VOLUME
  7. docker ps -q -a --filter "name=$NAME" | xargs -I {} docker rm -f {}
  8. docker run \
  9. --hostname $NAME \
  10. --name $NAME \
  11. --volume $VOLUME:/var/opt/jfrog/artifactory \
  12. --env PUID=$PUID \
  13. --env PGID=$PGID \
  14. -p 8081:8081 \
  15. -p 8082:8082 \
  16. --detach \
  17. --restart always \
  18. releases-docker.jfrog.io/jfrog/artifactory-oss:latest

启动的时间会略长,可以访问 http://127.0.0.1:8082/ui/ 登录,默认的用户名是admin,密码是password。

配置

我们首先设置ldap关联,打开如下菜单:Administration -> Security -> LDAP -> Add Setting

  • LDAP URL:ldap://10.1.172.136:389/dc=coder4,dc=com

  • User DN Pattern:cn={0},ou=rd

  • Manager DN:cn=readonly,dc=coder4,dc=com

  • Manager Password:readonly123

如下图所示:

f

接着,我们打开匿名访问权限(默认是关闭的),位于:

Administration -> Security -> Security onfiguration
选中Allow Anonymous Access,然后点击保存。如下图所示

f

最后,我们新建一个仓库:

Repositories -> Add Repositories -> Local Repositories

  • Key:homs-release / homs-snapshot

  • Package Type:Gradle

  • Repository Key:homs

  • Handle Releases / Handle Snapshots

同时,需要给用户配置权限:

  • Application -> Regisigrition -> Artifacts

至此,仓库侧的配置已经完成。

使用私有Maven仓库

首先,添加本地全局配置:

修改文件:~/.gradle/gradle.properties

  1. mavenReleaseRepo=http://127.0.0.1:8082/artifactory/homs-release/
  2. mavenSnapshotRepo=http://127.0.0.1:8082/artifactory/homs-snapshot/
  3. mavenUsername=zhangsan
  4. mavenPassword=123456

上述配置了私有仓库地址,测试和发布是分开的

接下来,我们在项目中修改

homs-demo/build.gradle:

  1. plugins {
  2. id 'java'
  3. id 'idea'
  4. id 'org.springframework.boot' version '2.5.3' apply false
  5. id 'io.spring.dependency-management' version '1.0.11.RELEASE' apply false
  6. id "io.freefair.lombok" version "6.1.0" apply false
  7. }
  8. subprojects {
  9. group = 'com.coder4'
  10. version = '0.0.1-SNAPSHOT'
  11. sourceCompatibility = '1.8'
  12. apply plugin: 'java'
  13. apply plugin: 'maven-publish'
  14. publishing {
  15. publications {
  16. "$project.name"(MavenPublication) {
  17. groupId project.group
  18. artifactId project.name
  19. version project.version
  20. from components.java
  21. }
  22. }
  23. repositories {
  24. maven {
  25. credentials {
  26. username mavenUsername
  27. password mavenPassword
  28. }
  29. url = version.endsWith('SNAPSHOT') ? mavenSnapshotRepo : mavenReleaseRepo
  30. }
  31. }
  32. }
  33. }

如上所示,我们修改了主项目中的配置

  • publising会为每个子项目添加发布任务

  • repositoreis指定了发布的私有仓库地址

在子项目中,我们需要略作修改,如下:

homs-client/build.gradle

  1. plugins {
  2. id 'java'
  3. // id 'io.spring.dependency-management'
  4. }
  5. dependencies {
  6. implementation platform('com.coder4:bom-homs:1.0')
  7. implementation platform('org.springframework.boot:spring-boot-dependencies:2.5.3')
  8. implementation 'com.google.protobuf:protobuf-java'
  9. implementation "io.grpc:grpc-stub"
  10. implementation "io.grpc:grpc-protobuf"
  11. implementation 'io.grpc:grpc-netty-shaded'
  12. implementation "org.slf4j:slf4j-api"
  13. implementation 'com.alibaba.nacos:nacos-client:2.0.3'
  14. implementation 'org.springframework.boot:spring-boot-autoconfigure:2.2.0.RELEASE'
  15. }

上述修改,去掉了spring dependency这个插件,转而使用platform模式。

这是一个spring + maven-publish插件共同使用导致的bug,建议都用platform来解决。

最后,我们尝试发布:

  1. gradle publish
  2. > Task :publishHoms-demo-clientPublicationToMaven2Repository
  3. Cannot upload checksum for snapshot-maven-metadata.xml because the remote repository doesn't support SHA-512. This will not fail the build.
  4. Cannot upload checksum for module-maven-metadata.xml because the remote repository doesn't support SHA-512. This will not fail the build.
  5. > Task :publishHoms-demo-serverPublicationToMaven2Repository
  6. Cannot upload checksum for snapshot-maven-metadata.xml because the remote repository doesn't support SHA-512. This will not fail the build.
  7. Cannot upload checksum for module-maven-metadata.xml because the remote repository doesn't support SHA-512. This will not fail the build.
  8. BUILD SUCCESSFUL in 2s
  9. 4 actionable tasks: 4 executed

成功!

引入依赖

首先配置全局依赖:

~/.gradle/init.gradle

  1. // project
  2. allprojects{
  3. repositories {
  4. mavenLocal()
  5. maven { url mavenSnapshotRepo }
  6. maven { url mavenReleaseRepo }
  7. maven { url 'https://maven.aliyun.com/repository/public/' }
  8. maven { url 'https://maven.aliyun.com/repository/jcenter/' }
  9. maven { url 'https://maven.aliyun.com/repository/google/' }
  10. maven { url 'https://maven.aliyun.com/repository/gradle-plugin/' }
  11. maven { url 'https://jitpack.io/' }
  12. }
  13. }
  14. // plugin
  15. settingsEvaluated { settings ->
  16. settings.pluginManagement {
  17. // Print repositories collection
  18. // println "Repositories names: " + repositories.getNames()
  19. // Clear repositories collection
  20. repositories.clear()
  21. // Add my Artifactory mirror
  22. repositories {
  23. mavenLocal()
  24. maven {
  25. url "https://maven.aliyun.com/repository/gradle-plugin/"
  26. }
  27. }
  28. }
  29. }

在下游中修改homs-start中添加依赖,和往常一样:

  1. plugins {
  2. id 'org.springframework.boot' version '2.5.6'
  3. id 'io.spring.dependency-management' version '1.0.11.RELEASE'
  4. id 'java'
  5. id 'maven-publish'
  6. }
  7. group = 'com.homs'
  8. version = '0.0.1-SNAPSHOT'
  9. sourceCompatibility = '1.8'
  10. repositories {
  11. mavenCentral()
  12. }
  13. dependencies {
  14. implementation 'org.springframework.boot:spring-boot-starter-web'
  15. testImplementation 'org.springframework.boot:spring-boot-starter-test'
  16. implementation 'com.coder4:homs-demo-client:0.0.1-SNAPSHOT'
  17. }
  18. test {
  19. useJUnitPlatform()
  20. }

尝试构建,成功!

  1. gradle build
  2. BUILD SUCCESSFUL in 7s

至此,我们成功引入了基于私有Maven仓库。