Intermediate Maven Michael Youngstrom
Notes This is a training NOT a presentation Please ask questions No making fun of my microphone Prerequisites – Introduction to Maven – Basic Java and XML skillz
Outline Review Introduction to Maven Plugins Properties Profiles
Review Maven is a build tool Maven project Structure Maven has a build lifecycle A maven artifact is identified with GAV Maven dependencies are glorious!
Maven Plugins Plugins extend Maven Plugins are identified with GAV Two ways to execute a plugin – Hook into build lifecycle – Invoke standalone
Hooking into build lifecycle Allows plugin to execute as part of maven build Three items to configure a plugin: – Phase – Goal – Configuration
Plugin Example... org.apache.maven.plugins maven-enforcer-plugin execute validate enforce...
Plugin Documentation Use Plugin reference documentation! Full NameDefault PhaseConfiguration
Plugin Configuration Configuration parameters go in the configuration element false... true
PluginManagement PluginManagement configures a plugin without executing it – Version – Configuration – Executions To execute use: – Regular Plugin entry – Plugin standalone command
Plugin Management Example org.apache.maven.plugins maven-enforcer-plugin true org.apache.maven.plugins maven-enforcer-plugin
Plugin Inheritance This is a rather complex topic Plugin inherits PluginManagement configuration Plugins and PluginManagement can inherit config from parent Beyond that you can figure it out on your own
How to Manage POM Complexity Effective POM – Flattens POM configuration – Dependency and DependencyMangement is resolved – Plugin and PluginManagement is resolved – Executions are Resolved – Properties are Resolved (more on this later) – Profiles are Resolved (more on this later) To use: – Select the “Effective POM” tab in pom editor in Eclipse – Invoke mvn help:effective-pom on command line
Invoking Plugins Standalone Plugins can also be invoked adhoc from command line – GroupId:ArtifactId:Version:Goal – Will use Plugin Management configuration – Configuration can be provided by system properties Can be shortened Must be configured in pom plugin or in settings.xml mvn org.apache.maven.plugins:maven-enforcer-plugin:1.0:enforce mvn enforcer:enforce
Plugin Documentation Helps Plugin reference documentation rocks! Short NameFull Name
Setting Configuration Standalone Expression: the property key to this parameter Expression Key mvn enforcer:enforce –Denforcer.fail=false
Lab 1: Maven Plugins _1_Maven_Plugins
Maven Properties Properties are the glue that tie configuration together Properties can come from several places: – element in pom – System Properties – Project Object Model Properties can be used as a value just about anywhere – Plugin Configuration – Resource Filtering Properties can only be simple primitive values
Maven Property Example... true org.apache.maven.plugins maven-enforcer-plugin 1.0 ${skipEnforcer}
POM Properties Element Properties can be nested... true ${skipTests}
System Properties Supplied to the command line using “-D” – mvn install –DskipEnforcer=true Supplied in the IDE using parameters dialog System Properties override POM properties
Project Object Model Properties Properties can be derived from POM structure POM elements = property keys – Expression: ${project.version} Element: – Expression: ${project.artifactId} Element: – Expression: ${project.build.sourceDirectory Special properties: – ${basedir}: Directory of current project – ${maven.build.timestamp} : Start of build.
Inherited Properties Properties can be inherited and overridden Current property value depends upon context... true ${skipTests}... false
Resource Filtering Project resources can use properties Resources filtered in process-resources phase Filtering can be turned off on a per resource directory basis Some Text File in /src/main/resources: ${someProperty} Some Text File in /src/main/resources: SomeValue... SomeValue
Properties and Plugin Expression Properties can also override plugin expression defaults Expression Key
Skip Enforcer Example #1... org.apache.maven.plugins maven-enforcer-plugin 1.0 true
Skip Enforcer Example #2 true org.apache.maven.plugins maven-enforcer-plugin 1.0
Skip Enforcer Example #3 org.apache.maven.plugins maven-enforcer-plugin 1.0 mvn clean install –Denforcer.skip=true
Lab 2: Maven Properties _2_Maven_Properties
Maven Profile Allows activating a set of alternative configurations Works for: – Properties – Dependencies – Plugins – etc. Inherits and extends non-profile configuration
Profile Example... enforcer false
Profile Activation Can be Activated: – By Default – Explicitly by name – Based on a Property – Based on operation system – Existence of a file
Activation from Command Line Activates a profile explicitly Multiple profile ids are comma delimited mvn clean install –P enforcer
Activation in IDE Build Can be set in build command configuration
Activation in IDE Development Can be set in project’s Maven config Usually used to change development mode
Lab 3: Maven Profiles _3_Maven_Profiles
Summary Plugins extend Maven’s capabilities Properties are the glue in Maven configuration Profiles enable alternate configurations Next training understanding Stack Starter’s Maven Configuration