如何读取属性文件并在项目Gradle脚本中使用值?
我正在使用Gradle脚本工作,需要在其中读取local.properties
文件并使用中的属性文件中的值build.gradle
。我以下面的方式来做。我运行了以下脚本,它现在引发错误,但是它也没有执行创建,删除和复制文件的操作。我试图打印变量的值,它显示正确的值。
有人可以让我知道这是否是正确的方法吗?我认为另一种方法是在中定义所有内容并在中gradle.properties
使用它build.gradle
。有人可以让我知道如何build.gradle
从中访问属性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
文件内部访问属性:
gradle.properties
:
applicationName=AdminprojectName=Hello Cool
build.gradle
:
task printProps { doFirst {
println applicationName
println projectName
}
}
如果您需要访问自定义文件或访问其中包含的属性.
(看起来需要这样做),则可以在build.gradle
文件中执行以下操作:
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
脚本中需要它。
properties.gradle
:
ext { subPath = "some/sub/directory"
fullPath = "$projectDir/$subPath"
}
build.gradle
apply from: 'properties.gradle'// prints the full expanded path
println fullPath
以上是 如何读取属性文件并在项目Gradle脚本中使用值? 的全部内容, 来源链接: utcz.com/qa/427981.html