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.
** People who are already using GCP for development / operation ** All you have to do is create a GCS bucket and set permissions, so it's easy to install!
** People who are tired of configuring Gradle subprojects using Git submodules ** It's easy because you can use it as if you were pulling a jar file from Maven Central!
** People who have already set up in-house repositories such as Sonatype Nexus but are tired of maintenance / maintenance ** GCS is a managed service, so Google will take care of it!
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:
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.
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
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
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'
}
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
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