Mixing Scala and Java in a maven project

Most of us work in environments with a considerable amount of java real estate. In order to integrate our scala code into that setup it’s sometimes necessary to mix Java and Scala in one project on a maven set up.

Here’s a working POM for such a project with respective source folders for both languages in

src/main/java

and

src/main/scala

	<build>
		<defaultGoal>package</defaultGoal>
		<resources>
			<resource>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
			</resource>
			<resource>
				<directory>src/test/resources</directory>
				<filtering>true</filtering>
			</resource>
		</resources>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
				<configuration>
					<encoding>${project.build.sourceEncoding}</encoding>
				</configuration>
				<executions>
					<execution>
						<goals>
							<goal>copy-resources</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>net.alchim31.maven</groupId>
				<artifactId>scala-maven-plugin</artifactId>
				<version>3.2.0</version>
				<configuration>
					<recompileMode>incremental</recompileMode>
					<args>
						<arg>-target:jvm-1.7</arg>
					</args>
					<javacArgs>
						<javacArg>-source</javacArg>
						<javacArg>1.7</javacArg>
						<javacArg>-target</javacArg>
						<javacArg>1.7</javacArg>
					</javacArgs>
				</configuration>
				<executions>
					<execution>
						<id>scala-compile</id>
						<phase>process-resources</phase>
						<goals>
							<goal>compile</goal>
						</goals>
					</execution>
					<execution>
						<id>scala-test-compile</id>
						<phase>process-test-resources</phase>
						<goals>
							<goal>testCompile</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
				</configuration>
				<executions>
					<execution>
						<phase>compile</phase>
						<goals>
							<goal>compile</goal>
						</goals>
					</execution>
				</executions>
			</plugin>

		</plugins>
		<pluginManagement>
			<plugins>
				<!--This plugin's configuration is used to store Eclipse m2e settings 
					only. It has no influence on the Maven build itself. -->
				<plugin>
					<groupId>org.eclipse.m2e</groupId>
					<artifactId>lifecycle-mapping</artifactId>
					<version>1.0.0</version>
					<configuration>
						<lifecycleMappingMetadata>
							<pluginExecutions>
								<pluginExecution>
									<pluginExecutionFilter>
										<groupId>
											net.alchim31.maven
										</groupId>
										<artifactId>
											scala-maven-plugin
										</artifactId>
										<versionRange>
											[3.1.6,)
										</versionRange>
										<goals>
											<goal>compile</goal>
											<goal>testCompile</goal>
										</goals>
									</pluginExecutionFilter>
									<action>
										<ignore></ignore>
									</action>
								</pluginExecution>
							</pluginExecutions>
						</lifecycleMappingMetadata>
					</configuration>
				</plugin>
			</plugins>
		</pluginManagement>
	</build>

Please do note that maven-compiler is actually not necessary as scala-compiler actually compiles the java code as well. The different executions make sure that scala-compiler comes first to allow access from java to scala too. I also force JDK 7 onto the scala-compiler. This might not be necessary in future releases of the scala-compiler plugin.

The plugin conf section is for eclipse’s m2e plugin. It suppresses pesky warnings.

Advertisements
This entry was posted in Scala and tagged , , . Bookmark the permalink.

One Response to Mixing Scala and Java in a maven project

  1. Pingback: Mixing Scala and Java in a Gradle project | Itellity Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s