Host Your Maven Artifacts Using Google Cloud Storage
If your team uses Java and Google Cloud, it might be convenient to explore using Google Cloud for your Maven artifacts.
Join the DZone community and get the full member experience.
Join For FreeIf you use Google Cloud and you use Java for your projects, then Google Cloud Storage is a great place to host your team's artifacts.
It is easy to set up and pretty cheap. It is also much simpler than setting up one of the existing repository options (JFrog, Nexus, or Archiva, for example) if you are not particularly interested in their features.
To get started you need to specify a Maven wagon which supports Google Cloud Storage. We will use the Google Storage wagon.
Let’s get started by creating a Maven project:
mvn archetype:generate -DgroupId=com.test.apps -DartifactId=GoogleWagonTest -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
We are going to add a simple service.
package com.test.apps;
public class HelloService {
public String sayHello() {
return "Hello";
}
}
Then we are going to add the Maven wagon which will upload and fetch our binaries to the Google Cloud Storage.
<build>
<extensions>
<extension>
<groupId>com.gkatzioura.maven.cloud</groupId>
<artifactId>google-storage-wagon</artifactId>
<version>1.0</version>
</extension>
</extensions>
</build>
Then we shall create the Google Cloud Storage bucket which will host our artifacts.
Our bucket will be called "mavenrepository."
Now that we have set up our bucket in Google we shall set the distribution management on our Maven project.
<distributionManagement>
<snapshotRepository>
<id>my-repo-bucket-snapshot</id>
<url>gs://mavenrepository/snapshot</url>
</snapshotRepository>
<repository>
<id>my-repo-bucket-release</id>
<url>gs://mavenrepository/release</url>
</repository>
</distributionManagement>
From the Maven documentation:
"Whereas the repositories element specifies in the POM the location and manner in which Maven may download remote artifacts for use by the current project, distributionManagement specifies where (and how) this project will get to a remote repository when it is deployed. The repository elements will be used for snapshot distribution if the snapshotRepository is not defined."
The next step is the most crucial and this has to to do with authenticating to Google Cloud.
You need to have the gcloud command line setup in your system and you must issue a login
‘gcloud auth login –brief’ with an account that has access to the bucket we created previously.
The other way is to use the GOOGLE_APPLICATION_CREDENTIALS
environmental variable. You can use this GOOGLE_APPLICATION_CREDENTIALS
in order to set the path to your Google application credentials file. The credentials file should also be able to access the bucket we created previously.
And now the easiest part, which is deploying:
mvn deploy
Now since your artifact has been deployed you can use it in another repo by specifying your repository and your wagon.
<repositories>
<repository>
<id>my-repo-bucket-snapshot</id>
<url>gs://mavenrepository/snapshot</url>
</repository>
<repository>
<id>my-repo-bucket-release</id>
<url>gs://mavenrepository/release</url>
</repository>
</repositories>
<build>
<extensions>
<extension>
<groupId>com.gkatzioura.maven.cloud</groupId>
<artifactId>google-storage-wagon</artifactId>
<version>1.0</version>
</extension>
</extensions>
</build>
That’s it! Next thing you know, your artifact will be downloaded by Maven through Google Cloud Storage and used as a dependency in your new project.
Published at DZone with permission of Emmanouil Gkatziouras, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments