When you’re going to write Maven plugins with Scala you’ve to consider two things:

  1. XDoclet plugin descriptors won’t work with Scala, so you’ve to use Java 5 annotations (in org.apache.maven.plugin-tools:maven-plugin-annotations)
  2. You have to make sure that the scala:compile goal is executed prior to plugin:descriptor, otherwise the maven-plugin-plugin is not able to detect the annotations. So, either you change the default phase of plugin:descriptor (which is generate-resources) to compile (or later), or you compile the Scala files before generate-resources, e.g. in the process-sources phase (which is BTW the same you would do in a mixed Java/Scala environment – see here).

Here an example Mojo written in Scala:

import java.io.File
import org.apache.maven.plugin.AbstractMojo
import org.apache.maven.plugins.annotations.Mojo
import org.apache.maven.plugins.annotations.Parameter

@Mojo(name = "test")
class TestMojo extends AbstractMojo {

  @Parameter(required = true)
  var myParam1: String = _

  @Parameter(required = false)
  var myParam2: String = _
  
  @Parameter(defaultValue = "${project.basedir}")
  var baseDirectory: File = _

  def execute() = {
    getLog.debug("Running Mojo test");
    getLog.debug("Base dir: " + baseDirectory)
    
    //TODO
  }
}

And the corresponding pom.xml:

<dependencies>
	<dependency>
		<groupId>org.apache.maven</groupId>
		<artifactId>maven-plugin-api</artifactId>
		<version>3.0.4</version>
	</dependency>
	<dependency>
		<groupId>org.apache.maven.plugin-tools</groupId>
		<artifactId>maven-plugin-annotations</artifactId>
		<version>3.2</version>
		<scope>provided</scope>
	</dependency>
	<dependency>
		<groupId>org.scala-lang</groupId>
		<artifactId>scala-library</artifactId>
		<version>${scala.version}</version>
	</dependency>
</dependencies>

<build>
	<plugins>
		<plugin>
			<groupId>org.scala-tools</groupId>
			<artifactId>maven-scala-plugin</artifactId>
			<version>2.15.2</version>
			<executions>
				<execution>
					<id>scala-compile-first</id>
					<phase>process-sources</phase>
					<goals>
						<goal>compile</goal>
					</goals>
				</execution>
				<execution>
					<id>scala-test-compile</id>
					<phase>process-test-sources</phase>
					<goals>
						<goal>testCompile</goal>
					</goals>
				</execution>
			</executions>
		</plugin>

		<plugin>
			<artifactId>maven-plugin-plugin</artifactId>
			<version>3.2</version>
			<configuration>
				<goalPrefix>myPlugin</goalPrefix>
			</configuration>
		</plugin>
	
		
		<plugin>
			<groupId>org.codehaus.mojo</groupId>
			<artifactId>build-helper-maven-plugin</artifactId>
			<version>1.8</version>
			<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-test-sources</phase>
					<goals>
						<goal>add-test-source</goal>
					</goals>
					<configuration>
						<sources>
							<source>src/test/scala</source>
						</sources>
					</configuration>
				</execution>
			</executions>
		</plugin>
	</plugins>
</build>

Leave a Reply

Your email address will not be published. Required fields are marked *


*