Use ant to automatically build and deploy Java Projects 4

Source: Internet
Author: User

 

 

4. Use ant Build and deploy Java EngineeringAnt can use commands such as javac, Java, and jar to execute Java operations, so as to easily build and deploy Java projects. Here are some knowledge points.
1. Use ant's javac task to compile Java programs
Ant's javac task is used to compile Java programs. The following is a simple example: First, we create a Java project named antstudy, create the src directory as the source code directory, and create the helloworld. Java class file under the src directory. The content of this type of file is as follows: Public ClassHelloworld { Public Static VoidMain (string [] ARGs) {system. Out. Println ("Hello, amigo") ;}} create a build under the root directory of the antstudy project. XML file: Compile the Java file under the src directory in this file, and put the compiled class file into the build/classes directory. Before compiling, clear the classes directory, the content of this file is as follows: <? XML version = "1.0"?> <Projectname = "javactest" default = "compile" basedir = ". "> <targetname =" clean "> <deletedir =" build "/> </Target> <targetname =" compile "depends =" clean "> <mkdirdir =" build/classes "/> <javacsrcdir =" src "destdir =" build/classes "/> </Target> </Project> run the build. XML file. The build/classes directory is added to the project, and the compiled helloworld directory is generated. class file.

Compile source code

 


Ant's main goal is to generate a Java application, which internally and well supports callingjavacThe compiler and other Java-related tasks are not surprising. The following describes how to compile Java code tasks:

<javac srcdir="src"/>
 

Search for this tagsrcAll files with the. Java extension in the directory, and calljavacCompiler to generate class files in the same directory. Of course, it is usually clearer to put class files in a separate directory structure. You can adddestdirAttribute to enable ant to do this. Other useful attributes include:

  • classpath: EquivalentjavacOf-classpath.
  • debug="true": Indicates that the compiler should compile the source file with debugging information.

javacAn important feature of a task is that it only compiles the source files that it deems necessary to compile. If a class file already exists and the corresponding source file has not changed since the class file is generated, the source file will not be re-compiled.javacThe task output shows the actual number of compiled source files. WritecleanIt is a good habit to remove any class files generated from the target directory. If you want to ensure that all source files have been compiled, you can use this task. This behavior depicts the characteristics of many ant tasks: If a task can determine that the requested operation does not need to be executed, the operation will be skipped.

Like Ant,javacThe compiler itself is also implemented in Java. ForjavacThe use of the task is very advantageous because it usually calls the Compiler class in the same Java Virtual Machine (JVM) Where ant runs. Every time Java code needs to be compiled, other generation tools usually need to run a newjavacProcess, and a new JVM instance is required. However, when ant is used, only a single JVM instance is required. It is used to run both ant and all necessary compilation tasks (and other related tasks, such as processing jar files ). This is a much more efficient way to use resources, which can greatly shorten the project generation time.

 

Compiler Options

As we can see in the previous section, ant'sjavacThe default behavior of a task is to call any JVM standard compiler that runs ant itself. However, sometimes you may want to call the compiler independently-for example, when you want to specify some memory options for the compiler, or you need to use a different level of compiler. To achieve this goal, you only needjavacOfforkSet propertytrue, Such as the following:

<javac srcdir="src" fork="true"/>
 

If you want to specify a differentjavacExecute the file and pass it a maximum memory setting. You can do this as follows:

<javac srcdir="src" fork="true" executable="d:/sdk141/bin/javac"
memoryMaximumSize="128m"/>
 

You can even configure ant to use a different compiler. Supported compilers include open-source jikes compilers and GCI compilers from the GNU Compiler Collection (GCC. (See references for more information about the two compilers .) You can specify these compilers in two ways: You can setbuild.compilerAttribute.javacAll scenarios of the task; or set eachjavacIn the taskcompilerAttribute.

javacThe task also supports many other options. Refer to the ant manual for more details (see references ).

 


 

2. Use ant Java tasks to run Java programs
Ant can use Java tasks to run Java programs. The following is an example in 1. The modified build. xml file is as follows: <? XML version = "1.0"?> <Projectname = "javatest" default = "jar" basedir = ". "> <targetname =" clean "> <deletedir =" build "/> </Target> <targetname =" compile "depends =" clean "> <mkdirdir =" build/classes "/> <javacsrcdir =" src "destdir =" build/classes "/> </Target> <targetname =" run "depends =" compile "> <javaclassname =" helloworld"> <classpath> <pathelementpath = "build/classes"/> </classpath> </Java> </Target> </Project> to run the build. XML file. You can see hello on the console. Output of the main method of world.
3. Use the ant jar task to generate the JAR File
The reader can go further on the basis of the above example to generate the jar package. The following target can be added under the target of run: <targetname = "jar" depends = "run"> <jardestfile = "helloworld. jar "basedir =" build/classes "> <manifest> <attributename =" Main-class "value =" helloworld "/> </manifest> </jar> </Target> set the default attribute of ant project to jar, run the build at the same time. XML file. After running, a jar package helloworld is generated in the project directory. jar. 4. Use ant's war task to package J2EE WEB projects
Create a J2EE WEB Project. The directory structure is shown in:

SRC is the source code directory, webroot is the JSP storage directory, and Lib is the project package directory. The build. xml file is created under the project directory of antwebproject, which is the ant component file of the project. You can put the src directory in the helloworld developed in the previous example. java file, and create index under webroot. the content of a JSP file is very simple, that is, output Hello information. The Code is as follows: <% @ page Language = "Java" contenttype = "text/html; charset = "UTF-8" pageencoding = "UTF-8" %> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd"> <HTML>
Related Article

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.