[JAVA] Build a Maven in-house repository on Google Cloud Storage

Introduction

This is a summary of how to build and use Maven in-house repository on Google Cloud Storage (GCS). It feels like a second brew of what is written in the document, but I could not find an article that comprehensively summarizes how to make an in-house repository, how to transfer the jar file after making it, and how to use it, so as a memorandum I will leave it.

By the way, since I am a Gradle user, I will summarize the method when using Gradle.

Someone who might want to give it a try

How to build and how to use

Gradle documentation

The official documentation is from here. By the way, according to Release Notes, it seems to be a feature added in version 4.2. .. It's been around for quite some time, I didn't know ... : sweat_drops:

Building an in-house repository

Just type a command to create a bucket.

$ export BUCKET_NAME='your-inhouse-repository'
$ gsutil mb -c standard -l asia-northeast1 gs://${BUCKET_NAME}

This is all you need to do it by yourself. If you are developing with multiple people, give the developer access to the bucket.

Below, the bucket name is your-inhouse-repository, cut the path for Maven maven under it, and talk on the assumption that Gradle refers to gcs: // your-inhouse-repository / maven. To proceed.

Authentication method

Since it is an in-house repository, it will not be published to the outside. It authenticates so that only accounts with permissions on the bucket can access it, but the documentation says:

When using a Google Cloud Storage backed repository default application credentials will be used with no further configuration required

What is "Application Default Credentials"? It seems that there is such a concept. It's called ADC in GCP.

When working locally, you can do gcloud init and gcloud auth application-default login. Then the ADC and stuff are in the right place, and Gradle will use it to authenticate.

$ gcloud init
$ gcloud auth application-default login

Upload jar file

This is a sample code on the library side. You can upload the jar file using the Maven Publish Plugin (https://docs.gradle.org/current/userguide/publishing_maven.html).

build.gradle


plugins {
    id 'java-library'
    id 'maven-publish'
}

group = 'your.libs'
version = '1.0.0'

publishing {
    publications {
        maven(MavenPublication) {
            from components.java
        }
    }
    repositories {
        //Specify the in-house repository as the upload destination.
        maven {
            url 'gcs://your-inhouse-repository/maven'
        }
    }
}

settings.gradle


rootProject.name = 'example-library'
$ gradle publish

Using jar files

This is the sample code on the side that uses the library. Simply add the referenced in-house repositories to repositories and define the dependencies in dependencies.

build.gradle


repositories {
    //Add the in-house repository as a reference.
    maven {
        url 'gcs://your-inhouse-repository/maven'
    }
}

dependencies {
    //All you have to do is add the jar as a dependency and Gradle will find it for you.
    implementation 'your.libs:example-library:1.0.0'
}

Tips when authentication information does not switch

I have multiple GCP accounts for personal and business use, but even if I switch credentials by doing gcloud init and gcloud auth application-default login, the information before Gradle switches I encountered the problem of referring to ... I didn't know the cause and wasted about a day. : sweat_drops:

** It's simple once you know it, but the cause was that the Gradle daemon was running and it was holding the old credentials. ** **

So once you stop the daemon, it will use the credentials after switching.

$ gradle --stop

Or if you explicitly tell them not to use the daemon, they will look up your current credentials.

$ gradle publish --no-daemon

Digression

As of July 25, 2020, a beta version of a service called Artifact Registry that specializes in managing build artifacts is available. It seems that it can also be used as Maven repository, but this is still like the alpha version, so I'm looking forward to it from now on. ..

Recommended Posts

Build a Maven in-house repository on Google Cloud Storage
Build a Maven repository on AWS S3
Build a Ruby on Rails development environment on AWS Cloud9
A memorandum when running Apache Maven on an in-house proxy
Build a data processing environment with Google Cloud DataFlow + Pub / Sub
Build a Minecraft server on AWS
Build a XAMPP environment on Ubuntu
How to create a Maven repository for 2020
Build a Java development environment on Mac
Build Java 8 development environment on AWS Cloud9
Build a JMeter environment on your Mac
[Android] [Java] Download images on GCS (Google Cloud Storage) with Stream using Glide
Build a test flow on CircleCI using Jib
Build a DHCP and NAT router on Ubuntu 16.04
Build a streaming server on your iOS app
Build a Laravel environment on an AWS instance
I tried setting up a Maven remote repository
Build a Java runtime environment on Sakura VPS
How to build a Pytorch environment on Ubuntu