Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
294 views
in Technique[技术] by (71.8m points)

java - How to read a properties files and use the values in project Gradle script?

I am working on a Gradle script where I need to read the local.properties file and use the values in the properties file in build.gradle. I am doing it in the below manner. I ran the below script and it is now throwing an error, but it is also not doing anything like creating, deleting, and copying the file. I tried to print the value of the variable and it is showing the correct value.

Can someone let me know if this is correct way to do this? I think the other way is to define everything in the gradle.properties and use it in the build.gradle. Can someone let me know how could I access the properties in build.gradle from build.properties?

build.gradle file:

apply plugin: 'java'

//-- set the group for publishing
group = 'com.true.test'

/**
 * Initializing GAVC settings
 */
def buildProperties = new Properties()
file("version.properties").withInputStream {
        stream -> buildProperties.load(stream)
}
//if jenkins build, add the jenkins build version to the version. Else add snapshot version to the version.
def env = System.getenv()
if (env["BUILD_NUMBER"]) buildProperties.test+= ".${env["BUILD_NUMBER"]}"
version = buildProperties.test
println "${version}"

//name is set in the settings.gradle file
group = "com.true.test"
version = buildProperties.test
println "Building ${project.group}:${project.name}:${project.version}"

Properties properties = new Properties()
properties.load(project.file('build.properties').newDataInputStream())
def folderDir = properties.getProperty('build.dir')
def configDir = properties.getProperty('config.dir')
def baseDir  = properties.getProperty('base.dir')
def logDir  = properties.getProperty('log.dir')
def deployDir  = properties.getProperty('deploy.dir')
def testsDir  = properties.getProperty('tests.dir')
def packageDir  = properties.getProperty('package.dir')
def wrapperDir  = properties.getProperty('wrapper.dir')


sourceCompatibility = 1.7
compileJava.options.encoding = 'UTF-8'

repositories {
     maven { url "http://arti.oven.c:9000/release" }
  }

task swipe(type: Delete) {
         println "Delete $projectDir/${folderDir}"
         delete "$projectDir/$folderDir"
         delete "$projectDir/$logDir"
         delete "$projectDir/$deployDir"
         delete "$projectDir/$packageDir"
         delete "$projectDir/$testsDir"
         mkdir("$projectDir/${folderDir}")
         mkdir("projectDir/${logDir}")
         mkdir("projectDir/${deployDir}")
         mkdir("projectDir/${packageDir}")
         mkdir("projectDir/${testsDir}")
}
task prepConfigs(type: Copy, overwrite:true, dependsOn: swipe) {
    println "The name of ${projectDir}/${folderDir} and ${projectDir}/${configDir}"
    from('${projectDir}/${folderDir}')
    into('${projectDir}/$configDir}')
    include('*.xml')
}

build.properties file:

# -----------------------------------------------------------------
# General Settings
# -----------------------------------------------------------------
application.name  = Admin
project.name = Hello Cool

# -----------------------------------------------------------------
# ant build directories
# -----------------------------------------------------------------
sandbox.dir = ${projectDir}/../..
reno.root.dir=${sandbox.dir}/Reno
ant.dir = ${projectDir}/ant
build.dir = ${ant.dir}/build
log.dir  = ${ant.dir}/logs
config.dir = ${ant.dir}/configs
deploy.dir  = ${ant.dir}/deploy
static.dir =  ${ant.dir}/static
package.dir = ${ant.dir}/package
tests.dir = ${ant.dir}/tests
tests.logs.dir = ${tests.dir}/logs
external.dir = ${sandbox.dir}/FlexCommon/External
external.lib.dir = ${external.dir}/libs
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If using the default gradle.properties file, you can access the properties directly from within your build.gradle file:

gradle.properties:

applicationName=Admin
projectName=Hello Cool

build.gradle:

task printProps {
    doFirst {
        println applicationName
        println projectName
    }
}

If you need to access a custom file, or access properties which include . in them (as it appears you need to do), you can do the following in your build.gradle file:

def props = new Properties()
file("build.properties").withInputStream { props.load(it) }

task printProps {
    doFirst {
        println props.getProperty("application.name")
        println props.getProperty("project.name")
    }
}

Take a look at this section of the Gradle documentation for more information.

Edit

If you'd like to dynamically set up some of these properties (as mentioned in a comment below), you can create a properties.gradle file (the name isn't important) and require it in your build.gradle script.

properties.gradle:

ext {
    subPath = "some/sub/directory"
    fullPath = "$projectDir/$subPath"
}

build.gradle

apply from: 'properties.gradle'

// prints the full expanded path
println fullPath

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...