logo

Apache Maven - Overview

Last Updated: 2021-11-19

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``

Set Main-Class

Set the main class in jar:

<build>
    <defaultGoal>compile</defaultGoal>

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>your.main.class</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

Copy Dependencies

<build>
    <plugins>
        <plugin>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <phase>install</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                    </configuration]]>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Classifier

In HDP 2.2, the jar of pig is published as pig-0.14.0.2.2.4.0-2633-h2.jar, however pom does not come with -h2: pig-0.14.0.2.2.4.0-2633.pom(both can be found here

The right way to add it as dependency is put h2 as classifier:

<dependency>
    <groupId>org.apache.pig</groupId>
    <artifactId>pig</artifactId>
    <version>0.14.0.2.2.4.0-2633</version>
    <classifier>h2</classifier>
</dependency>

Release

Prepare

It will update version to release version, tag code, commit and push. Meanwhile generate pom.xml.backup and release.properties files in the current dir.

$ mvn release:prepare

Perform

It will deploy package to nexus, checkout the tag code to local and update version to [new version]-SNAPSHOT, and cleanup the backup and properties files.

$ mvn release:perform