Build.xml
lastmod 2025-02-11created 2024-11-0414 edits
The default build file name of Apache Ant.
Basic structure
project
<?xml version="1.0" encoding="euc-kr"?>
<project name="plc_gateway" default="dist" basedir="." xmlns:artifact="antlib:org.apache.maven.artifact.ant">
...
</project>
- name attribute
- Specifies the name of the project.
- In the example above, the project name is set to "plc_gateway".
- default attribute
- Specifies the default target to run.
- If no target is specified, Ant runs the target specified in default.
- In the example, the "dist" target is set to run by default.
- basedir attribute
- Sets the base directory of the project.
- In this example it is set to ".", so the current directory becomes the base directory.
- xmlns:artifact attribute
- A namespace definition for using Maven Ant Tasks in an Ant build file.
- antlib:org.apache.maven.artifact.ant refers to the Ant library that supports Maven Artifact features (e.g. dependency download, installation).
- This lets you use Maven's dependency management features in Ant scripts.
property
<property file="build.properties"/>
<property name="base.dir" value="."/>
<property name="dist.dir" value="dist"/>
<property name="build.dir" value="build"/>
<property name="src.dir" value="src"/>
<property name="jar.file" value="mseo_plc_gateway_${version}.jar"/>
<property name="dist.file" value="mseo_plc_gateway_${version}.zip"/>
Defines properties that can be used as variables. The variable name defined in the name attribute can then be used in the form ${jar.file}.
taskdef
<taskdef name="ssh" classname="com.sshtools.ant.Ssh">
<classpath>
<pathelement location="maverick-ant.jar"/>
</classpath>
</taskdef>
The <taskdef> tag above is a setting for defining a custom task in Apache Ant.
Note that maverick-ant.jar is the library needed to use SFTP and SSH in ANT. It is usually used to upload libraries via sftp.
path
typedef
<typedef resource="org/apache/maven/artifact/ant/antlib.xml"
uri="antlib:org.apache.maven.artifact.ant"
classpathref="maven-ant-tasks.classpath"/>
artifact
<artifact:pom id="pom" file="pom.xml"/>
<artifact:dependencies filesetId="pom.fileset" pathId="pom.classpath"
pomRefId="pom"/>
target
<target name="import-dependency" description="copy dependency jar file to lib">
<copy todir="target/dependency">
<fileset refid="pom.fileset"/>
<mapper type="flatten"/>
</copy>
</target>
<target name="prepare" depends="import-dependency">
<tstamp>
<format property="DSTAMP" pattern="yyyy.mm.dd"/>
<format property="TSTAMP" pattern="HH:mm"/>
</tstamp>
<echo message="Build Start!! ======> ${DSTAMP}-${TSTAMP}"/>
</target>
<target name="clean" depends="prepare">
<delete dir="${dist.dir}"/>
<delete dir="${build.dir}"/>
</target>
<target name="compile" depends="clean">
<mkdir dir="${build.dir}"/>
<javac deprecation="off" srcdir="${src.dir}" destdir="${build.dir}"
listfiles="no" failonerror="true" encoding="utf-8" debug="on"
debuglevel="lines,vars,source">
<classpath>
<pathelement path="${base.dir}/lib"/>
<fileset dir="${base.dir}/lib">
<include name="*.jar"/>
</fileset>
</classpath>
<classpath refid="pom.classpath"/>
</javac>
</target>
<target name="mkjar" depends="compile">
<mkdir dir="${dist.dir}"/>
<jar destfile="${dist.dir}/lib/${jar.file}" basedir="${build.dir}"/>
</target>
<target name="dist" depends="mkjar">
<copy todir="${dist.dir}/lib">
<fileset dir="lib"/>
</copy>
<copy todir="${dist.dir}/lib">
<fileset dir="target/dependency"/>
</copy>
<copy todir="${dist.dir}/cfg">
<fileset dir="cfg"/>
</copy>
<copy todir="${dist.dir}/bin">
<fileset dir="resource"/>
</copy>
</target>
build.properties
A file used to define external variables and values in build scripts that use Apache Ant.
With this file, you can set and manage the variable values of build scripts in a central place.
Typically, a build.properties file may contain information such as the following:
<?xml version="1.0" encoding="euc-kr"?>
<project name="plc_gateway" default="dist" basedir="."
xmlns:artifact="antlib:org.apache.maven.artifact.ant">
<property file="build.properties"/>
<property name="base.dir" value="."/>
<property name="dist.dir" value="dist"/>
<property name="build.dir" value="build"/>
<property name="src.dir" value="src"/>
<property name="jar.file" value="mseo_plc_gateway_${version}.jar"/>
<property name="dist.file" value="mseo_plc_gateway_${version}.zip"/>
<path id="maven-ant-tasks.classpath" path="${base.dir}/maven-ant-tasks-2.1.3.jar"/>
...
It is usually added to the .gitignore file.
Add a specific local JAR file as a dependency
- IntelliJ IDEA#Add a specific local JAR file as a dependency
- Apache Ant -> build.xml#Add a specific local JAR file as a dependency
- Gradle -> build.gradle#Add a specific local JAR file as a dependency
<project name="MyProject" default="compile">
<property name="lib.dir" location="lib"/>
<target name="compile">
<javac srcdir="src" destdir="build/classes">
<classpath>
<pathelement path="${lib.dir}/mseo-common_1.0.0.jar"/>
</classpath>
</javac>
</target>
</project>