Ant

From Wiki RB4

Einleitung[edit]

Ants build file follows a very simple structure. Each <project> contains one or more <target>s; each target can depend on zero or more other targets. When you run ant, you specify which target you want ant to build; ant then runs all the targets that one depends on first, then runs the target you asked for. All other targets depend on init target, which means that "init" will always be run first.

Generally, the important strings -- directory names, file names, the classpath -- are declared as <property> elements. This allows you to easily change their value later on, even if you refer to them in several places. It also allows you to easily override their values, using (a) -Dfoo=bar on the command line, (b) the <ant> task, calling one ant file from another one, or (c) <property file="...">. Property precedence in Ant is a little upside down. The rule is, the first line that sets a property wins. That means that a value on the command line comes first, then a value set in a whatever build.xml file called this one, then the first value set in this file.

Installation[edit]

see HP Laptop or XP Laptop

Aufruf[edit]

ant {-D<property>=<value>} [-buildfile <BuildFile>] [-quiet] [-verbose] {<target>}

z.B. <DirectoryWithBuild.XML>\ant

By default, ant looks for a build file named build.xml in the current directory. This build file follows a very simple structure. Each <project> contains one or more <target>s; each target can depend on zero or more other targets. When you run ant, you specify which target you want ant to build; ant then runs all the targets that one depends on first, then runs the target you asked for.

Build.xml-Struktur[edit]

Project-Element[edit]

<project [name="<Name>"] [location="<Location>"] default="<DefaultTarget>" [basedir="<BaseDir>"] > {<Target>)}</project>

Attribute Description
location Sets the property to the absolute filename of the given file. If the value of this attribute is an absolute path, it is left unchanged (with / and \ characters converted to the current platforms conventions). Otherwise it is taken as a path relative to the project's basedir and expanded.

Stardard-Properties[edit]

<property environment="env" /> => ${env.PATH}

Target-Element[edit]

Ein Target enthält eine Menge von Tasks.

<target name="<Name>" [depends="<Target> {,<Target>}" [if="<MustProperty>"] [unless="<MustNotProperty>"] [description="<Description>"] > {<Task>}</target>

All other targets depend on "init," which means that "init" will always be run first. Ansonsten werden die Targets in der depends-Reihenfolge der Aufzählung ausgeführt.

Task[edit]

Ein Task ist ein Stück Code, das ausgeführt werden kann. Ein Task kann mehrere Attribute (oder Argumente, wenn man will) haben. Der Wert eines Attributs kann Referenzen auf eine Property enthalten. Diese Referenzierung wird aufgelöst, bevor der Task ausgeführt wird. Es gibt einen Satz von vorgefertigten Tasks. Some tasks use directory trees for the actions they perform. For example, the [javac|http://ant.apache.org/manual/CoreTasks/javac.html] task, which compiles a directory tree with .java files into .class files, is one of these directory-based tasks. Because some of these tasks do so much work with a directory tree, the task itself can act as an implicit [FileSet|http://ant.apache.org/manual/CoreTypes/fileset.html]. '*' matches zero or more characters, '?' matches one character. hen ** is used as the name of a directory in the pattern, it matches zero or more directories. For example: /test/** matches all files/directories under /test/, such as /test/x.java, or /test/foo/bar/xyz.html, but not /xyz.xml. There is one "shorthand" - if a pattern ends with / or \, then ** is appended.


copy[edit]

Beispiele: Copy a single file

<copy file="myfile.txt" tofile="mycopy.txt"/>

Copy a single file to a directory

<copy file="myfile.txt" todir="../some/other/dir"/>

Copy a directory to another directory

<copy todir="../new/dir">
  <fileset dir="src_dir"/>
</copy>

Copy a set of files to a directory

<copy todir="../dest/dir">
  <fileset dir="src_dir">
    <exclude name="**/*.java"/>
  </fileset>
</copy>

<copy todir="../dest/dir">
  <fileset dir="src_dir" excludes="**/*.java"/>
</copy>

Copy a set of files to a directory, appending .bak to the file name on the fly

<copy todir="../backup/dir">
  <fileset dir="src_dir"/>
    <globmapper from="*" to="*.bak"/>
</copy>

Copy a set of files to a directory, replacing @TITLE@ with Foo Bar in all files.

<copy todir="../backup/dir">
  <fileset dir="src_dir"/>
    <filterset>
      <filter token="TITLE" value="Foo Bar"/>
  </filterset>
</copy>


delete[edit]

echo[edit]

<echo [file="<Filename>"] [message="<Text>"]> </echo>


exec[edit]

jar[edit]

destfile introduced after 1.4.1
jarfile compatible 1.4.1 und 1.5.1
basedir .\\<Directory>


java[edit]

javac[edit]

mkdir[edit]

move[edit]

replaceregexp[edit]

sleep[edit]

<sleep seconds="<Number>" />

Properties[edit]

Properties (that is, variables) are a very important part of Ant. Generally, the important strings -- directory names, file names, the classpath -- are declared as <property> elements. This allows you to easily change their value later on, even if you refer to them in several places. It also allows you to easily override their values, using (a) -Dfoo=bar on the command line, (b) the <ant> task, calling one ant file from another one, or (c) <property file="...">. The <property file="..."> construct is usually used to include a file called build.properties. This is a convention used so that you can override default values for your local build environment, without having to modify build.xml (which may, after all, be a shared file, under version control). This means that build.properties should probably not be under version control. Property precedence in Ant is a little upside down. The rule is, the first line that sets a property wins. Properties können als Wert eines Task- Attributes benutzt werden, indem man den Namen der Property innerhalb des Ausdrucks ${<PropertyName>} im Attribut-Wert platziert.

Projekte[edit]

AntTest[edit]

  • cd C:\Uwes\Java\MyProjects\AntTest\
  • ant