Titel
Onderwerp
Verantwoordelijke afdeing, personen.
Datum
JTech Accelerator Program 2019
CI-CD DevOps – Advanced Maven
Bert Koorengevel - Java Software Engineer / CodeSmith, Ordina OSD - JTech
@BertKoor
Titel
Onderwerp
Verantwoordelijke afdeing, personen.
Datum
https://karussell.wordpress.com/2009/09/29/
3
Apache Maven
• Apache Software Foundation:
community for Open Source software projects
• Maven: a Yiddish word meaning
“accumulator of knowledge”
• Dominant build tool in the Java world
• Multi-platform (Windows / Unix),
command-line interface
4
• Making the build process easy
• Providing a uniform build system
• Providing quality project information
• Providing guidelines for best practices development
• Allowing transparent migration to new features
Maven Objectives
5
Maven pom.xml
• pom = Project Object Model
• Controls the what and how of the build proces
• What: groupId, artifactId, version,
packaging :
• jar = Java ARchive
• war = Web application ARchive
• ear = Java Enterprise ARchive
• pom = other (produces only the pom.xml) or controlling multi-module builds
• How: we’ll see later, controlled in principle by packaging and plugins
• “Convention over Configuration” : sticking to the conventions reduces configuration
• Convention: the pom.xml is located in the root folder of the project
• Reference documentation: https://maven.apache.org/pom.html
<?xml>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>nl.ordina.jtech</groupId>
<artifactId>MavenDemo</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
</project>
6
Maven Process Flow
‘Target’ folderMaven‘Source’ folder
pom.xml
Artifact
7
WTF is an
Artifact?
8
9
10
What is an Artifact?
• General definition: “any object made by human beings”
• In software: an application, module or component, typically archived (e.g. zip file)
• Storage in a Repository
• artifactId – identifier / name of the artefict
• groupId – serves as Namespace, so the artifactId need not be worldwide unique
• version – artifact versions are frozen. New build  increase version!
• Path in repository: / groupId / artifactId / version / artifactId-version.ext
SportsQuest/nl/ordina/jtech/sportsquest/ /1.2.3/SportsQuest-1.2.3.zip
11
Initializing a simple Maven Project
 Execute:
- mvn archetype:generate -DgroupId=nl.ordina -DartifactId=MyFirstPony
• New projectfolder MyFirstPony
is created, containing:
• Standard folder structure
with 2 java classes
• A pom.xml
12
 Visit https://github.com/IvoNet
 Repo: javaee8-payara-microprofile-archetype
 Generate your own project using this archetype
 Study what is generated
 Study the original pom.xml, how does it work?
Exploring a custom archetype
13
Packaging
• jar = Java ARchive
• war = Web application ARchive
• ear = Java Enterprise Archive
• ejb = Enterprise Java Bean
• pom = other (produces only the pom.xml)
or controlling multi-module builds
• maven-plugin
• maven-archetype
<?xml>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>nl.ordina.jtech</groupId>
<artifactId>MavenDemo</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
</project>
14
 Simple as 1, 2, 3 …
Artifact Versioning
15
Semantic Versioning : https://semver.org
Version: 1
• Major version
• Minor version
• Patch version
• Build number
• or Qualifier
.2.3-45
-SNAPSHOT
16
Maven Build Lifecycle
3 separate lifecycles:
 Clean : discard previous built files (workspace)
 Default : compilation & deployment (Maven repository)
 Site : generation of html reports (code quality, etc)
17
Lifecycle Phases
Each lifecycle consists of phases.
Most important phases of the default lifecycle:
 Compile
 Test
 Package
 Validate
 Install
 Deploy
18
• clean
----------------------------------------------------------------------
| | /| /  / |¯¯¯ | | /¯¯ ¯¯|¯¯ / |¯¯ ¯¯|¯¯ |
| |  / | /__  / |-- |  | ¯¯ | /__ |--/ | |
| | / | /  / |___ | | __/ | /  |  | |
----------------------------------------------------------------------
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building MavenDemo 0.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ MavenDemo ---
[INFO] Deleting C:WorkspaceMavenWorkshopMavenDemotarget
Convention:
Maven only touches the “target” folder
Slow-Motion …
19
• clean
• validate
• initialize
• generate-sources
• process-sources
• generate-resources
• process-resources
----------------------------------------------------------------------
| | /| /  / |¯¯¯ | | /¯¯ ¯¯|¯¯ / |¯¯ ¯¯|¯¯ |
| |  / | /__  / |-- |  | ¯¯ | /__ |--/ | |
| | / | /  / |___ | | __/ | /  |  | |
----------------------------------------------------------------------
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building MavenDemo 0.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ MavenDemo ---
[INFO] Deleting C:WorkspaceMavenWorkshopMavenDemotarget
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MavenDemo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
Conventions:
Output goes to /target/classes
Resource files are in /src/main/resources
Slow-Motion …
20
• clean
• validate
• initialize
• generate-sources
• process-sources
• generate-resources
• process-resources
• compile
----------------------------------------------------------------------
| | /| /  / |¯¯¯ | | /¯¯ ¯¯|¯¯ / |¯¯ ¯¯|¯¯ |
| |  / | /__  / |-- |  | ¯¯ | /__ |--/ | |
| | / | /  / |___ | | __/ | /  |  | |
----------------------------------------------------------------------
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building MavenDemo 0.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ MavenDemo ---
[INFO] Deleting C:WorkspaceMavenWorkshopMavenDemotarget
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MavenDemo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
----------------------------------------------------------------------
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building MavenDemo 0.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ MavenDemo ---
[INFO] Deleting C:WorkspaceMavenWorkshopMavenDemotarget
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MavenDemo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ MavenDemo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:WorkspaceMavenWorkshopMavenDemotargetclasses
Convention:
Java source files are in /src/main/java
Slow-Motion …
21
• clean
• validate
• initialize
• generate-sources
• process-sources
• generate-resources
• process-resources
• compile
• process-classes
• generate-test-sources
• process-test-sources
• generate-test-resources
• process-test-resources
----------------------------------------------------------------------
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building MavenDemo 0.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ MavenDemo ---
[INFO] Deleting C:WorkspaceMavenWorkshopMavenDemotarget
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MavenDemo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ MavenDemo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:WorkspaceMavenWorkshopMavenDemotargetclasses
[INFO] ------------------------------------------------------------------------
[INFO] Building MavenDemo 0.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ MavenDemo ---
[INFO] Deleting C:WorkspaceMavenWorkshopMavenDemotarget
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MavenDemo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ MavenDemo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:WorkspaceMavenWorkshopMavenDemotargetclasses
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ MavenDemo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
Convention:
Test output goes to /target/test-classes
Test resources are in /src/test/resources
Slow-Motion …
22
• clean
• validate
• initialize
• generate-sources
• process-sources
• generate-resources
• process-resources
• compile
• process-classes
• generate-test-sources
• process-test-sources
• generate-test-resources
• process-test-resources
• test-compile
[INFO] ------------------------------------------------------------------------
[INFO] Building MavenDemo 0.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ MavenDemo ---
[INFO] Deleting C:WorkspaceMavenWorkshopMavenDemotarget
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MavenDemo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ MavenDemo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:WorkspaceMavenWorkshopMavenDemotargetclasses
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ MavenDemo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ MavenDemo ---
[INFO] Deleting C:WorkspaceMavenWorkshopMavenDemotarget
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MavenDemo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ MavenDemo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:WorkspaceMavenWorkshopMavenDemotargetclasses
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ MavenDemo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ MavenDemo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:WorkspaceMavenWorkshopMavenDemotargettest-classes
Convention:
Test sources are in /src/test/java
Slow-Motion …
23
• clean
• validate
• initialize
• generate-sources
• process-sources
• generate-resources
• process-resources
• compile
• process-classes
• generate-test-sources
• process-test-sources
• generate-test-resources
• process-test-resources
• test-compile
• process-test-classes
• test
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ MavenDemo ---
[INFO] Deleting C:WorkspaceMavenWorkshopMavenDemotarget
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MavenDemo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ MavenDemo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:WorkspaceMavenWorkshopMavenDemotargetclasses
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ MavenDemo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ MavenDemo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:WorkspaceMavenWorkshopMavenDemotargettest-classes
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ MavenDemo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:WorkspaceMavenWorkshopMavenDemotargettest-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ MavenDemo ---
[INFO] Surefire report directory: C:WorkspaceMavenWorkshopMavenDemotargetsurefire-
reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running HelloMavenTest
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.016 sec
Results :
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
Slow-Motion …
24
• clean
• validate
• initialize
• generate-sources
• process-sources
• generate-resources
• process-resources
• compile
• process-classes
• generate-test-sources
• process-test-sources
• generate-test-resources
• process-test-resources
• test-compile
• process-test-classes
• test
• prepare-package
• package
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ MavenDemo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:WorkspaceMavenWorkshopMavenDemotargettest-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ MavenDemo ---
[INFO] Surefire report directory: C:WorkspaceMavenWorkshopMavenDemotargetsurefire-
reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running HelloMavenTest
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.016 sec
Results :
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ MavenDemo ---
[INFO] Surefire report directory: C:WorkspaceMavenWorkshopMavenDemotargetsurefire-
reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running HelloMavenTest
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.016 sec
Results :
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ MavenDemo ---
[INFO] Building jar: C:WorkspaceMavenWorkshopMavenDemotargetMavenDemo-0.0.jar
Slow-Motion …
25
• clean
• validate
• initialize
• generate-sources
• process-sources
• generate-resources
• process-resources
• compile
• process-classes
• generate-test-sources
• process-test-sources
• generate-test-resources
• process-test-resources
• test-compile
• process-test-classes
• test
• prepare-package
• package
• pre-integration-test
• integration-test
• post-integration-test
• verify
• install
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ MavenDemo ---
[INFO] Surefire report directory: C:WorkspaceMavenWorkshopMavenDemotargetsurefire-
reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running HelloMavenTest
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.016 sec
Results :
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ MavenDemo ---
[INFO] Building jar: C:WorkspaceMavenWorkshopMavenDemotargetMavenDemo-0.0.jar
T E S T S
-------------------------------------------------------
Running HelloMavenTest
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.016 sec
Results :
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ MavenDemo ---
[INFO] Building jar: C:WorkspaceMavenWorkshopMavenDemotargetMavenDemo-0.0.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ MavenDemo ---
[INFO] Installing C:WorkspaceMavenWorkshopMavenDemotargetMavenDemo-0.0.jar to
C:MavenReponlbertkoorMavenDemo0.0MavenDemo-0.0.jar
[INFO] Installing C:WorkspaceMavenWorkshopMavenDemopom.xml to
C:MavenReponlbertkoorMavenDemo0.0MavenDemo-0.0.pom
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ MavenDemo ---
[INFO] Building jar: C:WorkspaceMavenWorkshopMavenDemotargetMavenDemo-0.0.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ MavenDemo ---
[INFO] Installing C:WorkspaceMavenWorkshopMavenDemotargetMavenDemo-0.0.jar to
C:MavenReponlbertkoorMavenDemo0.0MavenDemo-0.0.jar
[INFO] Installing C:WorkspaceMavenWorkshopMavenDemopom.xml to
C:MavenReponlbertkoorMavenDemo0.0MavenDemo-0.0.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.028 s
[INFO] Finished at: 2016-01-20T10:45:32+01:00
[INFO] Final Memory: 16M/159M
[INFO] ------------------------------------------------------------------------
Slow-Motion …
26
• clean
• validate
• initialize
• generate-sources
• process-sources
• generate-resources
• process-resources
• compile
• process-classes
• generate-test-sources
• process-test-sources
• generate-test-resources
• process-test-resources
• test-compile
• process-test-classes
• test
• prepare-package
• package
• pre-integration-test
• integration-test
• post-integration-test
• verify
• install
• deploy
T E S T S
-------------------------------------------------------
Running HelloMavenTest
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.016 sec
Results :
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ MavenDemo ---
[INFO] Building jar: C:WorkspaceMavenWorkshopMavenDemotargetMavenDemo-0.0.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ MavenDemo ---
[INFO] Installing C:WorkspaceMavenWorkshopMavenDemotargetMavenDemo-0.0.jar to
C:MavenReponlbertkoorMavenDemo0.0MavenDemo-0.0.jar
[INFO] Installing C:WorkspaceMavenWorkshopMavenDemopom.xml to
C:MavenReponlbertkoorMavenDemo0.0MavenDemo-0.0.pom
[INFO] Installing C:WorkspaceMavenWorkshopMavenDemotargetMavenDemo-0.0.jar to
C:MavenReponlbertkoorMavenDemo0.0MavenDemo-0.0.jar
[INFO] Installing C:WorkspaceMavenWorkshopMavenDemopom.xml to
C:MavenReponlbertkoorMavenDemo0.0MavenDemo-0.0.pom
[INFO]
[INFO] --- maven-deploy-plugin:2.7:deploy (default-deploy) @ MavenDemo ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.232 s
[INFO] Finished at: 2016-01-20T10:41:10+01:00
[INFO] Final Memory: 16M/157M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy
(default-deploy) on project MavenDemo: Deployment failed: repository element was not
specified in the POM inside distributionManagement element or in -
DaltDeploymentRepository=id::layout::url parameter -> [Help 1]
“mvn clean install” suffices“mvn clean package” suffices
Slow-Motion …
27
Some Maven Conventions
 Maven only touches the “target” folder
 Locations:
- Java sources: /src/main/java
- Resources: /src/main/resources
- Output: /target/classes
- Test sources: /src/test/java
- Test resources: /src/test/resources
- Test output: /target/test-classes
28
Generic format:
 mvn [ phase | pluginId:goal | -options ] ...
 mvn [groupId:]pluginId[:version]:goal
implicit groupId = org.apache.maven and maven-{pluginId}-plugin
Some examples:
 mvn –v
 mvn archetype:generate -DartifactId=MyFirstPony
 mvn package
 mvn clean package
 mvn help:effective-pom
 mvn dependency:tree
Maven Commandline
29
Properties
<project>
...
<description>My project ${myProperty}</description>
<properties>
<myProperty>has a value</myProperty>
</properties>
mvn [goals] –DmyProperty="has no value"
30
Modules – aggregate build
 The Parent project containing the
modules has packaging “pom”
 Each module sits in its own
subdirectory and is a separate
Maven project that needs to be
built
<modules>
<module>core</module>
<module>model</module>
<module>webservice</module>
</modules>
31
Dependency Management
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
• Declares the version without
actually using the dependency
• Often used in Parent pom’s,
uniformity of versions across the
project
32
Change the build (other plugin behaviour) based on activation:
 Standard activation (activeByDefault)
 Command line activation
- parameter –PprofileName
 Property values
 JDK version
 OS name, family, version
 (in) Existance of a file
Profiles
33
 Just a dependency with Scope = import
 Article by Edwin Derks: “to bom or not to bom”
Importing a BOM pom with Dependency Management
34
 https://ordina-jtech.github.io/MetMavenKunJeAllesBouwen
Workshop – using plugins

Ordina Accelerator program 2019 - Maven

  • 1.
    Titel Onderwerp Verantwoordelijke afdeing, personen. Datum JTechAccelerator Program 2019 CI-CD DevOps – Advanced Maven Bert Koorengevel - Java Software Engineer / CodeSmith, Ordina OSD - JTech @BertKoor
  • 2.
  • 3.
    3 Apache Maven • ApacheSoftware Foundation: community for Open Source software projects • Maven: a Yiddish word meaning “accumulator of knowledge” • Dominant build tool in the Java world • Multi-platform (Windows / Unix), command-line interface
  • 4.
    4 • Making thebuild process easy • Providing a uniform build system • Providing quality project information • Providing guidelines for best practices development • Allowing transparent migration to new features Maven Objectives
  • 5.
    5 Maven pom.xml • pom= Project Object Model • Controls the what and how of the build proces • What: groupId, artifactId, version, packaging : • jar = Java ARchive • war = Web application ARchive • ear = Java Enterprise ARchive • pom = other (produces only the pom.xml) or controlling multi-module builds • How: we’ll see later, controlled in principle by packaging and plugins • “Convention over Configuration” : sticking to the conventions reduces configuration • Convention: the pom.xml is located in the root folder of the project • Reference documentation: https://maven.apache.org/pom.html <?xml> <project> <modelVersion>4.0.0</modelVersion> <groupId>nl.ordina.jtech</groupId> <artifactId>MavenDemo</artifactId> <version>1.0</version> <packaging>jar</packaging> </project>
  • 6.
    6 Maven Process Flow ‘Target’folderMaven‘Source’ folder pom.xml Artifact
  • 7.
  • 8.
  • 9.
  • 10.
    10 What is anArtifact? • General definition: “any object made by human beings” • In software: an application, module or component, typically archived (e.g. zip file) • Storage in a Repository • artifactId – identifier / name of the artefict • groupId – serves as Namespace, so the artifactId need not be worldwide unique • version – artifact versions are frozen. New build  increase version! • Path in repository: / groupId / artifactId / version / artifactId-version.ext SportsQuest/nl/ordina/jtech/sportsquest/ /1.2.3/SportsQuest-1.2.3.zip
  • 11.
    11 Initializing a simpleMaven Project  Execute: - mvn archetype:generate -DgroupId=nl.ordina -DartifactId=MyFirstPony • New projectfolder MyFirstPony is created, containing: • Standard folder structure with 2 java classes • A pom.xml
  • 12.
    12  Visit https://github.com/IvoNet Repo: javaee8-payara-microprofile-archetype  Generate your own project using this archetype  Study what is generated  Study the original pom.xml, how does it work? Exploring a custom archetype
  • 13.
    13 Packaging • jar =Java ARchive • war = Web application ARchive • ear = Java Enterprise Archive • ejb = Enterprise Java Bean • pom = other (produces only the pom.xml) or controlling multi-module builds • maven-plugin • maven-archetype <?xml> <project> <modelVersion>4.0.0</modelVersion> <groupId>nl.ordina.jtech</groupId> <artifactId>MavenDemo</artifactId> <version>1.0</version> <packaging>jar</packaging> </project>
  • 14.
    14  Simple as1, 2, 3 … Artifact Versioning
  • 15.
    15 Semantic Versioning :https://semver.org Version: 1 • Major version • Minor version • Patch version • Build number • or Qualifier .2.3-45 -SNAPSHOT
  • 16.
    16 Maven Build Lifecycle 3separate lifecycles:  Clean : discard previous built files (workspace)  Default : compilation & deployment (Maven repository)  Site : generation of html reports (code quality, etc)
  • 17.
    17 Lifecycle Phases Each lifecycleconsists of phases. Most important phases of the default lifecycle:  Compile  Test  Package  Validate  Install  Deploy
  • 18.
    18 • clean ---------------------------------------------------------------------- | |/| / / |¯¯¯ | | /¯¯ ¯¯|¯¯ / |¯¯ ¯¯|¯¯ | | | / | /__ / |-- | | ¯¯ | /__ |--/ | | | | / | / / |___ | | __/ | / | | | ---------------------------------------------------------------------- [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building MavenDemo 0.0 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ MavenDemo --- [INFO] Deleting C:WorkspaceMavenWorkshopMavenDemotarget Convention: Maven only touches the “target” folder Slow-Motion …
  • 19.
    19 • clean • validate •initialize • generate-sources • process-sources • generate-resources • process-resources ---------------------------------------------------------------------- | | /| / / |¯¯¯ | | /¯¯ ¯¯|¯¯ / |¯¯ ¯¯|¯¯ | | | / | /__ / |-- | | ¯¯ | /__ |--/ | | | | / | / / |___ | | __/ | / | | | ---------------------------------------------------------------------- [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building MavenDemo 0.0 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ MavenDemo --- [INFO] Deleting C:WorkspaceMavenWorkshopMavenDemotarget [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MavenDemo --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource Conventions: Output goes to /target/classes Resource files are in /src/main/resources Slow-Motion …
  • 20.
    20 • clean • validate •initialize • generate-sources • process-sources • generate-resources • process-resources • compile ---------------------------------------------------------------------- | | /| / / |¯¯¯ | | /¯¯ ¯¯|¯¯ / |¯¯ ¯¯|¯¯ | | | / | /__ / |-- | | ¯¯ | /__ |--/ | | | | / | / / |___ | | __/ | / | | | ---------------------------------------------------------------------- [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building MavenDemo 0.0 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ MavenDemo --- [INFO] Deleting C:WorkspaceMavenWorkshopMavenDemotarget [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MavenDemo --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource ---------------------------------------------------------------------- [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building MavenDemo 0.0 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ MavenDemo --- [INFO] Deleting C:WorkspaceMavenWorkshopMavenDemotarget [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MavenDemo --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ MavenDemo --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to C:WorkspaceMavenWorkshopMavenDemotargetclasses Convention: Java source files are in /src/main/java Slow-Motion …
  • 21.
    21 • clean • validate •initialize • generate-sources • process-sources • generate-resources • process-resources • compile • process-classes • generate-test-sources • process-test-sources • generate-test-resources • process-test-resources ---------------------------------------------------------------------- [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building MavenDemo 0.0 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ MavenDemo --- [INFO] Deleting C:WorkspaceMavenWorkshopMavenDemotarget [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MavenDemo --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ MavenDemo --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to C:WorkspaceMavenWorkshopMavenDemotargetclasses [INFO] ------------------------------------------------------------------------ [INFO] Building MavenDemo 0.0 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ MavenDemo --- [INFO] Deleting C:WorkspaceMavenWorkshopMavenDemotarget [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MavenDemo --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ MavenDemo --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to C:WorkspaceMavenWorkshopMavenDemotargetclasses [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ MavenDemo --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource Convention: Test output goes to /target/test-classes Test resources are in /src/test/resources Slow-Motion …
  • 22.
    22 • clean • validate •initialize • generate-sources • process-sources • generate-resources • process-resources • compile • process-classes • generate-test-sources • process-test-sources • generate-test-resources • process-test-resources • test-compile [INFO] ------------------------------------------------------------------------ [INFO] Building MavenDemo 0.0 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ MavenDemo --- [INFO] Deleting C:WorkspaceMavenWorkshopMavenDemotarget [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MavenDemo --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ MavenDemo --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to C:WorkspaceMavenWorkshopMavenDemotargetclasses [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ MavenDemo --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ MavenDemo --- [INFO] Deleting C:WorkspaceMavenWorkshopMavenDemotarget [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MavenDemo --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ MavenDemo --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to C:WorkspaceMavenWorkshopMavenDemotargetclasses [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ MavenDemo --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ MavenDemo --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to C:WorkspaceMavenWorkshopMavenDemotargettest-classes Convention: Test sources are in /src/test/java Slow-Motion …
  • 23.
    23 • clean • validate •initialize • generate-sources • process-sources • generate-resources • process-resources • compile • process-classes • generate-test-sources • process-test-sources • generate-test-resources • process-test-resources • test-compile • process-test-classes • test [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ MavenDemo --- [INFO] Deleting C:WorkspaceMavenWorkshopMavenDemotarget [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MavenDemo --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ MavenDemo --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to C:WorkspaceMavenWorkshopMavenDemotargetclasses [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ MavenDemo --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ MavenDemo --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to C:WorkspaceMavenWorkshopMavenDemotargettest-classes [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ MavenDemo --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to C:WorkspaceMavenWorkshopMavenDemotargettest-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ MavenDemo --- [INFO] Surefire report directory: C:WorkspaceMavenWorkshopMavenDemotargetsurefire- reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running HelloMavenTest Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.016 sec Results : Tests run: 4, Failures: 0, Errors: 0, Skipped: 0 Slow-Motion …
  • 24.
    24 • clean • validate •initialize • generate-sources • process-sources • generate-resources • process-resources • compile • process-classes • generate-test-sources • process-test-sources • generate-test-resources • process-test-resources • test-compile • process-test-classes • test • prepare-package • package [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ MavenDemo --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to C:WorkspaceMavenWorkshopMavenDemotargettest-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ MavenDemo --- [INFO] Surefire report directory: C:WorkspaceMavenWorkshopMavenDemotargetsurefire- reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running HelloMavenTest Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.016 sec Results : Tests run: 4, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ MavenDemo --- [INFO] Surefire report directory: C:WorkspaceMavenWorkshopMavenDemotargetsurefire- reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running HelloMavenTest Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.016 sec Results : Tests run: 4, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ MavenDemo --- [INFO] Building jar: C:WorkspaceMavenWorkshopMavenDemotargetMavenDemo-0.0.jar Slow-Motion …
  • 25.
    25 • clean • validate •initialize • generate-sources • process-sources • generate-resources • process-resources • compile • process-classes • generate-test-sources • process-test-sources • generate-test-resources • process-test-resources • test-compile • process-test-classes • test • prepare-package • package • pre-integration-test • integration-test • post-integration-test • verify • install [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ MavenDemo --- [INFO] Surefire report directory: C:WorkspaceMavenWorkshopMavenDemotargetsurefire- reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running HelloMavenTest Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.016 sec Results : Tests run: 4, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ MavenDemo --- [INFO] Building jar: C:WorkspaceMavenWorkshopMavenDemotargetMavenDemo-0.0.jar T E S T S ------------------------------------------------------- Running HelloMavenTest Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.016 sec Results : Tests run: 4, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ MavenDemo --- [INFO] Building jar: C:WorkspaceMavenWorkshopMavenDemotargetMavenDemo-0.0.jar [INFO] [INFO] --- maven-install-plugin:2.4:install (default-install) @ MavenDemo --- [INFO] Installing C:WorkspaceMavenWorkshopMavenDemotargetMavenDemo-0.0.jar to C:MavenReponlbertkoorMavenDemo0.0MavenDemo-0.0.jar [INFO] Installing C:WorkspaceMavenWorkshopMavenDemopom.xml to C:MavenReponlbertkoorMavenDemo0.0MavenDemo-0.0.pom Tests run: 4, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ MavenDemo --- [INFO] Building jar: C:WorkspaceMavenWorkshopMavenDemotargetMavenDemo-0.0.jar [INFO] [INFO] --- maven-install-plugin:2.4:install (default-install) @ MavenDemo --- [INFO] Installing C:WorkspaceMavenWorkshopMavenDemotargetMavenDemo-0.0.jar to C:MavenReponlbertkoorMavenDemo0.0MavenDemo-0.0.jar [INFO] Installing C:WorkspaceMavenWorkshopMavenDemopom.xml to C:MavenReponlbertkoorMavenDemo0.0MavenDemo-0.0.pom [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 4.028 s [INFO] Finished at: 2016-01-20T10:45:32+01:00 [INFO] Final Memory: 16M/159M [INFO] ------------------------------------------------------------------------ Slow-Motion …
  • 26.
    26 • clean • validate •initialize • generate-sources • process-sources • generate-resources • process-resources • compile • process-classes • generate-test-sources • process-test-sources • generate-test-resources • process-test-resources • test-compile • process-test-classes • test • prepare-package • package • pre-integration-test • integration-test • post-integration-test • verify • install • deploy T E S T S ------------------------------------------------------- Running HelloMavenTest Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.016 sec Results : Tests run: 4, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ MavenDemo --- [INFO] Building jar: C:WorkspaceMavenWorkshopMavenDemotargetMavenDemo-0.0.jar [INFO] [INFO] --- maven-install-plugin:2.4:install (default-install) @ MavenDemo --- [INFO] Installing C:WorkspaceMavenWorkshopMavenDemotargetMavenDemo-0.0.jar to C:MavenReponlbertkoorMavenDemo0.0MavenDemo-0.0.jar [INFO] Installing C:WorkspaceMavenWorkshopMavenDemopom.xml to C:MavenReponlbertkoorMavenDemo0.0MavenDemo-0.0.pom [INFO] Installing C:WorkspaceMavenWorkshopMavenDemotargetMavenDemo-0.0.jar to C:MavenReponlbertkoorMavenDemo0.0MavenDemo-0.0.jar [INFO] Installing C:WorkspaceMavenWorkshopMavenDemopom.xml to C:MavenReponlbertkoorMavenDemo0.0MavenDemo-0.0.pom [INFO] [INFO] --- maven-deploy-plugin:2.7:deploy (default-deploy) @ MavenDemo --- [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.232 s [INFO] Finished at: 2016-01-20T10:41:10+01:00 [INFO] Final Memory: 16M/157M [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project MavenDemo: Deployment failed: repository element was not specified in the POM inside distributionManagement element or in - DaltDeploymentRepository=id::layout::url parameter -> [Help 1] “mvn clean install” suffices“mvn clean package” suffices Slow-Motion …
  • 27.
    27 Some Maven Conventions Maven only touches the “target” folder  Locations: - Java sources: /src/main/java - Resources: /src/main/resources - Output: /target/classes - Test sources: /src/test/java - Test resources: /src/test/resources - Test output: /target/test-classes
  • 28.
    28 Generic format:  mvn[ phase | pluginId:goal | -options ] ...  mvn [groupId:]pluginId[:version]:goal implicit groupId = org.apache.maven and maven-{pluginId}-plugin Some examples:  mvn –v  mvn archetype:generate -DartifactId=MyFirstPony  mvn package  mvn clean package  mvn help:effective-pom  mvn dependency:tree Maven Commandline
  • 29.
    29 Properties <project> ... <description>My project ${myProperty}</description> <properties> <myProperty>hasa value</myProperty> </properties> mvn [goals] –DmyProperty="has no value"
  • 30.
    30 Modules – aggregatebuild  The Parent project containing the modules has packaging “pom”  Each module sits in its own subdirectory and is a separate Maven project that needs to be built <modules> <module>core</module> <module>model</module> <module>webservice</module> </modules>
  • 31.
  • 32.
    32 Change the build(other plugin behaviour) based on activation:  Standard activation (activeByDefault)  Command line activation - parameter –PprofileName  Property values  JDK version  OS name, family, version  (in) Existance of a file Profiles
  • 33.
    33  Just adependency with Scope = import  Article by Edwin Derks: “to bom or not to bom” Importing a BOM pom with Dependency Management
  • 34.