<< Using ant cli instead of bash | ^ Site index ^ | ^ Tutorial index ^ | Using a script/program define target content >>

This chapter continues from where Creating the workspace definition left us.

Using ant to define target content

Let's write another target using ant, downloaded from ibiblio.org. The ant script refers to another target.

~/iwant-tutorial $ $EDITOR "as-iwant-tutorial-developer/i-have/wsdef/src/main/java/com/example/wsdef/IwanttutorialWorkspace.java"
package com.example.wsdef;
import java.util.Arrays;
import java.util.List;
import net.sf.iwant.api.core.Concatenated;
import net.sf.iwant.api.core.Concatenated.ConcatenatedBuilder;
import net.sf.iwant.api.core.HelloTarget;
import net.sf.iwant.api.model.SideEffect;
import net.sf.iwant.api.model.Target;
import net.sf.iwant.api.wsdef.IwantWorkspace;
import net.sf.iwant.api.wsdef.SideEffectDefinitionContext;
import net.sf.iwant.core.ant.AntGenerated;
import net.sf.iwant.core.download.FromRepository;
import net.sf.iwant.eclipsesettings.EclipseSettings;
public class IwanttutorialWorkspace implements IwantWorkspace {
@Override
public List<? extends Target> targets() {
return Arrays.asList(new HelloTarget("hello", "hello from iwant\n"));
return Arrays.asList(new HelloTarget("hello", "hello from iwant\n"),
antGenerated());
}
private static HelloTarget justATargetUsedByAntScript() {
return new HelloTarget("justATargetUsedByAntScript",
"content of a target");
}
private static Target antScript() {
ConcatenatedBuilder xml = Concatenated.named("script");
xml.string("<project name='hello' default='hello'>\n");
xml.string(" <target name='hello'>\n");
xml.string(" <echo message='Refreshing ${iwant-outfile}'/>\n");
xml.string(" <copy file='")
.nativePathTo(justATargetUsedByAntScript())
.string("' tofile='${iwant-outfile}'/>\n");
xml.string(" <echo file='${iwant-outfile}' append='true'"
+ " message=' appended by ant.'/>\n");
xml.string(" </target>\n");
xml.string("</project>\n");
return xml.end();
}
private static Target antGenerated() {
final String antGroup = "org/apache/ant";
final String antVersion = "1.7.1";
return AntGenerated
.with()
.name("antGenerated")
.antJars(
FromRepository.ibiblio().group(antGroup).name("ant")
.version(antVersion),
FromRepository.ibiblio().group(antGroup)
.name("ant-launcher").version(antVersion))
.script(antScript()).end();
}
@Override
public List<? extends SideEffect> sideEffects(
SideEffectDefinitionContext ctx) {
return Arrays
.asList(EclipseSettings
.with()
.name("eclipse-settings")
.modules(ctx.wsdefdefJavaModule(),
ctx.wsdefJavaModule()).end());
}
}
~/iwant-tutorial $ as-iwant-tutorial-developer/with/bash/iwant/list-of/targets
(0/1 S~ net.sf.iwant.api.javamodules.JavaClasses iwant-tutorial-wsdef-main-classes)
hello
antGenerated
~/iwant-tutorial $ as-iwant-tutorial-developer/with/bash/iwant/target/antGenerated/as-path | xargs cat
(0/1 D! net.sf.iwant.core.download.Downloaded ant-1.7.1.jar)
(0/1 D! net.sf.iwant.core.download.Downloaded ant-launcher-1.7.1.jar)
(0/1 D! net.sf.iwant.api.core.HelloTarget justATargetUsedByAntScript)
(0/1 D! net.sf.iwant.api.core.Concatenated script)
(0/1 D! net.sf.iwant.core.ant.AntGenerated antGenerated)
Buildfile: /home/hacker/iwant-tutorial/as-iwant-tutorial-developer/.i-cached/target/script

hello:
[echo] Refreshing /home/hacker/iwant-tutorial/as-iwant-tutorial-developer/.i-cached/target/antGenerated
[copy] Copying 1 file to /home/hacker/iwant-tutorial/as-iwant-tutorial-developer/.i-cached/target

BUILD SUCCESSFUL
Total time: 0 seconds
content of a target appended by ant.
<< Using ant cli instead of bash | ^ Site index ^ | ^ Tutorial index ^ | Using a script/program define target content >>