Multiproject builds: How are versions/group of sub-projects inherited from the root project?


(Daniel Sandberg) #1

I had problems getting it to work when using the maven-publish plugins. The sub-projects artifacts ended up being published with version = unspecified which haunted me for over an hour.

I managed to get it to work by specifically configure the version and group in a sub-project closure or by adding a apply from: ‘…/build.gradle’ in each sub-project.

Which one is “best practice”?
Coming from Maven the apply from: '…/build.gradle feels most “right”.

PS. Ignore the duplications…

Layout:

myApp/
  build.gradle
  settings.gradle
  subA/
    build.gradle
  subB
    build.gradle

myApp/build.gradle:

group = 'com.example'
version = '1.0.0-SNAPSHOT'

subprojects {
  group = 'com.example'
  version = '1.0.0-SNAPSHOT'  // "best practice"?
}

... common stuff ...

myApp/settings.gradle:

rootProject.name = 'myApp'
include 'subA', 'subB'

myApp/subA/build.gradle:

apply from: '../build.gradle'  // "best practice"?
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'maven-publish'

sourceCompatibility = 1.7
targetCompatibility = 1.7

publishing {
  publications {
    mavenJava(MavenPublication) {
      from components.java
    }
  }
}

myApp/subB/build.gradle:

apply from: '../build.gradle'  // "best practice"?
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'maven-publish'
apply plugin: 'war'

sourceCompatibility = 1.7
targetCompatibility = 1.7

publishing {
  publications {
    mavenWar(MavenPublication) {
      from components.web
    }
  }
}

(Rodrigo B. de Oliveira) #2

Hi Daniel,

As you noticed, group and version are project specific.

The recommended way of defining common behaviour in a multi-project setup is via an allprojects block:

    allprojects {
        group = 'com.example'
        version = '1.0.0-SNAPSHOT'
    }

Cheers,
Rodrigo


(Daniel Sandberg) #3

Ah, that actually makes perfect sense. Been raging through the documentation but ended up being confused.

Thank you very much.