Intro to Maven - raisercostin/software-wiki GitHub Wiki

Concepts

  • Maven Project Layout
    • Introduction to the Standard Directory Layout
    • structure
      pom.xml            <-- project definition
      .gitignore         <-- not maven specific but present in git projects
      README.md          <-- not maven but useful for github: a markdown file
      src/               <-- sources
        main/            <-- sources needed for library
          java/          <-- java sources
          resources/     <-- other files that will be added to library and reachable in classpath
          webapp/        <-- java web application resources
          scripts/
          php/           <-- other type of resources. With different compilators.
          scala/
        test/            <-- sources for Unit Tests. will not be bundled in library
          java/          <-- sources
          resources/     <-- sources
        it/              <-- sources for Integration Tests. will not be bundled in library
          java/
          resources/
      target/            <-- generated files. Can be deleted to remove all generated files.
      
  • Convention Over Configuration
  • artifacts (libraries, sources, docs, pom, metadata) - sample
    • library unique id: <groupId>:<artifactId>:<version>
      • groupId - is the group that manages the artifact/library. Is the reversed domain name to ensure the uniqueness of the group based on the domain uniqueness.
      • artifactId - is the artifact unique id inside a group. The artifact is the generalized name for a library. Could also be an archive with sources, documents, metadata etc.
      • version - is the version of the artifact. For details see semantic versioning.
    • library dependencies
    • repositories
    • Transitive Dependencies
  • declarative build tool
  • actions
    • phases and lifecycle These are executed via mvn <phase>: mvn clean, mvn compile, mvn test
    • plugins These are executed via mvn <plugin>:<plucinCommand>: mvn eclipse:clean, mvn eclipse:eclipse -DdownloadSources, mvn origin:origin -DdownloadSources
    • formatting : mvn formatter:format, mvn antrun:run

Resources

Advantages

  • manage integration with IDE: where are sources, dependencies, ...
  • automatic download of libraries, sources
  • no scm (git) pollution with binary files
  • manage transitive dependencies
  • manage integration with build servers: Jenkins needed for Continuous Integration

FAQ

  1. Where to find libraries?
  2. No compiler on mvn compile

Install

Download

Install

Unzip

Configure

Configuration files are in

  • <maven-install-folder>/config/settings.xml
  • ~/.m2/settings.xml

To configure credentials for servers (revomatico-repo, revomatico-repo-snapshots, revomatico-repo-site for example) you can add them to any of the settings.xml. See how to store them encrypted

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <servers>
    <server>
      <id>revomatico-repo</id>
      <username>raisercostin</username>
      <password>********</password>
    </server>
    <server>
      <id>revomatico-repo-snapshots</id>
      <username>raisercostin</username>
      <password>********</password>
    </server>
    <server>
      <id>revomatico-repo-site</id>
      <username>raisercostin</username>
      <password>********</password>
    </server>
  </servers>
</settings>

Windows

  • don't forget to add ..\apache-maven\bin to your PATH environment variable.

Ubuntu

  • sudo apt-get update
  • sudo apt-get install maven

Validate Install

> echo $M2_HOME
> echo %M2_HOME%
> echo $PATH|grep maven
> echo %PATH%|grep maven
> mvn -version
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T18:41:47+02:00)
Maven home: C:\dev\apache-maven\bin\..
Java version: 1.8.0_121, vendor: Oracle Corporation
Java home: D:\forgerock\jdk1.8.0_121\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "dos"

Usage

Create sample java maven project

mvn archetype:generate
> template: maven-archetype-quickstart
> version: 1.1
> groupId: ro.dcsi.internship
> artifactId: usersync
> version: 1.0-SNAPSHOT
> package: ro.dcsi.internship

Others

  • Windows
cd \dev\apache-maven
cd mvn-workspace
dir ..\
mvn archetype:generate
dir
mvn help:effective-pom
cd ship1
mvn help:effective-pom
mvn test
mvn clean
mvn test
mvn eclipse
mvn eclipse:help
mvn eclipse:eclipse
mvn run
mvn exec:java -Dexec.mainClass=ro.dcsi.internship.App
# display last plugin versions
mvn versions:display-plugin-updates

12606  java -version
12607  javac -version
12608  mvn -version
12609  mvn archetype:generate
12610  mc
12611  pwd
12612  cd work
12613  mvn archetype:generate
12614  mvn archetype:generate
12615  history
⚠️ **GitHub.com Fallback** ⚠️