Cannot run phploc through ANT - ant

I've looked at both these posts.. they don't help:
PHP build for Jenkins failing with 'Cannot run program "phploc"'
Cannot run phploc installed through composer
If I run phploc at a shell prompt, it works just fine. So it must be something with my build.xml file... but I don't know what.
<target name="phploc" description="Measure project size using PHPLOC">
<exec executable="phploc">
<arg path="${basedir}/../src" />
<arg value="--log-csv" />
<arg value="${basedir}/build/logs/phploc.csv" />
</exec>
</target>
This works fine:
C:\projects\project1\build>phploc ../src
phploc 2.0.6 by Sebastian Bergmann.
My folder structure is
c:\projects\project1
build
...
vendor
bin
...
src
tests
c:\projects\project1\build>ant
phploc:
BUILD FAILED
C:\projects\project1\build\build.xml:55: Execute failed: java.io.IOException:
Cannot run program "phploc": CreateProcess error=2, The system cannot find the
file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at java.lang.Runtime.exec(Runtime.java:620)
at org.apache.tools.ant.taskdefs.launcher.Java13CommandLauncher.exec(Jav
UPDATE
C:\>where phploc
INFO: Could not find files for the given pattern(s).
What am I missing?

From chat discussion, it came out to be a PATH related issue. That's exactly what I was thinking it to be.
Setting absolute path of phploc in <exec executable="phploc"> did the trick. It was working on command line from C:\projects\project1\build directory because phploc was in one of its sub-directory (vendor\bin) and this sub-directory was already in the PATH variable.
PATH=C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Calibre2\;C:\xampp\php;C:\ProgramData\ComposerSetup\bin;C:\Program Files (x86)\IDM Computer Solutions\UltraEdit\;C:\Program Files (x86)\Java;c:\ant\bin;.\vendor\bin
Note: Although providing absolute path worked here but it's a good practice to use relative path so that your project is portable. In this case, you could use basedir as the reference point for all relative paths.

Related

Ant inline exec arguments

I have tasks running in Ant, which I'm quite new to, as part of a CI chain of build events. I used a tutorial to create the file for testing/linting/etc a PHP application.
The first important directives in the build.xml are:
<property name="phpmd" value="phpmd"/>
<property name="phpunit" value="phpunit"/>
This works fine as is, assuming that phpmd/phpunit are on the path, and using phpunit as a further example, is run under the following target:
<target name="phpunit" unless="phpunit.done" depends="prepare" description="Run unit tests with PHPUnit">
<exec executable="${phpunit}" resultproperty="result.phpunit" taskname="phpunit">
<arg value="--configuration"/>
<arg path="${basedir}/phpunit.xml"/>
</exec>
<property name="phpunit.done" value="true"/>
</target>
All this works well as is- but I want to use docker from now on, which I had hoped would simply mean changing <property name="phpunit" value="phpunit"/> to <property name="phpunit" value="docker-compose run php phpunit"/>, but this instead gives me the following error:
Execute failed: java.io.IOException: Cannot run program "docker-compose run -w /var/www/src php phpunit" (in directory "/var/lib/jenkins/jobs/Blah blah blah/workspace/src"): error=2, No such file or directory
I know that you would usually add additional <arg/> nodes to targets- but is it not possible at all to provide the full command with inline arguments on the initial <property>?
Ant is obviously complaining because, along with those inline arguments, that executable doesn't exist. Will I have to use arg nodes and update every single target?
Using docker-compose alone works fine, but I need the args for the correct container and working directory to be used- preferably inline, otherwise I have to insert many arg nodes.
In the end I just created a property for phpunit arguments and then added it by using the <args line="${phpunitArgs}">.
Definitely not ideal, but at least it does what it is supposed to. I certainly prefer using Gulp! XML feels like a bad choice for a build system.

Executing Python script from Phing

I have the following in my Phing build file:
<target name="fixModifiedTime">
<echo msg="Fixing file modified time" />
<exec executable="python" >
<arg value="c:\scm\scripts\git-restore-mtime.py" />
<arg value="-v" />
</exec>
</target>
This is causing the following error in my Jenkins output:
[exec] Executing command: python c:\scm\scripts\git-restore-mtime.py -v 2>&1
[exec] 'python' is not recognized as an internal or external command,
[exec] operable program or batch file.
I am able to execute the same command from the Jenkins workspace directory and it works perfectly. The Python directory is added to my environment variables and the script has the right permissions.
I have also tried adding the script to my repository and running it from within the build environment but have the same error.
Any thoughts please?
I suggest the following:
Try creating Windows batch build step in your Jenkins and put the command in it. See if it runs
It's obvious your Jenkins environment is not the same is your console. See what's missing in Jenkins
Add the python home and path in the Jenkins configuration
I hope this helps

Using ANT to precompile handlebars.js templates

I'd like to use ant to compile several of my handlebars templates.
The command I'd like to run is:
handlebars templates -f templates/hbs.js
My ant file has the following:
<exec executable="handlebars">
<arg value="${src.templates.dir}"/>
<arg value="-f"/>
<arg value="${src.templates.dir}/hbs2.js"/>
</exec>
I get a BUILD FAILED error where it reads
Execute failed: java.io.IOException: Cannont run program "handlebars": CreateProcess error-2, the system cannot find the file specified.
I've also tried
<exec executable="handlebars.exe">
with the same result. Handlebars works because I can run the command from a terminal window from the same location as my ant build files.
I've got handlebars installed through node.js. I know I can probably get it to work by using node to build my project, but I'm hoping I don't have to convert my other ant tasks.
See the windows section of the "exec" task:
http://ant.apache.org/manual/Tasks/exec.html

sshexec command setup

I need to run ant script from a local machine which will invoke ant executions on remote machine.
So on the local ant file I have :
<target name="test">
<sshexec host="${host}" username="${user}"
password="${pwd}" trust="yes"
commandResource="(cd F:\execution; ant -f build.xml run)"/>
</target>
On remote machine I have build.xml` which contains
<target name="run">
<mkdir dir ="F:\Testfolder"/>
</target>
When I execute the loca ant script I get the following error :
java.io.FileNotFoundException: (cd F:\execution; ant -f build.xml run)
(The filename, directory name, or volume label syntax is incorrect)
Why am I getting this error?
We deploy a build file named "remote-build.xml" to the path /root/project/remote-build.xml on the remote machine, and then we use
<sshexec host="${host}"
username="${user}"
password="${pwd}"
trust="yes"
command="ant -f /root/project/remote-build.xml the-targets-to-execute" />
to execute the target.
You could create a file in remote machine ( F:\execution\runmanycommands.sh and execute that file . From the site
Run a set of commands from a command resource (file) on a remote
machine using key authentication with no passphrase
The commandResource expects single resource file execute. '(cd F:\execution; ant -f build.xml run)' is not a resource.
(I am not sure your over all goal. Looking at the number questions, I guess you need Continous Integration solution- should check jenkins with many of its plugins )
(BTW, you can all ant directly with ant -f , avoiding the need for cd)

Cannot run program "p4": CreateProcess error=2, The system cannot find the file specified

I'm developing automate deployment script for Coldfusion project.
Tool: cruisecontrol.net, ant script
Source control: perforce
Executing the following ant script from cruisecontrol.net i'm getting this error:
"Cannot run program "p4": CreateProcess error=2, The system cannot find the file specified"
But its working fine from command line:
ant -f deployment.xml
deployment.xml file content:
<!-- Get Latest revision from perforce -->
<echo message="Perforce code base Get Latest revision Started"/>
<p4sync port="${p4.server}"
client="${p4.workspace}"
globalopts="${p4.password}"
user="${p4.username}"
view="${p4.branch}"/>
<echo message="Perforce code base Get Latest revision completed"/>
ccnet.config:
<project name="TestMGDeployment">
<triggers>
<intervalTrigger seconds="300" />
</triggers>
<tasks>
<exec executable="C:\Apache\apache-ant-1.8.1\bin\ant.bat">
<baseDirectory>C:\cruisecontrol\Projects</baseDirectory>
<buildArgs>-f deployment.xml</buildArgs>
</exec>
</tasks>
</project>
Thanks,
Nagarajan
Your CruiseControl.net is probably running under different user account, make sure you have p4 in system PATH or specify the full path to the executable in your p4sync task.
Try running in command line instead of as a service to negate user environment definitions issue.
Check if you have setup the P4PORT environment variable. That should be set to: [your perforce server]:[perforce port].
For e.g., P4PORT=perforce.xyz.com:1666

Resources