Maven-Publish doesn't include dependencies in the project.pom file

We’re having the same problem as this very old thread Using the maven-publish plugin = no dependencies in pom.xml.

Our 5 subprojects each have their own build.gradle under the main folder with various compile time dependencies one or two runtime and JUNIT for testing. However when the POMs are generated for the publish only log4j is included in the POM file as a runtime dependency, nothing else (ie. common-io for example).

                publishing { 
                        publications { 
                                mavenJava (MavenPublication) {
                                        from components.java
                                        artifact sourceJar
                                        // artifact javadocJar
                                }
                        }
                }

                // publish what was tagged above 'mavenJava' is the container name
                artifactoryPublish { 
                        publications(publishing.publications.mavenJava) 
                }

The resulting pom:

<?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.test</groupId>
  <artifactId>common</artifactId>
  <version>4.0</version>
  <dependencies>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
      <scope>runtime</scope>
    </dependency>
  </dependencies>
</project>

We’re even having a weirder result if we try to add the dependencies list to the pom

    pom.withXml {
     def dependenciesNode = asNode().appendNode('dependencies')
    
        //Iterate over the compile dependencies (we don't want the test ones), adding a <dependency> node for each
        configurations.compile.allDependencies.each {
            def dependencyNode = dependenciesNode.appendNode('dependency')
            dependencyNode.appendNode('groupId', it.group)
            dependencyNode.appendNode('artifactId', it.name)
            dependencyNode.appendNode('version', it.version)
        }
 }

Adds a second block to the POM file:

<?xml version="1.0" encoding="UTF-8"?>
<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.dd</groupId>
  <artifactId>common</artifactId>
  <version>4.0</version>
  <dependencies>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
      <scope>runtime</scope>
    </dependency>
  </dependencies>
  <dependencies>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>
    <dependency>
      <groupId>com.jolbox</groupId>
      <artifactId>bonecp</artifactId>
      <version>0.8.0.RELEASE</version>
    </depend......

How are these dependencies being defined?

Hey Aram,
this is a known limitation of the maven-publish plugin (Can’t find the
corresponding ticket though). In your custom code for adding
dependencies to the pom you can change the lookup of the dependencies
Node using:

def dependenciesNode = asNode().getAt("dependencies")[0]

This will avoid adding a second dependencies block to your pom

In each sub-project we have a build.gradle file that has a dependency { compile … } block. 4 out of 5 are just that simple. The last one has some JOOQ code generation blocks as well with extra dependencies.

Thank you the [0] index got us to the right place.

you sir, are a saint!

@mark_vieira you’re awesome. I included this neato fix in gradle-fury (https://github.com/chrisdoyle/gradle-fury). I also found a partial solutions to “type” and “scope” but nothing on classifiers. Did you ever find a fix for that? As far as I can tell, it’s not in gradle’s ‘model’.

Actually all of above didn’t work for me . I found a solution like below finaly:

   pom.withXml {
            asNode().dependencies.'*'.findAll() {
                it.scope.text() == 'runtime'  && project.configurations.compile.allDependencies.find { dep ->
                    dep.name == it.artifactId.text()
                }
            }.each() {
                it.scope*.value = 'compile'
            }
        }