Posts Tagged ‘ant’

Visual C in Ant

February 1, 2009 2 comments

Sounds a little bit crazy, but you would probably need this more often than you think. If you develop some JNI applications this probability increases quite a bit.

Nowadays there is a big hype with the Continuous Integration and behind that usually sits a pretty solid build system. So most likely you will have to find a way to create your native libraries automatically and not from the development environment. Not to mention that you would probably want to generate your binaries on multiple environments to ensure compatibility for your JNI application. After all, it is write once, run anywhere.
And isn’t just nice to do everything from one place? One IDE, one build system, etc.

OK, I convinced you. But how do you do it? Ant is simply great when it comes about building Java applications, but what about compiling C code into native libraries? Now is the part where cpptasks jumps to give you a hand. So let’s see how your ANT file will look like for compiling your native library:

<property environment="env"/>
<condition property="iswin">
    <os family="windows"/>
<taskdef resource="cpptasks.tasks"/>
<mkdir dir="${native.out}"/>
<cc outtype="shared" subsystem="console" outfile="${native.out}/${}" objdir="${native.out}" name="msvc">
    <fileset dir="${native.src}" includes="*.c"/>
    <libset dir="${native.src}" libs="OtherLib"/>
    <sysincludepath location="${jdk.path}/include/"/>
    <sysincludepath location="${jdk.path}/include/win32" if="iswin"/>


  • iswin is an Ant property set only if we are running on windows
  • native.out is the directory where the libraries and object files are generated
  • native.src is the directory containing the C source files
  • is the name of the generated library
  • jdk.path is the JDK installation directory


As you can see the C compiler used is Visual C (name attribute of the cc task). And most probably you will need the WinAPI include file, so add the below inside your cc task:

<compiler name="msvc">
    <sysincludepath location="${env.WindowsSdkDir}/include"/>

And now comes the messy part. The environment variable WindowsSdkDir must be defined. Without this the compiler won’t even start and you will get an error like this:

Could not launch cl: Cannot run program "cl" (in directory "native\out"): CreateProcess error=2, The system cannot find the file specified


But fortunately there is a workaround and it is pretty simple, even tough not beautiful. In the Windows SDK distribution there is a .bat file that defines all the necessary environment variables: vsvars32.bat. And its location is defined in an environment variable: VS90COMNTOOLS for Visual Studio 2008 and VS80COMNTOOLS for Visual Studio 8.

So all you have to do is run (in the same CMD session) “%VS90COMNTOOLS%\vsvars32.bat” before running your Ant build (or launching your IDE). And no, there is no (clean) way to set an environment variable from Ant or Java.

Categories: Software Tags: ,

Automatically processed images

May 27, 2008 Leave a comment

As I’m not a graphic designer, I’m not interested very much in image processing. In this area my experience is pretty thin and reduced of simply retouching my own photos or small adapting of some icons and graphics for my applications.
Although, one part of the image processing raises more interest to me: automatic processing. Why would you need such thing? Maybe not clear in the beginning, but this could have huge impact on your projects, like automatic generation of image galleries, adding logos or watermark to pictures and you already got the idea and how useful could be.
Before going any further I have to make one small clarification. As I have mostly a Java background, I use Ant, which is the equivalent of make from the Linux world. And of course, I’ll be interested of integrating the automatic image processing with Ant.
Probably now, the Ant advanced users will jump right on and say: “That’s easy! Use the Image Ant task“. Not quite. This could be one solution, but it is far from easy and complete. Let’s start with the installation. First of all, you should download and install JAI libraries. Second you have to declare(define) the image task and then use it. And that’s the good part. The worst part is that you don’t have support for most of the common operations with images.
So I needed another solution. Something easy to integrate in Ant, cross-platform (at least for the sake of Java principle) and supporting at least the common operations with images. And I came across ImageMagick, a very well-known image processing program with command line interface.
As you all Ant users know, this is very easy to integrate using the exec task and it features huge amounts of image processing operations, enabling you to do almost anything you can think of.
Now for the sake of example, I’ll paste here some parts of build.xml and what you can do with it.

  • Change one color with another in an image (great for automatically creating skins)

    <exec executable="${imagemagick.dir}/convert">
        <arg value="skinnable.gif"/>
        <arg value="-fill"/>
        <arg value="blue"/>
        <arg value="-opaque"/>
        <arg value="#ffffff"/>
        <arg value="blue_skin.gif"/>

  • Generate images from text (great for automatically creating buttons)

    <exec executable="${imagemagick.dir}/convert">
        <!-- background color -->
        <arg value="-background"/>
        <arg value="#ffffff"/>
        <!-- color of the text margin -->
        <arg value="-stroke"/>
        <arg value="#202020"/>
        <arg value="-strokewidth"/>
        <arg value="1"/>
        <!-- color of the text -->
        <arg value="-fill"/>
        <arg value="#909090"/>
        <!-- path to font file -->
        <arg value="-font"/>
        <arg value="/path/to/font.ttf"/>
        <!-- font size -->
        <arg value="-pointsize"/>
        <arg value="14"/>
        <!-- keep the text in center -->
        <arg value="-gravity"/>
        <arg value="center"/>
        <!-- button label -->
        <arg value="label:${label}"/>
        <!-- button label -->
        <arg value="button.gif"/>

  • Generate thumbnails (great for automatically creating image galleries)

    <exec executable="${imagemagick.dir}/convert">
        <arg value="image.jpg"/>
        <arg value="-thumbnail"/>
        <!-- thumbnail size -->
        <arg value="150x150^"/>
        <arg value="-gravity"/>
        <arg value="center"/>
        <arg value="-crop"/>
        <arg value="150x150+0+0"/>
        <arg value="+repage"/>
        <arg value="thumb.jpg"/>

Categories: Software Tags: ,