How to add custom tags when publishing maven artifacts using Gradle?
- User
How to add custom tags when publishing maven artifacts using Gradle?
- Assistant
How to add custom tags when publishing maven artifacts using Gradle?
To add custom tags when publishing Maven artifacts using Gradle, you typically want to customize the generated pom.xml file or add metadata to the published artifact. Gradle uses the Maven Publish Plugin for this purpose.
If by "custom tags" you mean adding custom XML elements to the generated pom.xml, you can do this using the withXml block in your publishing configuration.
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
pom.withXml {
asNode().appendNode('myCustomTag', 'myCustomValue')
// Or for nested tags:
def custom = asNode().appendNode('customSection')
custom.appendNode('tag1', 'value1')
custom.appendNode('tag2', 'value2')
}
}
}
}
This will add the following to your generated pom.xml:
<myCustomTag>myCustomValue</myCustomTag>
<customSection>
<tag1>value1</tag1>
<tag2>value2</tag2>
</customSection>
If you want to add custom properties (key-value pairs) to the <properties> section of the POM:
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
pom {
properties = [
'my.custom.property': 'someValue',
'another.property': 'anotherValue'
]
}
}
}
}
This will result in:
<properties>
<my.custom.property>someValue</my.custom.property>
<another.property>anotherValue</another.property>
</properties>
If you want to add custom tags/metadata to the artifact in a repository like Nexus or Artifactory, this is usually done via repository-specific configuration or REST APIs, not via Gradle itself. Gradle's publishing plugins do not natively support adding arbitrary metadata/tags to the repository entry beyond what is in the POM.
| Goal | How to do it in Gradle |
|---|---|
| Custom XML in POM | pom.withXml { ... } |
| Custom properties in POM | pom { properties = [...] } |
| Custom tags in repository | Use repository-specific tools or APIs (not via Gradle) |
If you have a specific repository or a specific kind of "tag" in mind (e.g., Artifactory build info, Nexus labels, etc.), let me know for more targeted advice!