xmltask error when replacing node in xml file - ant

I got an error when runnning target replace to xmltask:
C:\Tools\build.xml:432: The following error occurred while executing this line:
C:\Tools\build.xml:408: Failed to specify text in replace
the problem is that my coworker had no issues running the same code on his computer. I can't figure out why I got the error while my coworker didn't.
the part of build.xml is as follow:
<target name="replace" depends="init" description="replace node.">
<xmltask source="${my-file}" dest="${my-file}">
<replace path=
"/*[local-name()='server']
/*[local-name()='profile']
/*[local-name() = 'subsystem'][1]"
withFile="${devlogfile}"/>
</xmltask>
</target>
<target name="init">
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="${ant.lib.dir}/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
<taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask">
<classpath>
<pathelement path="${ant.lib.dir}/xmltask.jar"/>
</classpath>
</taskdef>
</target>
the part of property file is as follow:
my-file=c:/old.xml
devlogfile=c:/new.xml

[Converted from the comments section]
Did you compare the jdk and classpath in the case of your co-worker and yourself ? It's impossible to tell with the information given so far, but it's possible you are using different parsers.
The error message suggests Ant doesn't find the devlogfile file itself.
Are you sure the properties file is passed properly to your ant runtime ? How are you passing it ?

Related

Ant for script how to continue if any iteration fails

I writing an ant build file to check all translate files in a directory.
I want the ant for script to continue checking the rest files if an checking error in any file appear.
My ant task:
<taskdef name="validate" classname="ValidateTranslateFile">
<classpath>
<pathelement location="${ant-libs-dir}/TranslateFileUtilities.jar" />
<pathelement location="../web/WEB-INF/lib/commons-io-2.5.jar" />
<pathelement location="../web/WEB-INF/lib/commons-lang3-3.5.jar" />
</classpath>
</taskdef>
<for param="program">
<path>
<fileset dir="../web/WEB-INF" includes="*.txt" />
</path>
<sequential>
<validate targetFile="#{program}" checkLanguages="true" checkKeysOrder="true" />
</sequential>
</for>
</target>
the Result:
it checks the files till the first error appear, and then the BUILD FAILED.
Could any one help me with that?
The for task is not part of standard ANT. It is a 3rd party extension documented here:
http://ant-contrib.sourceforge.net/tasks/tasks/for.html
The documentation suggests using a "keepgoing" attribute to ignore errors, this might be the answer you're looking for.
If this is a custom ANT task you are writing, perhaps you should consider refactoring the code to operate on a fileset? This would enable you to call task as follows:
<validate checkLanguages="true" checkKeysOrder="true">
<fileset dir="../web/WEB-INF" includes="*.txt" />
</validate>
Simpler and more robust.

Ant Throws Reference Error for Existing Jar File

I want to add task to my build.xml file:
<target name="forbidden-checks" depends="download-forbidden-checks">
<taskdef name="forbidden-apis" classname="de.thetaphi.forbiddenapis.AntTask"
classpathref="lib/forbiddenapis-2.2.jar"/>
<fa:forbiddenapis classpathref="build.classpath" dir="${build.dir}" targetVersion="${jdk.version}">
<bundledsignatures name="jdk-unsafe"/>
<bundledsignatures name="jdk-deprecated"/>
<bundledsignatures name="jdk-non-portable"/>
</fa:forbiddenapis>
</target>
I see that jar is downloaded, there is no problem. However I get an error:
download-forbidden-checks:
setup-maven-url:
download-via-maven:
[get] Getting: https://repo1.maven.org/maven2/de/thetaphi/forbiddenapis/2.2/forbiddenapis-2.2.jar
[get] To: /home/kamaci/pro/lib/forbiddenapis-2.2.jar
forbidden-checks:
BUILD FAILED
/home/kamaci/pro/build.xml:2391: Reference /home/kamaci/pro/lib/forbiddenapis-2.2.jar not found.
I checked if I have a problem with the path of jar. So, I've put forbiddenapis-2.2.jar to another place than the project. I've pointed that jar from my taskef but I got same error.
Any ideas?
EDIT 1:
I've changed it to that:
<target name="forbidden-checks" depends="download-forbidden-checks">
<taskdef name="forbidden-apis" classname="de.thetaphi.forbiddenapis.AntTask">
<classpath>
<pathelement location="/home/kamaci/pro/forbiddenapis-2.2.jar"/>
</classpath>
</taskdef>
<fa:forbiddenapis targetVersion="${jdk.version}">
<classpath>
<pathelement location="lib/forbiddenapis-2.2.jar"/>
</classpath>
<bundledsignatures name="jdk-unsafe"/>
<bundledsignatures name="jdk-deprecated"/>
<bundledsignatures name="jdk-non-portable"/>
</fa:forbiddenapis>
</target>
However, it didn't work:
Problem: failed to create task or type antlib:de.thetaphi.forbiddenapis:forbiddenapis
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.
No types or tasks have been defined in this namespace yet
This appears to be an antlib declaration.
Action: Check that the implementing library exists in one of:
-/usr/share/ant/lib
-/home/kamaci/.ant/lib
-a directory added on the command line with the -lib argument
When I run this:
ant forbidden-checks -lib lib/
it works. My project definition starts with that at build.xml:
Why I should add -lib? Is there any way to avoid it?
Pretty sure the issue is the classpathref as it is using a literal value and not a path element, so the jar may exist just not on the classpath, try creating a path element to use for the taskdef
<path id="lib.path">
<fileset dir="lib" includes="lib/*.jar"/>
</path>
<taskdef name="forbidden-apis" classname="de.thetaphi.forbiddenapis.AntTask"
classpathref="lib.path"/>
or defining the classpath inside the taskdef.
<taskdef name="forbidden-apis" classname="de.thetaphi.forbiddenapis.AntTask">
<classpath>
<pathelement location="lib/forbiddenapis-2.2.jar"/>
</classpath>
</taskdef>

ANT how to execute a JAR or EXE files on a specific folder

I have a POS software (Point Of Sale) runs on ANT, never worked on ant so please if possible provide full code to run either an EXE or a JAR executable file on a specific location.
I have those following codes and no use:
<target name="build">
<java fork="true" failonerror="yes" classname="com.CodeReview">
<classpath>
<pathelement location="xyz"/>
<pathelement path="${C:/xyz.jar}"/>
</classpath>
<arg line="-p"/>
<arg line="D:\Test"/>
</java>
</target>
PS: First code gave this error:
com.openbravo.pos.scripting.ScriptException:
Encountered ":/xyz.jar}\"/>\n \n\n ...
The error is caused by the ${ and } around C:/xyz.jar, which should not be there because C:/xyz.jar is a literal and not the name of a property whose value you want to use.
If you defined a property like this:
<property name="path.to.xyz.jar" value="C:/xyz.jar"/>
...then you could later reference it like this:
<pathelement path="${path.to.xyz.jar}"/>
Otherwise, to insert the value directly without using a property, the ${ and the } must be removed:
<pathelement path="C:/xyz.jar"/>

Could not load definitions from resource when using ant task

I'm using ant-contrib in my build script
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="ant/ant-contrib-0.6.jar"/>
</classpath>
</taskdef>
It works. But when I'm calling some target in this script from another ant file using ant task, I'm getting the error.
<ant antfile="build.xml" target="make" dir="${client.project.location}/ant"/>
Please, help me to fix the problem. Thanks
In your code we can see
<classpath>
<pathelement location="ant/ant-contrib-0.6.jar"/>
</classpath>
You need to give path location="/home/[some
path]/ant-contrib-0.6.jar"
you can do one more thing,you need to copy your ant-contrib-0.6.jar
into ANT_HOME/lib folder and remove your tag completely from you code.
Note: Second option is always better.
I had to set usenativebasedir="true" when calling ant task

Why cannot Ant taskdef cannot load a resource outside ./net

When declaring external ant tasks using taskdef, for instance ant-contrib, the proposed setup is to use the followin taskdef:
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="lib/ant-contrib/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
This works when antcontrib.properties is located in net/sf/antcontrib relative to the build.xml file.
But when I put it in lib/net/sf/antcontrib and changes the taskdef into
<taskdef resource="lib/net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="lib/ant-contrib/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
Ant is not able to find the properties file, it gives the error
[taskdef] Could not load definitions from resource
lib/net/sf/antcontrib/antcontrib.properties. It could not be found.
It seems like ant treats the lib directory separately and fails to load a taskdef resource from there.
As Alex said, you shouldn't need to unzip the jar. The <taskdef> can load antcontrib.properties directly out of the jar.
The error you got is because you changed the resource path, but the path to the file inside the compressed jar/zip is still the same. The taskdef isn't paying attention to the properties file you moved because the <classpath> you provided to <taskdef> tells it to only look in the jar.
Use antlib.xml resource:
Here is the taskdef definition that I use:
<property name="ant-contrib.jar" location="..."/>
<taskdef
resource="net/sf/antcontrib/antlib.xml"
uri="http://ant-contrib.sourceforge.net"
>
<classpath>
<pathelement location="${ant-contrib.jar}"/>
</classpath>
</taskdef>
You do not need to extract anything from the jar file. Also, uri attribute is optional if you do not want to use namespaces with antcontrib tasks.
To handle classpath for tasks definitions, I use a classpath ref in Ant, it's way easier. You can link either a directory containing classes, either a directory containing many .jar, either (of course) a single .jar.
For example :
<!-- Properties -->
<property name="lib" value="lib/" />
<property name="classes" value="bin/" />
<!-- Classpath definition -->
<path id="runtime-classpath" >
<pathelement location="${bin}" />
<fileset dir="${lib}">
<include name="*.jar"/>
</fileset>
</path>
<!-- Taskdefs definitions -->
<taskdef name="myTask" classname="org.stackoverflow.tasks.MyTask" classpathref="runtime-classpath" />
<!-- Tasks -->
<target name="test" description="Test Action">
<myTask parameter1="value" />
</target>

Resources