Publish a Java library to a Maven repository
A purpose of this tutorial is to demonstrate how to publish a Java library created in the Gradle project to a local Maven repository and then to the remote one using IntelliJ IDEA.
Let's start with creating a Gradle project.
Create a new Gradle project
- On the Welcome screen, select New Project.
If your starting point is a project that is already opened in IntelliJ IDEA then from the main menu, select
. On the page that opens, select Gradle, leave the default options and click Next.
- On the page that opens let's enter the name for our project. In our case it is gradle-publish, leave the rest of the options as default and click Finish.
IntelliJ IDEA creates a Gradle project and enables the Gradle tool window.
Now let's tweak the build.gradle file a little since we need to add support for a Java library and build our project.
Edit build.gradle
- In the Project tool winow, double-click the build.gradle file to open it in the editor.
At this point build.gradle contains the following code:
plugins { id 'java' } group 'org.example' version '1.0-SNAPSHOT' repositories { mavenCentral() } dependencies { testCompile group: 'junit', name: 'junit', version: '4.12' } In the
plugins
section, change'java'
to'java-library'
.plugins { id 'java-library' } group 'org.example' version '1.0-SNAPSHOT' repositories { mavenCentral() } dependencies { testCompile group: 'junit', name: 'junit', version: '4.12' }Click in the editor to load the changes to your project.
Use the src/main/java directory to add code for your library.
Now in the Gradle tool window, click the project node, click Tasks and then build.
- In the list that opens, double-click build to execute the build task that will generate our .jar file.
As a result, we have a generated .jar file located in the Project tool window, inside the build/libs directory.
Now let's follow the Maven conventions and specify Maven coordinates for our library. Since IntelliJ IDEA has already added GroupId
and Version
when we created our project, the only thing that we need to change is ArtifactId
.
Change the ArtifactId and generate the JAR file
In the Project tool window, double-click the settings.gradle file to open it in the editor. Change
rootProject.name
fromgradle-publish
tomy-artifact-id
.rootProject.name = 'my-artifact-id'Click to load the changes to your project.
In the Gradle tool window, click Tasks.
- In the build directory first double-click the clean task to execute it and then execute the build task.
IntelliJ IDEA will generate a .jar file with the information that is inline with the Maven naming conventions and the updated artifact name.
Now let's work with our build script further and publish the library into a local Maven repository.
Publish the library to a local Maven repository
Open the build.gradle file and add
id 'maven-publish'
to theplugins
section.Click to load the changes to your project.
In the Gradle tool window, in the publishing section double-click publishToMavenLocal to run the task.
We can edit the build.gradle file further to publish our library to the remote repository.
Publish to the remote repository
In the build.gradle file add the following section:
publishing { publications { } repositories { maven { name = "MyRepo" // optional target repository name url = "http://my.org.server/repo/url" credentials { username = 'alice' password = 'my-password' } } } }Click to load the changes to your project.
In the Gradle tool window, open the publishing section, and double-click publishAllPublicationsToMyRepository to run the task.
For more information on how to, for example, customize the POM file, use a different snapshot, or release repositories, see Gradle documentation.