maven.rst
author Oleksandr Gavenko <gavenkoa@gmail.com>
Sun, 03 Jan 2021 23:37:00 +0200
changeset 2492 bd3d45148652
parent 2396 f6aa28b6c706
child 2506 04649fd8a394
permissions -rw-r--r--
Fixed example.

.. -*- coding: utf-8; -*-

========
 Maven.
========
.. contents::
   :local:

Maven tutorial.
===============

* http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
* http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

Maven FAQ.
==========

* http://maven.apache.org/general.html

Maven plugins.
==============

* http://maven.apache.org/plugins/

Maven config location.
======================

``~/.m2/settings.xml``.

Maven package repositories
==========================

Maven Central official aliases (in past - mirrors):

* https://repo1.maven.org/maven2/
* https://uk.maven.org/maven2/
* https://central.maven.org/maven2/

Spring repos is server by Artifactory and available on:

* https://repo.spring.io/
* https://maven.springframework.org/

Sonatype Releases Repository:

* https://oss.sonatype.org/content/repositories/releases/

NetBeans:

* http://bits.netbeans.org/maven2/

JBoss Enterprise Maven Repository:

* https://maven.repository.redhat.com/ga/

https://mvnrepository.com/repos
  List of popular public Maven repositories.
http://blog.sonatype.com/2010/10/new-official-maven-central-repository-in-europe/
  New official Maven Central repository in Europe.

Generate simple project.
========================
::

  $ mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Debug Maven build.
==================

Run build with ``-X`` option for verbose logging.

Project directory layout.
=========================

http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
  Introduction to the Standard Directory Layout.

Search for maven artifact by Java package or class name.
========================================================

* https://repository.sonatype.org/
* http://search.maven.org/

Get help on Maven plug-in
=========================
::

  $ mvn help:describe -DartifactId=maven-war-plugin -DgroupId=org.apache.maven.plugins
  $ mvn help:describe -Dcmd=dependency:resolve -Ddetail

  $ mvn -X ...

What actual code processed by Maven (dump Maven config)::

  $ mvn help:effective-settings
  $ mvn help:effective-pom

Investigating and resolving dependencies
========================================

List dependencies::

  $ mvn dependency:tree
  $ mvn dependency:list

Resolve dependencies::

  $ mvn dependency:resolve
  $ mvn dependency:resolve-plugins

Just download dependencies::

  $ mvn dependency:get

or download specific dependency::

  $ mvn dependency:get -Dartifact=$groupId:$artifactId:$version

Reason for inclusion or omitting dependencies::

  $ mvn dependency:tree -Dverbose=true

Update ``pom.xml`` with latest dependency version::

  $ mvn versions:use-latest-releases -Dincludes=$groupId:$artifactId

Print build classpath::

  $ mvn dependency:build-classpath

Download all external dependencies sources and javadocs
=======================================================
::

  mvn dependency:resolve -Dclassifier=javadoc
  mvn dependency:resolve -Dclassifier=sources

Build sources and javadoc
=========================

::

  mvn source:jar
  mvn javadoc:jar

To install sources or javadoc locally::

  mvn source:jar install
  mvn javadoc:jar install
  mvn source:jar javadoc:jar install

To do that automatically on ``jar`` lifecycle::

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-source-plugin</artifactId>
        <executions>
          <execution>
            <id>attach-sources</id>
            <goals>
              <goal>jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

Force update of dependencies
============================

You can try redownload snapshots by::

  $ mvn -U compile

You can fix damaged local ``~/.m2`` with::

  $ mvn dependency:purge-local-repository

In order to perform really clean download::

  $ mvn -Dmaven.repo.local=$HOME/.my/other/repository clean install

Find newer library and plugin versions
======================================

Check commands from ``versions-maven-plugin``::

  $ mvn versions:display-dependency-updates
  $ mvn versions:display-plugin-updates

Update ``pom.xml`` with latest dependency version::

  $ mvn versions:use-latest-releases -Dincludes=$groupId:$artifactId

Update all versions in ``pom.xml`` to latest::

  $ mvn versions:use-latest-releases
  $ rm pom.xml.versionsBackup

or::

  $ mvn versions:use-latest-releases -DgenerateBackupPoms=false

Remove all ``SNAPSHOT`` versions::

  $ mvn versions:use-releases -DgenerateBackupPoms=false

More details about ``versions`` plugin::

  $ mvn help:describe -Dplugin=versions
  $ mvn help:describe -Dplugin=versions -Ddetail

Run Java main from Maven.
=========================
::

  mvn exec:java -Dexec.mainClass="com.vineetmanohar.module.Main" -Dexec.args="arg0 arg1 arg2"

How to run single unit test?
============================

``test`` property substituded to ``**/${test}.java`` pattern and override any
include/exclude patterns::

  $ mvn test -Dtest=SeriousComponentTest

or mostly same::

  $ mvn test-compile surefire:test -Dtest=RunMe

How do I skip the tests during the default lifecycle?
=====================================================
::

  $ mvn -DskipTests package
  $ mvn -Dmaven.test.skip=true package

Deploy parent pom without building children.
============================================
::

  $ mvn -N deploy

Install file to local repository
================================
::

  $ mvn install:install-file -Dfile=... -DgroupId=... -DartifactId=... -Dversion=...

Additional option provides packaging type (JAR/WAR), source and javadoc files::

  -Dpackaging=...
  -Dsource=...
  -Djavadoc=...

Run Ant from Maven.
===================

* https://support.sonatype.com/entries/20736282-executing-an-external-ant-script-in-a-maven-build
* https://support.sonatype.com/entries/20723081-running-an-inline-ant-script-in-a-maven-build
* https://support.sonatype.com/entries/20744068-writing-a-maven-plugin-with-ant