nant 下载可以用javac吗

Maven: The Complete Reference &&&- 1.7. Comparing Maven with Ant
1.7. Comparing Maven with Ant
The authors of this book have no interest in creating a feud between
Apache Ant and Apache Maven, but we are also cognizant of the fact
that most organizations have to make a decision between the two
standard solutions: Apache Ant and Apache Maven. In this section, we
compare and contrast the tools.
Ant excels at build process, it is a build system modeled after make
with targets and dependencies. Each target consists of a set of
instructions which are coded in XML. There is a copy task and a
javac task as well as a jar task. When you use Ant, you supply Ant
with specific instructions for compiling and packaging your
output. Look at the following example of a simple build.xml file:
A Simple Ant build.xml file.
&project name="my-project" default="dist" basedir="."&
&description&
simple example build file
&/description&
&!-- set global properties for this build --&
&property name="src" location="src/main/java"/&
&property name="build" location="target/classes"/&
&property name="dist"
location="target"/&
&target name="init"&
&!-- Create the time stamp --&
&!-- Create the build directory structure used by compile --&
&mkdir dir="${build}"/&
&target name="compile" depends="init"
description="compile the source " &
&!-- Compile the java code from ${src} into ${build} --&
&javac srcdir="${src}" destdir="${build}"/&
&target name="dist" depends="compile"
description="generate the distribution" &
&!-- Create the distribution directory --&
&mkdir dir="${dist}/lib"/&
&!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file --&
&jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/&
&target name="clean"
description="clean up" &
&!-- Delete the ${build} and ${dist} directory trees --&
&delete dir="${build}"/&
&delete dir="${dist}"/&
&/project&
In this simple Ant example, you can see how you have to tell Ant
exactly what to do. There is a compile goal which includes the javac
task that compiles the source in the src/main/java directory to the
target/classes directory. You have to tell Ant exactly where your
source is, where you want the resulting bytecode to be stored, and how
to package this all into a JAR file. While there are some recent
developments that help make Ant less procedural, a developer’s
experience with Ant is in coding a procedural language written in XML.
Contrast the previous Ant example with a Maven example. In Maven, to
create a JAR file from some Java source, all you need to do is create
a simple pom.xml, place your source code in
${basedir}/src/main/java and then run mvn install from the
command line. The example Maven pom.xml that achieves the same
results as the simple Ant file listed in
is shown in
A Sample Maven pom.xml.
&modelVersion&4.0.0&/modelVersion&
&groupId&org.sonatype.mavenbook&/groupId&
&artifactId&my-project&/artifactId&
&version&1.0&/version&
&/project&
That’s all you need in your pom.xml. Running mvn install from the
command line will process resources, compile source, execute unit
tests, create a JAR, and install the JAR in a local repository for
reuse in other projects. Without modification, you can run mvn site
and then find an index.html file in target/site that contains
links to JavaDoc and a few reports about your source code.
Admittedly, this is the simplest possible example project containing
nothing more than some source code and producing a simple JAR. It is a
project which closely follows Maven conventions and doesn’t require
any dependencies or customization. If we wanted to start customizing
the behavior, our pom.xml is going to grow in size, and in the
largest of projects you can see collections of very complex Maven POMs
which contain a great deal of plugin customization and dependency
declarations. But, even when your project’s POM files become more
substantial, they hold an entirely different kind of information from
the build file of a similarly sized project using Ant. Maven POMs
contain declarations: "This is a JAR project", and "The source code is
in src/main/java". Ant build files contain explicit instructions:
"This is project", "The source is in src/main/java", "Run javac
against this directory", "Put the results in target/classes",
"Create a JAR from the ….", etc. Where Ant had to be explicit about
the process, there was something "built-in" to Maven that just knew
where the source code was and how it should be processed.
The differences between Ant and Maven in this example are:
Apache Ant
Ant doesn’t have formal conventions like a common project directory
structure or default behavior. You have to tell Ant exactly where
to find the source and where to put the output. Informal
conventions have emerged over time, but they haven’t been codified
into the product.
Ant is procedural. You have to tell Ant exactly what to do and when
to do it. You have to tell it to compile, then copy, then compress.
Ant doesn’t have a lifecycle. You have to define goals and goal
dependencies. You have to attach a sequence of tasks to each goal
Apache Maven
Maven has conventions. It knows where your source code is because
you followed the convention. Maven’s Compiler plugin put the
bytecode in target/classes, and it produces a JAR file in target.
Maven is declarative. All you had to do was create a pom.xml file
and put your source in the default directory. Maven took care of
Maven has a lifecycle which was invoked when you executed mvn
install. This command told Maven to execute a series of sequential
lifecycle phases until it reached the install lifecycle phase. As a
side-effect of this journey through the lifecycle, Maven executed a
number of default plugin goals which did things like compile and
create a JAR.
Maven has built-in intelligence about common project tasks in the form
of Maven plugins. If you wanted to write and execute unit tests, all
you would need to do is write the tests, place them in
${basedir}/src/test/java, add a test-scoped dependency on
either TestNG or JUnit, and run mvn test. If you wanted to deploy a
web application and not a JAR, all you would need to do is change your
project type to war and put your docroot in
${basedir}/src/main/webapp. Sure, you can do all of this with
Ant, but you will be writing the instructions from scratch. In Ant,
you would first have to figure out where the JUnit JAR file should
be. Then you would have to create a classpath that includes the JUnit
JAR file. Then you would tell Ant where it should look for test source
code, write a goal that compiles the test source to bytecode, and
execute the unit tests with JUnit.
Without supporting technologies like antlibs and Ivy (even with these
supporting technologies), Ant has the feeling of a c`ustom procedural
build. An efficient set of Maven POMs in a project which adheres to
Maven’s assumed conventions has surprisingly little XML compared to
the Ant alternative. Another benefit of Maven is the reliance on
widely-shared Maven plugins. Everyone uses the Maven Surefire plugin
for unit testing, and if someone adds support for a new unit testing
framework, you can gain new capabilities in your own build by just
incrementing the version of a particular Maven plugin in your
project’s POM.
The decision to use Maven or Ant isn’t a binary one, and Ant still has
a place in a complex build. If your current build contains some highly
customized process, or if you’ve written some Ant scripts to complete
a specific process in a specific way that cannot be adapted to the
Maven standards, you can still use these scripts with Maven. Ant is
made available as a core Maven plugin. Custom Maven plugins can be
implemented in Ant, and Maven projects can be configured to execute
Ant scripts within the Maven project lifecycle.
Become a Member
Are you a current user of: Nexus Repository Manager
Repository ManagerNeither
MEMBERS & GROWING&&&2654人阅读
例如有如下窗体程序,在package "cn.jar.test" 中,同时,程序有2个资源文件about.txt和about.gif:
package cn.jar.
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
public class ResourceTest {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable(){
public void run(){
ResourceTestFrame frame=new ResourceTestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
@SuppressWarnings("serial")
class ResourceTestFrame extends JFrame{
public ResourceTestFrame(){
setTitle("ResourceTest");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
URL aboutURL=getClass().getResource("about.gif");
Image img=Toolkit.getDefaultToolkit().getImage(aboutURL);
setIconImage(img);
JTextArea textArea=new JTextArea();
InputStream stream=getClass().getResourceAsStream("about.txt");
Scanner in=new Scanner(stream);
while(in.hasNext()){
textArea.append(in.nextLine()+"/n");
add(textArea);
public static final int DEFAULT_WIDTH=300;
public static final int DEFAULT_HEIGHT=300;
目录结构如下:&&&&&&&&&&&&&&&&&&&&&&&&&
Step 1:切换到../src/cn/jar/test目录下,用javac编译:
&javac ResourceTest.java
Step 2:切换到../src目录下,运行该程序,检查是否有运行时错误:
java cn.jar.test.ResourceTest
Step 3: 把所有.class文件及资源文件about.txt, about.gif复制到另外一个test目录下,例如c:/src/cn/jar/test。同样是相同的目录结构cn/jar/test。
Step 4:生成jar包,切换到c:/src下:
jar cvf test.jar cn
Step 5: 加入清单文件: 在c:/src目录下创建test.mf文件,用记事本打开,加入如下行,注意输入完后一定要换行:
Main-Class: cn.test.jar.ResourceTest
Step 6: 更新默认的清单文件:
jar umf test.mf test.jar
于是一个jar包就生成打包好了。可以把test.jar包复制到另外一个目录,如c:/temp下,运行
java -jar test.jar
进行测试打包是否成功。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:138061次
积分:1758
积分:1758
排名:第19069名
原创:29篇
(1)(1)(3)(2)(5)(2)(2)(1)(4)(4)(2)(4)Apache Ant - Tutorial
Quick links
Table of Contents
Apache Ant Tutorial.
This tutorial describes the usage of Ant as a build tool to compile Java code, pack this code into an executable jar and how to create Javadoc.
The usage of Ant is demonstrated within Eclipse and from the command line.
This tutorial is based on Apache Ant 1.8.x.
1. Build tools
In software development the term building usually means the conversion of source code and other artifacts, like images or configuration files, into another artifact.
For example source code might be compiled into a JAR file, or you may create a new standalone application.
The build result can be shared with users of the software or used only internally.
A build tool is used to automate repetitive tasks during this process.
This can be, for example, compiling source code, running software tests and creating files and documentation for the software deployment.
Build tools typically run without a graphical user interface directly from the command line. As a user interface is not required for such builds, these builds are called headless.
Popular build tools in the Java space are Maven, Gradle and Apache Ant.
2. Apache Ant Overview
2.1. What is Apache Ant?
Apache Ant (Ant) is a general purpose build tool. Ant is an abbreviation for Another Neat Tool.
Ant is primarily used for building and deploying Java projects but can be used for every possible repetitive tasks, e.g. generating documentation.
2.2. Configuring Apache Ant
A Java build process typically includes:
the compilation of the Java source code into Java bytecode
creation of the .jar file for the distribution of the code
creation of the Javadoc documentation
Ant uses an xml file for its configuration.
The default file name is build.xml.
Ant builds are based on three blocks: tasks, targets and extension points.
A task is a unit of work which should be performed and constitutes of small atomic steps, for example compile source code or create Javadoc.
Tasks can be grouped into targets.
A target can be directly invoked via Ant.
Targets can specify their dependencies.
Ant will automatically execute all dependent targets.
For example if target A depends on B and Ant is instructed to run A, it first runs B before running A.
In your build.xml file you can specify the default target.
Ant executes this target, if no explicit target is specified.
3. Installation
3.1. Prerequisites
Ant requires the installation of the Java Development Kit (JDK).
for a description how to install Java.
3.2. Ubuntu
On Ubuntu use the apt-get install ant command to install Ant.
For other distributions please check the documentation of your vendor.
3.3. Windows
Download Apache Ant from .
Extract the zip file into a directory structure of your choice.
Set the ANT_HOME environment variable to this location and include the ANT_HOME/bin directory in your path.
Make also sure that the JAVA_HOME environment variable is set to the JDK.
This is required for running Ant.
Check your installation by opening a command line and typing ant -version into the commend line.
The system should find the command ant and show the version number of your installed Ant version.
4. Tutorial: Using Apache Ant
4.1. Using Ant for Java development
The following describes how you compile Java classes, create an executable JAR file and create Javadoc for your project with Apache Ant.
The following example assumes that your are using the Eclipse IDE to manage your Java project and your Ant build.
4.2. Create Java project
Create a Java project called de.vogella.build.ant.first in Eclipse.
Create a package called math and the following class.
package math;
public class MyMath {
public int multi(int number1, int number2) {
return number1 * number2;
Create the test package and the following class.
package test;
import math.MyMath;
public class Main {
public static void main(String[] args) {
MyMath math = new MyMath();
System.out.println(&Result is: & + math.multi(5, 10));
4.3. Create build.xml
Create a new file through the File ▸ New ▸ File menu and create the build.xml file.
Implement the following code to this file.
&?xml version=&1.0&?&
name=&Ant-Test& default=&main& basedir=&.&
name=&src.dir& location=&src&
name=&build.dir& location=&bin&
name=&dist.dir& location=&dist&
name=&docs.dir& location=&docs&
name=&clean&
dir=&${build.dir}&
dir=&${docs.dir}&
dir=&${dist.dir}&
name=&makedir&
dir=&${build.dir}&
dir=&${docs.dir}&
dir=&${dist.dir}&
name=&compile& depends=&clean, makedir&
srcdir=&${src.dir}& destdir=&${build.dir}&
name=&docs& depends=&compile&
packagenames=&src& sourcepath=&${src.dir}& destdir=&${docs.dir}&
dir=&${src.dir}&
name=&jar& depends=&compile&
destfile=&${dist.dir}\de.vogella.build.test.ant.jar& basedir=&${build.dir}&
name=&Main-Class& value=&test.Main&
name=&main& depends=&compile, jar, docs&
Main target
The code is documented, you should be able to determine the purpose of the different ant tasks via the documentation in the coding.
4.4. Run your Ant build from Eclipse
Run the build.xml file as an Ant Build in Eclipse.
After this process your data structure should look like this:
4.5. Run your Ant build from the command line
Open a command line and switch to your project directory.
Type the following commands.
# run the build
ant -f build.xml
# run ant with defaults (build.xml file and the default target)
The build should finish successfully and generate the build artifacts.
5. Apache Ant classpath
5.1. Setting the Ant classpath
Ant allows to create classpath containers and use them in tasks.
The following build.xml from a project called de.vogella.build.ant.classpath demonstrates this.
&?xml version=&1.0&?&
name=&Ant-Test& default=&Main& basedir=&.&
name=&src.dir& location=&src&
name=&lib.dir& location=&lib&
name=&build.dir& location=&bin&
id=&build.classpath&
dir=&${lib.dir}&
name=&**/*.jar&
name=&clean&
dir=&${build.dir}&
name=&makedir&
dir=&${build.dir}&
name=&compile& depends=&clean, makedir&
srcdir=&${src.dir}& destdir=&${build.dir}& classpathref=&build.classpath&
name=&Main& depends=&compile&
Main target
5.2. Print
You can use the following statements to write the full classpath to the console.
You can use this to easily verify if the classpath is correct.
&!-- Write the classpath to the console. Helpful for debugging --&
&!-- Create one line per classpath element--&
&pathconvert pathsep=&${line.separator}& property=&echo.classpath& refid=&junit.class.path&&
&/pathconvert&
&!-- Write the result to the console --&
&echo message=&The following classpath is associated with junit.class.path & /&
&echo message=&${echo.classpath}& /&
6. Run Java program
Ant allows to run Java programs.
You can specify properties which are passed as parameters to the main method of Java.
The following demonstrates how to run a Java program.
name=&Run java program& basedir=&.& default=&default&
name=&outputdir.property& value=&../../output/&
name=&inputdir.property& value=&../../output/&
name=&classpath.property& value=&${basedir}/../com.vogella.website.repository/target/repository/plugins/&
name=&default&
id=&runtime.path&
dir=&${classpath.property}& includes=&*.jar&
message = &Ant output directory: ${outputdir.property}&
message = &Ant input directory for tutorials: ${inputdir.property}&
message = &Ant classpath directory: ${classpath.property}&
classname=&com.vogella.test.Mail& fork=&false& classpathref=&runtime.path&
value=&${outputdir.property}&
value=&${inputdir.property}&
7. Running JUnit Tests via Ant
Ant allows to run
Ant defines junit task.
You only need to include the junit.jar and the compiled classes into the classpath for Ant and then you can run JUnit tests.
for an introduction into JUnit.
The following is a JUnit test for the previous example.
package test;
import math.MyMath;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class MyMathTest {
public void testMulti() {
MyMath math = new MyMath();
assertEquals(50, math.multi(5, 10));
You can run this JUnit unit test via the following build.xml.
This example assumes that the JUnit jar "junit.jar" is in folder "lib".
&?xml version=&1.0&?&
name=&Ant-Test& default=&main& basedir=&.&
name=&src.dir& location=&src&
name=&build.dir& location=&bin&
name=&test.dir& location=&src&
name=&test.report.dir& location=&testreport&
id=&junit.class.path&
location=&lib/junit.jar&
location=&${build.dir}&
name=&clean&
dir=&${build.dir}&
dir=&${test.report.dir}&
name=&makedir&
dir=&${build.dir}&
dir=&${test.report.dir}&
name=&compile& depends=&clean, makedir&
srcdir=&${src.dir}& destdir=&${build.dir}&
refid=&junit.class.path&
name=&junit& depends=&compile&
printsummary=&on& fork=&true& haltonfailure=&yes&
refid=&junit.class.path&
type=&xml&
todir=&${test.report.dir}&
dir=&${src.dir}&
name=&**/*Test*.java&
name=&main& depends=&compile, junit&
Main target
8. Useful Ant tasks
Ant provides the fail task which allows to fail the build if a condition is not met.
For example the following task would check for the existence of a file and if this file is not present the build would fail.
&fail message=&PDF file was not created.&&
&condition&
&available file=&${pdf.dir}/path/book.pdf& /&
&/condition&
Or you can check for the number of files generated.
&fail message=&Files are missing.&&
&condition&
&resourcecount count=&2&&
&fileset id=&fs& dir=&.& includes=&one.txt,two.txt&/&
&/resourcecount&
&/condition&
9. Eclipse Ant Editor
Eclipse has an ant editor which makes the editing of ant files very easy by providing syntax checking of the build file.
Eclipse has also an ant view.
In this view you execute ant files via double-clicking on the target.
By default Eclipse uses the ant version which ships with Eclipse.
Via the preference Ant ▸ Runtime ▸ Ant Home you can configure which version of Ant Eclipse should use.
10. Ant tips
10.1. Converting Paths
Apache Ant allows to convert relative paths to absolute paths.
The following example demonstrates that.
&!-- Location of a configuration --&
&property name=&my.config& value=&../my-config.xml&/&
&makeurl file=&${my.config}& property=&my.config.url&/&
This property can later be used for example as a parameter.
name=&highlight.xslthl.config& expression=&${my.config.url}&
10.2. Regular expressions
Ant can be used to replace text based on regular expressions.
The following Ant Target replaces tabs with double spaces.
name=&regular-expressions&
flags=&gs&
pattern=&(\t)&
expression=&
dir=&${outputtmp.dir}/&
name=&**/*&
11. About this website
12. Links and Literature
12.1. Source Code
12.2. General
12.3. vogella GmbH training and consulting support
The vogella company provides comprehensive
from experts in the areas of Eclipse RCP, Android, Git, Java, Gradle and Spring.
We offer both public and inhouse training. Whichever course you decide to take, you are guaranteed to experience
what many before you refer to as .
The vogella company offers
services, development support and coaching.
Our customers range from Fortune 100 corporations to individual developers.
Copyright ©
vogella GmbH.
Free use of the software examples is granted under the terms of the EPL License.
This tutorial is published under the

我要回帖

更多关于 nant汽车站 的文章

 

随机推荐