Compile Java Codes in Linux Shell instead of Ant Script

The following is frequently used ant script, compile some java source codes with a libary path, then copy all non-java files to compiled folder:

<property name="src.dir" value="${basedir}/src" />
<property name="src.class.dir" value="${webroot.dir}/web-inf/classes" />
<path id="lib.path">
    <fileset dir="${webroot.dir}/web-inf/lib">
        <include name="**/*.jar" />
    </fileset>
</path>
<target name="compile-src" depends="init" description="compile java sources">
    <javac srcdir="${src.dir}" destdir="${src.class.dir}" encoding="utf-8"
            debug="true" classpathref="lib.path" failonerror="false"/>
    <copy todir="${src.class.dir}">
        <fileset dir="${src.dir}">
            <exclude name="**/*.java" />
        </fileset>
    </copy>
</target>

The corresponding shell script is:

src_dir=/vagrant/codes/GCP6.1/src
web_build=/vagrant/codes/web_build
class_dir=$web_build/web_server/webapps/GCP6.1/WEB-INF/classes
lib_dir=/vagrant/codes/GCP6.1/WebRoot/WEB-INF/lib
find $src_dir -type f -name "*.java" -exec javac -cp "$lib_dir/*:$src_dir" -d $class_dir {} \;
rsync -auC --exclude=‘*.java‘ $src_dir/ $class_dir/

Take care there‘s no folder named "core" in $src_dir, or it will be excluded by the "-C" option of rsync command.

A Test Case

$ tree
.
├── classes
├── lib
│   ├── libadd.jar
│   └── libmul.jar
├── libadd
│   └── com
│       └── boco
│           └── gcp
│               └── add
│                   ├── MyAdd.class
│                   └── MyAdd.java
├── libmul
│   └── com
│       └── boco
│           └── gcp
│               └── mul
│                   ├── MyMul.class
│                   └── MyMul.java
└── main
    └── com
        └── boco
            └── gcp
                ├── Test.java
                └── util
                    └── MyUtil.java

17 directories, 8 files

$ find . -name ‘*.java‘ -exec cat {} \; -exec echo "--------" \;
package com.boco.gcp.util;

import com.boco.gcp.add.MyAdd;
import com.boco.gcp.mul.MyMul;

public class MyUtil {
    public int newadd(int x, int y) {
        MyAdd add = new MyAdd();
        MyMul mul = new MyMul();
        return add.myadd(3,5) + mul.mymul(3,5);
    }
}
--------
package com.boco.gcp;

import com.boco.gcp.mul.MyMul;
import com.boco.gcp.add.MyAdd;
import com.boco.gcp.util.MyUtil;

public class Test {
    public static void main(String[] args) {
        MyMul mul = new MyMul();
        MyAdd add = new MyAdd();
        System.out.println(mul.mymul(3,5));
        System.out.println(add.myadd(3,5));
        MyUtil util = new MyUtil();
        System.out.println(util.newadd(3,5));
    }
}
--------
package com.boco.gcp.mul;

public class MyMul {
    public int mymul(int x, int y) {
        return 10*x*y;
    }

    public static void main(String[] args) {
        MyMul mul = new MyMul();
        System.out.println(mul.mymul(3,5));
    }
}
--------
package com.boco.gcp.add;

public class MyAdd {
    public int myadd(int x, int y){
        return 10*x+y;
    }

    public static void main(String[] args) {
        MyAdd add = new MyAdd();
        System.out.println(add.myadd(3,5));
    }
}
--------
$ find main -name ‘*.java‘ -exec javac -cp ‘lib/*:main‘ {} -d classes \;
$ tree classes/
classes/
└── com
    └── boco
        └── gcp
            ├── Test.class
            └── util
                └── MyUtil.class

Compile Java Codes in Linux Shell instead of Ant Script,布布扣,bubuko.com

Compile Java Codes in Linux Shell instead of Ant Script

上一篇:Java程序员的推荐阅读书籍


下一篇:Java多线程间的数据共享