Gradle Implementing environment based build scripts


(Shah) #1

0
down vote
favorite
I want to implement Gradle tasks which identifies all the string tokens and dynamically replace it with values by reading it from other properties files.

So here my no of parameters are also not known and i need to find all those parameters and identify its values and write back the actual value during deployment.

I have found some examples on net using Ant replaceToken class but it found to be too static where you specify the name of the property and its value in your gradle task.

Any suggestion would be helpful.

Thanks


(Shah) #2

Hi/@ssaliman

I came across gradle-properties-plugin details https://github.com/stevesaliman/gradle-properties-plugin and tried to use for my implementation. Based on it i have below code -

  def depEnv = project.property("env")
depEnv = null ? "DEV" : depEnv
def template = file(configDir)
def propFile = "gradle-".concat(depEnv).concat(".properties")
def appProperties = file(buildconfigdir.concat('//').concat(propFile))

requiredProperties appProperties

outputs.file new File(resourceDir, appProperties)

		
		println "Executing"
		from file(configDir) {
			include '**//**'
			filter(ReplaceTokens, tokens:project.filterTokens)	
		}
		into resourceDir

The task execution fails at initial logic with error "Could not find method requiredProperties() for arguments. I have also added the plugin details as below in my build file -

plugins {
id 'net.saliman.properties' version '1.4.6'
}

Does it requries something more to do to use this plugin jar file. It is part of my gradle repository.


(Shah) #3

Gradle Experts,

Any input here ? This is still not working. Struggling from last 2 days. I have stopped applying any dynamics approach. I am hardcoding to replace my tokens under copy task as -

from fileTree(buildconfigdir) {
		include '**//gradle-DEV.properties'
	}
	from fileTree(configDir) {
		include '**//**'
	}
	into resourceDir	
	filter(ReplaceTokens, tokens:[USER:USER])

But it gives me an error - “A problem occurred evaluating root project ‘aura-console’.
Could not get unknown property ‘USER’ for task ‘:prepareProperties’ of type or
g.gradle.api.tasks.Copy.”

Requesting someone to provide your inputs for it.


(Steven C. Saliman) #4

I think the properties plugin will do exactly what you need it to do,
but I’m not sure you’re applying it right.

  1. You don’t need to define the properties file in your build. The
    plugin does that for you. You just need to call the build with the
    right environment name. For example, to load the “dev” file, you’d use
    "-PenvironmentName=dev" as part of your gradle command.

  2. The requiredProperties method takes an array of strings, not a file.
    It is used to make sure that all the properties you need are present in
    your file. For example, if you need a database connection in your test
    task, you’d use something like “requiredProperties ‘jdbcUrl’,
    ‘jdbcUser’, ‘jdbcPassword’”

  3. The requiredProperties method needs to be inside a task.

From there, you should be able to use a filter inside a copy task.

I hope this helps,

Steve