IntelliJ IDEA 2021.1 Help

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

  1. On the Welcome screen, select New Project.
    Welcome Screen

    If your starting point is a project that is already opened in IntelliJ IDEA then from the main menu, select File | New | Project.

  2. On the page that opens, select Gradle, leave the default options and click Next.

    New Project dialog: select Gradle

  3. 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.
    New Project dialog: project name

    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

  1. 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' }
  2. 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' }

  3. Click Gradle common icons gradle load changes in the editor to load the changes to your project.

  4. Use the src/main/java directory to add code for your library.

  5. Now in the Gradle tool window, click the project node, click Tasks and then build.

  6. In the list that opens, double-click build to execute the build task that will generate our .jar file.
    the Gradle tool window

    As a result, we have a generated .jar file located in the Project tool window, inside the build/libs directory.

    the Project tool window

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

  1. In the Project tool window, double-click the settings.gradle file to open it in the editor. Change rootProject.name from gradle-publish to my-artifact-id.

    rootProject.name = 'my-artifact-id'

  2. Click Gradle common icons gradle load changes to load the changes to your project.

  3. In the Gradle tool window, click Tasks.

  4. 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.

    the Project tool window

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

  1. Open the build.gradle file and add id 'maven-publish' to the plugins section.

  2. Click Gradle common icons gradle load changes to load the changes to your project.

  3. 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

  1. 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' } } } }

  2. Click Gradle common icons gradle load changes to load the changes to your project.

  3. In the Gradle tool window, open the publishing section, and double-click publishAllPublicationsToMyRepository to run the task.

    the Gradle tool window: publish task

For more information on how to, for example, customize the POM file, use a different snapshot, or release repositories, see Gradle documentation.

Last modified: 16 June 2021