[maven-publish plugin] How to change the artifact id without publishing twice or changing the directory name?

I have a multi-project repository. In this repository, there is a folder/project called storage-api. Using the maven-publish plugin, this folder/project produces a JAR named storage-api. The JAR is referenced by another project in my repository, and can also be used by projects outside of my repository.

I would like to publish the JAR named as xyz-storage-api instead of storage-api but without changing the directory name.

I have tried to change the build.gradle file like so:

publishing {
    publications {
        maven(MavenPublication) {
            artifactId = 'xyz-storage-api'
            from components.java
        }
    }

    ...
}

But when I run ./gradlew clean publishToMavenLocal, I can see that two copies of the artifact are published. One is storage-api JAR, and the other is xyz-storage-api JAR. But I only want to publish the xyz-storage-api JAR.

Additionally, I am worried that if the artifact id is only changed on publishing, the artifact would not be included correctly as a dependency in my other project’s POM file.

I have also tried to create a settings.gradle file for the project, but it didn’t work either.

Any suggestions on how I can resolve this issue? thanks

1 Like

If you are seeing two different artifacts/jars being published then that suggests there are two publications configured. If you run gradlew tasks do you see multiple publishPubABCPublicationToRepoRepository tasks that represent multiple publications? Are you applying any plugins that might already be configuring a publication?

By default the component/artifact identifier of the project uses the project name, so the simplest way is to change the project name and leave all the default jar and publication behaviour. This can be done without changing the directory name, however any project dependencies would need to use the new name, ie project(':xyz-storage-api'). Command line references to the project would also use that name, ie ./gradlew xyz-storage-api:dependencies.

If storage-api is the root project, then in settings.gradle:

rootProject.name = 'xyz-storage-api'

If a subproject:

include( 'storage-api' )
project( ':storage-api' ).name = 'xyz-storage-api'
2 Likes

If you are seeing two different artifacts/jars being published then that suggests there are two publications configured. If you run gradlew tasks do you see multiple publishPubABCPublicationToRepoRepository tasks that represent multiple publications? Are you applying any plugins that might already be configuring a publication?

Why didn’t I think of this? This was exactly the issue - I had multiple plugins for publishing enabled. By doing this:

publishing {
    publications {
        otherPub(MavenPublication) {
            artifactId = 'xyz-storage-api'
            from components.java
        }
    }

    ...
}

It worked perfectly well. Thank you