<< Using external libraries in workspace definition | ^ Site index ^ | ^ Tutorial index ^ | Test coverage report using jacoco >>

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

Defining Java modules

First we define one java source module with main and test java and test resources and dependency to the binary module junit.

We also add the module to the eclipse-settings side-effect, and also define a target that lists the java class directory artifacts of the module.

~/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.Concatenated;
import net.sf.iwant.api.core.HelloTarget;
import net.sf.iwant.api.javamodules.JavaBinModule;
import net.sf.iwant.api.javamodules.JavaModule;
import net.sf.iwant.api.javamodules.JavaSrcModule;
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.core.download.TestedIwantDependencies;
import net.sf.iwant.eclipsesettings.EclipseSettings;
public class IwanttutorialWorkspace implements IwantWorkspace {
private final JavaModule junit = JavaBinModule.providing(
TestedIwantDependencies.junit()).end();
private final JavaSrcModule exampleHello = JavaSrcModule.with()
.name("example-hello").mavenLayout().noMainResources()
.testDeps(junit).end();
@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"),
exampleHelloClasses());
}
private Target exampleHelloClasses() {
return Concatenated.named("hello-classes")
.unixPathTo(exampleHello.mainArtifact()).string("\n")
.unixPathTo(exampleHello.testArtifact()).string("\n").end();
}
@Override
public List<? extends SideEffect> sideEffects(
SideEffectDefinitionContext ctx) {
return Arrays
.asList(EclipseSettings
.with()
.name("eclipse-settings")
.modules(ctx.wsdefdefJavaModule(),
ctx.wsdefJavaModule()).end());
return Arrays.asList(EclipseSettings
.with()
.name("eclipse-settings")
.modules(ctx.wsdefdefJavaModule(), ctx.wsdefJavaModule(),
exampleHello).end());
}
}

Then we generate Eclipse settings for it so we can use Eclipse for writing the code.

~/iwant-tutorial $ as-iwant-tutorial-developer/with/bash/iwant/side-effect/eclipse-settings/effective
(0/1 S~ net.sf.iwant.api.javamodules.JavaClasses iwant-tutorial-wsdef-main-classes)
(0/1 D! net.sf.iwant.core.download.Downloaded junit-4.8.2.jar)
(0/1 D! net.sf.iwant.api.core.Concatenated eclipse-settings.bin-refs)
(example-hello)
( .project)
( .classpath)
( .settings/org.eclipse.jdt.core.prefs)
( .settings/org.eclipse.jdt.ui.prefs)
(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 import the project to Eclipse and write some code.

~/iwant-tutorial $ $EDITOR "example-hello/src/test/java/com/example/hello/HelloMainTest.java"
package com.example.hello;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class HelloMainTest {
@Test
public void greetingUsesGivenTarget() {
assertEquals("Hello world", HelloMain.greetingTo("world"));
assertEquals("Hello Finland", HelloMain.greetingTo("Finland"));
}
}
~/iwant-tutorial $ $EDITOR "example-hello/src/main/java/com/example/hello/HelloMain.java"
package com.example.hello;
public class HelloMain {
public static void main(String[] args) {
System.out.println(greetingTo(args[0]));
}
public static String greetingTo(String target) {
return "Hello " + target;
}
}

Finally, we can build and run some classes.

~/iwant-tutorial $ as-iwant-tutorial-developer/with/bash/iwant/target/hello-classes/as-path
(0/1 D! net.sf.iwant.api.javamodules.JavaClasses example-hello-main-classes)
(0/1 D! net.sf.iwant.api.javamodules.JavaClasses example-hello-test-classes)
(0/1 D! net.sf.iwant.api.core.Concatenated hello-classes)
/home/hacker/iwant-tutorial/as-iwant-tutorial-developer/.i-cached/target/hello-classes
~/iwant-tutorial $ as-iwant-tutorial-developer/with/bash/iwant/target/hello-classes/as-path | xargs cat
/home/hacker/iwant-tutorial/as-iwant-tutorial-developer/.i-cached/target/example-hello-main-classes
/home/hacker/iwant-tutorial/as-iwant-tutorial-developer/.i-cached/target/example-hello-test-classes
~/iwant-tutorial $ java -cp as-iwant-tutorial-developer/.i-cached/target/example-hello-main-classes com.example.hello.HelloMain tutorial
Hello tutorial
Output asserted

Next we will use an optional convenience class for easier definition of modules.

First we'll enable the needed plugin and refresh eclipse settings.

~/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.JavaSrcModule;
import net.sf.iwant.api.wsdef.IwantWorkspaceProvider;
import net.sf.iwant.api.wsdef.WorkspaceDefinitionContext;
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();
.mainDeps(ctx.wsdefdefModule())
.mainDeps(ctx.iwantPlugin().javamodules().withDependencies())
.end();
}
@Override
public String workspaceClassname() {
return "com.example.wsdef.IwanttutorialWorkspace";
}
}
~/iwant-tutorial $ as-iwant-tutorial-developer/with/bash/iwant/side-effect/eclipse-settings/effective
(0/1 S~ net.sf.iwant.api.javamodules.JavaClasses iwant-tutorial-wsdefdef-main-classes)
(0/1 D! net.sf.iwant.core.download.SvnExported iwant-plugin-javamodules-main-java)
svn exporting (may take a while) https://svn.code.sf.net/p/iwant/code/trunk/optional/iwant-plugin-javamodules/src/main/java@796
(0/1 D! net.sf.iwant.core.download.SvnExported iwant-plugin-ant-main-java)
svn exporting (may take a while) https://svn.code.sf.net/p/iwant/code/trunk/optional/iwant-plugin-ant/src/main/java@796
(0/1 D! net.sf.iwant.core.download.Downloaded ant-1.9.4.jar)
(0/1 D! net.sf.iwant.core.download.Downloaded ant-launcher-1.9.4.jar)
(0/1 D! net.sf.iwant.api.javamodules.JavaClasses iwant-plugin-ant)
(0/1 D! net.sf.iwant.api.javamodules.JavaClasses iwant-plugin-javamodules)
(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)
(example-hello)
( .project)
( .classpath)
( .settings/org.eclipse.jdt.core.prefs)
( .settings/org.eclipse.jdt.ui.prefs)
(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 will use the class JavaModule to define more modules and utilize some convenience functionality of 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.io.File;
import java.util.Arrays;
import java.util.List;
import net.sf.iwant.api.core.Concatenated;
import net.sf.iwant.api.core.Concatenated.ConcatenatedBuilder;
import net.sf.iwant.api.core.HelloTarget;
import net.sf.iwant.api.javamodules.JavaBinModule;
import net.sf.iwant.api.javamodules.JavaModule;
import net.sf.iwant.api.javamodules.JavaSrcModule;
import net.sf.iwant.api.javamodules.JavaSrcModule.IwantSrcModuleSpex;
import net.sf.iwant.api.model.Path;
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.core.download.TestedIwantDependencies;
import net.sf.iwant.eclipsesettings.EclipseSettings;
import net.sf.iwant.plugin.javamodules.JavaModules;
public class IwanttutorialWorkspace implements IwantWorkspace {
private final JavaModule junit = JavaBinModule.providing(
TestedIwantDependencies.junit()).end();
private final JavaSrcModule exampleHello = JavaSrcModule.with()
.name("example-hello").mavenLayout().noMainResources()
.testDeps(junit).end();
class ExampleModules extends JavaModules {
@Override
protected IwantSrcModuleSpex commonSettings(IwantSrcModuleSpex m) {
return super.commonSettings(m).testDeps(junit);
}
final JavaBinModule hamcrestCore = binModule("org/hamcrest",
"hamcrest-core", "1.3");
final JavaBinModule junit = binModule("junit", "junit", "4.11",
hamcrestCore);
final JavaSrcModule helloUtil = srcModule("example-helloutil")
.noMainResources().end();
final JavaSrcModule hello = srcModule("example-hello")
.noMainResources().mainDeps(helloUtil).end();
}
private final ExampleModules modules = new ExampleModules();
@Override
public List<? extends Target> targets() {
return Arrays.asList(new HelloTarget("hello", "hello from iwant\n"),
exampleHelloClasses());
classpathStringOfAll());
}
private Target exampleHelloClasses() {
return Concatenated.named("hello-classes")
.unixPathTo(exampleHello.mainArtifact()).string("\n")
.unixPathTo(exampleHello.testArtifact()).string("\n").end();
private Target classpathStringOfAll() {
ConcatenatedBuilder cp = Concatenated.named("all-as-cp");
cp.string(".");
for (Path jar : JavaModules.mainArtifactJarsOf(modules.allSrcModules())) {
cp.string(File.pathSeparator).nativePathTo(jar);
}
return cp.end();
}
@Override
public List<? extends SideEffect> sideEffects(
SideEffectDefinitionContext ctx) {
return Arrays.asList(EclipseSettings
.with()
.name("eclipse-settings")
.modules(ctx.wsdefdefJavaModule(), ctx.wsdefJavaModule(),
exampleHello).end());
return Arrays.asList(EclipseSettings.with().name("eclipse-settings")
.modules(ctx.wsdefdefJavaModule(), ctx.wsdefJavaModule())
.modules(modules.allSrcModules()).end());
}
}

We write some code to the new module

~/iwant-tutorial $ as-iwant-tutorial-developer/with/bash/iwant/side-effect/eclipse-settings/effective
(0/1 S~ net.sf.iwant.api.javamodules.JavaClasses iwant-tutorial-wsdef-main-classes)
(0/1 D! net.sf.iwant.core.download.Downloaded junit-4.11.jar)
(0/1 D! net.sf.iwant.core.download.Downloaded hamcrest-core-1.3.jar)
(0/1 D~ net.sf.iwant.api.core.Concatenated eclipse-settings.bin-refs)
(example-hello)
( .project)
( .classpath)
( .settings/org.eclipse.jdt.core.prefs)
( .settings/org.eclipse.jdt.ui.prefs)
(example-helloutil)
( .project)
( .classpath)
( .settings/org.eclipse.jdt.core.prefs)
( .settings/org.eclipse.jdt.ui.prefs)
(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)
~/iwant-tutorial $ $EDITOR "example-helloutil/src/test/java/com/example/helloutil/HelloUtilTest.java"
package com.example.helloutil;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class HelloUtilTest {
@Test
public void spaceSeparatedStrings() {
assertEquals("Hello world",
HelloUtil.spaceSeparatedWords("Hello", "world"));
assertEquals("w0 w1", HelloUtil.spaceSeparatedWords("w0", "w1"));
}
}
~/iwant-tutorial $ $EDITOR "example-helloutil/src/main/java/com/example/helloutil/HelloUtil.java"
package com.example.helloutil;
public class HelloUtil {
public static String spaceSeparatedWords(String word1, String word2) {
return word1 + " " + word2;
}
}
~/iwant-tutorial $ $EDITOR "example-hello/src/main/java/com/example/hello/HelloMain.java"
package com.example.hello;
import com.example.helloutil.HelloUtil;
public class HelloMain {
public static void main(String[] args) {
System.out.println(greetingTo(args[0]));
}
public static String greetingTo(String target) {
return "Hello " + target;
return HelloUtil.spaceSeparatedWords("Hello", target);
}
}

Now we can get all our classes as a classpath string.

~/iwant-tutorial $ as-iwant-tutorial-developer/with/bash/iwant/target/all-as-cp/as-path | xargs cat
(0/1 D! net.sf.iwant.api.javamodules.JavaClasses example-helloutil-main-classes)
(0/1 D~ net.sf.iwant.api.javamodules.JavaClasses example-hello-main-classes)
(0/1 D! net.sf.iwant.plugin.ant.Jar example-hello.jar)
(0/1 D! net.sf.iwant.plugin.ant.Jar example-helloutil.jar)
(0/1 D! net.sf.iwant.api.core.Concatenated all-as-cp)
.:/home/hacker/iwant-tutorial/as-iwant-tutorial-developer/.i-cached/target/example-hello.jar:/home/hacker/iwant-tutorial/as-iwant-tutorial-developer/.i-cached/target/example-helloutil.jar

Let's use the classpath to run the application again.

~/iwant-tutorial $ java -cp $(as-iwant-tutorial-developer/with/bash/iwant/target/all-as-cp/as-path | xargs cat) com.example.hello.HelloMain "same tutorial"
Hello same tutorial
Output asserted
<< Using external libraries in workspace definition | ^ Site index ^ | ^ Tutorial index ^ | Test coverage report using jacoco >>