Maven install uploadArchvies pom does not dependencies from configuration extension

I have multi module project. The parent project has defined list of configuration library. Each child module will be extending specific configuration instead of defining the dependencies again. When we execute the gradle task, install or uploadArchives, the generated pom does not have dependencies from configuration extension but it has dependencies from compile section. Example is given below.

repositories {
  mavenCentral()
  mavenLocal()
}
  group = 'com.test'
version = '0.0.3'
  apply plugin: 'java'
apply plugin: 'maven'
  configurations {
 springLib
   compile.extendsFrom springLib
            }
  dependencies {
  springLib "org.springframework:spring-core:3.2.2.RELEASE"
    compile "org.slf4j:slf4j-api:1.6.6"
}

The generate pom is

<?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>test</artifactId>
  <version>0.0.3</version>
  <dependencies>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.6.6</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
</project>

The generated pom has “slf4j” library in the dependency list but not the “spring-core”.

Is there any mistake in my gradle script? Is a known problem? If this is working as expected then why does gradle doing like this and how do I solve it.

Thanks, -Sridharan

The behavior is expected. Parent configurations won’t be considered when mapping configurations to scopes (doing so wouldn’t work for ‘compile’ and ‘runtime’, for example). I recommend to change the way you share common dependencies, but I’d have to know/see more about your current solution to give concrete recommendations.

There are lot of shared libraries defined and there are about 100+ sub project sharing them. It will be harder to define dependencies in each sub project. Is there any way to resolve this problem?

Thanks.

The usual way to deal with this is to declare an extra property in the parent build script that holds a map containing all the dependency information. Then subprojects can make use of this information in their ‘dependencies’ block. For example:

‘build.gradle’:

ext.libraries = [
    junit: "junit:junit:4.11",
    spring: [
        "org.springframework:spring-core:3.0.2.RELEASE",
        "org.springframework:spring-mvc:3.0.2.RELEASE"
    ]
    // for advanced cases you can use dependencies.create(...) on RHS
]

‘subproject/build.gradle’:

dependencies {
    compile libraries.spring
    testCompile libraries.junit
}

With this solution, you don’t need additional configurations, and you don’t need to do extra work to have the correct POMs generated.

Hi Peter,

Thank you very much. You made my day.

I like this solution for two reason.

  1. It is easier to read than defining custom configuration name and assinging values. 2. Dependencies are declared in dependencies section

I appreciate your quick help.

-Sridharan