Apache Maven - Overview

Add Log4j

Assume log4j properties are stored in src/main/resources/log4j.properties, add this to pom to enable log4j in unit test

<build>
  ...
  <resources>
    <resource>
      <directory>src/main/resources</directory>
    </resource>
  </resources>
  ...
</build>

Recursively add files

<fileSet>
    <directory>${project.basedir}/src/test/resources/examples</directory>
    <outputDirectory>/examples</outputDirectory>
    <includes>
        <include>**/*</include>
    </includes>
</fileSet>

Set Dependency As Optional

Assume Project-A depends on slf4j, Project-B depends on Project-A, then slf4j will not be treated as dependency in Project-B; slf4j must be added to Project-B's pom in order to be used

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.7</version>
    <optional>true</optional>
</dependency>

Multi-module projects

You will be prompted for the version number for each module of the project. If you prefer that every module gets the same version as the parent POM, you can set the option autoVersionSubmodules to true. Now you will be asked only once for the release version and the next development version.

Versions

  • Parent-Child Approach: The one uses, a super pom define all the dependencies and version numbers via dependencyManagement, the child pom only need to define the dependency but not version number

    • Pro: a tree-structure hierarchy; child pom only define the dependencies that it needs
    • Con: a child can only have one parent
  • Group dependencies in one pom, and compile as "pom", then in each project, add that pom as a dependency, using the following code

<dependency>
  <groupId>some-group</groupId>
  <artifactId>some-artifact</artifactId>
  <version>1.0</version>
  <type>pom</type>
</dependency>
  • Pro: do not need to explicitly list all the dependencies as parent-child approach; can add multiple pom as dependencies.
  • Con: it is a all-or-none approach, not sure if the extra loaded dependencies would affect the size or the efficiency of the final package.

Dependency Tree

$ mvn dependency:tree
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'dependency'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Jetty HelloWorld
[INFO]    task-segment: [dependency:tree]
[INFO] ------------------------------------------------------------------------
[INFO] [dependency:tree {execution: default-cli}]
[INFO] org.example:hello-world:jar:0.1-SNAPSHOT
[INFO] \- org.eclipse.jetty:jetty-server:jar:9.0.0:compile
[INFO]    +- org.eclipse.jetty:javax.servlet:jar:3.0.0.v201112011016:compile
[INFO]    +- org.eclipse.jetty:jetty-continuation:jar:9.0.0:compile
[INFO]    \- org.eclipse.jetty:jetty-http:jar:9.0.0:compile
[INFO]       \- org.eclipse.jetty:jetty-io:jar:9.0.0:compile
[INFO]          \- org.eclipse.jetty:jetty-util:jar:9.0.0:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4 seconds
[INFO] Finished at: Thu Jan 24 16:19:08 EST 2013
[INFO] Final Memory: 11M/68M
[INFO] ------------------------------------------------------------------------

Update Version

$ mvn versions:set -DnewVersion=``release version``