Maven-publish not publishing POM


(David Harrigan) #1

Hi,

Java 1.7u25 Gradle 1.9-rc-2

Hi,

I have a simple project with a build.gradle configured thus (showing jar tasks and publishing configuration):

task commonJar(type: Jar) {
    from sourceSets.main.output
    exclude "com/fubar/fu/bar/**"
    manifest {
        attributes(
            "Created-By" : "${System.getProperty("java.version")} (${System.getProperty("java.specification.vendor")})",
            "Implementation-Version" : version
        )
    }
   }
  task commonSourcesJar(type: Jar) {
    classifier "sources"
    from sourceSets.main.allJava
    exclude "com/fubar/fu/bar/**"
    manifest {
        attributes(
            "Created-By" : "${System.getProperty("java.version")} (${System.getProperty("java.specification.vendor")})",
            "Implementation-Version" : version
        )
       }
   }
  publishing {
    publications {
        common(MavenPublication) {
            artifactId 'fubar-common'
            artifact commonJar
                                                                                                                                                                               }
           commonSources(MavenPublication) {
            artifactId 'fubar-common'
            artifact commonSourcesJar {
                classifier "sources"
            }
           }
       }
       repositories {
        maven {
            url "${repositoryUrl}/libs-${project.version.endsWith('-SNAPSHOT') ? 'snapshot' : 'release' }-local"
        }
       }
   }

When I do a publish, Gradle is informing me that it has published my (snapshots) to Artifactory. However, when I go and look at artifactory and the POM, this is all I see:

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.fubar</groupId>
  <artifactId>fubar-common</artifactId>
  <version>0.1.0-SNAPSHOT</version>
  <packaging>pom</packaging>
</project>

There are no dependencies listed at all. Thus, when I import the fubar-common jar into another Gradle project, no dependencies are resolved.

I’m at a loss. I even tried removing the commonJar task and using “from components.java” in the publications block with the same result. I also did a “println configurations.runtime.allDependencies” in the publications block and nothing is output. Very weird (since I do have dependencies in this project…)

Anyone else got this same problem?

-=david=-


(Rolf Suurd) #2

If you are running the task by invoking “gradle publish”, you might want to try to be more specific, such as “gradle publishCommonPublicationToMavenRepository”

In addition, does the output of generatePomFileForCommonPublication (build/publications/common/pom-default.xml) list all relevant dependencies?


(David Harrigan) #3

Hi,

Thank you for your reply. I tried “gradle clean publishCommonPublicationToMavenRepository” command and still the POM does not include dependencies. The output of build/publications/common/pom-default.xml is the same as the one above, i.e.,

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.fubar</groupId>
  <artifactId>fubar-common</artifactId>
  <version>0.1.0-SNAPSHOT</version>
  <packaging>pom</packaging>
</project>

Looks like I’m still not doing something right! :frowning:


(Rolf Suurd) #4

This is how we publish our dependencies:

task sourceJar(type: Jar) {
    from sourceSets.main.allJava
}
  publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
                          artifact sourceJar {
                classifier "sources"
            }
        }
    }
    repositories {
        maven {
            url "${nexusRepositories}/${project.version.endsWith('-SNAPSHOT') ? 'snapshots' : 'releases' }"
            credentials {
                username = uploadUserName
                password = uploadPassword
            }
        }
    }
}

For each project this results in 2 jars, one compiled jar (with dependencies defined in the pom) and a sources jar.

“from components.java” results in the compiled jar (with correct pom) The “artifact” block adds another jar to the publication (a publication can span multiple artifacts)

I’m pretty sure the “from components.java” is pretty much mandatory, judging from section 65.2.1 in the user guide, this causes the publication to include the runtime dependencies.


#5

You’re doing a couple of things wrong: - You’re creating 2 publications with the same GAV to publish 2 artifacts. This isn’t allowed. Instead, you should create a single publication and add multiple artifacts to it. - You’re not telling Gradle what the dependencies should be. The common way to do this is to use ‘components.java’ as mentioned by Rolf. I’d suggest checking out the [User Guide Chapter on the maven-publish plugin](http://www.gradle.org/docs/nightly/userguide/publishing_maven.html) to get a better feel for how the new publishing stuff works.


(David Harrigan) #6

Hi Daz,

Thank you for your reply. I’ll try out your suggestion with doing a single publication.

A question however, I want to be able to filter the artifact that is created (hence the reason why I have a custom commonJar task that filters out some fubar stuff. I can’t seem to do this if I use components.java since it includes everything. Perhaps again, I’m doing it wrong? Can I filter at the components.java level as well?

-=david=-


(David Harrigan) #7

Hi,

Eventually got round to trying this out. I’ve had marginal success, I now have this:

publishing {
    publications {
        common(MavenPublication) {
            artifactId 'platform-common'
            from components.java
              artifact commonSourcesJar {
                classifier "sources"
            }
        }
    }
    repositories {
        maven {
            url "${repositoryUrl}/libs-${project.version.endsWith('-SNAPSHOT') ? 'snapshot' : 'release' }-local"
        }
    }
}

This uploads the classes jar and also the sources jar successfully AND creates the POM for me with dependencies. However, the classes jar contains classes that I don’t want included (hence the reason I had a custom commonJar Task:Jar above, to filter out classes I don’t want (fubar)). If I remove the “from components.java” and use “artifact commonJar” derived from the task, then my classes jar is filtered as I expect but when uploaded an empty-without-dependencies POM is created!

So, how do I upload a jar, filtered with excluded classes and either A) use “from components.jar” or B) use artifact commonJar within the publications block!

-=david=-


#8

You might have some luck by first clearing the default artifacts and then adding your own:

publishing {
    publications {
        common(MavenPublication) {
            artifactId 'platform-common'
            from components.java
            artifacts.clear()
            artifact commonJar
            artifact commonSourcesJar {
                classifier "sources"
            }
        }
    }

That should leave the dependencies in place but allow you to override the default artifact.


#9

Or even set the classifier attribute on the ‘commonSourceJar’ task and use:

artifacts = [commonJar, commonSourceJar]

to replace the default artifacts.


(David Harrigan) #10

Hi,

Perfect! Both approaches work. I favour the later since it gives me control over how each jar is created. Ultimately, I’ve ended up with this:

publishing {
    publications {
        common(MavenPublication) {
            artifactId 'platform-common'
            from components.java
            artifacts = [commonJar, commonSourcesJar]
        }
    }
    repositories {
        maven {
            url "${repositoryUrl}/libs-${project.version.endsWith('-SNAPSHOT') ? 'snapshot' : 'release' }-local"
        }
    }
}

Thank you Daz!

-=david=-