How do you set the maven artifact ID of a Gradle project?

MavenGradleMaven Publish

Maven Problem Overview


From the gradle maven-publish plugin's documentation, it's clear that you set the groupId and version of the project directly in build.gradle:

group = 'org.gradle.sample'
version = '1.0'

However, the artifactId appears to be taken from the name of the folder you are working within. Is there a way to set the artifactId explicitly?

Maven Solutions


Solution 1 - Maven

From 36.2.3. Identity values in the generated POM

publishing {
    publications {
        maven(MavenPublication) {
            groupId 'org.gradle.sample'
            artifactId 'project1-sample'
            version '1.1'

            from components.java
        }
    }
}

The artifact ID defaults to the project name configured in settings.gradle, which in turn defaults to the project directory's name.

You'll need the appropriate plugin.

plugins {
    id 'maven-publish'
}

Solution 2 - Maven

Related to the root settings.gradle file, you can change the name of the root project with:

rootProject.name = 'myproject'

But if you want to change the name of a sub-project (for example, the default "app" sub-project of an AndroidStudio project), you can do something like this, still in the root settings.gradle file:

rootProject.children.each {
    it.name = ('app' == it.name ? 'MyAppName' : it.name)
}

Solution 3 - Maven

This is the correct answer for the maven-publish plugin. This is intended as the successor for the older maven plugin.

If, as I am, you are stuck with the older plugin, the correct answer to "How do I set the maven artifact id for a gradle project" is:

uploadArchives {
	repositories {
		mavenDeployer {
			pom.artifactId = 'project-sample'
		}
	}
}

Solution 4 - Maven

If you have a multi-module project, and you want the artifacts' names to differ from the Directory (which is set in the settings.gradle), then I think a better approach is to have a jar block for each sub-project, and there you can write the baseName, which will be the artifact-id. Then, rather than re-writing the publishing/publications block for each sub-project, you write it only once in the main build.gradle this way:

for each sub-project build.gradle:

jar {
    baseName = 'new-artifact-name-A'  //A beacause you also have B, C modules... 
}

in the main build.gradle:

publishing {
    publications {
        mavenJava(MavenPublication) {
           artifactId jar.baseName
           from components.java
        }
    }
}

Solution 5 - Maven

For building android and publishing to artifactory using jenkins, I configured the settings below in the app modules's build.gradle for configuring group id, artifact id, and version.

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    group "com.company.division.productgroup" //add group id
    version "8.8.8" //add version

    defaultConfig {

        minSdkVersion 9
        targetSdkVersion 21
        versionCode 32
        versionName "$version"
        archivesBaseName = "android-appname" //add artifact id

    }

Solution 6 - Maven

> However, the artifactId appears to be taken from the name of the folder you are working within. Is there a way to set the artifactId explicitly?

A simple answer to this is to set the jar.baseName which then overrides the directory name.

// changes the name of the jar from the directory name
jar.baseName = 'some_arifact_name';

This seems to work for me.

Solution 7 - Maven

In Gradle, you can set jar.archiveName to override the use of the working folder's name...

group = 'com.example'
version = '0.0.1-SNAPSHOT'
jar.archiveName = "myproject-0.0.1-SNAPSHOT.jar"

Solution 8 - Maven

You can add a conditional to change your artifactId as well.

publishing {
    publications {
        maven(MavenPublication) {

            //adding conditional
            artifactId = artifactId == 'original-name' ? 'my-specific-name' : artifactId

            from components.java
        }
    }
}

Also you can add a switch if you have many project names to change.

publishing {
    publications {
        maven(MavenPublication) {

            //adding switch
            switch(artifactId) {
                case 'first-project-original-name':
                    artifactId = 'my-first-specific-name'
                    break
                case 'second-project-original-name':
                    artifactId = 'my-second-specific-name'
                    break
                default:
                    break
            }

            from components.java
        }
    }
}

Solution 9 - Maven

This is how I did that for my Android library. The solution is in Kotlin DSL (build.gradle.kts):

plugins {
    id("maven-publish")
    // ...
}

afterEvaluate {
    publishing {
        publications {
            create<MavenPublication>("Release") {
                // ...
                groupId = "ir.mahozad.android"
                artifactId = "pie-chart"
                version = project.version.toString()
                artifact(sourcesArtifact)
                artifact(javadocArtifact)
                // ...
            }
        }
    }
}

You can see the complete build script file here.

See a similar answer here.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionArmandView Question on Stackoverflow
Solution 1 - MavenPeter NiederwieserView Answer on Stackoverflow
Solution 2 - MavenAlex DommaschView Answer on Stackoverflow
Solution 3 - MavenMatthew Mark MillerView Answer on Stackoverflow
Solution 4 - MavenOhadRView Answer on Stackoverflow
Solution 5 - MavenChinLoongView Answer on Stackoverflow
Solution 6 - MavenGrayView Answer on Stackoverflow
Solution 7 - MavenBrent BradburnView Answer on Stackoverflow
Solution 8 - MavenAlexis TamarizView Answer on Stackoverflow
Solution 9 - MavenMahozadView Answer on Stackoverflow