How to set packaging to pom in Gradle instead of defaulting to jar - maven

I have a project that generates a Bill of Materials (BOM). When I execute gradle build it generates an empty jar, containing only a META-INF folder.
However I am able to publish the pom (BOM) to Nexus correctly, with the side effect of also uploading the empty jar.
According to the maven plugin doc https://docs.gradle.org/current/userguide/maven_plugin.html we should be able to set the packaging:
packaging archiveTask.extension
Here, uploadTask and archiveTask refer to the tasks used for uploading
and generating the archive
How can I set the packaging to pom?
Example of Gradle uploaded pom:
<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ttt.a</groupId>
<artifactId>my-bom</artifactId>
<version>Something-SNAPSHOT</version>
When I upload it with maven instead of gradle, there is an additional:
<packaging>pom</packaging>
UPDATE:
Full build.gradle config:
buildscript {
repositories {
maven {
url "http://myrepo"
}
}
dependencies {
classpath "io.spring.gradle:dependency-management-plugin:1.0.4.RELEASE"
classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.5"
classpath 'org.asciidoctor:asciidoctor-gradle-plugin:1.5.7'
} }
apply plugin: 'java' apply plugin: 'maven' apply plugin: "io.spring.dependency-management" apply plugin: "jacoco" apply plugin: 'org.asciidoctor.convert' apply plugin: 'org.sonarqube'
group = project.properties['groupId'] version = project.properties['version'].toString()
description = """Bill of Materials"""
sourceCompatibility = 1.8 targetCompatibility = 1.8
ext {
xxx = '1.0.0'
yyy = '1.2.0'
... }
repositories {
maven {
url "http://myrepo"
} }
dependencyManagement {
dependencies {
dependency "com.myorg:xxx:${xxx}"
dependency "com.myorg:yyy:${yyy}"
...
} }
uploadArchives {
repositories {
mavenDeployer {
snapshotRepository(url: 'http://myrepo') {
authentication(userName: "$System.env.NEXUS_USER", password: "$System.env.NEXUS_PASSWORD")
}
}
} }
asciidoctor {
sourceDir = file('src/docs/asciidoc/')
sources {
include '*.adoc'
}
outputDir = file("build/docs/${version}") }
task generateDummyBom {
doLast {
project.buildDir.mkdirs()
new File("$project.buildDir/dummy.pom").write("<project></project>\n")
}
ext.bomFile = file("$project.buildDir/dummy.pom") }
artifacts {
archives(generateDummyBom.bomFile) {
builtBy generateDummyBom
} }
jar.enabled = false

I found that the maven plugin seems to ignore the packaging property. After some experimentation, I found that it sets the packaging property to the extension of the file in your artifact. So, the way to get the packaging property set to pom is to create a dummy artifact with a file having a .pom extension, as below.
// The real file that we want to publish is the pom generated implicitly by the
// maven publishing plugin.
//
// We need to generate at least one file that we can call an archive so that the
// maven plugin will actually publish anything at all. Luckily, if the file
// that we call an archive is a .pom file, it's magically discarded, and we are
// only left with the implicitly-generated .pom file.
//
// We need the extension of the file to be `.pom` so that the maven plugin will
// set the pom packaging to `pom` (i.e. `<packaging>pom</packaging>`). Otherwise,
// packaging would be set to `xml` if our only file had an `.xml` extension.
task generateDummyBom {
doLast {
// Since we don't depend on anything else, we have to create the build dir
// ourselves.
project.buildDir.mkdirs()
// The file actually has to have xml in it, or Sonatype will reject it
new File("$project.buildDir/${project.artifactId}.pom").write("<project></project>\n")
}
ext.bomFile = file("$project.buildDir/${project.artifactId}.pom")
}
artifacts {
archives(generateDummyBom.bomFile) {
builtBy generateDummyBom
}
}
jar.enabled = false
Update: If you apply the java plugin, you will need to remove the jar archive from your archives.
// Remove the default jar archive which is added by the 'java' plugin.
configurations.archives.artifacts.with { archives ->
def artifacts = []
archives.each {
if (it.file =~ 'jar') {
// We can't just call `archives.remove(it)` here because it triggers
// a `ConcurrentModificationException`, so we add matching artifacts
// to another list, then remove those elements outside of this iteration.
artifacts.add(it)
}
}
artifacts.each {
archives.remove(it)
}
}
Second update: Replaced "dummy.pom" with "${project.artifactId}" above.

I couldn't get Garrett's solution to work, but I did have success like this:
dependencies {
// ...Omitted...
}
tasks.named('generatePomFileForMavenJavaPublication') {
pom.with {
description = 'Parent BOM'
withXml {
// ...Omitted...
}
}
}
// Removing all jar artifacts from the mavenJava publication
// appears to automagically cause packaging to be set to 'pom'!
publishing.publications.named('mavenJava') {
artifacts.removeIf { artifact ->
artifact.extension == 'jar'
}
}

Related

How do I suppress POM and IVY related warnings in Gradle 7?

After upgrading to Gradle 7 I have many warnings like:
Cannot publish Ivy descriptor if ivyDescriptor not set in task ':myProject:artifactoryPublish' and task 'uploadArchives' does not exist.
Cannot publish pom for project ':myProject' since it does not contain the Maven plugin install task and task ':myProject:artifactoryPublish' does not specify a custom pom path.
The artifactoryPublish task works fine.
My Gradle script:
buildscript {
repositories{
maven {
url = '...'
credentials {
username '...'
password '...'
}
metadataSources {
mavenPom()
artifact()
}
}
}
dependencies {
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.24.12"
}
}
apply plugin: 'java'
apply plugin: 'maven-publish'
apply plugin: org.jfrog.gradle.plugin.artifactory.ArtifactoryPlugin
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
suppressAllPomMetadataWarnings()
}
}
}
group = '...'
artifactory {
contextUrl = '...'
publish {
repository {
repoKey = '...'
username = '...'
password = '...'
}
defaults {
publishConfigs('archives')
publishIvy = false
publications("mavenJava")
}
}
}
How do I disable those warnings?
It looks like you mixed between the old Gradle publish-configuration method and the new Gradle publication method.
You applied the maven-publish plugin which allows creating publications. In artifactory.default, you added the "mavenJava" publication as expected.
However, the archives publish-configuration doesn't exist in your build.gradle file. Basically, publish-configurations are created by the legacy maven plugin. The configured mavenJava publication does the same as the archives publish-configuration and therefore all of the JARs are published as expected.
To remove the warning messages you see, remove the publishConfigs('archives') from artifactory.default clause:
artifactory {
publish {
defaults {
publishConfigs('archives') // <-- Remove this line
publishIvy = false
publications("mavenJava")
}
}
}
Read more:
Gradle Artifactory plugin documentation
Example

Gradle-Publish -- no pom.xml file being created

I'm trying to publish a library to an AWS S3 Maven repository using this guide. After finally getting it to upload the artifacts to the S3 bucket without error, I made it a dependency of a new project per the guide.
When I tried to build the new project, an error occurred stating that it couldn't find the first of my library's dependencies. Sure enough, there was no pom.xml file generated that would have included that dependency (and others).
Not knowing a lot about how program Gradle tasks, I think the problem is within the pom.withXml {} portion of the script. Or the problem may occur before that since there's not even an empty pom.xml file.
Here is the entire script:
apply plugin: 'com.android.library'
apply plugin: 'maven-publish'
// update these next lines to fit your submodule
group = 'com.myproject'
version = '1.0'
// Add sources as an artifact
task sourceJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier "source"
}
// Loop over all variants
android.libraryVariants.all { variant ->
variant.outputs.all { output ->
// This creates a publication for each variant
publishing.publications.create(variant.name, MavenPublication) {
// The sources artifact from earlier
artifact sourceJar
// Variant dependent artifact, e.g. release, debug
artifact source: output.outputFile, classifier: output.name
// Go through all the dependencies for each variant and add them to the POM
// file as dependencies
pom.withXml {
def dependencies = asNode().appendNode('dependencies')
// Filter out anything that's not an external dependency. You shouldn't
// be publishing artifacts that depend on local (e.g. project) dependencies,
// but who knows...
configurations.getByName(variant.name + "CompileClasspath").allDependencies
.findAll { it instanceof ExternalDependency }
.each {
def dependency = dependencies.appendNode('dependency')
dependency.appendNode('groupId', it.group)
dependency.appendNode('artifactId', it.name)
dependency.appendNode('version', it.version)
}
}
}
}
}
// Ensure that the publish task depends on assembly
tasks.all { task ->
if (task instanceof AbstractPublishToMaven) {
task.dependsOn assemble
}
}
// Configure the destination repository with
// S3 URL and access credentials
publishing {
repositories {
maven {
url "s3://sdk.myproject.com.s3.amazonaws.com"
credentials(AwsCredentials) {
accessKey AWS_ACCESS_KEY
secretKey AWS_SECRET_KEY
}
}
}
}
Any ideas about what's going wrong? Thank you!

Not Publishing the mentioned file(war/tar/zip) to artifactory in gradle script

I wrote a gradle script where I am creating the zip and war file and then I need to upload/publish it to the artifactory but the issue is I specified the war file in my artifact task even after that it is publishing everything to the artifactory zip,tar and war instead of only war file.
apply plugin: 'war'
apply plugin: 'java'
apply plugin: 'distribution'
//-- set the group for publishing
group = 'com.xxx.discovery'
/**
* Initializing GAVC settings
*/
def buildProperties = new Properties()
file("version.properties").withInputStream {
stream -> buildProperties.load(stream)
}
//add the jenkins build version to the version
def env = System.getenv()
if (env["BUILD_NUMBER"]) buildProperties.coveryadBuildVersion += "_${env["BUILD_NUMBER"]}"
version = buildProperties.coveryadBuildVersion
println "${version}"
//name is set in the settings.gradle file
group = "com.aaa.covery"
version = buildProperties.discoveryadBuildVersion
println "Building ${project.group}:${project.name}:${project.version}"
repositories {
maven {
url "http://cxxxxt.tshaaaaa.tho.com:9000/artifactory/libselease"
}
maven {
url "http://cxxxxt.tshaaa.tho.com:9000/artifactory/cache"
}
}
dependencies {
compile ([
"com.uters.omni:HermesSessionAPI:1.2",
"com.uters.proxy:ProxyResources:1.1",
"com.uters.omni:SeshataDirectory:1.0.1" ,
"com.euters.omni:CybeleInfrastructure:1.1.2",
"com.euters:JSONBus:1.4.1",
"javaee:javaee-api:5"
])
}
distributions {
main {
contents {
from {
war.outputs
}
}
}
}
// for publishing to artifactory
artifacts {
archives war
}
According to gradle distribution plugin documentation:
All of the files in the “src/$distribution.name/dist” directory will automatically be included in the distribution.
And also,
The distribution plugin adds the distribution archives as candidate for default publishing artifacts.
In other words, by default all the files will be published so this explains the behavior you're experiencing.
What you can probably do in order to workaround this behavior is to define the contents copySpec more accurately by explicitly exclude the unwanted files, i.e.:
distributions {
main {
contents {
exclude('**/.zip')
exclude('**/.tar')
from {
war.outputs
}
}
}
}
Note that I didn't try the above by myself though so some fine tuning might be needed. However I believe that you can find the data you need in the CopySpec Interface documentation

Publish (rootproject) pom without (rootproject) publishing artifact / packaging = pom

I'm migrating one of our projects from maven to gradle: it's a gradle multi-project & all subprojects are publishing artifacts to artifactory. So far so good.
The legacy (maven-based) build environment however also expects the root project to publish a pom file with the "packaging" node equal to "pom" (common maven behaviour, so it seems)
So now, I'm trying to have this generated by Gradle, but only find ways to customize an automatically generated pom for each artifact, I can't find a way to generate/upload a pom without publishing an actual artifact.
Workaround for now is to have the root project use the java plugin, generate/install an empty jar and manipulate the generated pom to conform to maven expectations (packaging=pom), but that's a hack.
Is there a way to have this root pom file generated with gradle ?
Example project:
settings.gradle
rootProject.name = 'MultiProject'
include 'child01', 'child02'
rootProject.children.each { it.name = rootProject.name + "-" + it.name }
build.gradle
subprojects {
apply plugin: 'java'
}
allprojects {
apply plugin: 'maven'
group = 'my_group'
version = '0.0.1-SNAPSHOT'
}
EDIT (current workaround), addition to build.gradle
// workaround to generate pom
apply plugin: 'java'
configurations {
pomCreation
}
task createPom {
ext.newPomFile = "${buildDir}/blabla.pom"
doLast {
pom {
project {
packaging 'pom'
}
}.writeTo(newPomFile)
}
}
install.dependsOn(createPom)
artifacts {
pomCreation file(createPom.newPomFile)
}
I would use the gradle maven-publish plugin for that. With that plugin you can define your specific pom and don't have to upload other artifacts. Here an example:
publishing {
publications {
maven(MavenPublication) {
pom.withXml{
def xml = asNode()
xml.children().last() + {
delegate.dependencies {
delegate.dependency {
delegate.groupId 'org.springframework'
delegate.artifactId 'spring-context'
delegate.version( '3.2.8.RELEASE' )
}
}
}
}
}
}

avro gradle plugin sample usage

I am trying to use the avro-gradle-plugin on github, but have not gotten any luck getting it to work. Does anyone have any sample code on how they get it to work?
I figured out how to do it myself. The following is a snippet that I would like to share for people who might run into the same issues as I did:
apply plugin: 'java'
apply plugin: 'avro-gradle-plugin'
sourceCompatibility = "1.6"
targetCompatibility = "1.6"
buildscript {
repositories {
maven {
// your maven repo information here
}
}
dependencies {
classpath 'org.apache.maven:maven-artifact:2.2.1'
classpath 'org.apache.avro:avro-compiler:1.7.1'
classpath 'org.apache.avro.gradle:avro-gradle-plugin:1.7.1'
}
}
compileAvro.source = 'src/main/avro'
compileAvro.destinationDir = file("$buildDir/generated-sources/avro")
sourceSets {
main {
java {
srcDir compileAvro.destinationDir
}
}
}
dependencies {
compileAvro
}
I found "com.commercehub.gradle.plugin.avro" gradle plugin to work better.
use the folllowing:
// Gradle 2.1 and later
plugins {
id "com.commercehub.gradle.plugin.avro" version "VERSION"
}
// Earlier versions of Gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "com.commercehub.gradle.plugin:gradle-avro-plugin:VERSION"
}
}
apply plugin: "com.commercehub.gradle.plugin.avro"
more details at https://github.com/commercehub-oss/gradle-avro-plugin
When evaluating a plugin the following questions needs to be asked:
Are generated files included into source jar?
Is plugin fast? Good plugin use avro tools api instead of forking VM for every file. For large amount of files creating VM for every file can take 10min to compile.
Do you need intermediate avsc files?
Is build incremental (i.e. do not regenerate all files unless one of the sources changed)?
Is plugin flexible enough to give access to generated schema files, so further actions, such as registration schema in schema repository can be made?
It is easy enough to implement without any plugin if you are not happy with plugin or need more flexibility.
//
// define source and destination
//
def avdlFiles = fileTree('src/Schemas').include('**/*.avdl')
// Do NOT generate into $buildDir, because IntelliJ will ignore files in
// this location and will show errors in source code
def generatedJavaDir = "generated/avro/java"
sourceSets.main.java.srcDir generatedJavaDir
//
// Make avro-tools available to the build script
//
buildscript {
dependencies {
classpath group:'org.apache.avro', name:'avro-tools' ,version: avro_version
}
}
//
// Define task's input and output, compile idl to schema and schema to java
//
task buildAvroDtos(){
group = "build"
inputs.files avdlFiles
outputs.dir generatedJavaDir
doLast{
avdlFiles.each { avdlFile ->
def parser = new org.apache.avro.compiler.idl.Idl(avdlFile)
parser.CompilationUnit().getTypes().each { schema ->
def compiler = new org.apache.avro.compiler.specific.SpecificCompiler(schema)
compiler.compileToDestination(avdlFile, new File(generatedJavaDir))
}
}
}
}
//
// Publish source jar, including generated files
//
task sourceJar(type: Jar, dependsOn: buildAvroDtos) {
from sourceSets.main.allSource
// Package schemas into source jar
into("Schemas") { from avdlFiles }
}
// Clean "generated" folder upon "clean" task
clean {
delete('generated')
}
Configuration for avro with gradle as build tool need to add along with applying java plugin.
below changes in settings.gradle
pluginManagement {
repositories {
gradlePluginPortal()
mavenCentral()
}
}
below changes in build.gradle
plugins {
id "com.github.davidmc24.gradle.plugin.avro" version "1.3.0"
}
repositories {
mavenCentral()
}
dependencies {
implementation "org.apache.avro:avro:1.11.0"
}
generateAvroJava {
source("${projectDir}/src/main/resources/avro")//sourcepath avrofile
}
if you want to generate setter methods too add this task too in build.gradle
avro {
createSetters = true
}
link for reference

Resources