To allow the compilation of Scala source code in Maven we will will require maven plugin called maven-scala-plugin (plugin home page). This will take three steps:
First step.
To use it we’ll have to define additional Maven Repositories in you project’s <project> tag:
<repositories> <repository> <id>scala-tools.org</id> <name>Scala-tools Maven2 Repository</name> <url>https://oss.sonatype.org/content/groups/scala-tools/</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>scala-tools.org</id> <name>Scala-tools Maven2 Repository</name> <url>https://oss.sonatype.org/content/groups/scala-tools/</url> </pluginRepository> </pluginRepositories>
Alternatively you can configure then in your settings.xml file for your Maven distribution but that would make your project environment dependent.
Second step.
Define a plugin for your project by adding the following section inside <project> -> <build> -> <plugins> tag:
<plugin> <groupId>org.scala-tools</groupId> <artifactId>maven-scala-plugin</artifactId> <version>${scala.maven.version}</version> <executions> <execution> <goals> <goal>compile</goal> <goal>testCompile</goal> </goals> </execution> </executions> </plugin>
Third step.
The last requirement is to define a dependency for your project containing Scala Library. To do that add this section into <project> -> <dependencies>:
<dependency> <groupid>org.scala-lang</groupid> <artifactid>scala-library</artifactid> <version>_here_define_scala_version_you_want_to_use_</version> </dependency>
Result.
This all should result in a pom.xml that looks like this example:
<?xml version="1.0" encoding="UTF-8"?> <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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>pl.marpiec.scala</groupId> <artifactId>maven-example</artifactId> <version>1.0-SNAPSHOT</version> <properties> <scala.lang.version>2.9.1</scala.lang.version> <scala.maven.version>2.9.1</scala.maven.version> </properties> <repositories> <repository> <id>scala-tools.org</id> <name>Scala-tools Maven2 Repository</name> <url>https://oss.sonatype.org/content/groups/scala-tools/</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>scala-tools.org</id> <name>Scala-tools Maven2 Repository</name> <url>https://oss.sonatype.org/content/groups/scala-tools/</url> </pluginRepository> </pluginRepositories> <dependencies> <!-- Scala --> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> <version>${scala.lang.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.scala-tools</groupId> <artifactId>maven-scala-plugin</artifactId> <version>${scala.maven.version}</version> <executions> <execution> <goals> <goal>compile</goal> <goal>testCompile</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
Project structure.
When you have prepared pom.xml you can start creating your scala code. You should put it in src -> main -> scala directory (or src -> test -> scala for test code). This is the example from Intellij IDEA IDE:

Building.
And you can easily build your project just by executing mvn package command, example:

Running.
That will create jar package that you can run by that command:

That's all, I hope that you will have a great experience using Maven and Scala for your project.
No comments:
Post a Comment