开发者

Running Different Bash Commands Based on Java Version

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 Ic开发者_如何学运维edTea.

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?


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".


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.


    #!/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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜