Compile Java Codes in Linux Shell instead of Ant Script
2014-03-18 08:10
441 查看
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:
The corresponding shell script is:
Take care there's no folder named "core" in $src_dir, or it will be excluded by the "-C" option of rsync command.
<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
相关文章推荐
- 4 Ways of Executing a Shell Script in UNIX / Linux
- Shell script for logging cpu and memory usage of a Linux process
- the diary of script in linux
- how to run 2 or more commands in a shell script [for Linux & Windows]
- [linux]How to set PATH in shell script, and keep it avaiable even after it exits
- Startup a JAVA standalone program in UNIX use shell script
- How to run a Java Program as a daemon (service) on Linux (openSUSE) using a shell script
- HowTo: Run the .sh File Shell Script In Linux / UNIX
- the diary of homework of script check dir in linux
- How to execute shell script in Java?
- how to compile source code of "Data Structures & Algorithm Analysis in Java" writen by Mark Allen Weiss
- How to execute shell script in Java?
- dubbo-Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError
- Eclipse 报 “Exception in thread "main" java.lang.OutOfMemoryError: Java heap space ”错误
- java本地调用cmd,shell命令,远程调用Linux执行命令方法总结
- linux下shell对java程序的管理
- Spring Boot 启动报错 Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 37
- java.lang.NoSuchFieldError: No static field xxx of type I in class Lcom/XX/R$id; or its superclasses
- Caused by java.lang.UnsatisfiedLinkError: dlopen failed: "/data/app/com.google.android.webview-1/lib/arm/libwebviewchromium.so" is 32-bit instead of 64-bit
- spring quartz:Instantiation of bean failed; nested exception is java.lang.IncompatibleClassChangeErr 分类: Java spring 2015-01-07 19:00 438人阅读 评论(0) 收藏