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 org.fluentjava.iwant.api.core.Concatenated;
import org.fluentjava.iwant.api.core.Concatenated.ConcatenatedBuilder;
import org.fluentjava.iwant.api.core.HelloTarget;
import org.fluentjava.iwant.api.model.SideEffect;
import org.fluentjava.iwant.api.model.Target;
import org.fluentjava.iwant.api.wsdef.SideEffectDefinitionContext;
import org.fluentjava.iwant.api.wsdef.TargetDefinitionContext;
import org.fluentjava.iwant.api.wsdef.Workspace;
import org.fluentjava.iwant.core.ant.AntGenerated;
import org.fluentjava.iwant.core.download.FromRepository;
import org.fluentjava.iwant.eclipsesettings.EclipseSettings;
public class IwanttutorialWorkspace implements Workspace {
@Override
public List<? extends Target> targets(TargetDefinitionContext ctx) {
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.repo1MavenOrg().group(antGroup)
.name("ant").version(antVersion).jar(),
FromRepository.repo1MavenOrg().group(antGroup)
.name("ant-launcher").version(antVersion).jar())
.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~ org.fluentjava.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 -r cat
(0/1 D! org.fluentjava.iwant.core.download.GnvArtifact ant-1.7.1.jar)
(0/1 D! org.fluentjava.iwant.core.download.GnvArtifact ant-launcher-1.7.1.jar)
(0/1 D! org.fluentjava.iwant.api.core.HelloTarget justATargetUsedByAntScript)
(0/1 D! org.fluentjava.iwant.api.core.Concatenated script)
(0/1 D! org.fluentjava.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