9

I have multiple java projects. these projects are creating jar,war and ear files using gradle. In each project I have used manifest file to maintain the meta data like version,date-time... Fro this I have included the manifest file creation logic in every build.gradle file.

manifest {
     attributes( 
    'Bundle-Vendor' : "$BUNDLE_VENDOR",
    'Build-Time': new Date().format("yyyy-MM-dd'T'HH:mm:ssZ")) 
}

But in Gradle there is a feature call sharedManifest. I have defined the bellow two tasks in main project build.gradle script. But in the every jar and war file have the default MANIFEST.MF file created by Gradle.

ext.sharedManifest = manifest {

    attributes( 
        'Bundle-Vendor' : "$BUNDLE_VENDOR", 
         'Build-Time': new Date().format("yyyy-MM-dd'T'HH:mm:ssZ")
     ) 
}

task fooJar(type: Jar) {
    manifest = project.manifest {
        from sharedManifest
    }
}

task fooWar(type: War) {
    manifest = project.manifest {
        from sharedManifest
    }
}

jar.manifest.writeTo("/MANIFEST.MF") war.manifest.writeTo("/MANIFEST.MF")

please can some one give the suggestion how to do this.

mehdi lotfi
  • 11,194
  • 18
  • 82
  • 128
user3496599
  • 1,207
  • 2
  • 12
  • 28

2 Answers2

10

The easiest way to share manifest logic within a build is a configuration rule such as:

allprojects {
    tasks.withType(Jar) { // includes War and Ear
        manifest {
            attributes ...
        }
    }
}
Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
  • I have added this code in my main project build.gradle file like this allprojects { tasks.withType(War) { manifest { attributes( 'Bundle-Vendor' : "$BUNDLE_VENDOR", 'Build-Time': new Date().format("yyyy-MM-dd'T'HH:mm:ssZ") ) } } } but it manifest file I can see only Manifest-Version: 1.0 – user3496599 Aug 04 '14 at 11:38
  • Your snippet works just fine for me. Either you are looking in the wrong place (note that you used `withType(War)` which doesn't include Jars and Ears), or something else is wrong with your build. – Peter Niederwieser Aug 04 '14 at 20:27
  • Hi Peter, If I used above code snippet do I want to use war.manifest.writeTo("/MANIFEST.MF") write the manifest in to sub-projects or it is automatically override the default manifest file. In my case I cant override or replace the default manifest file. – user3496599 Aug 13 '14 at 12:36
2

Also, there is another way to create a shared manifest:

Create a java.gradle file to keep configurations for Java subprojects and put inside:

ext.sharedManifest = manifest {
    attributes(
.......
    )
}

Then, in the root build.gradle apply this configuration for subprojects

subprojects {
    apply from: "$rootDir/gradle/java.gradle"
.....
}

And there is possible to reuse this shared manifest and add extra attributes:

Subproject A:

jar {
    manifest {
        from sharedManifest
        attributes(
                'JavaFX-Application-Class': 'com.main.SomeClass',
.....
        )
    }
}
ngranin
  • 181
  • 2
  • 14