I'm trying to retrieve a String defined in the class Core.java as follows:
public static final String PLATFORM_VERSION = "3.0.1_170518";
So I have created a test ant file (because I want to store it as an ant property to be used in compilation process).
<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." default="get-core-version" name="test">
<target name="get-core-version">
<exec executable="bash"
outputproperty="coreVersionTemp"
failonerror="true">
<arg value="-c"/>
<arg value="cat ./Core.java | grep VERSION"/>
</exec>
<echo message=""ResultadoIntermedio": ${coreVersionTemp}"/>
<exec executable="bash"
outputproperty="coreVersion"
failonerror="true">
<arg value="-c"/>
<arg value="echo ${coreVersionTemp} | cut -d'\"' -f2"/>
</exec>
<echo message="Resultado: ${coreVersion}"/>
</target>
The ant code is split to locate exactly where is failing. The ant returns:
Buildfile: E:\git\test.xml
get-core-version:
[echo] "ResultadoIntermedio": public static final String PLATFORM_VERSION = "3.0.1_170518";
BUILD FAILED
E:\git\test.xml:14: exec returned: 1
Total time: 0 seconds
As you can see, the error is on 'cut' command. ResultadoIntermedio is correct. If we execute the full command on bash, we have the expected result as well:
$ cat ./Core.java | grep VERSION | cut -d '"' -f2
3.0.1_170518
The problem is, I think, in the escape characters after the -d option of cut. I have tried:
'"'
'\"'
\'\"\'
'"'
\'"\'
'"'
And some other combinations... how can I do this correctly?
Thank you very much.
Taking a workaround replacing '"' to ':' in the property with Javascript works fine, but is a little nasty solution.
<target name="get-core-version">
<exec executable="bash"
outputproperty="coreVersionTemp"
failonerror="true">
<arg value="-c"/>
<arg value="cat ./Core.java | grep VERSION"/>
</exec>
<echo message=""ResultadoIntermedio": ${coreVersionTemp}"/>
<!-- <propertyregex property="coreVersionTemp2" input="coreVersionTemp" regexp=""" replace=":" global="true"/> -->
<script language="javascript">
var temp = project.getProperty("coreVersionTemp");
project.setProperty("coreVersionTemp", temp.replaceAll("\"", ":"));
</script>
<echo message=""ResultadoIntermedio2": ${coreVersionTemp}"/>
<exec executable="bash"
outputproperty="coreVersion"
failonerror="true">
<arg value="-c"/>
<arg value="echo '${coreVersionTemp}' | cut -d: -f2"/>
</exec>
<echo message="Resultado: ${coreVersion}"/>
</target>
</project>