You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by Stefan Bodewig <bo...@apache.org> on 2008/10/30 17:55:53 UTC

JavaFront update: TagBuilder

just committed what could be the initial version for a fluent builder:

$ cat src/etc/examples/Simple3.java
package org.example;

import org.apache.ant.javafront.annotations.AntProject;
import org.apache.ant.javafront.annotations.AntTarget;
import org.apache.tools.ant.Project;
import org.apache.ant.javafront.builder.TagBuilder;

@AntProject(Name="simple3", BaseDir="..", DefaultTarget="hello")
public class Simple3 {
    private Project p;

    public void setProject(Project p) {
        this.p = p;
    }

    @AntTarget(Name="-setup")
    public void setup() {
        TagBuilder.forProject(p)
            .newProperty().withName("world").andValue("world").execute();
    }

    @AntTarget(Depends="-setup")
    public void hello() {
        TagBuilder.forProject(p)
            .newTag("echo")
            .withAttribute("message", "Hello, ${world}!")
            .execute();
    }
}
$ ant -lib build/lib/ant-javafront-0.1.jar -f src/etc/examples/Simple3.java
Buildfile: /home/stefan/dev/ASF/ant-javafront/src/etc/examples/Simple3.java

-setup:
ProjectHelper class org.apache.ant.javafront.helper.JavaFrontHelper can't parse Antlib descriptors, falling back to ProjectHelper2.

hello:
     [echo] Hello, world!

BUILD SUCCESSFUL
Total time: 0 seconds
$ ant -lib build/lib/ant-javafront-0.1.jar -f src/etc/examples/Simple3.java -Dworld=Welt
Buildfile: /home/stefan/dev/ASF/ant-javafront/src/etc/examples/Simple3.java

-setup:
ProjectHelper class org.apache.ant.javafront.helper.JavaFrontHelper can't parse Antlib descriptors, falling back to ProjectHelper2.

hello:
     [echo] Hello, Welt!

BUILD SUCCESSFUL
Total time: 0 seconds

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


Re: JavaFront update: TagBuilder

Posted by Gilles Scokart <gs...@gmail.com>.
2008/10/31 Stefan Bodewig <bo...@apache.org>:
> On Fri, 31 Oct 2008, Gilles Scokart <gs...@gmail.com> wrote:
>
>> 2008/10/30 Stefan Bodewig <bo...@apache.org>:
>>>    public void hello() {
>>>        TagBuilder.forProject(p)
>>>            .newTag("echo")
>>>            .withAttribute("message", "Hello, ${world}!")
>>>            .execute();
>>>    }
>>
>> Some alternatives  :
>>
>> public void hello() {
>>       EchoTask echo = TagBuilder.create(EchoTask.class , p);
>>       echo.setMessage("Hello, ${world}!");
>>       echo.execute();
>> }
>
> TagBuilder uses UnknownElement which means it can resolve task
> implementations, typedefs and so on.  Your example wouldn't resolve
> properties, BTW.

The idea was not to instanciate and execute the Task directly.
The idea was to produce a "builder" object with a more strongly typed API.

The echo object created can actually be a recorder object that fill an
UnknownElement.  The fact that this recorder can be declared as an
EchoTask is just a way to provide stronger typing.

This is the kind of technique is used by jmock or easymock.

(Note that's more complexe in my second example because there you have
to execute your project into a specific ClassLoader where all tasks
class are transformed into a recorder.)

>
> In current svn it reads
>
> EchoBuilder.echoMessage(p, "Hello, ${world}!");

This is obviously shorter :-)

-- 
Gilles Scokart

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


Re: JavaFront update: TagBuilder

Posted by Stefan Bodewig <bo...@apache.org>.
On Fri, 31 Oct 2008, Gilles Scokart <gs...@gmail.com> wrote:

> 2008/10/30 Stefan Bodewig <bo...@apache.org>:
>>    public void hello() {
>>        TagBuilder.forProject(p)
>>            .newTag("echo")
>>            .withAttribute("message", "Hello, ${world}!")
>>            .execute();
>>    }
> 
> Some alternatives  :
> 
> public void hello() {
>       EchoTask echo = TagBuilder.create(EchoTask.class , p);
>       echo.setMessage("Hello, ${world}!");
>       echo.execute();
> }

TagBuilder uses UnknownElement which means it can resolve task
implementations, typedefs and so on.  Your example wouldn't resolve
properties, BTW.

In current svn it reads

EchoBuilder.echoMessage(p, "Hello, ${world}!");

Stefan

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


Re: JavaFront update: TagBuilder

Posted by Gilles Scokart <gs...@gmail.com>.
2008/10/30 Stefan Bodewig <bo...@apache.org>:
>    public void hello() {
>        TagBuilder.forProject(p)
>            .newTag("echo")
>            .withAttribute("message", "Hello, ${world}!")
>            .execute();
>    }

Some alternatives  :

public void hello() {
      EchoTask echo = TagBuilder.create(EchoTask.class , p);
      echo.setMessage("Hello, ${world}!");
      echo.execute();
}

Or:

public void hello() {
    TagBuilder.execute(new EchoTask() {{
          setMessage("Hello, ${world}!");
    }});
}




-- 
Gilles Scokart

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