如何读取属性文件并使用项目 Gradle 脚本中的值?

2022-08-31 13:48:48

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

有人可以让我知道这是否是正确的方法吗?我认为另一种方法是定义中的所有内容,并在.有人可以让我知道我如何从中访问属性吗?gradle.propertiesbuild.gradlebuild.gradlebuild.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

答案 1

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

gradle.properties:

applicationName=Admin
projectName=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.gradlebuild.gradle

properties.gradle:

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

build.gradle

apply from: 'properties.gradle'

// prints the full expanded path
println fullPath

答案 2

我们可以使用一个单独的文件(在我的情况下)来抽象出所有的配置。config.groovy

在此示例中,我们使用三个环境,即:

  1. 开发
  2. 测试

它具有属性服务器名称服务器端口和资源在这里,我们期望第三个属性资源在多个环境中可能是相同的,因此我们抽象出了该逻辑,并在必要时在特定环境中重写:

config.groovy

resources {
    serverName = 'localhost'
    serverPort = '8090'
}

environments {
    dev {
        serverName = 'http://localhost'   
        serverPort = '8080'
    }

    test {
        serverName = 'http://www.testserver.com'
        serverPort = '5211'
        resources {
            serverName = 'resources.testserver.com'
        }
    }

    prod {
        serverName = 'http://www.productionserver.com'
        serverPort = '80'
        resources {
            serverName = 'resources.productionserver.com'
            serverPort = '80'
        }
    }
}

属性文件准备就绪后,我们可以使用以下 in 来加载这些设置:build.gradle

build.gradle

loadProperties()

def loadProperties() {
    def environment = hasProperty('env') ? env : 'dev'
    println "Current Environment: " + environment

    def configFile = file('config.groovy')
    def config = new ConfigSlurper(environment).parse(configFile.toURL())
    project.ext.config = config
}

task printProperties {
    println "serverName:  $config.serverName"
    println "serverPort:  $config.serverPort"
    println "resources.serverName:  $config.resources.serverName"
    println "resources.serverPort:  $config.resources.serverPort"
}

让我们使用不同的输入集运行这些:

  1. gradle -q printProperties

    Current Environment: dev
    serverName:  http://localhost
    serverPort:  8080
    resources.serverName:  localhost
    resources.serverPort:  8090
    
  2. gradle -q -Penv=dev printProperties

    Current Environment: dev
    serverName:  http://localhost
    serverPort:  8080
    resources.serverName:  localhost
    resources.serverPort:  8090
    
  3. gradle -q -Penv=test printProperties

    Current Environment: test
    serverName:  http://www.testserver.com
    serverPort:  5211
    resources.serverName:  resources.testserver.com
    resources.serverPort:  8090
    
  4. gradle -q -Penv=prod printProperties

    Current Environment: prod
    serverName:  http://www.productionserver.com
    serverPort:  80
    resources.serverName:  resources.productionserver.com
    resources.serverPort:  80
    

推荐