Eclipse integration and debugging of ant Projects

Source: Internet
Author: User
Integrate and debug ant projects in eclipse

Cheungmine

2012-1 1

Abstract: The benefit of using ant to build small or large Java projects is self-evident. This article shows how to integrate ant projects in eclipse and configure ant build script build. xml and. Java source code files. This problem occurs when we import a Java project built with ant into eclipse: Debug ant build. the XML script and the ant task used to debug the Java source code file.

1. quickly build the eclipse + ant environment from scratch

For simplicity, the eclipse development environment in this article uses the one-click combination Development Kit tegra Android developer pack (nvpack) provided by NVIDIA. You can download the desired version from the following URL:

Http://developer.nvidia.com/tegra-resources

I selected: tegra Android developer pack 1.0r7, which contains the following development components:

  • Android SDK R18

  • Android ndk r7c

  • JDK 6u24

  • Cygwin 1.7

  • Eclipse 3.7.1

  • CDT 8.0.0

  • ADT 15.0.0

  • Apache ant 1.8.2

  • NVIDIA debug manager for eclipse 12.0.0

  • Tegra sample code

For how to install this nvpack in Ubuntu, refer to my article:

One-click solution for installing the eclipse Android/C ++ development environment in Ubuntu

2. Create a simple ant Project

The directory structure of the example project zebra is as follows:

zebra/    -----build.xml    +----src/         +----org/              +----cheungmine/                   +----study/                        ----Main.java

The content of build. XML is as follows:

<?xml version="1.0" encoding="UTF-8"?><project name="zebra" default="compile">    <description>My first complete build file, including packaging and executing a Java program.</description>    <target name="init" description="Create the output directories">        <mkdir dir="build/classes"/>        <mkdir dir="dist"/>    </target>    <target name="compile" depends="init" description="Compiles into the output directories">        <javac srcdir="src" debug="true" destdir="build/classes" includeantruntime="false"></javac>    </target>    <target name="package" depends="compile" description="Creates the JAR package files">        <jar destfile="dist/zebra.jar" basedir="build/classes"></jar>    </target>    <target name="clean" description="Delete the output directories">        <delete dir="build"/>        <delete dir="dist"/>    </target>        <target name="exec" depends="compile" description="Running programs under Ant">        <!-- $ java -cp build/classes org.cheungmine.study.Main hello world . -->        <java classname="org.cheungmine.study.Main" classpath="build/classes">            <arg value="hello"/>            <arg value="world"/>            <!--                 the following arg uses the file attribute, which tells Ant to resolve                 that attribute to an absolute file location before calling the program.            -->            <arg file="."/>        </java>        <!--        <echo level="verbose" message="running"/>        <echo level="info" message="running"/>        <echo level="warning" message="running"/>        <echo level="error" message="running"/>        -->    </target></project>

The content of Main. Java is as follows:

package org.cheungmine.study;public class Main {    public static void main(String args[]) {        System.out.println("org.cheungmine.study");        for(int i=0; i<args.length; i++) {            System.out.println(args[i]);        }    }}


3. Import the ant project to eclipse

The above project is successfully built under ant. Import it to eclipse. Select menu:

Eclipse-> file-> New-> project...-> JAVA project from existing ant buildfile

Press [next] and select <yourpath> \ zebra \ build. XML in ant buildfile.

Press [finish]. In this way, the ant project will be imported into eclipse.

Locate the file build. XML in the package explorer of eclipse and set breakpoints on any line,

Choose eclipse> WINDOW> show View> ant to make sure that ant and outline appear in the window on the right. If no content of build. xml exists in outline, switch to the ant window and select Add buildfiles to add build. xml. The following appears in outline:Figure 1:

Figure 1Debug build. XML in eclipse

Select outline-> Exec-> right-click and choose debug as-> ant build. The breakpoint on the left is debugged and executed. This is only to debug the ant build. xml file. The following method is used to directly debug Java code. In this example, Main. Java is used.


4. Use the ant build file as the project Builder

Java builder is used by default when eclipse Java IDE is used. Every time we save files, Java builder will run silently in the background and compile these files immediately. It is one of the major features of Eclipse: Java builder allows us to skip the compilation process completely, because our program is always in the compiled State, even if it is full of errors. Therefore, we can run the Java program immediately after typing, without first passing through a long and tedious compilation process. This feature saves eclipse users a lot of time and debate, and is also one of the reasons why eclipse is widely used in programmers.

But what if we want to compile files? What if we want to create a jar file for the entire project and copy the file to a specific directory every time we modify the project? If we want all of this to happen in the background, don't tell eclipse every time? We can sit down, relax, write some code, and have a cup of coffee, so that eclipse can process complicated building processes in the background, even without having to know that this process is in progress in the background.

Does it sound like a dream? Not dreaming. We can really do this. We only need to add an ant build file to our project as a builder, which defines all the complexity of the build process. After doing so, the magic will begin.

Right-click the project in the package explorer view and click Properties (or eclipse menu> Project> properties). Expand builders and click New to add a new builder myantbuilder to the project. The added image is as follows:Figure 2:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.