Maven

From semanticweb.org
Jump to: navigation, search

Maven is a build-tool for Java, muche like ANT, but with a more declarative approach and better dependency management.


Don't use this page without have a look at the maven page of the KEKS project: http://code.google.com/p/keks/wiki/Maven


As the documentation for Maven is in many places sparse or out of date, here is a quick intro to get you started.

We assume you use:

  • Eclipse 3.2.x
  • JUnit 4.x
  • SubVersion

We also assume the project you want to use is configured for Maven. Creating Maven projects is trickier than building existing ones.

Contents

[edit] Installing Maven

  • Download Maven here http://maven.apache.org/download.html
  • Unzip to somewhere
  • Add the bin directory to your PATH
  • Make sure JAVA_HOME is set to the location of your JDK
  • At the command line, run "mvn --version" to verify that it is correctly installed.
  • Call "mvn" alone once more to create the .m2 directory in the profile. Ignore the "Build failure" error.
  • Do not install an Eclipse integration plugin. They don't work.

[edit] Configure Maven

Create a local directory where you want Maven to store all downloaded dependencies (might grow up to 1-2 GB). Let'S say you named this directory C:/dev/maven-repo.

Go to or create a file settings.xml in your user home, e.g.

C:\Documents and Settings\{myusername}\.m2

with this content:

<settings>
  <localRepository>C:/dev/maven-repo/</localRepository>
</settings>

You can also add you private passwords for some servers here, but that's not needed now:

<?xml version="1.0" encoding="UTF-8"?>
<settings 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/settings-1.0.0.xsd">
  <localRepository>C:/dev/maven-repo/</localRepository>
  <servers>
    <server>
      <id>theoretiker</id>
      <username>max</username>
      <password>********</password>
    </server>
  </servers>
</settings>

[edit] Configure Eclipse

Eclipse needs to know where the repository is, so we set

Window > Preferences > Java > Build Path > Classpath Variables
Name: M2_REPO
Path: C:/dev/maven-repo/

[edit] How to use it

The best way to use Maven is currently to use the commandline. Navigate in your shell/dos box to the path where your project root is.

  • Clean project
mvn clean
  • Build, test and put into local repository if tests are ok
mvn install
  • Create web site (which contains a number of reports) into ./target/site
mvn site:site

[edit] How to modify (e.g. dependencies)

Look into pom.xml. You can add yourself as a contributor or take a look at the dependencies.

You do not modify dependencies in Eclipse, instead you declare them in the POM. After you did that, run

mvn eclipse:eclipse

which will re-generate the .classpath file to include these dependencies. Of course, Maven will download them for you.

To find the right groupId and artifactId search here: http://mvnrepository.com/

[edit] How to put dependencies to local repository

Sometimes the big Maven repo does not contain the lib you look for. Then download the jar yourself somewhere and do, e.g. for Java Mail

mvn install:install-file -Dfile=mail.jar -DgroupId=javax.mail -DartifactId=mail -Dversion=1.3.2 -Dpackaging=jar

with the appropriate settings for -Dfile and -Dversion. -DgroupId is usually the domain name and -DartifactId the name of the jar.

[edit] Learn more

These links where helpful for me to understand Maven better:

Personal tools
Namespaces

Variants
Actions
Navigation
services
Toolbox