Friday, January 4, 2008
Posted by David BernardI'll try to provide a mini-guide about maven for scala's project. For more info, go to maven home or plugin's home.
Maven is a builder like make or ant, written in java. It's a commande line tool, IDE (Eclipse, Netbeans, IDEA) have plugins to handle and integrate project powered by maven. It could be used to create lib (jar), webapps (ear) and any other type of "artifact". It prefers convention over configuration, and configuration over instruction. What that mean exactly ?
Before creating your first scala project, You need to know some info:
mvn help
In the following, we will run maven for the first time. Maven download what it need to work from remote repositories, and cache the downloaded artifact into its local repository (default is $HOME/.m2/repository
). It only download what it need for the requested phases/goals (lazy downloading). So the first runs could be very long.
$HOME/bin/soft-linux/jdk-1.5.0_03
)$HOME/bin/soft-java/apache-maven-2.0.8
)
mvn help
, you should see
usage: mvn [options] [<goal(s)>] [<phase(s)>] Options: -q,--quiet Quiet output - only show errors ...
You could create a project skeleton with your favorite file system tools (following directory layout as below) or you could use archetypes. Maven Archetypes are project'skeleton that could be used to create new project.
mvn org.apache.maven.plugins:maven-archetype-plugin:1.0-alpha-7:create \ -DarchetypeGroupId=org.scala-tools.archetypes \ -DarchetypeArtifactId=scala-archetype-simple \ -DarchetypeVersion=1.1 \ -DremoteRepositories=http://scala-tools.org/repo-releases \ -DgroupId=your.proj.gid -DartifactId=your-proj-id
At the end of the process you should see something like
... [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1 second [INFO] Finished at: Sat Jan 05 17:39:47 CET 2008 [INFO] Final Memory: 6M/63M [INFO] ------------------------------------------------------------------------
!! Success you now have a empty project under your-proj-id directory with the following directory layout :
your-proj-id/ |-- pom.xml `-- src |-- main | `-- scala | `-- your | `-- proj | `-- gid | `-- App.scala `-- test `-- scala `-- your `-- proj `-- gid `-- AppTest.scala
In fact, the project is not empty it contains an helloworld application (App.scala) and a JUnit test (AppTest.scala).
In the next step, you will request phase (or goals). The results will be put under your-proj-id/target directory. The target directory is the working directory where every plugin put the result of computation. If you want to clean up, request the goal "clean"
mvn clean
# only compile mvn compile
If it's the first time you use maven with scala, the build should failed with a message like
... [ERROR] FATAL ERROR [INFO] ------------------------------------------------------------------------ [INFO] The PluginDescriptor for the plugin Plugin [org.scala-tools:maven-scala-plugin] was not found. [INFO] ...
I prefer the second solution (in this case):
# only compile mvn -U compile
now you should see
... [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ ...
The skeleton create a JUnit test AppTest.scala as sample, try to compile and run it
# compile + compile test + run test mvn test
you should get :
... ------------------------------------------------------- T E S T S ------------------------------------------------------- Running your.proj.gid.AppTest Tests run: 2, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.054 sec <<< FAILURE! Results : Failed tests: testKO(your.proj.gid.AppTest) Tests run: 2, Failures: 1, Errors: 0, Skipped: 0 [INFO] ------------------------------------------------------------------------ [ERROR] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] There are test failures. Please refer to /home/dwayne/tmp/your-proj-id/target/surefire-reports for the individual test results.
BUILD FAILURE, it's not good! So read the log on console :
So you could read the problem in .../your-proj-id/target/surefire-reports/your.proj.gid.AppTest.txt
... testKO(your.proj.gid.AppTest) Time elapsed: 0.01 sec <<< FAILURE! junit.framework.AssertionFailedError at junit.framework.Assert.fail(Assert.java:47) at junit.framework.Assert.assertTrue(Assert.java:20) at junit.framework.Assert.assertTrue(Assert.java:27) at your.proj.gid.AppTest.testKO(AppTest.scala:26) at your.proj.gid.AppTest.testKO(AppTest.scala:26) ...
So edit the test and fix it (it's easy), and rerun test until it pass. Why the empty project is created with a failed test? to check that test are running and are used.
# compile + run test + generate the jar mvn package
If you fixed the test in Step 3, then a jar should be generated under the target directory. The jar doesn't contains the test classes, only the classes from src/main/scala/...
I expect this overlook and quick tutorial could help you to start playing with maven and scala. I plan to write other articles about "maven for scala" (about, the pom.xml and repositories). If you want to know more about maven and don't want to wai futur article, I suggest you browse the documentation of maven. I also suggest you to take a look at the maven-scala-plugin 2.x documentation, you'll see how to generate scaladoc, choose the scala version, or run a scala console with the project dependencies.
If you have question ask, If you want I detail some point here or in a futur article, ask.