如何读取属性文件并使用项目 Gradle 脚本中的值? [英] How to read a properties files and use the values in project Gradle script?

查看:18
本文介绍了如何读取属性文件并使用项目 Gradle 脚本中的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 Gradle 脚本,我需要在其中读取 local.properties 文件并使用 build.gradle 中的属性文件中的值.我正在按照以下方式进行操作.我运行了下面的脚本,它现在抛出一个错误,但它也没有做任何事情,比如创建、删除和复制文件.我试图打印变量的值,它显示了正确的值.

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.

有人可以告诉我这是否是正确的方法吗?我认为另一种方法是在 gradle.properties 中定义所有内容并在 build.gradle 中使用它.有人可以告诉我如何从 build.properties 访问 build.gradle 中的属性吗?

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 文件:

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 文件:

# -----------------------------------------------------------------
# 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

推荐答案

如果使用默认的 gradle.properties 文件,您可以直接从 build.gradle 中访问属性代码>文件:

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
    }
}

如果您需要访问自定义文件,或访问包含 . 的属性(看起来您需要这样做),您可以在 build.gradle 中执行以下操作 文件:

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")
    }
}

查看Gradle 文档的这一部分了解更多信息.

如果您想动态设置其中一些属性(如下面的评论中所述),您可以创建一个 properties.gradle 文件(名称不重要)并要求它在您的 build.gradle 脚本中.

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

这篇关于如何读取属性文件并使用项目 Gradle 脚本中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆