• 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
    
  • 相关阅读:
    Java语法基础-final关键字
    Java语法基础-异常处理
    Java语法基础-序列化
    Java的知识储备及面试-几个方面
    一篇分析测试开发人员的职业发展方向的好文章-学习笔记
    web 自动化测试 selenium基础到应用(目录)
    打包Scala jar 包的正确步骤
    IT项目管理
    寄存器 & 汇编指令
    Window环境下编写Shellcode(入门篇)
  • 原文地址:https://www.cnblogs.com/darkmatter/p/3606797.html
Copyright © 2020-2023  润新知