How to change the Path variable in ant exec task? - ant

I tried setting the Path variable in ant exec task using this link. But its not working somehow. I am trying to add THRIFT_HOME which is set to basedir currently and I have the thrift.exe file in the basedir.
Also, when I change executable attribute value to thrift.exe, the thrift compiler works properly but the basedir is not added to the Path.
<exec executable="thrift">
<env key="Path" value="${env.Path};${THRIFT_HOME}" />
</exec>
I also tried using the setx command as:
<exec executable="setx">
<arg value="Path"/>
<arg value="%Path%;${THRIFT_HOME}"/>
</exec>
but it still does not work.
Any help would be appreciated!

how about this:
<property environment="env"/>
<!-- to see if you can access it -->
<echo message="${env.Path}" />
<exec executable="thrift">
<env key="Path" value="${env.Path};${THRIFT_HOME}" />
</exec>
I've found a hint here: http://www.factsandpeople.com/facts-mainmenu-5/23-other-software-technologies/125-using-environment-variables-in-ant-environment-variables-are-not-evaluated

I am currently using on Windows 7 and the problem was with spaces in the folder name. That's a big problem when working on Windows.
I tried placing the thrift executable at /d/thrift/ and it worked and assigning THRIFT_HOME = /d/thrift. Then I tried /d/thrift demo/ and there is the problem.
D:\noknok_fido_to_ostp\noknok_fido_to_ostp_workspace\Demo\build.xml:33: Execute failed: java.io.IOException: Cannot run program "thrift-0.9.0.exe": CreateProces
s error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:460)
at java.lang.Runtime.exec(Runtime.java:593)
at org.apache.tools.ant.taskdefs.Execute$Java13CommandLauncher.exec(Execute.java:862)
at org.apache.tools.ant.taskdefs.Execute.launch(Execute.java:481)
at org.apache.tools.ant.taskdefs.Execute.execute(Execute.java:495)
at org.apache.tools.ant.taskdefs.ExecTask.runExecute(ExecTask.java:631)
at org.apache.tools.ant.taskdefs.ExecTask.runExec(ExecTask.java:672)
at org.apache.tools.ant.taskdefs.ExecTask.execute(ExecTask.java:498)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:392)
at org.apache.tools.ant.Target.performTasks(Target.java:413)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
at org.apache.tools.ant.Main.runBuild(Main.java:811)
at org.apache.tools.ant.Main.startAnt(Main.java:217)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
at java.lang.ProcessImpl.start(ProcessImpl.java:30)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:453)
... 23 more
Another problem is that /d/thrift doesn't work in Eclipse Ant plugin build. I tried it using command prompt and the build was successful.
This teaches a new lesson: Always build from command prompt!
Thanks to all who answered for this question.

And what about searchpath attribute ?
searchpath: When this attribute is true, then system path environment variables will be searched when resolving the location of the executable. since Ant 1.6.3
So I think something like this:
<exec executable="thrift" searchpath="true">
<env key="Path" value="${env.Path};${THRIFT_HOME}" />
</exec>
should work.

I found it works by quote the value of variable
<exec executable="setx">
<arg line="Path "${env.Path};c:\test Path"" />
<arg line="/m" />
</exec>
https://stackoverflow.com/a/23777929/3659861

Related

ANT error CreateProcess error=2

When I try to call simple JAR file via ANT. Whenever i execute I get the following error:
C:\temp\My\My_Ant.xml:20: Execute failed: java.io.IOException: Cannot run program "C:\PROGRA~1\Java\jre7\bin\java.exe -jar C:\temp\My\javatest.jar" (in directory "C:\temp\My"): CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1047)
at java.lang.Runtime.exec(Runtime.java:617)
at org.apache.tools.ant.taskdefs.launcher.Java13CommandLauncher.exec(Java13CommandLauncher.java:58)
at org.apache.tools.ant.taskdefs.Execute.launch(Execute.java:426)
at org.apache.tools.ant.taskdefs.Execute.execute(Execute.java:440)
at org.apache.tools.ant.taskdefs.ExecTask.runExecute(ExecTask.java:629)
at org.apache.tools.ant.taskdefs.ExecTask.runExec(ExecTask.java:670)
at org.apache.tools.ant.taskdefs.ExecTask.execute(ExecTask.java:496)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:435)
at org.apache.tools.ant.Target.performTasks(Target.java:456)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1405)
at org.apache.tools.ant.Project.executeTarget(Project.java:1376)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1260)
at org.apache.tools.ant.Main.runBuild(Main.java:854)
at org.apache.tools.ant.Main.startAnt(Main.java:236)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:285)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:112)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.(ProcessImpl.java:385)
at java.lang.ProcessImpl.start(ProcessImpl.java:136)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1028)
... 24 more
The Java program is simply printing "hello world!"
I call the ANT in the following way:
C:\Tools\ANT\apache-ant-1.9.7\bin\ant -buildfile My_Ant.xml
Any idea why I am getting this? when I run this via command line I get the correct message.
C:\temp\My>C:\PROGRA~1\Java\jre7\bin\java.exe -jar JavaTest.jar
hello from Java
Edit:
Ant Script:
<?xml version="1.0" encoding="UTF-8"?>
<project name="Issue Management" default="startActivity" xmlns:if="ant:if" xmlns:unless="ant:unless">
<property name="javaPath" value="C:\PROGRA~1\Java\jre7\bin\java.exe"/>
<property name="AnalyzerPath" value="C:\temp\my"/>
<property name="Analyzer" value="javatest.jar"/>
<!--Setting the location of ANT Contrib starts
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="C:/Tools/ANT/apache-ant-1.8.2/ant-contrib-1.0b3.jar" />
</classpath>
</taskdef>-->
<tstamp>
<format property="current.time" pattern="yyyyMMdd_HHmmss" />
</tstamp>
<target name="startActivity">
<echo>Issue Management script started at ${current.time}</echo>
<exec executable="${javaPath} -jar ${AnalyzerPath}\${Analyzer}" resultproperty="BuildErrorCode" failonerror="true" dir="${AnalyzerPath}">
<arg value="--help"/>
</exec>
<echo>Issue Management script ended at ${current.time}</echo>
</target>
</project>
The executable parameter needs to be set to the name or path of just the executable; command arguments should be specified separately using nested <arg> elements, for example:
<exec executable="${javaPath}" resultproperty="BuildErrorCode"
failonerror="true" dir="${AnalyzerPath}">
<arg value="-jar" />
<arg value="${AnalyzerPath}\${Analyzer}" />
<arg value="--help" />
</exec>
When you specified the whole command line as the executable, it was treated as a single command with embedded spaces, and so was not found:
Cannot run program "C:\PROGRA~1\Java\jre7\bin\java.exe -jar C:\temp\My\javatest.jar"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Apache Ant: Unable to call Composer's vendor/bin scripts

I am using Apache Ant for my builds. I have some composer scripts belonging to several vendors in vendor/bin folder. I have added this folder to the system path and if I run the commands on my command window in works but in the build file i get an error.
Is there anything I should be doing differently? Before is an example:
<?xml version="1.0" encoding="UTF-8"?>
<project name="MyProject" default="build" basedir=".">
<target name="phpcpd" description="Find duplicate code using PHPCPD">
<exec executable="phpcpd">
<arg value="--version" />
</exec>
</target>
</project>'
I get this when I run ant phpcpd
phpcpd:
BUILD FAILED
C:\xxxxxx\xxxxxxx\build.xml:96: Execute failed: java.io.IO Exce
ption: Cannot run program "phpcpd": CreateProcess error=2, The system
cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1041)
at java.lang.Runtime.exec(Runtime.java:617)
at org.apache.tools.ant.taskdefs.launcher.Java13CommandLauncher.exec(Jav
a13CommandLauncher.java:41)
at org.apache.tools.ant.taskdefs.Execute.launch(Execute.java:428)
at org.apache.tools.ant.taskdefs.Execute.execute(Execute.java:442)
at org.apache.tools.ant.taskdefs.ExecTask.runExecute(ExecTask.java:628)
at org.apache.tools.ant.taskdefs.ExecTask.runExec(ExecTask.java:669)
at org.apache.tools.ant.taskdefs.ExecTask.execute(ExecTask.java:495)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
...
But phpcpd --version works on command prompt
ANT is not aware where phpcpd is as it doesn't share path with your Command Promt.
One way around it is to create a .bat file to run phpcpd
Create a phpcpd.bat with the following:
#echo off
phpcpd --version
Your build scripts to be updated from :
<exec executable="phpcpd">
<arg value="--version" />
</exec>
To: <exec executable="phpcpd.bat"/>
Above assumed with Windows Command Promt
My solution was to use the .phar files of the scripts. That way, the build file became platform independent to a large extent. So
<target name="phpcpd" description="Find duplicate code using PHPCPD">
<exec executable="phpcpd">
<arg value="--version" />
</exec>
</target>
Became:
<target name="phpcpd" description="Find duplicate code using PHPCPD">
<exec executable="php">
<arg value="${phpcpd}" />
<arg value="--version" />
</exec>
</target>
Where ${phpcpd} is the path to the phar file
I'm using absolute paths and a configurable executable.properties like this:
build.xml
<project name="build">
<property file="executable.properties" />
<target name="run-phpcd" unless="${phpcpd.skip}">
<exec executable="${phpcpd.executable}"><!-- .. --></exec>
</target>
</project>
executable.dist.properties
phpcpd.skip = no
#phpcpd.executable = C:\path\to\phpcpd.bat
phpcpd.executable = /path/to/phpcpd.sh
Both files can be committed to your VCS, for usage copy the template file (*.dist.properties) and rename it to executable.properties. Add this file to the ignore list of your VCS.

Ant build using thrift fails with Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified

What I want to do is:
<exec executable="thrift" dir="${thriftsrc}">
<arg value="--gen java"/>
<arg value="-out ${src}"/>
<arg value="mqlServer.idl"/>
</exec>
I have copied thrift.exe in C:\Windows\System32\ so the file is definitely in the PATH. I have tried several executable Arguments, full path, with and without .exe but it is not working in any variant.
But this is working very well:
<exec executable="perl" dir="${generators}">
<arg value="compactTalib.pl"/>
<arg value="${talibsrc}"/>
</exec>
Any Ideas how I can get my thirft compiler invoked in my ant build?
First, go to a DOS prompt and type in "thrift". Does it "work"? I'd expect it to give you an error, but at least find the exe. If it doesn't find the exe, solve that problem before going back to Ant.
Second, echo ${thriftsrc} in Ant. Is that C:\Windows\System32? If not, omit the dir argument. It's optional so you might remove it anyway and just use the path.
Finally, I see another problem that you haven't hit yet. This is going to get passed in as a single parameter "--gen java".
<arg value="--gen java"/>
The relevant part of the doc is:
is a single command-line argument containing a
space character, not separate commands "-l" and "-a".
This is likely how the thrift command should be called:
<exec executable="thrift" dir="${thriftsrc}">
<arg value="--gen"/>
<arg value="java"/>
<arg value="-out"/>
<arg value="${src}"/>
<arg value="mqlServer.idl"/>
</exec>
Also, consider adding failonerror to your <exec> task:
<exec executable="thrift" dir="${thriftsrc}" failonerror="yes">
This will cause the Ant script to end with an error message which will help with troubleshooting.

Ant can't find a executable in the Windows path

I got a simple ant target :
<target name="doxygen">
<exec executable="doxygen" dir="${basedir}/doxygen">
<arg value="Doxyfile" />
</exec>
</target>
I'm on Windows Seven.
When i try the same command line ( doxygen Doxyfile ) in the Windows console, it works perfectly. The doxygen executable can by found because i added the good path in my PATH environment variable.
But ANT juste can't find the doxygen executable and i get the following error :
build.xml:83: Execute failed: java.io.IOException: Cannot run program "doxygen.exe" : CreateProcess error=2
How can i make ANT to use the Windows PATH environment variable ?
I already tried the searchpath property, but i don't works.
You want to find where Doxygen is currently installed on your system. Then make a property with that value, so it can be overridden by people that installed doxygen somewhere else.
<property name="doxygen.path" location="C:\Program Files\Doxygen"/>
<target name="doxygen">
<exec executable="${doxygen.path}/doxygen" dir="${basedir}/doxygen">
<arg value="Doxyfile" />
</exec>
</target>

Ant can't find an ant class (DateUtils) while building

I'm trying to get a project to build via ant. It builds just fine via ant on my local machine, but on our build machine I get :
java.lang.NoClassDefFoundError: org/apache/tools/ant/util/DateUtils
at org.apache.tools.ant.DefaultLogger.formatTime(DefaultLogger.java:323)
at org.apache.tools.ant.DefaultLogger.buildFinished(DefaultLogger.java:170)
at org.apache.tools.ant.Project.fireBuildFinished(Project.java:
at org.apache.tools.ant.Main.runBuild(Main.java:778)
at org.apache.tools.ant.Main.startAnt(Main.java:217)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:257)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:104)
Caused by: java.lang.ClassNotFoundException: org.apache.tools.ant.util.DateUtils
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:319)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:264)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:332)
This happens within the jaxb-code-generation target and happens when it calls a simple ant copy method. The class it can't find (DateUtils) is within the ant.jar itself and I can't figure out how to get it to load the ant.jar for this target (which I would have thought already had the ant.jar available since it's running within ant). I know this is a classpath problem but cannot figure out how to fix it.
Here's the part of the ant build file up to the copy that fails:
<target name="jaxb-code-generation" depends="xjc-typedef-target,-do-init,-init-macrodef-javac">
<mkdir dir="${build.generated.sources.dir}/jaxb" xmlns:s="http://xml.netbeans.org/schema/JAXBWizConfig"/>
<mkdir dir="build/generated/jaxbCache" xmlns:s="http://xml.netbeans.org/schema/JAXBWizConfig"/>
<mkdir dir="build/generated/jaxbCache/Listing"/>
<xjc package="Listing" destdir="build/generated/jaxbCache/Listing" catalog="catalog.xml">
<classpath>
<pathelement location="${src.dir}"/>
<pathelement path="${jaxbwiz.xjcrun.classpath}"/>
</classpath>
<arg value="-xmlschema"/>
<schema file="xml-resources/jaxb/Listing/Listing.xsd"/>
<depends file="xml-resources/jaxb/Listing/Listing.xsd"/>
<produces dir="build/generated/jaxbCache/Listing"/>
</xjc>
<copy todir="${build.generated.sources.dir}/jaxb">
<fileset dir="build/generated/jaxbCache/Listing"/>
</copy>
Again, this works fine on my local machine. Both my local machine and the build machine are running Java 1.6 and Ant 1.7.1 and both are linux (mine is Linux Mint and the build machine is Ubuntu).
UPDATE: Data point: I created a new build.xml that just copies a file, that's it. it used the same ant method and that worked fine when run solo.
I had this same problem (same error, at least) in what seems like a related build problem.
The solution that worked for me was simply to set ANT_HOME environment variable.
This seems unnecessary, because ant is in the PATH, so I'd expect it would have found everything it needed, but it appeared to not. When I set ANT_HOME to the correct path, the same project then built.
Sometimes I use this:
jarfinder
You can check org/apache/tools/ant/util/DateUtils
I am not sure which you miss, but maybe helps to guess.
Run xjc native with something like this:
<target name="xjc">
<exec executable="xjc">
<arg value="-d"/>
<arg value="src"/>
<arg value="-p"/>
<arg value="bla.bla.bla"/>
<arg value="${xjc.in.dir}/bla.xsd"/>
</exec>
</target>
Works flawless for me.
found here

Resources