Creating a Scala Mojo

Configuration

to create a scala mojo you need to add scala support into your pom.xml for a normal mojo project

        
<project>
   ...
	<dependencies>
		<dependency>
			<groupId>org.apache.maven</groupId>
			<artifactId>maven-plugin-api</artifactId>
			<version>2.0</version>
		</dependency>
		<dependency>
			<groupId>com.jsuereth</groupId>
			<artifactId>scala-mojo-support</artifactId>
			<version>0.4</version>
		</dependency>
        ...
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.scala-tools</groupId>
				<artifactId>maven-scala-plugin</artifactId>
				<executions>
					<execution>
						<goals>
							<goal>add-source</goal>
							<goal>compile</goal>
							<goal>testCompile</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<artifactId>maven-plugin-plugin</artifactId>
				<dependencies>
					<dependency>
						<groupId>com.jsuereth</groupId>
						<artifactId>scala-mojo-support</artifactId>
						<version>0.4</version>
					</dependency>
				</dependencies>
			</plugin>
			...
		</plugins>
	</build>
	<repositories>
		<repository>
			<id>scala-tools.org</id>
			<name>Scala-tools Maven2 Repository</name>
			<url>http://scala-tools.org/repo-releases</url>
		</repository>
		...
	</repositories>
	<pluginRepositories>
		<pluginRepository>
			<id>scala-tools.org</id>
			<name>Scala-tools Maven2 Repository</name>
			<url>http://scala-tools.org/repo-releases</url>
		</pluginRepository>
		...
	</pluginRepositories>
	...
</project>

            
      

Creating a Scala Mojo class

To create a scala mojo goal, simple create a scala class that extends AbstractMojo and annotate it appropriately, e.g.

        
package org.scala_tools.mojo

import org.apache.maven.plugin._
import java.io.File

/**
 * Goal which echos "HAI"
 *
 * @goal echo
 * 
 * @phase process-sources
 */
class TestMojo extends AbstractMojo {
  /**
   * Location of the file.
   * @parameter expression="${project.build.directory}"
   * @required
   */
  var outputDirectory : File = _;
  
  @throws(classOf[MojoExecutionException])
  override def execute() {
    getLog.error("HAI")
    getLog.error("outputDirectory = " + outputDirectory);//.getAbsolutePath);
  }
}
      
      

Changing the scala version

Scala Mojos are tied to using the version of scala that the scala-mojo-support is compiled with, and therefore should not depend on any other scala versions.