如何使用gradle将.properties文件添加到WAR中?

时间:2011-07-13 17:58:15

标签: gradle

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

如何使用gradle将.properties文件添加到WAR中? 该文件后来被引入该项目,但没有 加入?

的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

4 个答案:

答案 0 :(得分:44)

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

答案 1 :(得分:30)

这样的事情应该有效:

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

如果要指定属性文件所在的目录:

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

答案 2 :(得分:5)

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/')
        }
    }
}

有关详细信息,请查看在线文档:Chapter 26. The War Plugin

答案 3 :(得分:0)

我通常使用环境文件夹,从中根据deploy变量选择给定的配置文件。例:

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

该属性通过gradle传递:

./gradlew buildDocker  -Penv=prod