intermediate maven michael youngstrom. notes this is a training not a presentation please ask...

37
Intermediate Maven Intermediate Maven Michael Youngstrom

Upload: jacob-hawkins

Post on 13-Dec-2015

226 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Intermediate Maven Michael Youngstrom. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Maven – Basic

Intermediate MavenIntermediate MavenMichael Youngstrom

Page 2: Intermediate Maven Michael Youngstrom. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Maven – Basic

Notes

• This is a training NOT a presentation• Please ask questions• Prerequisites– Introduction to Maven– Basic Java and XML skillz

Page 3: Intermediate Maven Michael Youngstrom. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Maven – Basic

• Review Introduction to Maven• Plugins• Properties• Profiles

Page 4: Intermediate Maven Michael Youngstrom. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Maven – Basic

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!

Page 5: Intermediate Maven Michael Youngstrom. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Maven – Basic

Maven Plugins

• Plugins extend Maven• Plugins are identified with GAV• Two ways to execute a plugin– Hook into build lifecycle– Invoke standalone

Page 6: Intermediate Maven Michael Youngstrom. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Maven – Basic

Hooking into build lifecycle

• Allows plugin to execute as part of maven build• Three items to configure a plugin:– Phase– Goal– Configuration

Page 7: Intermediate Maven Michael Youngstrom. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Maven – Basic

Plugin Example<project> ... <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-enforcer-plugin</artifactId> <version>1.0</version> <configuration> ... </configuration> <executions> <execution> <id>execute</id> <phase>validate</phase> <goals><goal>enforce</goal></goals> <configuration> ... </configuration> </execution> </executions> </plugin> </plugins> </build></project>

Page 8: Intermediate Maven Michael Youngstrom. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Maven – Basic

Plugin Documentation

• Use Plugin reference documentation!

Full Name

Default Phase

Configuration

Page 9: Intermediate Maven Michael Youngstrom. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Maven – Basic

Plugin Configuration

• Configuration parameters go in the configuration element.<project> ... <plugin> ... <configuration> <fail>false</fail> </configuration> <executions> <execution> ... <configuration> <failFast>true</failFast> </configuration> </execution> </executions> </plugin></project>

Page 10: Intermediate Maven Michael Youngstrom. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Maven – Basic

PluginManagement

• PluginManagement configures a plugin without executing it– Version– Configuration– Executions

• To execute use:– Regular Plugin entry– Plugin command

Page 11: Intermediate Maven Michael Youngstrom. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Maven – Basic

Plugin Mangement Example

<project> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-enforcer-plugin</artifactId> <version>1.0</version> <configuration> ... <ignoreCache>true</ignoreCache> </configuration> </plugin> </plugins> </pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-enforcer-plugin</artifactId> </plugin> </plugins> </build></project>

Page 12: Intermediate Maven Michael Youngstrom. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Maven – Basic

Plugin Inheritance

• This is a complex world!• Configuration– Plugin Inherits PluginManagement configuration– First level configuration elements are inherited– Execution configuration extend base configuration

Page 13: Intermediate Maven Michael Youngstrom. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Maven – Basic

How to Mange 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

Page 14: Intermediate Maven Michael Youngstrom. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Maven – Basic

Invoking Plugins Standalone

• Plugins can also be invoked adhoc from command line– GroupId:ArtifactId:Version:Goal– Will use Plugin Management configuration

• 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

Page 15: Intermediate Maven Michael Youngstrom. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Maven – Basic

Plugin Documentation Helps

• Plugin reference documentation is your friend!Short Name

Full Name

Page 16: Intermediate Maven Michael Youngstrom. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Maven – Basic

Setting Configuration Standalone

• Expression: shows the key to this parameter

Expression Key

mvn enforcer:enforce –Denforcer.fail=false

Page 17: Intermediate Maven Michael Youngstrom. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Maven – Basic

Lab 1: Maven Plugins

https://tech.lds.org/wiki/Intermediate_Maven#Lab_1_Maven_Plugins

Page 18: Intermediate Maven Michael Youngstrom. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Maven – Basic

Maven Properties

• Properties are the glue that tie configuration together• Properties can come from many places:– <properties/> element in pom– System Properties– Project Object Model– Inherited from parent

• Properties can be used as a value just about anywhere– Plugin Configuration– Resource Filtering

• Properties can only be simple primitive values• Properties follow the syntax ${someProperty}

Page 19: Intermediate Maven Michael Youngstrom. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Maven – Basic

Maven Property Example

<project> ... <properties> <skipEnforcer>true</skipEnforcer> </properties> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-enforcer-plugin</artifactId> <version>1.0</version> <configuration> <skip>${skipEnforcer}</skip> </configuration> </plugin> </plugins> </build></project>

Page 20: Intermediate Maven Michael Youngstrom. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Maven – Basic

POM Properties Element

• Properties can be nested

<project> ... <properties> <skipTests>true</skipTests> <skipEnforcer>${skipTests}</skipEnforcer> </properties></project>

• System Properties override POM properties

Page 21: Intermediate Maven Michael Youngstrom. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Maven – Basic

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

Page 22: Intermediate Maven Michael Youngstrom. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Maven – Basic

Project Object Model Properties

• Properties can be derived from POM structure• POM elements = property keys– Expression: ${project.version}

• Element: <project><version/></project>– Expression: ${project.artifactId}

• Element: <project><artifactId/></project>– Expression: ${project.build.sourceDirectory

• <project><build><sourceDirectory/></build></project>• Special properties:– ${basedir}: Directory of current project– ${maven.build.timestamp} : Start of build.

Page 23: Intermediate Maven Michael Youngstrom. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Maven – Basic

Inherited Properties

• Properties can be inherit and overridden

<project> ... <properties> <skipTests>true</skipTests> <skipEnforcer>${skipTests}</skipEnforcer> </properties></project>

<project> <parent> ... </parent> <properties> <skipTests>false</skipTests> </properties></project>

Page 24: Intermediate Maven Michael Youngstrom. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Maven – Basic

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

<project> ... <properties> <someProperty>SomeValue</someProperty> </properties></project>

Page 25: Intermediate Maven Michael Youngstrom. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Maven – Basic

Properties and Plugin Expression

• Properties can also override plugin expression defaults

Expression Key

Page 26: Intermediate Maven Michael Youngstrom. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Maven – Basic

Skip Enforcer Example #1

<project> ... <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-enforcer-plugin</artifactId> <version>1.0</version> <configuration> <skip>true</skip> </configuration> </plugin></project>

Page 27: Intermediate Maven Michael Youngstrom. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Maven – Basic

Skip Enforcer Example #2

<project> <properties> <enforcer.skip>true</enforcer.skip> </properties> <build> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-enforcer-plugin</artifactId> <version>1.0</version> </plugin> </build></project>

Page 28: Intermediate Maven Michael Youngstrom. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Maven – Basic

Skip Enforcer Example #3

<project> <build> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-enforcer-plugin</artifactId> <version>1.0</version> </plugin> </build></project>

mvn clean install –Denforcer.skip=true

Page 29: Intermediate Maven Michael Youngstrom. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Maven – Basic

Lab 2: Maven Properties

https://tech.lds.org/wiki/Intermediate_Maven#Lab_2_Maven_Properties

Page 30: Intermediate Maven Michael Youngstrom. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Maven – Basic

Maven Profile

• Allows activating a set of alternative configurations

• Works for:– Properties– Dependencies– Plugins– etc.

• Inherits and extends non-profile configuration

Page 31: Intermediate Maven Michael Youngstrom. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Maven – Basic

Profile Example

<project> ... <profiles> <profile> <id>enforcer</id> <activation/> <properties> <enforcer.skip>false</enforcer.skip> </properties> </profile> </profiles></project>

Page 32: Intermediate Maven Michael Youngstrom. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Maven – Basic

Profile Activation

• Can be Activated:– By Default– Explicitly by name– Based on a Property– Based on operation system– Existence of a file

Page 33: Intermediate Maven Michael Youngstrom. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Maven – Basic

Activation from Command Line

• Activates a profile explicitly• Multiple profile ids are comma delimited

mvn clean install –P enforcer

Page 34: Intermediate Maven Michael Youngstrom. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Maven – Basic

Activation in IDE Build

• Can be set in build command configuration

Page 35: Intermediate Maven Michael Youngstrom. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Maven – Basic

Activation in IDE Development

• Can be set in project’s Maven config• Usually used to change development mode

Page 36: Intermediate Maven Michael Youngstrom. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Maven – Basic

Lab 3: Maven Profiles

https://tech.lds.org/wiki/Intermediate_Maven#Lab_3_Maven_Profiles

Page 37: Intermediate Maven Michael Youngstrom. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Maven – Basic

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