Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Gradle Multi-Project Builds: Parent Pom Structure

DZone's Guide to

Gradle Multi-Project Builds: Parent Pom Structure

Want to learn how to use a parent pom structure in Gradle? Check out this post on how to use multi-project builds in Gradle with the parent pom structure.

· Java Zone ·
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

When you come from a Maven background, most likely, you have been used to the parent pom structure. Now, when it comes to Gradle, things are a little bit different.

Imagine the scenario of having a project with the interfaces and various other implementations. This is going to be our project structure.

multi-project-gradle
-- specification
-- core
-- implementation-a
-- implementation-b


The specification project contains the interfaces, which the implementations will be based upon. The core project will contain functionality that needs to be shared among implementations.

The next step is to create each project inside the multi-project-gradle. Each project is actually a directory with the builde.gradle file.

plugins {
    id 'java'
}

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
}


Once this is done, you need to create a link between the parent project and the child project.
To do so, you create the multi-project-gradle/settings.gradle and include the other projects.

rootProject.name = 'com.gkatzioura'
include 'specification'
include 'core'
include 'implementation-a'
include 'implementation- b'


Now, if you set the build.gradle file for every sub project, you’ve just included the JUnit dependency and the Maven central repository everywhere.

One of the main benefits on using multi-project builds is removing duplication. To do so, we shall create the multi-project-gradle/build.gradle file and add the JUnit dependency and the Maven central reference.

subprojects {
    apply plugin: 'java'

    repositories {
        mavenCentral()
    }

    dependencies {
        testCompile group: 'junit', name: 'junit', version: '4.12'
    }

}


Now, we can add our dependencies to each project and even specify the dependencies needed from the sub-projects.

For example, the core project uses the specification project.

dependencies {
  compile project(':specification')
}


And, each implementation project uses the core project.

dependencies {
    compile project(':core')
}


You can find the project on GitHub.

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:
java ,tutorial ,maven ,gradle ,multi-project builds ,builds ,junit ,dependencies ,parent pom ,structure

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}