370 likes | 483 Views
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.
E N D
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 <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>
Plugin Documentation • Use Plugin reference documentation! Full Name Default Phase Configuration
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>
PluginManagement • PluginManagement configures a plugin without executing it • Version • Configuration • Executions • To execute use: • Regular Plugin entry • Plugin standalone command
Plugin Management 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>
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 mvnhelp: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 pomplugin or in settings.xml mvn org.apache.maven.plugins:maven-enforcer-plugin:1.0:enforce mvnenforcer:enforce
Plugin Documentation Helps • Plugin reference documentation rocks! Short Name Full Name
Setting Configuration Standalone • Expression: the property key to this parameter Expression Key mvnenforcer:enforce –Denforcer.fail=false
Lab 1: Maven Plugins https://tech.lds.org/wiki/Intermediate_Maven#Lab_1_Maven_Plugins
Maven Properties • Properties are the glue that tie configuration together • Properties can come from several places: • <properties/> 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 <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>
POM Properties Element • Properties can be nested <project> ... <properties> <skipTests>true</skipTests> <skipEnforcer>${skipTests}</skipEnforcer> </properties> </project>
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: <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.
Inherited Properties • Properties can be inherited and overridden • Current property value depends upon context <project> ... <properties> <skipTests>true</skipTests> <skipEnforcer>${skipTests}</skipEnforcer> </properties> </project> <project> <parent> ... </parent> <properties> <skipTests>false</skipTests> </properties> </project>
Resource Filtering • Project resources can use properties • Resources filtered in process-resources phase • Filtering can be turned off on a per resource directory basis <project> ... <properties> <someProperty>SomeValue</someProperty> </properties> </project> Some Text File in /src/main/resources: ${someProperty} Some Text File in /src/main/resources: SomeValue
Properties and Plugin Expression • Properties can also override plugin expression defaults Expression Key
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>
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>
Skip Enforcer Example #3 mvn clean install –Denforcer.skip=true <project> <build> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-enforcer-plugin</artifactId> <version>1.0</version> </plugin> </build> </project>
Lab 2: Maven Properties https://tech.lds.org/wiki/Intermediate_Maven#Lab_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 <project> ... <profiles> <profile> <id>enforcer</id> <activation/> <properties> <enforcer.skip>false</enforcer.skip> </properties> </profile> </profiles> </project>
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 https://tech.lds.org/wiki/Intermediate_Maven#Lab_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