如何设置 Gradle 项目的 maven 工件 ID?

从级别 Maven 发布插件的文档,很明显,你设置的 groupIdversion的项目直接在 build.gradle:

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

但是,artifactId似乎取自您正在使用的文件夹的名称。有明确设置 artifactId的方法吗?

117515 次浏览

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

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

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

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

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


}

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"

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.

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

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.