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.WorkspaceModuleContext;
import net.sf.iwant.api.wsdef.WorkspaceModuleProvider;
import net.sf.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! 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 org.apache.commons.math.fraction.Fraction;
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.SideEffectDefinitionContext;
import net.sf.iwant.api.wsdef.TargetDefinitionContext;
import net.sf.iwant.api.wsdef.Workspace;
import net.sf.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~ 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 -r cat 
(0/1 D! net.sf.iwant.api.core.HelloTarget arithmeticWithExtLib)
1/2 + 2/4 = 1
Output asserted