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

9

118

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?

Pitching answered 18/7, 2014 at 14:43 Comment(0)
P
120

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'
}
Pulsatile answered 18/7, 2014 at 14:46 Comment(7)
Hi Peter, thanks for the response. Looking at the documentation I've linked to, which is also 65.4, we obviously have different versions of the doc.Pitching
Ah, it's in 65.2.3 of the 2.0 doc. My comprehension skills are obviously poor; I don't know how I missed this when searching the page for 'artifact' - apologies.Pitching
It's example 65.4 in the chapter you linked to.Pulsatile
Haha well I would, but I'd quite like to set the project name properly, rather than overriding it for MavenPublication, and somehow I'm struggling with that.Pitching
If you want to change the project name (which is kind of a separate question), you can do so in settings.gradle (e.g. rootProject.name = "something").Pulsatile
make sure you have plugin: 'maven-publish' otherwise you will get Could not find method publishing()Biannual
Important: Setting the project name in settings.gradle (e.g. rootProject.name = "org.myorg.myproject") was critical to fixing problems where Jenkins build was uploading to a groupId based defaulting to Jenkins workspace folder name.Bibliography
A
23

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)
}
Afro answered 25/3, 2016 at 21:25 Comment(1)
As mentioned by @FarrukhNajmi in a comment, adding the rootProject.name in the settings.gradle file stops the artifactID becoming the Jenkins job name when building with Jenkins.Formosa
T
9

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'
        }
    }
}
Takeoff answered 12/2, 2016 at 19:41 Comment(2)
This is a bad idea. Doing it here means that any dependencies you have declared against the project relatively from elsewhere will not get the correct artifactId when a pom is generated for them.Solder
I don't recall this being true (it has been 18 months since I dealt with this) but if so the correct answer is to upgrade your maven plugin to the new one with the better model. Thank you!Takeoff
N
8

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
        }
    }
}
Nora answered 5/4, 2016 at 4:59 Comment(1)
Could not find method jar() for argumentsBellina
K
3

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

    }
Krohn answered 5/9, 2016 at 2:36 Comment(2)
archivesBaseName no effectJanitress
In more recent versions of Gradle/Android, I'm finding that there is a setArchivesBaseName() method provided by BasePluginConvention that is available in place of direct assignment, and it works for me on Android Gradle plugin 3.1.4 and Gradle 4.10.2. docs.gradle.org/current/javadoc/org/gradle/api/plugins/…Gandy
E
2

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"
Eula answered 25/9, 2017 at 0:51 Comment(1)
jar.archiveName is deprecated as well as jar.baseNameKlaus
G
2

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.

Gordie answered 22/6, 2018 at 17:7 Comment(0)
R
1

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.

Refugiorefulgence answered 12/2, 2022 at 17:57 Comment(0)
S
0

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
        }
    }
}
Stripling answered 31/5, 2021 at 20:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.