Tuesday, November 10, 2015

Create new Scala Maven project for Eclipse

Scala with Maven tutorial
http://docs.scala-lang.org/tutorials/scala-with-maven.html

Download latest version of Scala http://www.scala-lang.org/download/2.11.7.html

Upgrade Scala version

[root@localhost Downloads]# dnf  upgrade  scala-2.11.7.rpm


Create Scala Maven project from archetype using

   mvn archetype:generate

Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 686: scala
Choose archetype:
1: remote -> co.cask.cdap:cdap-spark-scala-archetype (-)
2: remote -> com.github.igor-petruk.archetypes:maven-archetype-scala-executable (Creates executable Scala Project that is ready to run with 'java -jar')
3: 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.)
4: remote -> eu.stratosphere:quickstart-scala (-)
5: remote -> net.alchim31.maven:scala-archetype-simple (The maven-scala-plugin is used for compiling/testing/running/documenting scala code in maven.)
6: remote -> net.ladstatt:scala-javafx-archetype (The Scala JavaFX Archetype provides a simple template for a scala based 
    JavaFX project)
7: remote -> org.apache.camel.archetypes:camel-archetype-component-scala (Creates a new Camel component with Scala.)
8: remote -> org.apache.camel.archetypes:camel-archetype-scala (Creates a new Camel project using Scala DSL.)
9: remote -> org.apache.flink:flink-quickstart-scala (-)
10: remote -> org.fusesource.scalate.tooling:scalate-archetype-empty (An archetype which creates an empty Scalate web application)
11: remote -> org.fusesource.scalate.tooling:scalate-archetype-guice (An archetype which creates an empty Scalate Guice web application)
12: remote -> org.fusesource.scalate.tooling:scalate-archetype-guice_2.10 (An archetype which creates an empty Scalate Guice web application)
13: remote -> org.fusesource.scalate.tooling:scalate-archetype-guice_2.9 (An archetype which creates an empty Scalate Guice web application)
14: remote -> org.fusesource.scalate.tooling:scalate-archetype-jersey (An archetype which creates an empty Scalate web application)
15: remote -> org.fusesource.scalate.tooling:scalate-archetype-jersey_2.10 (An archetype which creates an empty Scalate web application)
16: remote -> org.fusesource.scalate.tooling:scalate-archetype-jersey_2.9 (An archetype which creates an empty Scalate web application)
17: remote -> org.fusesource.scalate.tooling:scalate-archetype-sitegen (An archetype which creates an empty Scalate static website generation project)
18: remote -> org.fusesource.scalate.tooling:scalate-archetype-sitegen_2.10 (An archetype which creates an empty Scalate static website generation project)
19: remote -> org.fusesource.scalate.tooling:scalate-archetype-sitegen_2.9 (An archetype which creates an empty Scalate static website generation project)
20: remote -> org.openjdk.jmh:jmh-scala-benchmark-archetype (Generates Scala benchmarking project, uses JMH bytecode processors)
21: remote -> org.scala-tools.archetypes:scala-archetype-simple (The maven-scala-plugin is used for compiling/testing/running/documenting scala code in maven.)
22: remote -> org.scalatra.scalate.tooling:scalate-archetype-guice_2.10 (An archetype which creates an empty Scalate Guice web application)
23: remote -> org.scalatra.scalate.tooling:scalate-archetype-guice_2.11 (An archetype which creates an empty Scalate Guice web application)
24: remote -> org.scalatra.scalate.tooling:scalate-archetype-jersey_2.10 (An archetype which creates an empty Scalate web application)
25: remote -> org.scalatra.scalate.tooling:scalate-archetype-jersey_2.11 (An archetype which creates an empty Scalate web application)
26: remote -> org.scalatra.scalate.tooling:scalate-archetype-sitegen_2.10 (An archetype which creates an empty Scalate static website generation project)
27: remote -> org.scalatra.scalate.tooling:scalate-archetype-sitegen_2.11 (An archetype which creates an empty Scalate static website generation project)
28: remote -> org.wicketstuff.scala:wicket-scala-archetype (Basic setup for a project that combines Scala and Wicket, 
        depending on the Wicket-Scala project. Includes an example Specs 
        test.)
29: 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.)
30: remote -> pl.org.miki:scala-quickstart-archetype (Customizable cruft-free Scala archetype. Options:
-sourceFolders: [all-in-src-java, scala-only, both-split-src]. DEFAULT: all-in-src-java.
--all-in-src-java: Scala and Java code are both in src/main/java (same for test). Plays nicely with IDEs.
--scala-only: only src/main/scala set up, and set as source folder.
--both-split-src: both src/main/java and src/main/scala. Can play merry havock with IDEs, so not the default.
-testLibrary: [scalatest, specs2, scalacheck-only, junit-only]. DEFAULT: scalatest.
--scalatest: adds Scalatest AND Scalacheck AND JUnit for most of your testing needs.
--specs2: adds specs2 AND Scalacheck AND JUnit.
--scalacheck-junit: adds Scalacheck AND JUnit.
--junit-only: adds JUnit ONLY.
-scalaVersion: 2.10.x+. DEFAULT: 2.11.2.
*The Scala tool version is generated automatically.
*Some Scala versions may not be compatible with selected test library versions. Adjust as necessary.)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): : 

Choose simple archetype ( number 5 or 30 )

https://github.com/mikolak-net/scala-quickstart-archetype 

Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): : 5
Choose net.alchim31.maven:scala-archetype-simple version: 
1: 1.4
2: 1.5
3: 1.6
Choose a number: 3: 3
Downloading: http://maven.repository.redhat.com/earlyaccess/all/net/alchim31/maven/scala-archetype-simple/1.6/scala-archetype-simple-1.6.jar
Downloading: http://maven.repository.redhat.com/techpreview/all/net/alchim31/maven/scala-archetype-simple/1.6/scala-archetype-simple-1.6.jar
Downloading: https://repo.maven.apache.org/maven2/net/alchim31/maven/scala-archetype-simple/1.6/scala-archetype-simple-1.6.jar
Downloaded: https://repo.maven.apache.org/maven2/net/alchim31/maven/scala-archetype-simple/1.6/scala-archetype-simple-1.6.jar (5 KB at 16.5 KB/sec)
Downloading: http://maven.repository.redhat.com/earlyaccess/all/net/alchim31/maven/scala-archetype-simple/1.6/scala-archetype-simple-1.6.pom
Downloading: http://maven.repository.redhat.com/techpreview/all/net/alchim31/maven/scala-archetype-simple/1.6/scala-archetype-simple-1.6.pom
Downloading: https://repo.maven.apache.org/maven2/net/alchim31/maven/scala-archetype-simple/1.6/scala-archetype-simple-1.6.pom
Downloaded: https://repo.maven.apache.org/maven2/net/alchim31/maven/scala-archetype-simple/1.6/scala-archetype-simple-1.6.pom (4 KB at 33.7 KB/sec)
Define value for property 'groupId': : scala
Define value for property 'artifactId': : class-finder
Define value for property 'version':  1.0-SNAPSHOT: : 
Define value for property 'package':  scala: : dave
Confirm properties configuration:
groupId: scala
artifactId: class-finder
version: 1.0-SNAPSHOT
package: dave
 Y: : 


Created project
[dave@localhost scala]$ find 
.
./class-finder
./class-finder/pom.xml
./class-finder/.gitignore
./class-finder/src
./class-finder/src/main
./class-finder/src/main/scala
./class-finder/src/main/scala/dave
./class-finder/src/main/scala/dave/App.scala
./class-finder/src/test
./class-finder/src/test/scala
./class-finder/src/test/scala/samples
./class-finder/src/test/scala/samples/junit.scala
./class-finder/src/test/scala/samples/specs.scala
./class-finder/src/test/scala/samples/scalatest.scala



Add into pom.xml  the build-helper-maven-plugin to add new source folders as described here http://docs.scala-lang.org/tutorials/scala-with-maven.html

Import existing  Maven  project into Eclipse

Edit .project and .classpath files to include Scala

Optionally
  -- Run mvn eclipse:eclipse in project dir
  --- Configure Eclipse workspace

dave@localhost class-finder]$ mvn -Declipse.workspace="/home/dave/workspace/scala"  eclipse:configure-workspace

.project

[dave@localhost class-finder]$ more .project 
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
    <name>class-finder</name>
    <comment></comment>
    <projects>
    </projects>
    <buildSpec>
        <buildCommand>
            <name>org.eclipse.jdt.core.javabuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>org.scala-ide.sdt.core.scalabuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>org.eclipse.m2e.core.maven2Builder</name>
            <arguments>
            </arguments>
        </buildCommand>
    </buildSpec>
    <natures>
        <nature>org.scala-ide.sdt.core.scalanature</nature>
        <nature>org.eclipse.jdt.core.javanature</nature>
        <nature>org.eclipse.m2e.core.maven2Nature</nature>
    </natures>
</projectDescription>

.classpath

[dave@localhost class-finder]$ more .classpath 
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" output="target/classes" path="src/main/java">
        <attributes>
            <attribute name="optional" value="true"/>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="src" output="target/test-classes" path="src/test/java">
        <attributes>
            <attribute name="optional" value="true"/>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMTyp
e/JavaSE-1.8">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="src" output="target/classes" path="src/main/scala">
        <attributes>
            <attribute name="optional" value="true"/>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="src" output="target/test-classes" path="src/test/scala">
        <attributes>
            <attribute name="optional" value="true"/>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="output" path="target/classes"/>
</classpath>


pom.xml

[dave@localhost class-finder]$ more pom.xml 
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>scala</groupId>
    <artifactId>class-finder</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <scala.version>2.11.7</scala.version>
        <scala.version.tools>2.11</scala.version.tools>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>3.2.0</version>
                <executions>
                    <execution>
                        <!-- this is so we don't end with a compile error in maven-compiler-plugin -->        
                        <phase>process-sources</phase>                     
                        <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                        <configuration>
                            <mainSourceDir>${project.build.sourceDirectory}/../java</mainSourceDir>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </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>
 <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>dave.MainClass</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
        </plugin>


        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>${scala.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.scalacheck</groupId>
            <artifactId>scalacheck_${scala.version.tools}</artifactId>
            <version>1.11.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.scalatest</groupId>
            <artifactId>scalatest_${scala.version.tools}</artifactId>
            <version>2.2.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>


Run mvn package 

Open project in Eclipse IDE



Add launcher for command line
<build>
        <plugins>
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
      <launchers>
        <launcher>
          <id>foo</id>
          <mainClass>dave.App</mainClass>
          <!-- args are optional -->
          <args>
            <arg>arg1</arg>
          </args>
          <!-- jvmArgs are optional -->
          <jvmArgs>
            <jvmArg>-Xmx128m</jvmArg>
            <jvmArg>-Djava.library.path=...</jvmArg>
          </jvmArgs>
        </launcher>
        <!-- you could define other launcher -->
      </launchers>
    </configuration>


Run project  from command line


mvn scala:run

[INFO] launcher 'foo' selected => dave.App
Hello World!
concat arguments = arg1
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------


Known errors

http://scala-ide.org/docs/current-user-doc/faq/index.html#Know_Issue

Add m2e-scala http://alchim31.free.fr/m2e-scala/update-site/

If you see errors in using prototype modify the pom.xml

[INFO] /home/dave/git/scala/class-finder/src/main/scala:-1: info: compiling
[INFO] Compiling 1 source files to /home/dave/git/scala/class-finder/target/classes at 1447218248818
[ERROR] scalac error: bad option: '-make:transitive'


see here http://stackoverflow.com/questions/12812458/getting-started-with-scala-scalatest-and-maven

Try to change versions in pom.xml or  change options for scala-maven-plugin

https://github.com/davidB/scala-maven-plugin
  

https://github.com/scala/scala-module-dependency-sample/blob/master/maven-sample/pom.xml


Error related to versions of Scala artifacts - change the repository configuration
[ERROR] Failed to execute goal on project class-finder: Could not resolve dependencies for project scala:class-finder:jar:1.0-SNAPSHOT: Could not find artifact org.scalaz.stream:scalaz-stream_2.11:jar:0.7a in jboss-earlyaccess-repository (http://maven.repository.redhat.com/earlyaccess/all/) -> [Help 1]