You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by none ofyourbusiness <mv...@hotmail.com> on 2006/01/23 15:24:00 UTC

Creating a task for getting the current target in a project

hi,

In our ant script we do an antcall at a certain moment. This antcall call's 
another xml ant script with a specific target. This called xml ant script 
contains a project with mulitple targets, and outside those targets, it also 
checks for the availability of certain 3th party jars and puts there path in 
properties.

This all works very well for most targets (fe: build, compile, pre-compile, 
etc), but for some targets (fe package and clean) these 3th party jars are 
not required. This causes these 2 targets to fail...

Thats why we would like to make these dependancies optional for these 2 
targets. We would like check for the availibilty of these jars only if the 
target is something else then "clean" or "package"

so first i looked for a system property that gives the current target, but 
that doesnt seem to exist. After this i tried making a task for this myself, 
but i couldnt get it working...

I hope someone of you could help me with this...

I tried using following methods:

getOwningTarget
--> works, but only when the task is called in the target itself
since we need it outside the target, this method is useless

getCurrentTarget
--> allways gives back value null

getImplicitTarget
--> result isnt null, but a toString() of the target results gives ""

So i didnt achieve much... :/

below you can find a copy of the source that ive been playing around with:

package com.eds.ant.taskdefs;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.helper.AntXMLContext;

public class CurrentTarget extends Task {

	private String addproperty = null;

    /**
     * Defines the name of a property to be created from input. Behaviour is
     * according to property task which means that existing properties
     * cannot be overridden.
     *
     * @param addproperty Name for the property to be created from input
     */
    public void setAddproperty(String addproperty) {
        this.addproperty = addproperty;
    }

    /**
     * Actual method executed by ant.
     *
     * @throws BuildException on error
     */
    public void execute() throws BuildException {
		if (addproperty == null || "".equals(addproperty.trim()))
			throw new BuildException("Property 'addproperty' was not set.");
		Project project = getProject();
		System.out.println(project.toString());
		AntXMLContext context = (AntXMLContext) 
project.getReference("ant.parsing.context");
		context = new AntXMLContext(project);
		System.out.println(context.toString());
		org.apache.tools.ant.Target target1 = context.getImplicitTarget();
		org.apache.tools.ant.Target target2 = context.getCurrentTarget();
		org.apache.tools.ant.Target target3 = getOwningTarget();
		String value = null;
		if (target1 != null) {
			value = target1.toString();
			System.out.println("getimptar isnt empty " + value);
		} else {
			System.out.println("getimptar is empty");
		}
		if (target2 != null) {
			value = target2.toString();
			System.out.println("getcurtar isnt empty " + value);
		} else {
			System.out.println("getcurtar is empty");
		}
		if (target3 != null) {
			value = target3.toString();
			System.out.println("getowntar isnt empty " + value);
		} else {
			System.out.println("getowntar is empty");
		}

		System.out.println(value);
        if (addproperty != null && value != null) {
            getProject().setNewProperty(addproperty, value);
        }
    }

}

_________________________________________________________________
Bescherm je Inbox: Phishing - hoe te herkennen, rapporteren en voorkomen    
http://www.msn.be/security/phishing/


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: Creating a task for getting the current target in a project

Posted by Stefan Bodewig <bo...@apache.org>.
On Mon, 23 Jan 2006, none ofyourbusiness <mv...@hotmail.com> wrote:

> Thats why we would like to make these dependancies optional for
> these 2 targets. We would like check for the availibilty of these
> jars only if the target is something else then "clean" or "package"

<target name="mark-optional">
  <property name="optional!" value="t"/>
</target>

<target name="check-third-party" unless="optional!">
  .. check for jar and fail if not available ..
</target>

<target name="compile" depends="check-third-party">
   ...
</target>

<target name="package" depends="mark-optional, compile">
  ...
</target>

> so first i looked for a system property that gives the current
> target, but that doesnt seem to exist. After this i tried making a
> task for this myself, but i couldnt get it working...

And you don't really have a chance to, only the project instance knows
which targets it is going to execute and it doesn't expose the
information.

Stefan

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org