Deploying a Java Project for the Ant starter Tutorial

Source: Internet
Author: User

Ant can instead use commands such as Javac, Java, and jars to perform Java operations, making it easy to build and deploy Java engineering.


1. Using Ant's Javac command to compile Java programs

Ant's Javac command is used to implement the functionality of compiling Java programs. Here's a simple example: first we build a Java project called Javapro,

Create the SRC directory as the source code directory and create the Testant.java file in the SRC directory. The contents of this type of file are as follows:

Package Com.home;public class Testant{public static void Main (String args[]) {System.out.println ("Hello World");}}

Note that it is with the package name.

At the same time, build the Build.xml file in the root directory of the Javapro project, compile the Java file in the SRC directory,

And put the compiled class file into the Build/classes directory, the entire project directory structure is as follows:

| Javapro

|src

|build

|classes

|build.xml


Before compiling, clear the classes directory, the contents of the file are as follows:

<?xml version= "1.0"?> <project name = "Testant" default= "Compile" basedir= "." >    <target name= "clean" >        <delete dir= "${basedir}/build"/>    </target>    < Target Name= "Compile"  depends = "clean" >        <mkdir dir = "${basedir}/build/classes"/>        <javac Srcdir = "${BASEDIR}/SRC" Destdir = "${basedir}/build/classes"/>    </target></project>

After the ant command is executed under the Javapro root directory, you can see the newly generated build/classes subdirectory in that directory.

The testant.class file generated after compilation is in this directory.


2. Execute Java program with java commandthe ability to run Java programs can be implemented using Java commands in Ant. You can make a slight change on the above build.xml basis:

<?xml version= "1.0"?> <project name = "Testant" default= "Run" basedir= "." >    <target name= "clean" >        <delete dir= "${basedir}/build"/>    </target>    < Target Name= "Compile"  depends = "clean" >        <mkdir dir = "${basedir}/build/classes"/>        <javac Srcdir = "${BASEDIR}/SRC" Destdir = "${basedir}/build/classes"/>    </target>     <target name= "Run"  depends = "compile" >        <java classname = "Com.home.TestAnt" >            <classpath>               < Pathelement path= "${basedir}/build/classes"/>            </classpath>        </java>    </target ></project>
after executing the command we can see the output in the console: "[Java] Hello World"


3. Using jar command to generate JAR fileYou can also build the jar package by adding a name to the jar's target, based on the example above:

<?xml version= "1.0"?> <project name = "Testant" default= "Jar" basedir= "." > <target name= "clean" > <delete dir= "${basedir}/build"/> </target> <target name= "c Ompile "depends =" clean "> <mkdir dir =" ${basedir}/build/classes "/> <javac srcdir =" ${basedir}/s RC "Destdir =" ${basedir}/build/classes "/> </target> <target name=" Run "depends=" compile "> & Lt;java classname = "Com.home.TestAnt" ><classpath> <pathelement path= "${basedir}/build/classe s/"/> </classpath> </java> </target> <target name=" jar "depends=" R Un "> <jar destfile=" Testant.jar "basedir=" ${basedir}/build/classes/"> &LT;MANIFEST&GT;&L T;attribute name= "Main-class" value= "com.home.TestAnt"/> </manifest> </jar> &L T;/target ></project>

After ant execution is complete, you can see that a Testant.jar jar package is generated under the project root directory. In cmd, you can execute the jar package by running the following command

Java-jar Testant.jar


4. Use the War command to package a Web projectbuild a Web project, where SRC is the Java source code directory, Webroot is the JSP directory,

Lib directory that references the package to the project. Build the Build.xml file under the Testwebant project directory, which is the ant component file for the project.

Use MyEclipse to create a new Web project with the following structure:



Next, write the Build.xml file, which reads as follows:

<?xml version= "1.0"? ><project name= "Testwebant" default= "war" basedir= "." ><property name= "Classes" value= "${basedir}/build/classes"/><property name= "Build" value= "${basedir}/ Build "/><property name=" Lib "value=" ${basedir}/webroot/web-inf/lib "/><!--Delete the build path--><target Name= "clean" ><delete dir= "${build}"/></target><!--establish build/classes path and compile class file to Build/classes Path under--><target name= "compile" depends= "clean" ><mkdir dir= "${classes}"/><javac srcdir= "${basedir}/ src "destdir=" ${classes} "/></target><!--Fight War package--><target name=" war "depends=" compile "><war Destfile= "${build}/testwebant.war" webxml= "${basedir}/webroot/web-inf/web.xml" ><!--copy WebRoot except Web-inf And Meta-inf's two folders--><fileset dir= "${basedir}/webroot" includes= "**/*.jsp"/><!--Copy the jar package under the Lib directory-->< Lib dir= "${lib}"/><!--copy of the class file under Build/classes--><classes dir= "${classes}"/></war></target></project> 
after running ant, the Testwebant.war file is generated .

You can then run the Web project in the appropriate directory for the Web container (such as Tomcat) (${tomcata installation directory}\webapps).


Itmyhome

Original: Wheat Field Technology Blog


Deploying a Java Project for the Ant starter Tutorial

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.