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 callingjavac
The compiler and other Java-related tasks are not surprising. The following describes how to compile Java code tasks:
<javac srcdir="src"/>
Search for this tagsrc
All files with the. Java extension in the directory, and calljavac
Compiler 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 adddestdir
Attribute to enable ant to do this. Other useful attributes include:
classpath
: Equivalentjavac
Of-classpath
.
debug="true"
: Indicates that the compiler should compile the source file with debugging information.
javac
An 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.javac
The task output shows the actual number of compiled source files. Writeclean
It 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,javac
The compiler itself is also implemented in Java. Forjavac
The 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 newjavac
Process, 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'sjavac
The 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 needjavac
Offork
Set propertytrue
, Such as the following:
<javac srcdir="src" fork="true"/>
If you want to specify a differentjavac
Execute 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.compiler
Attribute.javac
All scenarios of the task; or set eachjavac
In the taskcompiler
Attribute.
javac
The 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>