The use of Ant (build.xml configuration exercise)

First of all, Apache Ant is mainly a set of tools for compiling, testing and deploying Java applications.

Official download address: http://ant.apache.org/

Unzip the downloaded zip package, and configure the environment variable ANT_HOME in the system to point to its bin directory.

Open the command line test: enter the ant command, and the following information appears, indicating that the configuration is successful!

Buildfile: build.xml does not exist!
Build failed

For specific commands, please refer to the official manual. The following is mainly about using it in the eclipse integrated development environment.

Eclipse3.7 integrates ant and builds the build.xml file in your application. Among them, each build.xml build file defines a unique project (project tag), each project has multiple targets (target tag), and there can be dependencies between targets (depends attribute of target), in each target Multiple tasks can be defined.

A simple configuration example is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<project name="antTest">
<target name="sayHello">
<echo message="Hello ant!" />
</target>
</project>

 Execution: Right-click "Run As" -> "Ant Build" on the build.xml file, the Console window will print Hello ant! and output BUILD SUCCESSFUL to indicate that the execution is successful. (enter ant sayHello in the command line window to test)

Common main tasks for deploying Java projects include:

1.javac compilation

2.java execution

3.jar generates jar file

An example configuration is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<!--Packaging jar file-->
<project name="jar包名" default="release" basedir=".">
	<property name="debug" value="on" />
	<property name="package.prefix" value="com/xxx" />
        <property name="classes.dir" value="${basedir}/bin" />
<property name="servlet.lib" value="${basedir}/lib" />
	<property name="jar.name" value="jar包名.jar" />
        <!-- Clean the old classes -->
	<target name="clean">
		<delete>
			<fileset dir="${classes.dir}">
			</fileset>
		</delete>
	</target>
        <!-- Compiles the source directory -->
	<target name="compile.java" depends="clean">
		<javac debug="${debug}" encoding="GBK"
			destdir="${classes.dir}" includeantruntime="on">
			<src path="${basedir}/src directory" />
			<classpath id="class.path.dir">
				<fileset dir="../web/WEB-INF/lib">
					<include name="*.jar" />
					<exclude name="${jar.name}" />
				</fileset>
                                <fileset dir="${servlet.lib}">
                                        <include name="**/*.jar" />
                                </fileset>
			</classpath>
		</javac>
	</target>
	<!-- Creates the class package -->
	<target name="package" depends="compile.java">
		<jar jarfile="../web/WEB-INF/lib/${jar.name}">
			<fileset dir="${classes.dir}">
				<include name="${package.prefix}/**/*.class" />
			</fileset>
		</jar>
	</target>
	<!-- execute result. -->
	<target name="release" depends="package">
		<echo message="Build success!" />
	</target>
</project>

 Among them, several properties are set, which can be referenced in the following way by ${}.

The result of the above configuration is to generate a jar file in the specified lib directory, and put the specific class file into the specified bin directory.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326361166&siteId=291194637