ぺーぺーSEのブログ

備忘録・メモ用サイト。

MavenユーザのためのScalaの始め方

GatlingってScalaなのね、、、ってきっかけで調べてみた。

Mavenプロジェクト作ってEclipseで編集できるようになるまで

以下の(1)~(5)を実施。

(1)mvn archetype:generateで「net.alchim31.maven:scala-archetype-simple」を選ぶ。

mvn archetype:generate -B ^
    -DarchetypeGroupId=net.alchim31.maven ^
    -DarchetypeArtifactId=scala-archetype-simple ^
    -DarchetypeVersion=1.6 ^
    -DgroupId=org.sample ^
    -DartifactId=ScalaMavenSample ^
    -Dversion=1.0-SNAPSHOT ^
    -Dpackage=org.sample

(2)pom.xmlを修正する。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.sample</groupId>
  <artifactId>ScalaMavenSample</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>${project.artifactId}</name>
  <description>My wonderfull scala app</description>
  <inceptionYear>2015</inceptionYear>
  <licenses>
    <license>
      <name>My License</name>
      <url>http://....</url>
      <distribution>repo</distribution>
    </license>
  </licenses>

  <properties>
    <maven.compiler.source>1.6</maven.compiler.source>
    <maven.compiler.target>1.6</maven.compiler.target>
    <encoding>UTF-8</encoding>
    <scala.version>2.11.5</scala.version>
    <scala.compat.version>2.11</scala.compat.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.scala-lang</groupId>
      <artifactId>scala-library</artifactId>
      <version>${scala.version}</version>
    </dependency>

    <!-- Test -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.specs2</groupId>
      <artifactId>specs2-core_${scala.compat.version}</artifactId>
      <version>2.4.16</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.scalatest</groupId>
      <artifactId>scalatest_${scala.compat.version}</artifactId>
      <version>2.2.4</version>
      <scope>test</scope>
    </dependency>
    <!-- 一:ここから追加 -->
    <dependency>
      <groupId>org.specs2</groupId>
      <artifactId>specs2-junit_${scala.compat.version}</artifactId>
      <version>2.4.16</version>
      <scope>test</scope>
    </dependency>
    <!-- 一:ここまで追加 -->
  </dependencies>

  <build>
    <sourceDirectory>src/main/scala</sourceDirectory>
    <testSourceDirectory>src/test/scala</testSourceDirectory>
    <plugins>
      <plugin>
        <!-- see http://davidb.github.com/scala-maven-plugin -->
        <groupId>net.alchim31.maven</groupId>
        <artifactId>scala-maven-plugin</artifactId>
        <version>3.2.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>
        <!-- 三:ここから追加 -->
        <configuration>
          <launchers>
            <launcher>
              <id>hello</id>
              <mainClass>org.sample.App</mainClass>
              <args>
                <arg>${basedir}</arg>
              </args>
            </launcher>
          </launchers>
        </configuration>
        <!-- 三:ここまで追加 -->
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.18.1</version>
        <configuration>
          <useFile>false</useFile>
          <disableXmlReport>true</disableXmlReport>
          <!-- If you have classpath issue like NoDefClassError,... -->
          <!-- useManifestOnlyJar>false</useManifestOnlyJar -->
          <includes>
            <include>**/*Test.*</include>
            <include>**/*Suite.*</include>
          </includes>
        </configuration>
      </plugin>
      <!-- 四:ここから追加 -->
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <executions>
          <execution>
            <id>add-source</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>add-source</goal>
            </goals>
            <configuration>
              <sources>
                <source>src/main/scala</source>
              </sources>
            </configuration>
          </execution>
          <execution>
            <id>add-test-source</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>add-test-source</goal>
            </goals>
            <configuration>
              <sources>
                <source>src/test/scala</source>
              </sources>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <!-- 四:ここまで追加 -->
    </plugins>
  </build>
</project>

修正の詳細は以下。

  • 一:テスト用のライブラリが不足しているので足す。
  • 二:Scala 2.11から無くなったオプションなので消す。
  • 三:mvn scala:runしたときに実行されるメインを指定。
  • 四:mvn eclipse:eclipseしたときの設定をマシにする設定。

(3)mvn eclipse:eclipseする。

(4)mvn eclipse:eclipseしても不完全なので「.classpath」と「.project」を修正する。
.classpath」の

  <classpathentry kind="src" path="src/test/scala" output="target/test-classes" including="**/*.java"/>
  <classpathentry kind="src" path="src/main/scala" including="**/*.java"/>

  <classpathentry kind="src" path="src/test/scala" output="target/test-classes" including="**/*.scala"/>
  <classpathentry kind="src" path="src/main/scala" including="**/*.scala"/>

へ変更。
.project」の

  <buildSpec>
    <buildCommand>
      <name>org.eclipse.jdt.core.javabuilder</name>
    </buildCommand>
  </buildSpec>
  <natures>
    <nature>org.eclipse.jdt.core.javanature</nature>
  </natures>

  <buildSpec>
    <buildCommand>
      <name>org.scala-ide.sdt.core.scalabuilder</name>
    </buildCommand>
  </buildSpec>
  <natures>
    <nature>org.scala-ide.sdt.core.scalanature</nature>
    <nature>org.eclipse.jdt.core.javanature</nature>
  </natures>

へ変更。

(5)Eclipseにインポート→「既存プロジェクトをワークスペースへ」。
「ライフサイクル構成でカバーされていないプラグインの実行」ってエラーでたら下記を参考。
http://tyru.hatenablog.com/entry/20140905/eclipse_lifecycle_error
http://blog.livedoor.jp/pg_spr/archives/1023860035.html

参考

EclipseでScalaプログラミングを始めるための基礎知識
Scala公式サイト(英語)
Scala-IDEのサイト(英語)
Scalaのクラスとオブジェクト、パターンマッチ(※classとobjectの違い)
Maven (Scala-maven-plugin) でScala packageをビルドして実行して、サイクルを回す
scala-maven-plugin
scala-archetype-simple
Scala with Maven
m2eclipse-scala