@@ -145,10 +146,10 @@ new target "use". Before we can use our new task we have to declare it with
</project>
</project>
</pre>
</pre>
Important is the <i>classpath</i>-attribute. Ant searches in its /lib directory for
<p>Important is the <i>classpath</i>-attribute. Ant searches in its /lib directory for
tasks and our task isn't there. So we have to provide the right location. </p>
tasks and our task isn't there. So we have to provide the right location. </p>
<p>Now we can type in <tt>ant</tt> and all should work ...
<p>Now we can type in <tt>ant</tt> and all should work ...</p>
<pre class="output">
<pre class="output">
Buildfile: build.xml
Buildfile: build.xml
@@ -178,7 +179,7 @@ setting a reference to the project and calling the <i>execute()</i> method.</p>
<p><i>Setting a reference to the project</i>? Could be interesting. The Project class
<p><i>Setting a reference to the project</i>? Could be interesting. The Project class
gives us some nice abilities: access to Ant's logging facilities getting and setting
gives us some nice abilities: access to Ant's logging facilities getting and setting
properties and much more. So we try to use that class:
properties and much more. So we try to use that class:</p>
<pre class="code">
<pre class="code">
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Project;
@@ -200,7 +201,7 @@ and the execution with <tt>ant</tt> will show us the expected
<pre class="output">
<pre class="output">
use:
use:
Here is project 'MyTask'.
Here is project 'MyTask'.
</pre></p>
</pre>
<a name="derivingFromTask"></a>
<a name="derivingFromTask"></a>
@@ -210,7 +211,7 @@ That class is integrated in Ant, get's the project-reference, provides documenta
fiels, provides easier access to the logging facility and (very useful) gives you
fiels, provides easier access to the logging facility and (very useful) gives you
the exact location where <i>in the buildfile</i> this task instance is used.</p>
the exact location where <i>in the buildfile</i> this task instance is used.</p>
<p>Oki-doki - let's us use some of these:
<p>Oki-doki - let's us use some of these:</p>
<pre class="code">
<pre class="code">
import org.apache.tools.ant.Task;
import org.apache.tools.ant.Task;
@@ -227,27 +228,27 @@ public class HelloWorld extends Task {
}
}
}
}
</pre>
</pre>
which gives us when running
<p>which gives us when running</p>
<pre class="output">
<pre class="output">
use:
use:
[helloworld] Here is project 'MyTask'.
[helloworld] Here is project 'MyTask'.
[helloworld] I am used in: C:\tmp\anttests\MyFirstTask\build.xml:23:
[helloworld] I am used in: C:\tmp\anttests\MyFirstTask\build.xml:23:
</pre>
</pre>
<a name="accessTaskProject">
<a name="accessTaskProject"></a>
<h2>Accessing the Task's Project</h2>
<h2>Accessing the Task's Project</h2>
<p>The parent project of your custom task may be accessed through method <code>getProject()</code>. However, do not call this from the custom task constructor, as the return value will be null. Later, when node attributes or text are set, or method <code>execute()</code> is called, the Project object is available.</p>
<p>The parent project of your custom task may be accessed through method <code>getProject()</code>. However, do not call this from the custom task constructor, as the return value will be null. Later, when node attributes or text are set, or method <code>execute()</code> is called, the Project object is available.</p>
<p>Here are two useful methods from class Project:
<p>Here are two useful methods from class Project:</p>
@@ -674,12 +675,12 @@ and <code><junitreport></code>. So we add to the buildfile:
description="Runs unit tests and creates a report"
description="Runs unit tests and creates a report"
/>
/>
...
...
</pre></p>
</pre>
<p>Back to the <i>src/HelloWorldTest.java</i>. We create a class extending
<p>Back to the <i>src/HelloWorldTest.java</i>. We create a class extending
<i>BuildFileTest</i> with String-constructor (JUnit-standard), a <i>setUp()</i>
<i>BuildFileTest</i> with String-constructor (JUnit-standard), a <i>setUp()</i>
method initializing Ant and for each testcase (targets use.*) a <i>testXX()</i>
method initializing Ant and for each testcase (targets use.*) a <i>testXX()</i>
method invoking that target.
method invoking that target.</p>
<pre class="code">
<pre class="code">
import org.apache.tools.ant.BuildFileTest;
import org.apache.tools.ant.BuildFileTest;
@@ -721,10 +722,10 @@ public class HelloWorldTest extends BuildFileTest {
assertLogContaining("Nested Element 2");
assertLogContaining("Nested Element 2");
}
}
}
}
</pre></p>
</pre>
<p>When starting <tt>ant</tt> we'll get a short message to STDOUT and
<p>When starting <tt>ant</tt> we'll get a short message to STDOUT and
a nice HTML-report.
a nice HTML-report.</p>
<pre class="output">
<pre class="output">
C:\tmp\anttests\MyFirstTask>ant
C:\tmp\anttests\MyFirstTask>ant
Buildfile: build.xml
Buildfile: build.xml
@@ -753,20 +754,20 @@ test:
BUILD SUCCESSFUL
BUILD SUCCESSFUL
Total time: 7 seconds
Total time: 7 seconds
C:\tmp\anttests\MyFirstTask>
C:\tmp\anttests\MyFirstTask>
</pre></p>
</pre>
<a name="Debugging"></a
<a name="Debugging"></a>
<h2>Debugging</h2>
<h2>Debugging</h2>
<p>Try running Ant with the flag <code>-verbose</code>. For more information, try flag <code>-debug</code>.</p>
<p>Try running Ant with the flag <code>-verbose</code>. For more information, try flag <code>-debug</code>.</p>
<p>For deeper issues, you may need to run the custom task code in a Java debugger. First, get the source for Ant and build it with debugging information.</p>
<p>For deeper issues, you may need to run the custom task code in a Java debugger. First, get the source for Ant and build it with debugging information.</p>
<p>Since Ant is a large project, it can be a little tricky to set the right breakpoints. Here are two important breakpoints for version 1.8:
<p>Since Ant is a large project, it can be a little tricky to set the right breakpoints. Here are two important breakpoints for version 1.8:</p>
<p>If you need to debug when a task attribute or the text is set, begin by debugging into method <code>execute()</code> of your custom task. Then set breakpoints in other methods. This will ensure the class byte-code has been loaded by the Java VM.</p>
<p>If you need to debug when a task attribute or the text is set, begin by debugging into method <code>execute()</code> of your custom task. Then set breakpoints in other methods. This will ensure the class byte-code has been loaded by the Java VM.</p>
Thank you for your continuous support to the Openl Qizhi Community AI Collaboration Platform. In order to protect your usage rights and ensure network security, we updated the Openl Qizhi Community AI Collaboration Platform Usage Agreement in January 2024. The updated agreement specifies that users are prohibited from using intranet penetration tools. After you click "Agree and continue", you can continue to use our services. Thank you for your cooperation and understanding.