Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Create Gradle sharedManifest for multiple projects?

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.

like image 553
user3496599 Avatar asked Aug 04 '14 10:08

user3496599


People also ask

Can I have multiple build Gradle?

Hello , can i create multiple build. gradle for one project? Yes. You can have multiple build files in one project.

How to create a multi-project using Gradle?

Let's create a project containing its subprojects and build the Gradle project. Co'sider the below project structure in which the root project name is Multi_project, and the subproject name is sub_project. Create a new root directory in which we want to create a multi-project. Use the below command to create a new directory:

What is cross-project configuration in Gradle?

The project API of the Gradle provides a method called project (); it takes the path as an argument and returns the object of the project for the current path. When a project build configures from a build script, then it is called cross-project configuration.

What are the advantages of Gradle over other Gradle libraries?

Gradle can handle smallest and largest projects easily. Small projects have a single build file and a source tree. It is very easy to digest and understand a project that has been split into smaller, inter-dependent modules. Gradle perfectly supports this scenario that is multi-project build.

Where do I put a Gradle build file?

A build.gradle file in the root or master directory. Child directories that have their own *.gradle build files (some multi-project builds may omit child project build scripts).


2 Answers

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 ...
        }
    }
}
like image 86
Peter Niederwieser Avatar answered Oct 16 '22 14:10

Peter Niederwieser


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',
.....
        )
    }
}
like image 42
ngranin Avatar answered Oct 16 '22 16:10

ngranin