apache-ant等

学java,很好,
JKD,环境变量那些先要弄好
先来写个类,再运行.
没有IDE可用哦.

弄个干净的文件夹,我喜欢叫aaaaa,五A级干净文件.
source code肯定要写在src目录下,
在里面弄个类,弄好了编译,再运行.
1
        类源文件:
        类肯定要有包,这里都是超级规范的动作!
        那个类必须要写在一串包里面:[就是一串很深的目录里]
================================================================================
        package com.home.hubin;
       
        public class HelloWorld {
            public static void main(String[] args) {
                System.out.println("Hello World, I am the java king!");
            }
        }
================================================================================
        写好了回到aaaaa目录
       
2
        class文件
        编译好的class文件不可以和源文件放一起,一定要放classes目录
        我放在build\classes[md build\classes]
       

3
        有源文件,有JDK,还不运行干什么?
        编译运行一起来
        开cmd!
        aaaaa目录下有src 和 build目录
================================================================================
D:\aaaaa>javac -sourcepath src -d build\classes src\com\home\hubin\HelloWorld.java

D:\aaaaa>java -cp build\classes com.home.hubin.HelloWorld
Hello World, I am the java king!
================================================================================

好了,可以运行了,有的人也用过这方式做软件,就class文件和bat文件
虽然可以运行,但是太丑了,这里说的超级规范,不允许这么运行!

至少也要弄个jar文件出来才像样嘛!
超级规范规定jar文件只可以放在jar目录里
创建jar目录[jar和class都是差不多性质的,所以也放build目录下]

弄个jar文件非常容易,弄个可以运行的jar文件也容易,加个说明就可以了
echo Main-Class: com.home.hubin.HelloWorld>myManifest
然后
D:\aaaaa>jar cfm build\jar\HelloWorld.jar myManifest -C build\classes .
一个jar弄好了
跑起来非常正常
D:\aaaaa>java -jar build\jar\HelloWorld.jar
Hello World, I am the java king!
去看看里面的信息:
重要:发现了,外面的那个什么myManifest无所谓的,到了里面从新弄文件的,
但是信息还是取的这里的哦!!

--------------------------
好了,但是觉得不觉得有点烦,用ant吧,apache的高手写的东西
看看效果怎么样:

aaaaa下,就一个src包,源文件肯定要的,然后就一个build.xml了
其他的什么都不要了:
本来就应该这样,有了源文件,有了JDK,还需要其他干什么呢?
================================================================================
<project>

    <target name="clean">
        <delete dir="build"/>
    </target>

    <target name="compile">
        <mkdir dir="build/classes"/>
        <javac srcdir="src" destdir="build/classes"/>
    </target>

    <target name="jar">
        <mkdir dir="build/jar"/>
        <jar destfile="build/jar/HelloWorld.jar" basedir="build/classes">
            <manifest>
                <attribute name="Main-Class" value="com.home.hubin.HelloWorld"/>
            </manifest>
        </jar>
    </target>

    <target name="run">
        <java jar="build/jar/HelloWorld.jar" fork="true"/>
    </target>

</project>
================================================================================
这里有四个任务,都不要说明,看xml文件就可以知道他们是做什么的了
现在可以:
ant compile
ant jar
ant run

或者一起来:

ant compile jar run
试一下,反正还可以clean啊!
========================================还要再比这个好么到哪里去找==============
D:\aaaaa>ant compile jar run
Buildfile: build.xml

compile:
    [mkdir] Created dir: D:\aaaaa\build\classes
    [javac] Compiling 1 source file to D:\aaaaa\build\classes

jar:
    [mkdir] Created dir: D:\aaaaa\build\jar
      [jar] Building jar: D:\aaaaa\build\jar\HelloWorld.jar

run:
     [java] Hello World, I am the java king!

BUILD SUCCESSFUL
Total time: 1 second
D:\aaaaa>ant clean
Buildfile: build.xml

clean:
   [delete] Deleting directory D:\aaaaa\build

BUILD SUCCESSFUL
Total time: 0 seconds
================================================================================

好归好,写死了,怎么办??
================================================================================
<project name="HelloWorld" basedir="." default="main">

    <property name="src.dir"     value="src"/>

    <property name="build.dir"   value="build"/>
    <property name="classes.dir" value="${build.dir}/classes"/>
    <property name="jar.dir"     value="${build.dir}/jar"/>

    <property name="main-class"  value="com.home.hubin.HelloWorld"/>



    <target name="clean">
        <delete dir="${build.dir}"/>
    </target>

    <target name="compile">
        <mkdir dir="${classes.dir}"/>
        <javac srcdir="${src.dir}" destdir="${classes.dir}"/>
    </target>

    <target name="jar" depends="compile">
        <mkdir dir="${jar.dir}"/>
        <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
            <manifest>
                <attribute name="Main-Class" value="${main-class}"/>
            </manifest>
        </jar>
    </target>

    <target name="run" depends="jar">
        <java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
    </target>

    <target name="clean-build" depends="clean,jar"/>

    <target name="main" depends="clean,run"/>

</project>
================================================================================
这样就写活了,慢慢看可以看懂的
这里设置了default="main",直接ant就可以了,然后一个一个 depends 上去
也可以单独一个一个运行

是不是很完美呢?最好再可以调用外面的JAR包就更好了,调调log4j好了

外面的JAR包要放lib目录下,自己生产的要放在jar目录下
=============================先把log4j的包考近来================================
<project name="HelloWorld" basedir="." default="main">

        <property name="lib.dir"        value="lib"/>
       
        <path id="classpath">
                <fileset dir="${lib.dir}" includes="**/*.jar"/>
        </path>

    <property name="src.dir"     value="src"/>

    <property name="build.dir"   value="build"/>
    <property name="classes.dir" value="${build.dir}/classes"/>
    <property name="jar.dir"     value="${build.dir}/jar"/>

    <property name="main-class"  value="com.home.hubin.HelloWorld"/>



    <target name="clean">
        <delete dir="${build.dir}"/>
    </target>

    <target name="compile">
        <mkdir dir="${classes.dir}"/>
        <javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath"/>
    </target>

    <target name="jar" depends="compile">
        <mkdir dir="${jar.dir}"/>
        <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
            <manifest>
                <attribute name="Main-Class" value="${main-class}"/>
            </manifest>
        </jar>
    </target>

    <target name="run" depends="jar">
        <java fork="true" classname="${main-class}">
            <classpath>
                <path refid="classpath"/>
                <path location="${jar.dir}/${ant.project.name}.jar"/>
            </classpath>
        </java>
    </target>

    <target name="clean-build" depends="clean,jar"/>

    <target name="main" depends="clean,run"/>

</project>


================================================================================
package com.home.hubin;

import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;

public class HelloWorld {
        static Logger logger = Logger.getLogger(HelloWorld.class);

    public static void main(String[] args) {
                BasicConfigurator.configure();
                logger.info("Hello,log4j");
        System.out.println("Hello World, I am the java king!");
        System.out.println("================log4j===============");
    }
}

================================================================================
这里写了BasicConfigurator.configure();是为了不出现以下警告:
D:\aaaaa>ant
Buildfile: build.xml

clean:

compile:
    [mkdir] Created dir: D:\aaaaa\build\classes
    [javac] Compiling 1 source file to D:\aaaaa\build\classes

jar:
    [mkdir] Created dir: D:\aaaaa\build\jar
      [jar] Building jar: D:\aaaaa\build\jar\HelloWorld.jar

run:
     [java] log4j:WARN No appenders could be found for logger (com.home.hubin.HelloWorld).
     [java] log4j:WARN Please initialize the log4j system properly.
     [java] Hello World, I am the java king!
     [java] ================log4j===============

main:

BUILD SUCCESSFUL
Total time: 1 second
D:\aaaaa>
一般是不用写的,因为我们必须要为log4j写配置文件
下面就要说,写了配置文件就把这行删除
重要:如果写了这个,logger.info("Hello,log4j");这个东西会出来的
这也不错,可以懒得写配置文件了
0 [main] INFO com.home.tst.HelloWorld  - Hello,log4j
上行是输出
===========================================
<project name="HelloWorld" basedir="." default="main">

        <property name="lib.dir"        value="lib"/>
       
        <path id="classpath">
                <fileset dir="${lib.dir}" includes="**/*.jar"/>
        </path>

    <property name="src.dir"     value="src"/>

    <property name="build.dir"   value="build"/>
    <property name="classes.dir" value="${build.dir}/classes"/>
    <property name="jar.dir"     value="${build.dir}/jar"/>

    <property name="main-class"  value="com.home.hubin.HelloWorld"/>



    <target name="clean">
        <delete dir="${build.dir}"/>
    </target>

    <target name="compile">
        <mkdir dir="${classes.dir}"/>
        <javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath"/>
        <copy todir="${classes.dir}">
            <fileset dir="${src.dir}" excludes="**/*.java"/>
        </copy>
    </target>

    <target name="jar" depends="compile">
        <mkdir dir="${jar.dir}"/>
        <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
            <!--
                        <manifest>
                <attribute name="Main-Class" value="${main-class}"/>
            </manifest>
                        -->
        </jar>
    </target>

    <target name="run" depends="jar">
       
                <java fork="true" classname="${main-class}" >
               
            <classpath>
                <path refid="classpath"/>
                <path location="${jar.dir}/${ant.project.name}.jar"/>
            </classpath>
        </java>
    </target>

    <target name="clean-build" depends="clean,jar"/>

    <target name="main" depends="clean,run"/>

</project>

===================================这是可以的.里面的注释可有可以无======================

你可能感兴趣的:(java,apache,log4j,xml,ant)