MavenでScalaプロジェクト

昔はarchetype:generateゴールに設定をいっぱいつけて、
archetypeCatalogをhttp://scala-tools.org/ってとこを見たりしたりで
ながーーーいコマンドを打つ必要があったみたい。

最近ではセントラルリポジトリにもscala-archetype-simpleのカタログが
入ってて、長いコマンド無しでscalaプロジェクトをはじめることができる。
http://search.maven.org/#search%7Cga%7C1%7Carchetype%20scala

では、mavenarchetype:generateでscalaプロジェクトの作成の仕方です。

「mvn archetype:generate」実行
ばばーっとセントラルリポジトリにあるカタログ一覧が現時点で603コも出てきて、
”Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): :”と表示されます。
つまり”番号選ぶ or フィルタして。”と言われているので、”scala”と打ってEnterします。
すると、現時点で、10コscala関連のカタログがヒットします。

1: remote -> com.github.igor-petruk.archetypes:maven-archetype-scala-executable (Creates executable Scala Project that is ready to run with 'java -jar')
2: remote -> de.schlichtherle:javafx-scala-demo (An archetype for a standalone JavaFX 2.0 application written in Scala.
The generated application is translated from the Colorful Circles demo from the JavaFX 2.0 SDK.)
3: remote -> org.apache.camel.archetypes:camel-archetype-scala (Creates a new Camel project using Scala DSL.)
4: remote -> org.fusesource.scalate.tooling:scalate-archetype-empty (An archetype which creates an empty Scalate web application)
5: remote -> org.fusesource.scalate.tooling:scalate-archetype-guice (An archetype which creates an empty Scalate Guice web application)
6: remote -> org.fusesource.scalate.tooling:scalate-archetype-jersey (An archetype which creates an empty Scalate web application)
7: remote -> org.fusesource.scalate.tooling:scalate-archetype-sitegen (An archetype which creates an empty Scalate static website generation project)
8: remote -> org.scala-tools.archetypes:scala-archetype-simple (The maven-scala-plugin is used for compiling/testing/running/documenting scala code in maven.)
9: remote -> org.wicketstuff.scala:wicket-scala-archetype (-)
10: remote -> org.wicketstuff.scala:wicketstuff-scala-archetype (Basic setup for a project that combines Scala and Wicket, depending on the Wicket-Scala project. Includes an example Specs test.)

あとは、一番シンプルな8番 scala-archetype-simple を選んであげればジェネレートしてくれます。
その後は普通のmvn archetype:generateと同じ。

Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): : 8
Choose org.scala-tools.archetypes:scala-archetype-simple version:
1: 1.0
2: 1.1
3: 1.2
4: 1.3
Choose a number: 4:
Define value for property 'groupId': : com.hello
Define value for property 'artifactId': : helloworld
Define value for property 'version':  1.0-SNAPSHOT: :
Define value for property 'package':  com.hello: :
Confirm properties configuration:
groupId: com.hello
artifactId: helloworld
version: 1.0-SNAPSHOT
package: com.hello
 Y: : Y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: scala-archetype-simple:1.3
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.hello
[INFO] Parameter: artifactId, Value: helloworld
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: com.hello
[INFO] Parameter: packageInPathFormat, Value: com/hello
[INFO] Parameter: package, Value: com.hello
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: groupId, Value: com.hello
[INFO] Parameter: artifactId, Value: helloworld
[INFO] project created from Archetype in dir: C:\workspace\helloworld
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9:55.996s
[INFO] Finished at: Thu Jun 14 16:53:31 JST 2012
[INFO] Final Memory: 8M/19M
[INFO] ------------------------------------------------------------------------

で、できたプロジェクトは以下のディレクトリ構成になります。

フォルダー パスの一覧
ボリューム シリアル番号は です
C:.
│  .gitignore
│  pom.xml
│
└─src
    ├─main
    │  └─scala
    │      └─com
    │          └─hello
    │                  App.scala
    │
    └─test
        └─scala
            └─samples
                    junit.scala
                    scalatest.scala
                    specs.scala

しかも良いことにpom.xmlには既にscalaの設定がされてる。
長いので一部だけ…。

  <build>
    <sourceDirectory>src/main/scala</sourceDirectory>
    <testSourceDirectory>src/test/scala</testSourceDirectory>
    <plugins>
      <plugin>
        <groupId>org.scala-tools</groupId>
        <artifactId>maven-scala-plugin</artifactId>
        <version>2.15.0</version>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>testCompile</goal>
            </goals>
            <configuration>
              <args>
                <arg>-make:transitive</arg>
                <arg>-dependencyfile</arg>
                <arg>${project.build.directory}/.scala_dependencies</arg>
              </args>
            </configuration>
          </execution>
        </executions>
      </plugin>

とかね、mavenにはscalaコンパイルとかするためにmaven-scala-pluginを利用するんだけど、
その設定(mvn compileでscalaコンパイルを動かす設定とか)が書かれてるのでかなり楽チンになった。

mvn compile とか mvn test とかで、最初からscalaを実行してくれます。

ということで、ぐぐると結構古い情報ばっかりだったので、記事にしてみました。
正直sbt(Simple Build Tool)とかgradleとかを調査してたんだけど、両方なんだかまだメンドクサイし、mavenで簡単にできないかなーと思ったら普通に簡単だったので素晴らしいですね。

まだまだmavenは使える!!