Convert Makefile content to Apache Ant - makefile

What would be the syntax in 'build.xml' of an Ant build system
that corresponds to the following content of a Makefile (GNU Make):
target: dependency0 dependency1
shell command 1 # Not java, cc or the like
shell command 2
shell command 3

Example
$ ant
dependency0:
[echo] Hello world one
dependency1:
[echo] Hello world two
build:
[exec] command 1
[exec] command 2
[exec] command 3
build.xml
<project name="demo" default="build">
<target name="dependency0">
<echo>Hello world one</echo>
</target>
<target name="dependency1">
<echo>Hello world two</echo>
</target>
<target name="build" depends="dependency0,dependency1">
<exec executable="echo">
<arg line="command 1"/>
</exec>
<exec executable="echo">
<arg line="command 2"/>
</exec>
<exec executable="echo">
<arg line="command 3"/>
</exec>
</target>
</project>

Related

How to run Maven command in ant script?

I am trying to create an ant task to execute a maven command, but I am getting error while running the ant task
<target name="Junit">
<exec dir="./MServer/BuildServer/Workspc/CustMgmt" executable="cmd">
<arg value="/C"/>
<arg value="E:\EOM Setup\maven-3.3.9\bin\mvn.bat"/>
<arg value="test" />
</exec>
</target>
While am running this, am getting an error:
'E:\EOM' is not recognized as internal or external command, operable program or batch file
(I am running on Windows 7)
You can get around this issue by using Ant's property task with the location attribute (as opposed to the more common value attribute). This will store the value as a properly formatted path. In addition, you can use this to reference mvn.bat instead of typing out the entire path every time.
<target name="Junit">
<property name="mvn.executable" location="E:\EOM Setup\maven-3.3.9\bin\mvn.bat" />
<exec dir="./MServer/BuildServer/Workspc/CustMgmt" executable="cmd">
<arg value="/C"/>
<arg value="${mvn.executable}"/>
<arg value="test" />
</exec>
</target>

Ant build executes cordova

I created an ant build for my cordova project as following:
<project default="build">
<target name="init-android">
<exec executable="cordova">
<arg value="platform"/>
<arg value="add"/>
<arg value="android"/>
</exec>
<exec executable="cordova">
<arg value="build"/>
</exec>
</target>
</project>
But I got this error:
C:\path_to_project\build.xml:3: Execute failed: java.io.IOException:
Cannot run program "cordova": CreateProcess error=2, The system cannot
find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
at java.lang.Runtime.exec(Runtime.java:615)
at org.apache.tools.ant.taskdefs.launcher.Java13CommandLauncher.exec(Jav
a13CommandLauncher.java:41)
I can run cordova command with no problem from the command prompt, I have:
JAVA_HOME = C:/Program Files/Java/jdk1.7.0_10/
ANT_HOME = C:\Program Files\Java\apache-ant-1.9.2
NODEJS_HOME = C:\Program Files\nodejs
and they are all in my path. I don't understand why it doesn't work. Please help. Thanks
Generally when working with a Java application to launch programs in Windows, I often have to execute cmd.exe and pass it the full path to the program I actually want to run. This allows system environment variables and such to be set up the way you expect. Try this:
<project default="build">
<target name="init-android">
<exec executable="cmd.exe">
<arg value="/C"/>
<arg value="cordova"/>
<arg value="platform"/>
<arg value="add"/>
<arg value="android"/>
</exec>
<exec executable="cmd.exe">
<arg value="/C"/>
<arg value="cordova"/>
<arg value="build"/>
</exec>
</target>
</project>
If that still doesn't work, give the full path for cordova. An environment variable should work if you have one defined.

install highcharts exporting server

I'm trying to install highcharts exporting server(by java). I want to write below maven command line in build.xml file of my project. Not run from cmd.
$ cd highcharts-export/
$ mvn install
How should I do that in build.xml file?
I wrote following in build.xml file and run by ANT. But nothing happens. And idea?
<target name="buildHighchartsExporting" description="Builds highcharts exporting">
<exec dir="${basedir}/highcharts-export" executable="cmd">
<arg value="mvn" />
<arg line="-f highcharts-export/pom.xml install" />
</exec>
<target name="buildWarFile" description="Builds highcharts exporting War">
<exec dir="${basedir}/highcharts-export/highcharts-export-web" executable="cmd">
<arg value="mvn" />
<arg line="clean package" />
</exec>
If you are just trying to execute a project where the pom.xml is at another folder, you can use the -f parameter:
6.1.6. Using a Custom POM or Custom Settings File
If you don’t like the pom.xml file name, the location of your user-specific Maven
settings, or the default location of your global settings file, you
can customize any of these things with the following options:
-f, --file <file>
Forces the use of an alternate POM file
This way, from where you'd execute:
$ cd highcharts-export/
$ mvn install
You can just use:
$ mvn -f highcharts-export/pom.xml install
Running mvn from an ANT build.xml file
You can use the Ant Exec Task:
<target name="buildProject" description="Builds the project">
<exec dir="${source.dir}\${projectName}" executable="cmd">
<arg value="mvn" />
<arg line="-f highcharts-export/pom.xml install" />
</exec>
</target>
Or, more specifically (to your example):
<target name="buildWarFile" description="Builds highcharts exporting War">
<exec dir="${basedir}/highcharts-export" executable="cmd">
<arg value="mvn" />
<arg line="clean package" />
</exec>

How can I run perl and ruby scripts as tasks in ant?

I would like to be able to run ruby and perl scripts from build.xml in ant.
Languages like Ruby have Java implementations.
<project name="RunRubyExample">
<property environment="env" />
<script language="ruby" manager="bsf">
<classpath>
<fileset dir="${env.JRUBY_HOME}/lib" includes="*.jar" />
</classpath>
print 'hello world'
</script>
</project>
See the list of languages supporting the JSR233 standard.
Unfortunately no Java version of Perl is available. The only way to run Perl scripts is to invoke the interpreter directly:
<project name="RunPerlExample">
<exec executable="perl" failonerror="true">
<arg value="-e" />
<arg value="print 'hello world'" />
</exec>
</project>
You can always use the ant`s exec task to run arbitrary programs, such as ruby and perl. For example and from the docs:
<target name="help">
<exec executable="cmd">
<arg value="/c"/>
<arg value="ant.bat"/>
<arg value="-p"/>
</exec>
</target>

Ant exec - cannot run program 'start' CreateProcess error=2

I can't run the windows 'start' using ant exec. Ant version 1.7.1.
Here is sample build.xml to recreate the problem
<project name="test" basedir="." default="test-target">
<target name="test-target">
<exec executable="start">
<arg line="cmd /c notepad" />
</exec>
</target>
</project>
getting the following error when I execute this build file:
Execute failed: java.io.IOException: Cannot run program "start": Cre
ateProcess error=2, The system cannot find the file specified
My env is Windows XP, Ant 1.7.1
I am trying to run this from DOS prompt.
I rule out any PATH related issues, as I could run 'start cmd /c notepad' from DOS promt manually.
Any suggestions on how to fix this?
cheers
a s
start is not an executable but is an internal command of the cmd.exe shell, so to start something you'd have to:
<exec executable="cmd.exe">
<arg line="/c start notepad" />
</exec>
EDIT:
For spawning multiple windows, this should work:
<target name="spawnwindows">
<exec executable="cmd.exe" spawn="yes">
<arg line="/c start cmd.exe /k echo test1" />
</exec>
<exec executable="cmd.exe" spawn="yes">
<arg line="/c start cmd.exe /k echo test2" />
</exec>
</target>
but you mentioned that spawn="true" is not applicable for your environment, why is that?
my solution
<project name="test" basedir="." default="test-target">
<target name="start-init">
<exec executable="where" outputproperty="START">
<arg line="start" />
</exec>
</target>
<target name="test-target">
<exec executable="${START}">
<arg line="cmd /c notepad" />
</exec>
</target>
</project>
How about <exec executable="start.exe"> ? Or start.bat ?
Also, where is basedir="." pointing to? If you place a <echo message="basedir = ${basedir}"/> just before your <exec> tag, does it print the correct folder (the one with the "start" program in it)?
Additionally, you could add <echoproperties /> before <exec> to see all visible properties.

Resources