116

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?

Mahozad
  • 18,032
  • 13
  • 118
  • 133
Armand
  • 23,463
  • 20
  • 90
  • 119

9 Answers9

115

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'
}
granadaCoder
  • 26,328
  • 10
  • 113
  • 146
Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
  • 1
    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. – Armand Jul 18 '14 at 14:48
  • 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. – Armand Jul 18 '14 at 14:48
  • It's *example* 65.4 in the chapter you linked to. – Peter Niederwieser Jul 18 '14 at 14:49
  • 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. – Armand Jul 18 '14 at 14:57
  • 16
    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"`). – Peter Niederwieser Jul 18 '14 at 16:05
  • make sure you have ```plugin: 'maven-publish'``` otherwise you will get `Could not find method publishing()` – best wishes Feb 01 '18 at 05:19
  • 2
    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. – Farrukh Najmi Jan 25 '19 at 14:50
21

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)
}
Alex Dommasch
  • 211
  • 2
  • 4
  • 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. – Fodder Nov 14 '19 at 02:07
8

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'
        }
    }
}
Matthew Mark Miller
  • 3,128
  • 1
  • 16
  • 11
  • 3
    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. – kebernet Nov 16 '17 at 18:36
  • 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! – Matthew Mark Miller Nov 21 '17 at 17:02
7

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
        }
    }
}
OhadR
  • 8,276
  • 3
  • 47
  • 53
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

    }
ChinLoong
  • 1,735
  • 24
  • 26
  • archivesBaseName no effect – act262 Mar 20 '18 at 08:24
  • 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. https://docs.gradle.org/current/javadoc/org/gradle/api/plugins/BasePluginConvention.html – John Michelau Oct 23 '19 at 21:09
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"
Brent Bradburn
  • 51,587
  • 17
  • 154
  • 173
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.

Gray
  • 115,027
  • 24
  • 293
  • 354
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.

Mahozad
  • 18,032
  • 13
  • 118
  • 133
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
        }
    }
}
Alexis Tamariz
  • 646
  • 5
  • 4