29
WAR
   - META-INF
   - WEB-INF
       - classes
           - META-INF
               - myApp.properties <-- Needs added

How do I add a .properties file into my WAR using gradle? The file was later introduced into the project but doesn't get added?

build.gradle

import org.apache.commons.httpclient.HttpClient
import org.apache.commons.httpclient.methods.GetMethod

group = 'gradle'
version = '1.0'

apply plugin: 'war'
apply plugin: 'jetty'
apply plugin: 'eclipse'

eclipseProject 
{
  projectName = 'crap'
}

defaultTasks 'build'

dependencies 
{
   //all my dependencies
}

war 
{        
  classpath fileTree('lib')
}

jar.enabled = true

[jettyRun, jettyRunWar]*.daemon = true
stopKey = 'stoppit'
stopPort = 9451
httpPort = 8080
scanIntervalSeconds = 1
stackoverflow
  • 18,348
  • 50
  • 129
  • 196

4 Answers4

51
war {
    from('<path-to-props-file>') {
        include 'myApp.properties'
        into('<target-path>')
    }
}
Rene Groeschke
  • 27,999
  • 10
  • 69
  • 78
  • 4
    +1 for adding the into(''). Exactly what I was looking for – n4rzul May 30 '13 at 18:24
  • 1
    Works great with jar too! This really should be part of the standard docs. Without StackOverflow this would be impossible to discover. – Axel Fontaine Apr 19 '14 at 12:02
  • @AxelFontain well 'jar' is just another archive task, so it has all the api methods of archive tasks. Do you think that's not reflected enough in the Gradle Userguide? At least it should be clear after having a look in the Gradle DSL reference. – Rene Groeschke Apr 22 '14 at 10:47
  • 3
    To me, it wasn't clear at all. Maybe it's just me, but I had no idea how do to this from reading http://www.gradle.org/docs/current/dsl/org.gradle.api.tasks.bundling.Jar.html And truth be told I still don't. I see no script block definition for from. There is a method, but it doesn't mention anything about how to combine it with include and into. I'm sure it's clear for you, but for me it isn't. And judging by the number of upvotes of your answer I'm probably not alone... – Axel Fontaine Apr 22 '14 at 10:57
  • 1
    I'm also totally lost with gradle. I'm sure that one day it will all be clear and simple, but for now I just copy&paste SO snippets because the docs don't help me at all. I must be missing some basics... maybe it is the groovy language I should learn? For example, the DSL states that "from" is a method accepting a path. So what's this syntax with curly braces? Where are "include" and "into" documented? I've never felt so dumb in years oO – xtian Jun 19 '16 at 17:00
32

Something like this should work:

war {
    from('<path-to-props-file>') {
        include 'myApp.properties'
    }
}

If you want to specify which directory you want the properties file to be located in:

war { 
    from('<path-to-props-file>') { 
        include 'myApp.properties' 
        into('<targetDir>') 
    }
} 
Thunderforge
  • 19,637
  • 18
  • 83
  • 130
Benjamin Muschko
  • 32,442
  • 9
  • 61
  • 82
6

eg1:

war {
    webInf{
        from('PATH_TO_SOURCE_FOLDER') {
            include 'FILE_TO_BE_INCLUDED'
            into('TARGET_FOLDER_RELATIVE_TO_WEB_INF_DIR')
        }
    }
}

eg2:

war {
    webInf{
        from('src/META-INF') {
            include 'persistence.xml'
            into('classes/META-INF/')
        }
    }
}

For more information check the online documentation: Chapter 26. The War Plugin

Marcelo C.
  • 3,822
  • 2
  • 22
  • 11
1

I normally use an environments folder from which I pick a given configuration file based on the deploy variable. Ex.:

from("environments/system.${env}.properties"){
        include "system.${env}.properties"
        into 'WEB-INF'
        rename("system.${env}.properties", 'system.properties')
}

the property is passed through gradle as:

./gradlew buildDocker  -Penv=prod
chico
  • 11
  • 1
  • 1