Jump to content

Running an External Scala Script in a Maven Build

+ 1
  tmo9d's Photo
Posted Sep 30 2009 12:19 PM

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

Configure the script goal of the Maven Scala plugin and specify a Scala script in the scriptFile configuration parameter.

<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.mhandbook</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>


In this example, the script is stored in an external file in ${basedir}/src/main/scala in CreateDeps.scala.

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 );


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


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.

Cover of Maven: The Definitive Guide
Learn more about this topic from Maven: The Definitive Guide. 

Written by Maven creator Jason Van Zyl and his team at Sonatype, Maven: The Definitive Guide clearly explains how this popular tool can bring order to your software development projects. The first part of the book demonstrates Maven's capabilities through the development of several sample applications from ideation to deployment, and the second part offers a complete reference guide. Concise and to the point, this is the only guide you need to manage your project.

Learn More Read Now on Safari


Tags:
1 Subscribe


0 Replies