This chapter continues from where Creating the workspace definition left us.
Using external libraries in workspace definition
Next we add downloaded classes to be used in the workspace definition.
~/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.JavaBinModule;
import org.fluentjava.iwant.api.javamodules.JavaSrcModule;
import org.fluentjava.iwant.api.model.Target;
import org.fluentjava.iwant.api.wsdef.WorkspaceModuleContext;
import org.fluentjava.iwant.api.wsdef.WorkspaceModuleProvider;
import org.fluentjava.iwant.core.download.Downloaded;
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(JavaBinModule.providing(commonsMathJar()).end(),
ctx.wsdefdefModule())
.end();
}
@Override
public String workspaceFactoryClassname() {
return "com.example.wsdef.IwanttutorialWorkspaceFactory";
}
private static Target commonsMathJar() {
final String v = "1.2";
return Downloaded.withName("commonsMathJar")
.url("http://repo1.maven.org/maven2/commons-math/commons-math/"
+ v + "/commons-math-" + v + ".jar")
.md5("5d3ce091a67e863549de4493e19df069");
}
}
Eclipse settings need to be regenerated, because we have a new dependency.
~/iwant-tutorial $ as-iwant-tutorial-developer/with/bash/iwant/side-effect/eclipse-settings/effective
(0/1 D! org.fluentjava.iwant.core.download.Downloaded commonsMathJar)
(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)
Now we can use commons-math in the workspace definition.
~/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.apache.commons.math.fraction.Fraction;
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;
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"),
arithmeticWithExtLib());
}
private static Target arithmeticWithExtLib() {
return new HelloTarget("arithmeticWithExtLib", "1/2 + 2/4 = "
+ new Fraction(1, 2).add(new Fraction(2, 4)).intValue() + "\n");
}
@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
arithmeticWithExtLib
~/iwant-tutorial $ as-iwant-tutorial-developer/with/bash/iwant/target/arithmeticWithExtLib/as-path | xargs -r cat
(0/1 D! org.fluentjava.iwant.api.core.HelloTarget arithmeticWithExtLib)
1/2 + 2/4 = 1
Output asserted