By Adrian Null
IntroductionMaven is a build/project management tool. It favours âconvention over configurationâ; it can greatly simplify builds for âstandardâ projects and a Maven user can usually understand the structure of another Maven project just by looking at its pom.xml
(Project Object Model). Maven is a plugin-based architecture, making it easy to add new libraries and modules to existing projects. For example, adding a new dependency usually involves only 5 extra lines in the pom.xml
. These âartifactsâ are downloaded from repositories such as The Central Repository.
You can also check out the official example project which uses the same Scala plugin we will show here.
Jumping AheadIf youâre familiar with Maven, you can go ahead with the Scala Maven Plugin.
The Scala Maven PluginWeâll be using the Scala Maven Plugin (GitHub repo, website) (formerly known as the maven-scala-plugin; renamed to honour the new naming policy where only Maven core plugins are prefixed with âmavenâ), by far the dominant plugin for Scala projects. Note: the plugin includes Scala from the Central Repository so thereâs no need to install it yourself if youâre compiling with Maven.
Getting Maven Linux (Debian)On Debian and Debian-derivatives, Maven is usually available via apt-get
. Just do (sudo) apt-get install maven
and youâre good to go.
OSX prior to 10.9 (Mavericks) comes with Maven 3 built in. If you donât have it, you can get it with the package managers MacPorts, Homebrew, or Fink. The Scala Maven Plugin requires Maven 3.0+
Manually (Red Hat Linux, OSX, Windows)You can download Maven from its Apache homepage. After extracting it (tar -zxvf apache-maven-X.X.X-bin.tar.gz
, or use something like 7-zip) to your directory of choice (on Linux and OSX, Unix-like systems, I like to put them in /opt/
. On Windows I would probably put this in C:/
), you need to add Maven to your environment Path variable:
/usr/bin
, which is already on your Path
ln -s /usr/bin/mvn /opt/apache-maven-X.X.X/bin/mvn
bin
folder directly to your path, using your shell configuration file (e.g. ~/.bash_profile
)
export PATH=$PATH:/opt/apache-maven-X.X.X/bin
to .bash_profile
(or whatever profile for the shell you use)echo "export PATH=$PATH:/opt/apache-maven-X.X.X/bin" >> ~/.bash_profile
mvn
shell script in an existing path location
$HOME/bin
in your path$HOME/bin
(mv apache-maven-X.X.X "$HOME/bin/"
)mvn
in $HOME/bin
"$HOME/bin/apache-maven-X.X.X/bin/mvn" $@
to it, and chmod u+x mvn
to make it executable$HOME/bin
is usually added to the userâs path by default, and if not, itâs a useful thing to do/have anyways. The shell script simply invokes the Maven location (which is at some other location) and passes on the argumentsC:\apache-maven-X.X.X
). Use backslashes to be safe, and do not include a trailing slash%MAVEN3_HOME%\bin
;%MAVEN3_BIN%
to itThe easiest way to create new projects is using an âarchetypeâ. An archetype is a general skeleton structure, or template for a project. Think back to âconvention over configurationâ; in our case, the Scala Maven Plugin provides an archetype for scala projects.
You run the archetype plugin like this:
mvn archetype:generate -DarchetypeGroupId=net.alchim31.maven -DarchetypeArtifactId=scala-archetype-simple
If this is your first time, youâll notice that Maven is downloading many jar files. Maven resolves dependencies and downloads them as needed (and only once). Right now, Maven is downloading its core plugins.
Next, Maven will ask you for a groupId, artifactId, and package. You can read the guide to naming conventions, but in short:
The groupId and artifactId together should serve as a globally unique identifier for your project
When itâs done, you should see a new folder named with the artifactId. cd
into it and run:
Youâll see Maven downloading dependencies including the Scala library (as mentioned above), JUnit, ScalaTest, and Specs2 (the latter three are test frameworks; the archetype includes an example âHello worldâ program, and tests with each of the frameworks).
Explaining this ArchetypeIn your project root, youâll see a pom.xml
, src
folder, and target
folder (target folder only appears after building). Note: this archetype also includes a .gitignore
Inside the src
folder youâll see main
and test
; main
includes your application code, and test
includes your test suites. Inside each of those youâll find a scala
folder, followed by your package structure (actually, test/scala
includes a sample package, but you should replace this with your own package and tests). If you want to mix Scala and Java source code, simply add a java
folder inside main
or test
.
target
includes generated/built files, such as .class
and .jar
files. You can read about pom.xml
at the Maven page.
Example structure:
Again, you can read more about the Scala Maven Plugin at its website.
Creating a JarBy default, the jar created by the Scala Maven Plugin doesnât include a Main-Class
attribute in the manifest. I had to add the Maven Assembly Plugin to my pom.xml
in order to specify custom attributes in the manifest. You can check the latest version of this plugin at the project summary or at The Central Repository
<project ...>
<modelVersion>X.X.X</modelVersion>
...
<licenses>
...
</licenses>
<properties>
...
</properties>
<dependencies>
...
</dependencies>
<build>
...
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.your-package.MainClass</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
After adding this, mvn package
will also create [artifactId]-[version]-jar-with-dependencies.jar
under target
. Note: this will also copy the Scala library into your Jar. This is normal. Be careful that your dependencies use the same version of Scala, or you will quickly end up with a massive Jar.
mvn dependency:copy-dependencies
: copy all libraries and dependencies to the target/dependency
foldermvn clean
mvn package
: compile, run tests, and create jarThe first thing I do is look for âMavenâ in the project page. For example, Googleâs [Guava] page includes Maven Central links. As you can see in the previous link, The Central Repository conveniently includes the snippet you have to add to your pom.xml
on the left sidebar.
If you canât find Maven information at the project page, try a Google search for â[project name] mavenâ. Sometimes, you still wonât find anything. For scopt (Scala command line option parser), I couldnât find the latest version from Google. However, manually searching The Central Repository did
Afterwards, running
Will download any new dependencies before packaging
Other Useful ReadingIâm not going to explain all of Maven in this tutorial (though I hope to add more in the future, because I do feel that the resources are a bit scattered), so here are some useful articles:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4