How to add file dynamically in META-INF folder using gradle


(Anindya Chatterjee) #1

I am migrating a maven project to a gradle one. In maven project, we used to have profiles for different environments and it adds context.xml from a dir to META-INF of the generated war.

The resource dir structure is as follows:

|   |-- resources
|   |   |-- META-INF-Contexts
|   |   |   |-- dev
|   |   |   |   `-- context.xml
|   |   |   |-- qa
|   |   |   |   `-- context.xml
|   |   |   |-- prod
|   |   |       `-- context.xml
|   |   |-- app.properties
|   `-- webapp
|       |-- favicon.ico
|       `-- WEB-INF
|           `-- web.xml

Now in gradle I want to pass a property (env) and depending on the value, it should take the proper context.xml file and place it under META-INF directory.

The approach I tried is as follows:

ext {
    isDev = System.properties['env'] == 'dev'
    isQa = System.properties['env'] == 'qa'
    isProd = System.properties['env'] == 'prod'
}

war {
    if (isDev) {
        metaInf { from 'src/main/resources/META-INF-Contexts/dev/context.xml' }
    } else if (qa) {
        metaInf { from 'src/main/resources/META-INF-Contexts/qa/context.xml' }
    } else {
        metaInf { from 'src/main/resources/META-INF-Contexts/prod/context.xml' }
    }
}

but it is not putting the context.xml inside the META-INF directory. Any help would be appreciated.


(James Justinic) #2

As posted, the example script does not execute. The code tries to access a property called qa, but the property is set as isQa. However, your post suggests you are building a correct WAR that has everything expected except for the context.xml in META-INF, so you are likely missing needed information from your post.


(Anindya Chatterjee) #3

Sorry that was a typo. It should be isQa of course. Original build script is correct. I typed it wrongly here. Changing it to isQa also does not make it work as expected.


(James Justinic) #4

If it wasn’t clear from my previous post, “you are likely missing needed information from your post” was not based on the typo posted, but after fixing it. When I change, qa to isQa (and fill in the external gaps, file structure, etc.) it works as you have described is desired.

This suggests that you have additional typos or haven’t posted something critical that is affecting the behavior. I have exactly the following in a build.gradle file, created dummy context.xml files in the specified locations, and see the correct one in the META-INF folder based on a value I pass for env:

apply plugin: 'war'
  
ext {
    isDev = System.properties['env'] == 'dev'
    isQa = System.properties['env'] == 'qa'
    isProd = System.properties['env'] == 'prod'
}

war {
    if (isDev) {
        metaInf { from 'src/main/resources/META-INF-Contexts/dev/context.xml' }
    } else if (isQa) {
        metaInf { from 'src/main/resources/META-INF-Contexts/qa/context.xml' }
    } else {
        metaInf { from 'src/main/resources/META-INF-Contexts/prod/context.xml' }
    }
}