This guide will walk you through the process of creating a Java library in a Maven project, push a built artifact to Gemfury, and then import it as a dependency in another Maven project.
Before you get started, be sure you have the following:
Maven is a project management tool, commonly used in the Java ecosystem. It can be used to specify project information for Java libraries, so the library can easily be linked in other Maven projects. Maven is also used to manage dependencies, so libraries can be fetched and integrated into a project.
…
To get started in creating your Java library, initialize your Maven project with mvn archetype:generate
:
$ mvn archetype:generate -DgroupId=com.mycompany.foo -DartifactId=foolib \
-DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 \
-DinteractiveMode=false
$ cd foolib
Please take note to use your own values for groupId
and artifactId
.
This command will create an initial pom.xml
file to describe your project. The file will contain the project’s name and its dependencies.
At this point, you may customize the generated pom.xml
with the info of your project (e.g. version
, url
). When ready, be sure to commit pom.xml
into your project’s Git repo.
…
Now that you have created your Java library, you can now push the generated artifact into your Gemfury account. To do that, you can use the Maven deploy function.
First, you will need to configure your Maven project. Add a new repository
entry in the distributionManagement
section on the pom.xml
of your project with your Repository URL. The distributionManagement
tag should be a direct child of your project
tag.
…
Now that you have uploaded the artifact containing your Java library in your Gemfury account, you can use your library as a dependency in a Maven project.
To use your library, you can reference the artifact as a dependency in your project’s pom.xml
.
<project xmlns=...>
...
<dependencies>
<dependency>
<groupId>com.mycompany.foo</groupId>
<artifactId>foolib</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
</dependencies>
...
</project>
Next, you will need to add your Gemfury account Maven’s repository, so Maven can find the artifact.
…
Gradle has built-in support to upload packages to Maven repositories using its publish plugin. Your Gemfury account is fully compatible with this method.
To configure Gradle, add a new maven
type repository entry in the repositories
section (part of publishing
configuration) in the build.gradle.kts
of your project with your Repository URL.
The resulting build.gradle.kts
should include this:
plugins {
id("maven-publish")
}
publishing {
repositories {
maven {
name = "furyMaven"
url = uri("https://maven.fury.io/ACCOUNT/")
credentials {
username = System.getenv("FURY_PUSH_USER")
password = System.getenv("FURY_PUSH_TOKEN")
}
}
}
publications {
register("mavenJava", MavenPublication::class) {
from(components["java"])
}
}
}
The publications
configuration tells Gradle to build a maven type (e.g. MavenPublication
) package containing Java code. This is the instruction which produces the Jar file that Gradle will upload to your Gemfury account.
…
You can use a library from your Gemfury account as a dependency in a Gradle project. To do that, you can reference the artifact as a dependency in your project’s build.gradle.kts
:
dependencies {
implementation("com.example.group:artifact")
}
Next, you will add your Gemfury Gradle repository:
repositories {
maven {
url = uri("https://maven.fury.io/ACCOUNT/")
}
}
If the artifact is set to public, no other settings are needed. If the artifact is set to private, you will need to expand the above entry with this authentication configuration:
…