Question

Our Gradle script consists of root "war" project and multiple (10+) child "jar" projects. Each JAR project depends on many (30-60) JARs. Partial example:

dependencies {
    compile 'dwr:dwr:3.0.0.117'
    compile 'annotations:annotations:1.0' 

    compile 'abdera:abdera-core:0.4.0-incubating'
    compile 'abdera:abdera-extensions-json:0.4.0-incubating'
    compile 'abdera:abdera-extensions-main:0.4.0-incubating'
    compile 'abdera:abdera-i18n:0.4.0-incubating'
    compile 'abdera:abdera-parser:0.4.0-incubating'


    compile 'aopalliance:aopalliance:1.0'

    compile 'apacheds:apacheds-core:1.0.2'
    compile 'apacheds:apacheds-core-shared:1.0.2'
    compile 'apacheds:apacheds-kerberos-shared:1.0.2'
    compile 'apacheds:apacheds-protocol-changepw:1.0.2'
    compile 'apacheds:apacheds-protocol-kerberos:1.0.2'
    compile 'apacheds:apacheds-protocol-ldap:1.0.2'
    compile 'apacheds:apacheds-protocol-ntp:1.0.2'
    compile 'apacheds:apacheds-protocol-shared:1.0.2'
    compile 'apacheds:apacheds-server-jndi:1.0.2'
    compile 'apacheds:apacheds-server-main:1.0.2'
    compile 'apacheds:apacheds-server-ssl:1.0.2'

    compile 'commons:commons-logging:1.0.4'
    compile 'commons:commons-beanutils'
    compile 'commons:commons-cli:1.0'
    compile 'commons:commons-codec:1.3'
    compile 'commons:commons-collections:3.2.1'
    compile 'commons:commons-digester'
    compile 'commons:commons-discovery:0.2'
    compile 'commons:commons-fileupload:1.2'
    compile 'commons:commons-io:1.2'
    compile 'commons:commons-lang:2.4'
    compile 'commons:commons-pool:1.5.4'

    compile 'dom4j:dom4j-full'
    compile 'ehcache:ehcache:1.6.2'
    compile 'eigenbase:eigenbase-properties'
    compile 'eigenbase:eigenbase-resgen'
    compile 'eigenbase:eigenbase-xom'
    compile 'FastInfoset:FastInfoset:1.2.7'
    compile 'fop:fop'
    compile 'freehep:freehep-export:2.1.1'
    compile 'freehep:freehep-graphics2d:2.1.1'
    compile 'freehep:freehep-graphicsio:2.1.1'
    compile 'freehep:freehep-graphicsio-emf:2.1.1-adapted'
    compile 'freehep:freehep-graphicsio-java:2.1.1'
    compile 'freehep:freehep-graphicsio-pdf:2.1.1'
    compile 'freehep:freehep-graphicsio-ps:2.1.1'
    compile 'freehep:freehep-graphicsio-svg:2.1.1'
    compile 'freehep:freehep-graphicsio-swf:2.1.1'
    compile 'freehep:freehep-graphicsio-tests:2.1.1'
    compile 'freehep:freehep-io:2.0.2'
    compile 'freehep:freehep-swing:2.0.3'
    compile 'freehep:freehep-util:2.0.2'
    compile 'freehep:freehep-xml:2.1.1'
}

I would like to avoid having such long duplicated list of dependencies for each component. There should be a way to group dependencies, for example "freehep", "commons", "apacheds", "abdera", "dwr", and than each component would reference those groups.

Was it helpful?

Solution

Add the following to the root project:

ext {
  apache_commons = [ 'commons-io:commons-io:2.4', 'org.apache.commons:commons-lang3:3.1' ]
}

then include it in subprojects like this:

dependencies {
  compile apache_commons
}

This way you can define as many dependency lists as you want and include them as frequently as you want, in any combinations.

OTHER TIPS

You can define a "super-project" with some name, e.g. 'myProject' and put all your current projects into this one as subprojects.

project(':myProject') {
    subprojects {
        dependencies {
            // your global dependencies
        }
    }

    project('myWarProject') {
    }

    project('myJarProject1') {
    }

    project('myJarProject2') {
    }
}

But I think, it's not a good idea to organize projects like this. I don't really understand why do you have so many projects with the same dependencies. Normally you should have one war-project with all war-specific dependencies and some other non-war relevant projects with APIs and implementations of defined interfaces. In this case all dependencies of the API project will be automatically added as transitive to all other projects, you just have to define the API project as a compile dependency.

def jar1 = project('myJarProject1') {
    dependencies {
        // dependencies of project-1
    }
}

def jar2 = project('myJarProject2') {
    dependencies {
        // dependencies of project-2
    }
}

project('myWarProject') {
    dependencies {
        compile jar1 // includes all project-1 dependencies as transitive
        compile jar2 // includes all project-2 dependencies as transitive

        // add war-specific dependencies here

    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top