Maven

 Maven is a software project management and comprehension tool. It has several very nice features like modules and dependencies.

Dependencies

Example: If I want my project to use OpenRDF library's HTTP client library. I specify to maven project configuration (pom.xml) file that I it to use the artifact (that's how maven names it) by adding

 <dependencies>
   ...
  <dependency>
   <groupId>org.openrdf.sesame</groupId>
   <artifactId>sesame-http-client</artifactId>
   <version>2.3-pr1</version>
  </dependency>
  ...
 </dependencies> 

When you will try to compile your program afterwards, maven will automatically download the needed jar files, as well as their dependencies. In case of OpenRDF there are MANY dependencies one normally cannot keep track of. So it will do a very nice job and download only the needed ones and you just don't have to care! Isn't it nice?

Modules

Each module has metadata assigned to it and it's specified in the pom.xml file. It looks like the following

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>kwarc.info.LaMaPUn</groupId>
  <artifactId>KnowledgeDatabase</artifactId>
  <version>0.0.1</version>
  <packaging>jar</packaging>
  <name>KnowledgeDatabase</name>
  <url>http://maven.apache.org</url>

  <modules>
        <module>kdb-core</module>
  </modules>
</project>

Each module has several identification fields, namely groupId and artifactId. These are used to uniquely identify the module and are usually some URIs. They are also used to get discovered by the dependency download module. Also it has a version field, making it possible to keep several versions of same module. The modules tag, specifies child modules of the current module, introducing a hierarchical dependency relationship.

Maven Tools

Luckily enough you don't have to write the XML's by hand, rather there is the mvn tool which does it all for you. Here is a list of most common tasks:

  • create a root (independent) project
    mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=my-app
    

This will create a directory called my-app where the source/test code will be living. In my-app/pom.xml will be the project's main configuration file.

  • create a module (app-core) inside the project. Run following command from directory where main pom.xml is located:
    mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=app-core
    

This will make the link between the main pom.xml and the child pom.xml (located in my-app/app-core) with the tag:

<Module>app-core</Module> 

and the child pom.xml with

  <parent>
     <artifactId>...</artifactId>
     ...
  </parent>
  • to compile, go to the root of the module/project and run
      mvn compile
    
  • to run the project/module, execute
      mvn exec:java -Dexec.mainClass=com.mycompany.app.Main -Dexec.args="some arguments to program"
    

Notice that you don't need to specify the LONG classpaths as you would have needed otherwise. All this is taken care by maven!