How to compile and build C++ projects with Ant? - java

I have several Java components all are built by Ant, however I would like to add one more component (written in C++) and compile/build it in Ant as well.
Is there a tutorial which I can follow or a short way to achieve this?
Thanks in advance.

Call make as ant exec task
You can pass env variables to the make
<exec executable="make" dir="${cpp.project.dir}">
<env key="KEY" value="VALUE"/>
</exec>

Ant is ancient, but you still can do this with a lot of pain.
I would recommend to use modern build system Gradle which has C++ support as well. You can find tutorials on their web-site.

It seems like you are not the first trying to accomplish this kind of task:
Building C++ projects with ANT
Axis C++ ANT Build Guide
cpptasks for Apache Ant

Use this:
<?xml version="1.0"?>
<project name="hello" default="compile">
<taskdef resource="cpptasks.tasks"/>
<target name="compile">
<cc outfile="main" objdir="obj" outtype="executable">
<fileset dir="./" includes="*.cpp"/>
<compiler id="Linuxgcc" name="g++">
<compilerarg value="-fPIC"/>
</compiler>
<linker id="LinuxLinker" name="g++" libtool="true">
<linkerarg value="-g" if="debug"/>
<linkerarg value="-fPIC"/>
<libset libs="stdc++"/>
</linker>
</cc>
</target>
</project>
Make sure you place cpptasks.jar(http://www.java2s.com/Code/Jar/c/Downloadcpptasksjar.htm) in your ant's lib folder

Related

Dependent ant task is executing before finishing the dependency task execution

I have written an ant script to build a plugin and publish feature as follows:
<project default="temp" name="build">
<target name="feature_export" depends="build-exility-Client">
<echo>inside feature_export</echo>
<pde.exportFeatures destination="C:\Users\akhilesh.kj\Desktop\Plugin" exportSource="false" exportType="directory" features="com.exility.exilant.feature" useJARFormat="true" />
</target>
<target name="temp" depends="feature_export">
<p2.publish.featuresAndBundles
metadataRepository="file:/d:/a"
artifactRepository="file:/d:/a"
publishArtifacts="true"
compress="true"
source="C:\Users\akhilesh.kj\Desktop\Plugin"/>
</target>
</project>
Here, first 'feature_export' is getting executed.But before completing the execution(exporting the plugin and feature) Task 'temp' get started.Since task 'temp' is using plugin and feature jar that is output of 'feature_export' task,So it is not giving result as expected.
All I want is that firstly, complete 'feature_export' task with proper output then start 'temp' task.
I tried with sleep and waitfor command but that does not work for me.
Please help!
This is a known limitation
Eclipse Bugtracker
It seems that they won't fix it.

Compiling an eclipse GWT project from the command line, without eclipse: compile error

We got a GWT project in Eclipse, that otherwise works.
Now I want to have a script that runs on the server, which pulls the latest version from source control and compiles it on the server and deploys it.
This will save us a lot of manual work and allow us to deploy new version when on a connection with limited bandwidth (since we won't have to upload the application to the server).
After pulling the latest version of the source code, the script tries to compile the code using the following command:
java -cp "/path/eclipse/plugins/com.google.gwt.eclipse.sdkbundle_2.5.0.v201211121240-rel-r42/gwt-2.5.0/*:/path/company/projects/pull-compile-deploy/X/X/src:/path/company/projects/pull-compile-deploy/X/X/war/WEB-INF/lib/*" com.google.gwt.dev.Compiler nl.company.projects.X
Compiling module nl.company.projects.X
Finding entry point classes
[ERROR] Unable to find type 'nl.company.projects.X.client.XMain'
[ERROR] Hint: Previous compiler errors may have made this type unavailable
[ERROR] Hint: Check the inheritance chain from your module; it may not be inheriting a required module or a module may not be adding its source path entries properly
All source code is in /path/company/projects/pull-compile-deploy/X/X/src and all used .jars (except for the GWT stuff) are in /path/company/projects/pull-compile-deploy/X/X/war/WEB-INF/lib/. Obviously something goes wrong.
Questions: The file /path/company/projects/pull-compile-deploy/X/X/src/nl/company/projects/X/client/XMain.java does exist and should imho be in the classpath?!
Anyone Any idea what might go wrong here?
Is it maybe possible to see in some log exactly the commands that eclipse executes for compilation? I looked at the build.xml that eclipse can export, but it seems that does not contain a target to compile for production.
something else: apperantly GWT expects the X.gwt.xml to be at /path/company/projects/pull-compile-deploy/X/X/src/nl/company/project/X.gwt.xml, whereas eclipse put it in /path/company/projects/pull-compile-deploy/X/X/src/nl/company/project/X/X.gwt.xml (i.e. nested one directory deeper), I fixed this by creating a symbolic link.
Further Edit:
Since one answer focused on how I invoked the compilation tools, I have rewritten that in Ant, see below.
The problem remains of course.
<!-- Compile the source using javac. -->
<target name="compile" depends="init">
<javac srcdir="src/" destdir="bin/">
<classpath refid="project.classpath"/>
</javac>
</target>
<!-- Use the GWT-compiler. -->
<target name="gwt-compile" depends="compile">
<java failonerror="true" fork="true" classname="com.google.gwt.dev.Compiler">
<classpath>
<path refid="project.classpath"/>
<pathelement location="src/"/>
<pathelement location="bin/"/>
</classpath>
<jvmarg value="-Xmx512M"/>
<arg value="${module.name}"/>
</java>
</target>
Anything wrong with the above Ant-script?
module.name = nl.company.projects.X and the path with refid="project.classpath" contains all used libraries aswell as the GWT libraries (gwt-user.jar, gwt-dev.jar and validation-api-1.0.0.GA(-source).jar).
The XMain class inherits nothing (other than from Object) and only implements EntryPoint (which is included in the gwt-user.jar). So I do not think the problem is related to the second hint that the compiler gives.
Any ideas?
GWT requires you to javac your classes, it needs both the *.java and the *.class files.
This has not always been the case, and should change back in the future (see https://code.google.com/p/google-web-toolkit/issues/detail?id=7602 for instance), but for now that's the state of affair: you need to javac before you can com.google.gwt.dev.Compiler.
javac -cp "/path/eclipse/plugins/com.google.gwt.eclipse.sdkbundle_2.5.0.v201211121240-rel-r42/gwt-2.5.0/*:/path/company/projects/pull-compile-deploy/X/X/war/WEB-INF/lib/*" -sourcepath /path/company/projects/pull-compile-deploy/X/X/src /path/company/projects/pull-compile-deploy/X/X/src/nl/company/projects/X.java -d /path/company/projects/pull-compile-deploy/X/X/bin
java -cp "/path/eclipse/plugins/com.google.gwt.eclipse.sdkbundle_2.5.0.v201211121240-rel-r42/gwt-2.5.0/*:/path/company/projects/pull-compile-deploy/X/X/src:/path/company/projects/pull-compile-deploy/X/X/bin:/path/company/projects/pull-compile-deploy/X/X/war/WEB-INF/lib/*" com.google.gwt.dev.Compiler nl.company.projects.X
(please double-check the above commands before use)
EDIT: (in response to your "question" re. the X.gwt.xml): GWT expects the X.gwt.xml at nl/company/projects/X.gwt.xml because that's what you told it to use: module.name = nl.company.projects.x. If the file is at nl/company/projects/X/X.gt.xml then use nl.company.projects.X.X as the module name. Using a symbolic link here is likely to be the problem: the source path for the module (search for <source at https://developers.google.com/web-toolkit/doc/latest/DevGuideOrganizingProjects#DevGuideModuleXml) will then be nl/company/projects/client and thus won't include nl/company/projects/X/client where your XMain class lives; it's this unavailable to the GWT compiler.
That said, I totally agree with SSR: use a decent build tool: Ant, Maven, Gradle, Make, whatever, it'll make your life so much easier. A build tool that manages dependencies (Ant+Ivy, Maven, Gradle) is even better IMO.
Why would you put yourself through such non-standard build exercise like this.
If it is non-academic project then USE maven. If you find maven difficult then use ant.
Examples for both type are provided by GWT team - http://code.google.com/p/google-web-toolkit/source/browse/#svn%2Ftrunk%2Fsamples.
Note - maven has plugins to do most of the stuff you are trying in standardized way.

Change working directory in ant junit task

I have a ant file that runs JUnits tests. These tests rely on a relative path to certain configuration files. I've tried to set the working directory for the batch test, but fail.
I want the working directory to be ${plugins.dir}/${name}
The JUnit part of the ant script:
<junit haltonfailure="no" printsummary="on" fork="true" showoutput="true" dir="${plugins.dir}/${name}">
<jvmarg value="-Duser.dir=${plugins.dir}/${name}"/>
<classpath>
<path refid="project.classpath"/>
<pathelement location="${plugins.dir}/${dependency}/#dot/"/>
<pathelement location="${plugins.dir}/${name}/" />
</classpath>
<formatter type="xml" />
<sysproperty key="basedir" value="${plugins.dir}/${name}"/>
<sysproperty key="dir" value="${plugins.dir}/${name}"/>
<batchtest todir="${junit.output}">
<fileset dir="${dir}">
<include name="**\*AllTests.class" />
</fileset>
</batchtest>
</junit>
I've googled and searched but the workarounds I've found have been to set the "dir", "sysproperty" or "jvmarg". As you can see I've tried them all :)
Is there a way to print the current dir in the tag? It doesnt support . That would allow me to verify if the dir is actually changed and to what.
One wildcard in this equation is that this is being run in Hudson that starts upp an eclipse process that starts antrunner. This is so we can run both junit and eclipse plugin junit tests. Shouldn't have anything to do with the problem I think.
I think you are right with setting the basedir property (see projects attributes). However, since it is a property of ANT (and not of the JVM) it is READ ONLY!
Does it effect other target if you set the basedir when calling your ant task? See Command Line reference.
ant -Dbasedir=somedir
Alternatively, span a new ant process to call your junit target. See the AntCall task or Ant task. Following examples assumes that the junit target contains your junit task. I used this task for other properties (never needed the basedir property so far.
<antcall target="junit">
<param name="basedir" value="${plugins.dir}/${name}"/>
</antcall>
<ant dir="${plugins.dir}/${name}" target="junit" />
I had the same scenario and in my case I saw that dir="...." is ignored if run in same jvm so I simply added fork='true' and it worked.
Quote from Apache's documentation site "dir - The directory in which to invoke the VM. Ignored if fork is disabled.". More here.
I'm using NetBeans. When I add to Ant properties (from Tools, Options, Java, Ant) work.dir=C:/MyWorkingDir/ it executes ant with the following command and changes the working dir to C:\MyWorkingDir:
ant -f D:\\workspace\\lib\\project -Dfork=true -Djavac.includes=com/myapp/MyTest.java -Dtest.includes=com/myapp/MyTest.java "-Dwork.dir=C:/MyWorkingDir/" test-single

Apache ANT Standalone GUI for easy execution of targets

Does anyone know of a GUI written for Apache ANT. We're looking into developing a GUI to execute some of our developer tools for some of the designers and artists on our team.
I found a couple on the Ant External website but most of them are used for creating ANT files not simply listing the public targets available.
http://ant.apache.org/external.html
The ANT forms project has some tasks that enable you to generate simple forms that can be used to invoke ANT targets.
Here's an example with three buttons:
<project default="menu">
<property environment="env"/>
<path id="runtime.cp">
<pathelement location="${env.ANTFORM_HOME}/lib/antform.jar"/>
</path>
<target name="menu">
<taskdef name="antmenu" classname="com.sardak.antform.AntMenu" classpathref="runtime.cp"/>
<antmenu image="${env.ANTFORM_HOME}/doc/images/logo-small.jpg" title="My simple form" stylesheet="${env.ANTFORM_HOME}/style.test">
<label>A short label with a few explanatory words concerning the menu at hand.</label>
<button label="Echo 1 target" target="echo1"/>
<button label="Echo 2 target" target="echo2"/>
<button label="Echo 3 target" target="echo3"/>
</antmenu>
</target>
<target name="echo1">
<echo>DO SOMETHING</echo>
</target>
<target name="echo2">
<echo>DO SOMETHING</echo>
</target>
<target name="echo3">
<echo>DO SOMETHING</echo>
</target>
</project>
Usually most of the GUI for Ant or Maven are part of the IDE. I use IntelliJ that has an excellent support for Ant and Maven. Lists all my goals and I easily view any of them.
Antelope is a pretty excellent standalone GUI.
http://antelope.tigris.org/
I always quite liked this project, its implemented with xsl and requires no other dependencies.
http://antprettybuild.sourceforge.net/

Run some or all sections of an ant file

Is there a way to specify an ANT file to run only one/some/all of its sections?
Split your build into appropriate targets so that you can call individual targets separately, and then you can specify the target you want to run from the command line.
Personally I like having "real" targets without any dependencies (which I can run independently) and then "fake" targets which are just dependent on the real ones, for convenience (e.g. "clean-build"). The alternative of having test depend on compile etc always ends up getting in the way for me :(
You can group targets together using dependencies:
<target name="A">
<target name="B">
<target name="C" depends="A,B">
runs
A, B, then C.
You can also chain these to arbitrary depth. For example you could create an empty target "D" that depends on A, B which will only run A and B.
<project....
<target name="all">
...
</target>
<target name="some">
...
</target>
</project>
run
ant all
or
ant some
Define appropriate targets in your build file and then run
ant 'target name'
to run that particular one. You will have to configure target dependencies such that the ones you want to run separately can do so correctly.
It's a good practise to define these top-level targets with a description.
<target name="clean" description="Cleans up built artifacts">
Then you can run
ant -projecthelp
and this will display the targets with descriptions, thus telling you what targets are available. This will make life a lot easier further down the road, when you've forgotten what targets you've defined.

Categories

Resources