Version 3, last updated by David Bernard at Apr 02 19:45 2011 UTC

Or how to use maven-eclipse-plugin which emits Eclipse metadata

Assumes:

  • Eclipse 3.5 (3.4 should work too)
  • Scala IDE for Eclipse plugin 2.7.5.final - 2.8.0 nighly
  • Maven 2.2.1 or 3.0 beta1+

Provides:

  • Generate Eclipse project files
  • Import/refresh generated project files in Eclipse and edit/build/run/debug with no other modifications needed
  • Still be able to build/test/run/package using the Maven CLI, independently of Eclipse

The workflow :

  • First time
    1. edit pom.xml (use instruction from maven-scala-plugin and complete with pom.xml fragment from below)
    2. run mvn eclipse:clean eclipse:eclipse to update eclipse meta-data
    3. configure M2_REPO classpath variable (for eclipse) to reference downloaded jar files. This is only needed once for the workspace, not for each project.
    4. from eclipse : Window > Preferences > Java > Build Path > Classpath Variable to point to your $HOME/.m2/repository directory
    5. or from shell : run mvn -Declipse.workspace=<path to workspace> eclipse:configure-workspace ( See eclipse:configure-workspace ) to add it automatically.
  • Import project under eclipse (choose `File > Import... > Existing Projects into Workspace)
  • after edition of pom.xml
    1. run mvn eclipse:clean eclipse:eclipse to update eclipse meta-data
    2. refresh project under eclipse
  • code under eclipse
  • build the project with maven :
    • from command line
    • or from eclipse external tools, that call command line
    • or from eclipse maven plugin (like m2eclipse)

Configure maven-eclipse-plugin :

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-eclipse-plugin</artifactId>
    <version>2.8</version>
    <!-- see http://maven.apache.org/plugins/maven-eclipse-plugin/eclipse-mojo.html for more information -->
    <configuration>
      <downloadSources>true</downloadSources>
      <downloadJavadocs>true</downloadJavadocs>
      <projectnatures>
        <projectnature>org.scala-ide.sdt.core.scalanature</projectnature>
        <projectnature>org.eclipse.jdt.core.javanature</projectnature>
      </projectnatures>
      <buildcommands>
        <buildcommand>org.scala-ide.sdt.core.scalabuilder</buildcommand>
      </buildcommands>
      <classpathContainers>
        <classpathContainer>org.scala-ide.sdt.launching.SCALA_CONTAINER"</classpathContainer>
        <classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>
      </classpathContainers>
      <excludes>
        <exclude>org.scala-lang:scala-library</exclude>
        <exclude>org.scala-lang:scala-compiler</exclude>
      </excludes>
      <sourceIncludes>
        <sourceInclude>**/*.scala</sourceInclude>
        <sourceInclude>**/*.java</sourceInclude>
      </sourceIncludes>
    </configuration>
  </plugin>

For more configuration options or information take a look at the site of maven-eclipse-plugin

Configure source folders :

If you have a Scala only project, you need to configure sourceDirectory and testSourceDirectory as is ( required by maven-eclipse-plugin not by maven-scala-plugin ) :

<build>
  <sourceDirectory>src/main/scala</sourceDirectory>
  <testSourceDirectory>src/test/scala</testSourceDirectory>
  ...

For more configuration options or information take a look at the site of maven POM Overview (Technical Project Descriptor)

If you have a mixed Java/Scala project with code into separated root directory (like src/(main|test)/scala and src/(main|test)/java) :

        <!-- Adds src/main/scala and src/test/scala as source folders, from http://groups.google.com/group/liftweb/browse_thread/thread/3dac7002f9e59546/3918bba2f7a92cd3?pli=1 -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <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-sources</phase>
                    <goals>
                        <goal>add-test-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>src/test/scala</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

For more configuration options or information take a look at the site of build-helper-maven-plugin.

For configuration of the compilation under maven see doc of maven-scala-plugin :