<< Using an iwant plugin (for untarring) | ^ Site index ^ | ^ Tutorial index ^ | Defining Java modules >>

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 net.sf.iwant.api.javamodules.JavaBinModule;
import net.sf.iwant.api.javamodules.JavaSrcModule;
import net.sf.iwant.api.model.Target;
import net.sf.iwant.api.wsdef.IwantWorkspaceProvider;
import net.sf.iwant.api.wsdef.WorkspaceDefinitionContext;
import net.sf.iwant.core.download.Downloaded;
public class IwantTutorialWorkspaceProvider implements IwantWorkspaceProvider {
@Override
public JavaSrcModule workspaceModule(WorkspaceDefinitionContext ctx) {
return JavaSrcModule.with().name("iwant-tutorial-wsdef")
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();
.mainJava("src/main/java")
.mainDeps(ctx.iwantApiModules())
.mainDeps(JavaBinModule.providing(commonsMathJar()).end(),
ctx.wsdefdefModule()).end();
}
@Override
public String workspaceClassname() {
return "com.example.wsdef.IwanttutorialWorkspace";
}
private static Target commonsMathJar() {
final String v = "1.2";
return Downloaded
.withName("commonsMathJar")
.url("http://mirrors.ibiblio.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! net.sf.iwant.core.download.Downloaded commonsMathJar)
(0/1 S~ net.sf.iwant.api.javamodules.JavaClasses iwant-tutorial-wsdefdef-main-classes)
(0/1 D~ net.sf.iwant.api.javamodules.JavaClasses iwant-tutorial-wsdef-main-classes)
(0/1 D! net.sf.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 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.eclipsesettings.EclipseSettings;
import org.apache.commons.math.fraction.Fraction;
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"),
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~ net.sf.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 cat
(0/1 D! net.sf.iwant.api.core.HelloTarget arithmeticWithExtLib)
1/2 + 2/4 = 1
Output asserted
<< Using an iwant plugin (for untarring) | ^ Site index ^ | ^ Tutorial index ^ | Defining Java modules >>