Compiling Scala sources

Prerequis : common usages

Compiling

Place scala sources files in :

src/main/scala

Place scala test sources files in :

src/test/scala

Then use the standard maven phases :

          
# compile only
mvn compile
# or compile and test
mvn test
# or compile, test and package
mvn package
       
        

Or the plugin specifics goal :

          
# compile source
mvn scala:compile
# compile test
mvn scala:testCompile
       
        

Or if you want to compile only source (from main and test) without calling previous phases like check resources, generate-sources,... :

          
# compile source and test with fsc
mvn scala:cc -Donce=true
# compile source and test with scalac
mvn scala:cc -Donce=true -Dfsc=false
       
        
WARNING:
  • This feature is provide for integration with editor.
  • using once and fsc => reuse server if previously started else start a new one but never stop it (except if you call scala:cc without -Donce=true, or if you kill the process yourself)

Increasing memory

        
<project>
  ...
      <plugin>
        <groupId>net.alchim31.maven</groupId>
        <artifactId>scala-maven-plugin</artifactId>
        <version>4.8.1</version>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <jvmArgs>
            <jvmArg>-Xms64m</jvmArg>
            <jvmArg>-Xmx1024m</jvmArg>
          </jvmArgs>
        </configuration>
      </plugin>
  ...
</project>
            
      

Emacs user

If you are an emacs user, you can run the compilation with "-DemacsMode=true", then compilation error will be displayed without [WARNING] at the beginning of the line like :

[INFO] Compiling 1 source files to /home/dwayne/work/oss/scala-tools/vscaladoc/target/classes
[WARNING] Compilation failure

/project/src/main/scala/org/example/Main.scala:12: error: expected class or object definition
foo()
^
one error found
       

Compiler Arguments

It is common to wish to send arguments to the scalac compiler during compilation of your project. To do so, make use of the args configuration parameter like so:

<project>
  ...
  <plugin>
    <groupId>net.alchim31.maven</groupId>
    <artifactId>scala-maven-plugin</artifactId>
    <version>4.8.1</version>
    <configuration>
      <args>
        <arg>-unchecked</arg>
        <arg>-deprecation</arg>
        <arg>-explaintypes</arg>
      </args>
    </configuration>
  </plugin>
  ...
</project>
                

Use local scala's jar

If you want to try to compile with a local installation of scala for any reason like :

  • testing an unreleased version of Scala, for instance to report/reproduce a bug in a maven project
  • running integration builds against a different branch of the Scala compiler (community build)
  • using special flavors of Scala, such as paradise/macros

You should define property "scala.home". DO NOT DEPLOY (or install) with this option, it'll break user's projects.

In this case ${scala.home}/lib/scala-library.jar and ${scala.home}/lib/scala-compiler.jar are used to compile, to run,...

mvn compile -Dscala.home=<path/of/scalaHome>