tags:

views:

249

answers:

3

I'm trying to develop a bash build script for a Java project that will be run on Ubuntu and Fedora. Ubuntu uses the gcj compiler while Fedora uses IcedTea.

Both report their errors and warning in slightly different ways, and I want to ignore the warnings (I know, not generally a good idea, but some of the warnings are simply idiotic).

For gcj, I want to run:

javac *.java 2>&1 | grep -A 4 "error:"

but for IcedTea, I want to run:

javac *.java 2>&1 | grep -A 4 "error:\|errors\|.java:"

I'm still new to bash, so how would I write an if statement that would run one versus the other based upon the javac version?

+1  A: 
    #!/bin/sh

    JAVAC_VERSION="`java -version 2>&1 /dev/null | awk '/IcedTea/ {print $4}' | sed -e 's/[\(0-9]//g'`"
    ICEDTEA="IcedTea"

    if [ ${JAVAC_VERSION} = ${ICEDTEA} ]; then
        javac *.java 2>&1 | grep -A 4 "error:\|errors\|.java:"
    else
        javac *.java 2>&1 | grep -A 4 "error:"
    fi

    exit 0

That should do it - if i understood your question correctly. How you get the version - im not quite sure of, but if my javac -version is incorrect just change it accordingly to your needs.

Anders
This is close, but I'm getting the error " [: javac: binary operator expected" on the second if statement.
Chris S
Also, if I put a "echo $JAVAC_VERSION" right after the first line, it shows nothing.
Chris S
Acknowledged, let me try it out.
Anders
But Lachlan version was more clever. accept that.
Anders
+3  A: 

Assuming your java and javac binaries match, and that icedtea is the special case.

#!/bin/bash

ERROR="error:"

java -version 2>&1 | grep -i icedtea > /dev/null
if [ $? -eq 0 ]; then
   ERROR="error:\|errors\|.java:"
fi

javac *.java 2>&1 | grep -A 4 $ERROR

On my system, icedtea and sun have the same output for "javac -version", but not for "java -version".

Lachlan Roche
Anders
+1  A: 

Writing Java build scripts in bash (or any other shell language) has a number of problems:

  • scripts tend to be non-portable due to shell differences, different command locations, incompatible command options and so on ... even if you try to make the portable.

  • scripts cannot cope with dependencies (or at least not easily)

  • scripts cannot cope with recompiling only stuff that has changed

Instead, I suggest that you write a "build.xml" file and use the Ant build tool. Ant has the advantage of running on any build platform that runs Java, and of taking care of the vast majority of platform differences. It is sort of like a better "Make" designed specifically for building Java.

Stephen C
-1 XML is somehow better than Makefile? ant is a solution in search of a problem. it exists simply because its author could not keep tabs where they belong and spaces where they're allowed in Makefiles.
just somebody
It is not the use XML that makes Ant better than Make. (That is just a trivial syntactic difference IMO.) Rather it is Ant's execution model that makes all of the difference. The problem with Make is that it generates shell commands to be executed in a particular order, and those commands are typically non-portable. By contrast, with Ant you declare the tasks to be performed and let the Ant application (and plugin classes) figure out how to perform the tasks.
Stephen C