The Maven Cookbook

3.3. Running an External Scala Script in a Maven Build

3.3. Running an External Scala Script in a Maven Build

3.3.1. Task

You need to execute an external Scala script as a part of your Maven build.

3.3.2. Action

Similar to Example 3.1, “Executing an Inline Scala Script with the Maven Scala Plugin”, configure the script goal of the Maven Scala plugin and specify a Scala script in the scriptFile configuration parameter.

Example 3.2. Configuring the Maven Scala Plugn Script Goal to Execute an External Script

<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.sonatype.mcookbook</groupId>
  <artifactId>scala-script-ex</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>scala-script-ex</name>
  <dependencies>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-model</artifactId>
      <version>2.2.0</version>
    </dependency>
    <dependency>
      <groupId>org.scala-lang</groupId>
      <artifactId>scala-library</artifactId>
      <version>2.7.3</version>
    </dependency>
    <dependency>
      <groupId>org.scala-tools</groupId>
      <artifactId>maven-scala-plugin</artifactId>
      <version>2.10.1</version>
    </dependency>
    <dependency>
      <groupId>org.scalaforge</groupId>
      <artifactId>scalax</artifactId>
      <version>0.1</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.scala-tools</groupId>
        <artifactId>maven-scala-plugin</artifactId>
        <version>2.10.1</version>
        <executions>
          <execution>
            <id>scala-magic</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>script</goal>
            </goals>
            <configuration>
              <keepGeneratedScript>true</keepGeneratedScript>
              <scriptFile>${basedir}/src/main/scala/CreateDeps.scala</scriptFile>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  <repositories>
    <repository>
      <id>scala-tools</id>
      <url>http://scala-tools.org/repo-releases/</url>
    </repository>
  </repositories>
</project>

The script that this build will execute is the same Scala script that was executed in Example 3.1, “Executing an Inline Scala Script with the Maven Scala Plugin”. In this example, the script is stored in an external file in ${basedir]/src/main/scala, and in Example 3.1, “Executing an Inline Scala Script with the Maven Scala Plugin”, the script was listed inline in the project's POM.

Example 3.3. The CreateDeps.scala Script

import java.io.{File,  PrintWriter, FileWriter};
import scalax.io.FileExtras;
import scala.collection.mutable.HashSet;

val outputDir = project.getBuild().getOutputDirectory();
val depsFile = new FileExtras( new File( outputDir, "deps.txt" ) );
val pw = depsFile.printWriter;
val depSet = new HashSet[String];
for( d <- project.getDependencies() ) {
  depSet += d.getGroupId + ":" + d.getArtifactId + ":" + d.getVersion;
}
pw.writeLines( depSet.toSeq );

Just like the example in Section 3.2, “Running an Inline Scala Script in a Maven Build”, this script creates a file named deps.txt in ${basedir}/target/classes which contains a list of the project's dependencies.

org.scalaforge:scalax:0.1
org.apache.maven:maven-model:2.2.0
org.scala-tools:maven-scala-plugin:2.10.1
org.scala-lang:scala-library:2.7.3

3.3.3. Detail

There is very little difference between the example from Section 3.2, “Running an Inline Scala Script in a Maven Build” and the example in this section other than the location of the Scala script. If you look in ${basedir}/target/.scalaScriptGen you can see that the Scala plugin creates a file named CreateDeps_1.scala that contains a class named CreateDeps_1.