- 浏览: 30489 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
xwl1991:
String str="1989-11-23 12: ...
java.util.Date和java.sql.Date转换
这是非常好的原文,你按照例子操作就知道结果了,就了解了,但一定要操作试试,不懂的在仔细阅读英文文档,应该能读懂
We want to separate the source from the generated files, so our java source files will be in src folder. All generated files should be under build, and there splitted into several subdirectories for the individual steps: classes for our compiled files and jar for our own JAR-file.
We have to create only the src directory. (Because I am working on Windows, here is the win-syntax - translate to your shell):
md src
The following simple Java class just prints a fixed message out to STDOUT, so just write this code into src\oata\HelloWorld.java.
package oata; public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Now just try to compile and run that:
md build\classes
javac -sourcepath src -d build\classes src\oata\HelloWorld.java
java -cp build\classes oata.HelloWorld
which will result in
Hello World
Creating a jar-file is not very difficult. But creating a startable jar-file needs more steps: create a manifest-file containing the start class, creating the target directory and archiving the files.
echo Main-Class: oata.HelloWorld>myManifest
md build\jar
jar cfm build\jar\HelloWorld.jar myManifest -C build\classes .
java -jar build\jar\HelloWorld.jar
Note: Do not have blanks around the >-sign in the echo Main-Class instruction because it would falsify it!
Four steps to a running application
After finishing the java-only step we have to think about our build process. We have to compile our code, otherwise we couldn't start the program. Oh - "start" - yes, we could provide a target for that. We should package our application. Now it's only one class - but if you want to provide a download, no one would download several hundreds files ... (think about a complex Swing GUI - so let us create a jar file. A startable jar file would be nice ... And it's a good practise to have a "clean" target, which deletes all the generated stuff. Many failures could be solved just by a "clean build".
By default Ant uses build.xml as the name for a buildfile, so our .\build.xml would be:
<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="oata.HelloWorld"/> </manifest>
</jar>
</target>
<target name="run"> <java jar="build/jar/HelloWorld.jar" fork="true"/> </target>
</project>
Now you can compile, package and run the application via
ant compile
ant jar
ant run
Or shorter with
ant compile jar run
Enhance the build file
Now we have a working buildfile we could do some enhancements: many time you are referencing the same directories, main-class and jar-name are hard coded, and while invocation you have to remember the right order of build steps.
The first and second point would be addressed with properties, the third with a special property - an attribute of the <project>-tag and the fourth problem can be solved using dependencies.
<project name="HelloWorld" basedir="." default="main"> <property name="src.dir" value="src"/> <property name="build.dir" value="build"/> <property name="classes.dir" value="build/classes"/> <property name="jar.dir" value="build/jar"/> <property name="main-class" value="oata.HelloWorld"/> <target name="clean"> <delete dir="build"/> </target> <target name="compile"> <mkdir dir="${classes.dir}"/> <javac srcdir="src" destdir="${classes.dir}"/> </target> <target name="jar" depends="compile"> <mkdir dir="${jar.dir}"/> <jar destfile="${jar.dir}/apache-ant.jar" basedir="${classes.dir}"> <manifest> <attribute name="Main-Class" value="${main-class}"/> </manifest> </jar> </target> <target name="run" depends="jar"> <java jar="${jar.dir}/apache-ant.jar" fork="true"/> </target> <target name="clean-build" depends="clean,jar"/> <target name="main" depends="clean,run"/> </project>
Now it's easier, just do a ant and you will get
Buildfile: build.xml clean: compile: [mkdir] Created dir: C:\...\build\classes [javac] Compiling 1 source file to C:\...\build\classes jar: [mkdir] Created dir: C:\...\build\jar [jar] Building jar: C:\...\build\jar\HelloWorld.jar run: [java] Hello World main: BUILD SUCCESSFUL
Using external libraries
Somehow told us not to use syso-statements. For log-Statements we should use a Logging-API - customizable on a high degree (including switching off during usual life (= not development) execution). We use Log4J for that, because
- it is not part of the JDK (1.4+) and we want to show how to use external libs
- it can run under JDK 1.2 (as Ant)
- it's highly configurable
- it's from Apache ;-)
We store our external libraries in a new directory lib. Log4J can be downloaded [1] from Logging's Homepage. Create the lib directory and extract the log4j-1.2.9.jar into that lib-directory. After that we have to modify our java source to use that library and our buildfile so that this library could be accessed during compilation and run.
Working with Log4J is documented inside its manual. Here we use the MyApp-example from the Short Manual [2]. First we have to modify the java source to use the logging framework:
package oata;
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 World"); // the old SysO-statement
}
}
Most of the modifications are "framework overhead" which has to be done once. The blue line is our "old System-out" statement.
Don't try to run ant - you will only get lot of compiler errors. Log4J is not inside the classpath so we have to do a little work here. But do not change the CLASSPATH environment variable! This is only for this project and maybe you would break other environments (this is one of the most famous mistakes when working with Ant). We introduce Log4J (or to be more precise: all libraries (jar-files) which are somewhere under .\lib) into our buildfile:
<project name="HelloWorld" basedir="." default="main">
...
<property name="lib.dir" value="lib"/>
<path id="classpath"> <fileset dir="lib" includes="**/*.jar"/> </path>
...
<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="src" destdir="${classes.dir}" classpathref="classpath"/>
</target>
<target name="run" depends="jar">
<java fork="true" classname="${main-class}">
<classpath>
<path refid="classpath"/>
<path location="${jar.dir}/apache-ant.jar"/>
</classpath>
</java>
</target> ... </project>
In this example we start our application not via its Main-Class manifest-attribute, because we could not provide a jarname and a classpath. So add our class in the red line to the already defined path and start as usual. Running ant would give (after the usual compile stuff):
[java] 0 [main] INFO oata.HelloWorld - Hello World
What's that?
- [java] Ant task running at the moment
- 0 sorry don't know - some Log4J stuff
- [main] the running thread from our application
- INFO log level of that statement
- oata.HelloWorld source of that statement
- - separator
- Hello World the message
For another layout ... have a look inside Log4J's documentation about using other PatternLayout's.
Configuration files
Why we have used Log4J? "It's highly configurable"? No - all is hard coded! But that is not the debt of Log4J - it's ours. We had coded BasicConfigurator.configure(); which implies a simple, but hard coded configuration. More confortable would be using a property file. In the java source delete the BasicConfiguration-line from the main() method (and the related import-statement). Log4J will search then for a configuration as described in it's manual. Then create a new file src/log4j.properties. That's the default name for Log4J's configuration and using that name would make life easier - not only the framework knows what is inside, you too!
log4j.rootLogger=DEBUG, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%m%n
This configuration creates an output channel ("Appender") to console named as stdout which prints the message (%m) followed by a line feed (%n) - same as the earlier System.out.println() :-) Oooh kay - but we haven't finished yet. We should deliver the configuration file, too. So we change the buildfile:
... <target name="compile"> <mkdir dir="${classes.dir}"/> <javac srcdir="src" destdir="${classes.dir}" classpathref="classpath"/> <copy todir="${classes.dir}"> <fileset dir="src" excludes="**/*.java"/> </copy> </target> ...
This copies all resources (as long as they haven't the suffix ".java") to the build directory, so we could start the application from that directory and these files will included into the jar.
Configuration files
Why we have used Log4J? "It's highly configurable"? No - all is hard coded! But that is not the debt of Log4J - it's ours. We had coded BasicConfigurator.configure(); which implies a simple, but hard coded configuration. More confortable would be using a property file. In the java source delete the BasicConfiguration-line from the main() method (and the related import-statement). Log4J will search then for a configuration as described in it's manual. Then create a new file src/log4j.properties. That's the default name for Log4J's configuration and using that name would make life easier - not only the framework knows what is inside, you too!
log4j.rootLogger=DEBUG, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%m%n
This configuration creates an output channel ("Appender") to console named as stdout which prints the message (%m) followed by a line feed (%n) - same as the earlier System.out.println() :-) Oooh kay - but we haven't finished yet. We should deliver the configuration file, too. So we change the buildfile:
...
<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="src" destdir="${classes.dir}" classpathref="classpath"/>
<copy todir="${classes.dir}">
<fileset dir="src" excludes="**/*.java"/>
</copy>
</target>
...
This copies all resources (as long as they haven't the suffix ".java") to the build directory, so we could start the application from that directory and these files will included into the jar.
相关推荐
### Apache Ant 使用说明详解 #### 一、Apache Ant 概述 Apache Ant 是一款流行的 Java 构建工具,主要用于将源代码和其他输入文件转换为可执行文件。它由 James Duncan Davidson 创立,并最初作为 Tomcat 的一部分...
总的来说,这个项目利用了Ant作为构建工具,结合Eclipse IDE进行开发,使用Subversion进行版本控制,并且有详细的构建配置和源代码结构,展示了标准的Java Web应用开发流程。熟悉Ant的使用对于管理和构建Java项目至...
本教程将详细讲解Ant的基本概念、使用方法以及最佳实践。 首先,对于初学者来说,理解Ant的基本结构至关重要。在Ant中,"project"是构建的基本单元,包含了构建过程中的所有任务。每个项目定义了目标(target),...
Apache Ant 是一个开源的构建工具,广泛用于Java项目构建,由Apache软件基金会开发。这个"apache-ant-1.6.5-bin.zip"文件是Ant的1.6.5版本的二进制发行版...详细的使用方法和特性可通过提供的readme.txt文件进行查阅。
### ant实用实例 详细解析ant的用法 #### 一、Ant简介与基本概念 Apache Ant 是一个Java环境下的开源项目构建工具,主要用于自动化构建、部署等任务。它使用XML来描述构建过程,并且提供了丰富的任务(task)集合,...
本书将向你全面介绍大多数Java项目的核心步骤:编译、测试、执行、打包和交付,并告诉你一些“超越极限”的Ant使用方法。贯穿全书,作者通过一个应用程序的逐步完善和复杂,来展现应用如何进展,以及如何解决构造和...
这个压缩包文件名为"AntInAction2ndEdition",暗示了它可能包含了《Ant in Action》第二版的资源,这是一本深入讲解Ant用法和实践的经典书籍。 Ant的核心概念是构建文件(build.xml),这是一个XML文档,定义了一...
本书将向你全面介绍大多数Java项目的核心步骤:编译、测试、执行、打包和交付,并告诉你一些“超越极限”的Ant使用方法。贯穿全书,作者通过一个应用程序的逐步完善和复杂,来展现应用如何进展,以及如何解决构造和...
Ant 使用详细介绍 Ant 是什么? Ant 是一个构建工具,它可以帮助我们将项目开发过程中需要完成的各种步骤组织起来,通过一个简易的方式来构建整个项目。Ant 究竟能做什么呢?这取决于 Ant 的任务(Task),Ant 有...
在Ant构建过程中,JavaScript脚本方法的使用是一个强大的工具,它允许开发者自定义构建过程,实现更复杂的逻辑和任务自动化。Ant是一个Java库和命令行工具,它的设计目标是驱动软件构建过程,尤其是在Java项目中。这...
java ant use 介绍了java中ant的使用方法与技巧,
本书将向你全面介绍大多数Java项目的核心步骤:编译、测试、执行、打包和交付,并告诉你一些“超越极限”的Ant使用方法。贯穿全书,作者通过一个应用程序的逐步完善和复杂,来展现应用如何进展,以及如何解决构造和...
### ANT使用指南详解 #### 一、导言 Apache Ant 是一款开源的Java构建工具,类似于传统的Unix Make工具。Ant采用XML格式来定义构建过程,提供了丰富的任务...对于Java开发者而言,掌握Ant的使用方法是非常有益的。
本教程旨在提供Ant的基本使用方法,适合初学者学习。 #### 二、Ant的基础知识 ##### 2.1 什么是Ant? Apache Ant是一个基于Java的构建工具,其名称源自“Another Neat Tool”(另一个整洁的工具)的首字母缩写。...
【Ant简明教程:调用Bat方法】 Ant是Apache组织提供的一款开源的Java构建工具,它使用XML格式的配置文件来定义构建过程,包括编译、测试、打包、部署等任务。Ant以其灵活性和可扩展性深受Java开发者的喜爱。本教程...
### ant使用教程 #### 一、Ant简介 1. **什么是Ant** - Ant是一款用于自动化构建过程的软件工具,最初由Apache Software Foundation开发。它主要用于编译、测试...希望这篇教程能够帮助您更好地掌握Ant的使用方法。
理解这些任务的工作原理和使用方法至关重要。 4. **自定义任务**:Ant允许通过Java类来定义自定义任务,这扩展了Ant的功能,使其能够处理特定项目的需求。 5. **依赖管理**:在Ant中,你可以定义目标间的依赖关系...
本手册详细介绍了Ant的工作原理、基本概念、配置语法以及各种任务的使用方法,旨在帮助开发者高效地管理项目构建流程。 在Ant中,构建过程被定义在一个名为`build.xml`的XML文件中,这个文件包含了所有构建任务的...
通过阅读`命令.txt`、`ant知识.txt`、`ant构建Java.txt`、`ant例子.txt`这些文件,你可以深入了解Ant的使用方法,包括命令行调用、具体的构建步骤、Java项目构建实例以及各种任务的实践应用。这些资料将帮助你掌握...