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

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

问题描述

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

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

访问 build.gradle 中的属性。
$ b build.gradle file:

  apply plugin:'java'

// - 设置发布组
group ='com.true.test'

/ **
*初始化GAVC设置
* /
def buildProperties = new Properties()
file(version.properties)。withInputStream {
stream - > buildProperties.load(流)
}
//如果jenkins构建,将jenkins构建版本添加到版本中。否则,添加快照版本的版本。
def env = System.getenv()
if(env [BUILD_NUMBER])buildProperties.test + =。$ {env [BUILD_NUMBER]}
version = buildProperties.test
println$ {version}

// name设置在settings.gradle文件中
group =com.true.test
version = buildProperties .test
printlnBuilding $ {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'

知识库{
maven {urlhttp://arti.oven.c :9000 / release}
}

任务刷卡(类型:删除){
println删除$ projectDir / $ {folderDir}
delete$ projectDir / $ folderDir
删除$ projectDir / $ logDir
删除$ projectDir / $ deployDir
删除$ projectDir / $ packageDir
删除$ projectDir / $ testsDir
mkdir($ projectDir / $ {folderDir})
mkdir(projectDir / $ {logDir})
mkdir(projectDir / $ {deployDir})
mkdir(projectDir / $ {packageDir})
mkdir(projectDir / $ {testsDir})
}
任务prepConfigs(类型:Copy,覆盖:true,dependsOn: ('$ {projectDir} / $ {folderDir}'中的
){
println$ {projectDir} / $ {folderDir}和$ {projectDir} / $ {configDir}的名称

转换为('$ {projectDir} / $ configDir}')
i nclude('*。xml')
}

build。属性 file:

 #--------------- -------------------------------------------------- 
#常规设置
#-------------------------------------- ---------------------------
application.name = Admin
project.name = Hello Cool

#--------------------------------------------- --------------------
#ant建立目录
#----------------- ------------------------------------------------
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
外部.lib.dir = $ {external.dir} / libs


解决方案

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

gradle.properties

  applicationName = Admin 
projectName = Hello Cool

build.gradle

 任务printProps {
doFirst {
println applicationName
println projectName


code


如果你需要访问一个自定义文件,或访问其中包含的属性(因为看起来您需要这样做),您可以在 build.gradle file:

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

任务printProps {
doFirst {
printl n props.getProperty(application.name)
println props.getProperty(project.name)
}
}

查看本节Gradle文档获取更多信息。



编辑



d喜欢动态设置这些属性中的一部分(如下面的注释中所提到的),您可以创建一个 properties.gradle 文件(名称不重要)并且需要它在 build.gradle 脚本中。



properties.gradle

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

build.gradle

  apply from:'properties.gradle'

//打印完整扩展路径
println fu llPath


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

解决方案

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

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

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