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

Using a module of the workspace in the build

~/iwant-tutorial $ $EDITOR "as-iwant-tutorial-developer/i-have/wsdefdef/src/main/java/com/example/wsdefdef/IwantTutorialWorkspaceProvider.java"
package com.example.wsdefdef;
import org.fluentjava.iwant.api.javamodules.JavaSrcModule;
import org.fluentjava.iwant.api.wsdef.WorkspaceModuleContext;
import org.fluentjava.iwant.api.wsdef.WorkspaceModuleProvider;
public class IwantTutorialWorkspaceProvider implements WorkspaceModuleProvider {
@Override
public JavaSrcModule workspaceModule(WorkspaceModuleContext ctx) {
return JavaSrcModule.with().name("iwant-tutorial-wsdef")
.locationUnderWsRoot("as-iwant-tutorial-developer/i-have/wsdef")
.mainJava("src/main/java").mainDeps(ctx.iwantApiModules())
.mainDeps(ctx.wsdefdefModule()).end();
.mainDeps(exampleUtil(), ctx.wsdefdefModule()).end();
}
@Override
public String workspaceFactoryClassname() {
return "com.example.wsdef.IwanttutorialWorkspaceFactory";
}
public static JavaSrcModule exampleUtil() {
return JavaSrcModule.with().name("example-util")
.mainJava("src/main/java").end();
}
}
~/iwant-tutorial $ mkdir -p example-util/src/main/java/com/example/util
~/iwant-tutorial $ as-iwant-tutorial-developer/with/bash/iwant/side-effect/eclipse-settings/effective
(0/1 D! org.fluentjava.iwant.api.javamodules.JavaClasses example-util-main-classes)
(0/1 S~ org.fluentjava.iwant.api.javamodules.JavaClasses iwant-tutorial-wsdefdef-main-classes)
(0/1 D~ org.fluentjava.iwant.api.javamodules.JavaClasses iwant-tutorial-wsdef-main-classes)
(0/1 D! org.fluentjava.iwant.api.core.Concatenated eclipse-settings.bin-refs)
(as-iwant-tutorial-developer/i-have/wsdef)
( .project)
( .classpath)
( .settings/org.eclipse.jdt.core.prefs)
( .settings/org.eclipse.jdt.ui.prefs)
(as-iwant-tutorial-developer/i-have/wsdefdef)
( .project)
( .classpath)
( .settings/org.eclipse.jdt.core.prefs)
( .settings/org.eclipse.jdt.ui.prefs)

We import the new module to Eclipse and create a utility.

~/iwant-tutorial $ $EDITOR "example-util/src/main/java/com/example/util/ExampleUtil.java"
package com.example.util;
public class ExampleUtil {
public static String capitalize(String s) {
return s.toUpperCase();
}
}

Now the utility is available in our build as well as being served by it.

~/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.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.eclipsesettings.EclipseSettings;
import com.example.util.ExampleUtil;
import com.example.wsdefdef.IwantTutorialWorkspaceProvider;
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"),
targetUsingModuleFromSameWs());
}
private static Target targetUsingModuleFromSameWs() {
return new HelloTarget("targetUsingModuleFromSameWs",
ExampleUtil.capitalize("String made with ExampleUtil."));
}
@Override
public List<? extends SideEffect> sideEffects(
SideEffectDefinitionContext ctx) {
return Arrays.asList(EclipseSettings.with().name("eclipse-settings")
.modules(ctx.wsdefdefJavaModule(), ctx.wsdefJavaModule())
.end());
.modules(IwantTutorialWorkspaceProvider.exampleUtil()).end());
}
}
~/iwant-tutorial $ as-iwant-tutorial-developer/with/bash/iwant/list-of/targets
(0/1 S~ org.fluentjava.iwant.api.javamodules.JavaClasses example-util-main-classes)
(0/1 S~ org.fluentjava.iwant.api.javamodules.JavaClasses iwant-tutorial-wsdef-main-classes)
hello
targetUsingModuleFromSameWs
~/iwant-tutorial $ as-iwant-tutorial-developer/with/bash/iwant/target/targetUsingModuleFromSameWs/as-path | xargs -r cat
(0/1 D! org.fluentjava.iwant.api.core.HelloTarget targetUsingModuleFromSameWs)