Click here to lend your support to: scala-maven-plugin and make a donation at www.pledgie.com !

Creating/Using a Scalac plugin

Project Setup

A scalac maven-based project has the following structure

project-root/
  pom.xml
  src/
    main/
      scala/
        -Your plugin Source Here
      resources/
          scalac-plugin.xml
       

Please follow the Writing Scala Compiler Plugins tutorial for the content of the project.

A scalac plugin should *only* have dependencies on the scala-compiler artifact, and these dependencies should be of type "provided". Here is an example pom for a plugin project:

<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.scala-lang</groupId>
  <artifactId>my-test-plugin</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Dependency Resolution extensions for the scala compiler</name>
  <dependencies>
    <dependency>
      <groupId>org.scala-lang</groupId>
      <artifactId>scala-compiler</artifactId>
      <version>2.7.2</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>net.alchim31.maven</groupId>
        <artifactId>scala-maven-plugin</artifactId>
        <version>3.3.1</version>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <sourceDir>src/main/scala</sourceDir>
          <jvmArgs>
            <jvmArg>-Xms64m</jvmArg>
            <jvmArg>-Xmx1024m</jvmArg>
          </jvmArgs>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>
      

Now you need to make sure you install the plugin project into the local repository.

mvn install

Using a scalac plugin

To make use of one or more scalac plugins, you need to declare them inside your pom using the "compilerPlugins" configuration item.

      <plugin>
        <groupId>net.alchim31.maven</groupId>
        <artifactId>scala-maven-plugin</artifactId>
        <version>3.3.1</version>
        <configuration>
          ...
          <compilerPlugins>
            <compilerPlugin>
              <groupId>org.scala-lang</groupId>
              <artifactId>my-scalac-plugin</artifactId>
              <version>1.0-SNAPSHOT</version>
            </compilerPlugin>
          </compilerPlugins>
        </configuration>
      </plugin>
      

If you need to send arguments to the plugin, this can be done with the "args" confguration tag.