How to use variable outside the talk in gradle script


(Vinay Verma) #1

Hi,

I am stuck in a situation where need to read a file (for some values, lets say version number) from inside a war file and use it somewhere else in the same script (I am exploding the war file for this purpose using a Copy task). To explain the need, I will write down with the example below:

###Defined the variable:
def projVersion = “NULL”

###Exploding the war:
task explodedWar(type: Copy) {
from zipTree("$buildPath/projectName.war")
into file("$buildPath/projectName")
}
###Reading the file from exploded folder and getting a value:

task warVersion(dependsOn : ['explodedWar']) <<{
		Properties versionFile = new Properties()
		versionFile.load(new FileInputStream("$buildPath/projectName/META-INF/MANIFEST.MF"))
		ext.projVersion = versionFile.getProperty("Version")
}

###When Using the variables new value outside the task (This part is throwing Error):
println "Variables new value: " + warVersion.projVersion

###When Using the variables new value inside some other task (This part is Successful):
task VersionPrint(dependsOn : [‘warVersion’]) <<{
println “Project Version under print task” + warVersion.projVersion
}

Basically, I am able to use the new value of the variable inside any other task in the same script but when I am trying to use the variables new value outside the task areas (sorry but it’s a need), it is thowing error:

###Error (When using variable outside the task) ***
* What went wrong:
A problem occurred evaluating root project ‘Scripts’.
> Could not find property ‘projVersion’ on task ‘:warVersion’.

###Let me know, Am I trying to achieve something which is achievable? or is it going to be rule breaking way for gradle?
###If it is possible what I am searching for, please let me know the solution, how to achieve.


(Vinay Verma) #2

A solution worked for me: Link

Thanks :slight_smile: