How do you set the maven artifact ID of a gradle p

2019-03-08 01:29发布

问题:

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?

回答1:

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.



回答2:

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


回答3:

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)
}


回答4:

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
        }
    }
}


回答5:

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.



回答6:

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

    }


回答7:

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"


标签: maven gradle