LDSTechContributeSelf SupportForumWikiJiraProjects

Intermediate Maven

Revision as of 22:52, 6 June 2011 by Aebrown (Talk | contribs)

This training provides training on how to configure Maven Plugins, Properties, and Profiles. Because Java Stack 3 makes less assumptions about your project's build configuration a greater knowledge of Maven itself is vital to get the most out of development using the Java Stack Tools. By the end of this training you should have a rudimentary understanding of how to configure a Maven Plugin, customize its configuration for your needs, and know how to provide alternate groups of configuration using Maven Profiles.

Contents

Prerequisites

  • Must have previously completed the Introduction to the Java Stack training.
  • Must have previously completed the Introduction to Maven training.
  • Basic knowledge of Java language.
  • Basic knowledge of XML syntax and structure.
  • A workstation running Windows, Linux, or Mac OS.
  • A desire to learn.

Outline

Maven Plugins

  • What is a Maven Plugin
  • Configuring a Maven Plugin
  • Plugin Management
  • Invoking a Maven Plugin
  • Debugging using the Effective Pom Feature

Maven Properties

  • Introduce Project Properties
  • Uses of Project Properties
  • Property Inheritance
  • Configuring Plugin expressions with Properties

Maven Profile

  • Introduce Maven Profiles
  • Discuss Profile Activation

Slide Deck

Media:IntermediateMaven.ppt

Maven Plugins Presentation

Unfortunately we had some audio difficulties in this training. We've cleaned the audio up as much as we could. We'll try to fix the problem for next time.

Get Adobe Flash player

Lab 1 Maven Plugins

  • This lab will continue where the introduction labs left off. If you don't have your introduction lab you can download this one: Media:Finished-introduction-maven-project.zip
  • Add Maven enforcer plugin to your jar project using syntax similar to the following:
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-enforcer-plugin</artifactId>
            <version>1.0</version>
            <configuration>
                <rules>
                    <bannedDependencies>
                        <excludes>
                            <exclude>commons-logging:commons-logging</exclude>
                        </excludes>
                    </bannedDependencies>
                </rules>
            </configuration>
            <executions>
                <execution>
                    <id>execute</id>
                    <goals>
                        <goal>enforce</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
  • This plugin definition will verify that you don't have a commons-logging dependency in your project.
  • Verify this works by temporarily removing the commons-logging exclusion in the parent pom and try to build your project. It should fail with an enforcer rule violation.
  • Move this plugin configuration to the root pom using pluginManagement.
<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-enforcer-plugin</artifactId>
                <version>1.0</version>
                <configuration>
                    <rules>
                        <bannedDependencies>
                            <excludes>
                                <exclude>commons-logging:commons-logging</exclude>
                            </excludes>
                        </bannedDependencies>
                    </rules>
                </configuration>
                <executions>
                    <execution>
                        <id>execute</id>
                        <goals>
                            <goal>enforce</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </pluginManagement>
</build>
  • Modify your plugin definition in your jar project to simply execute the plugin as it is configured in the parent project.
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-enforcer-plugin</artifactId>
        </plugin>
    </plugins>
</build>
  • Ensure everything still works correctly both with and without the commons-logging exclusion.
  • Add an enforcer rule that ensures our HelloWorld class is being compiled. Since only our jar project has the HelloWorld.class file we need this configuration to only apply to our jar project. If we place the configuration in our parent pom then this configuration would apply to all projects that extend this parent. That is NOT what we want. To do this add the following configuration to your jar pom's enforcer:
<configuration>
    <rules>
        <requireFilesExist>
            <files>
                <file>target/classes/HelloWorld.class</file>
            </files>
        </requireFilesExist>
    </rules>
</configuration>
  • Run a Maven clean and then try and do a build.
  • The build should fail looking for the HelloWorld.class. Why does it not work?
  • There are actually 2 reasons why this is incorrect:
  • See if you can figure out how to configure the plugin such that the check for commons-logging takes place in the enforcers default build phase and the file exists test happens in the process-classes phase since that is after compile. Hint, to set a phase for an execution add something like the following as a child of the execution element.
<phase>process-classes</phase>

Lab 1 Solution

Get Adobe Flash player

Maven Properties Presentation

Get Adobe Flash player

Lab 2 Maven Properties

  • Change the reference to target/classes to the maven variable that references build output. Looking at the Effective POM you will see that the correct variable expression for the build output directory is ${project.build.outputDirectory}.
  • Using the properties element add a property to your parent project that skips the execution of the enforcer plugin. You can use syntax similar to the following:
<properties>
    <enforcer.skip>true</enforcer.skip>
</properties>
  • Run several builds purposely creating violations and fixing them to confirm that the enforcer plugin execution is skipped.
  • Attempt to enable the enforcer plugin for a specific build execution by overriding the skip property using a build parameter. The parameter can be added to a build using the parameter portion of the Maven Build dialog. Or using -D from the command line.
  • Make it so that the HelloWorld.class rule can be skipped or not independent of the commons-logging rule.
  • Hint: If both your commons-logging rule and your HelloWorld.class rule are both executing try making sure that your commons-logging rule is in its own execution configuration and not the default configuration.
  • Try to configure these properties in such a way that the enforcer.skip property will enable or disable both rules.

Lab 2 Solution

Get Adobe Flash player

Maven Profiles Presentation

Get Adobe Flash player

Lab 3 Maven Profiles

  • Modify your project so that the enforcer plugin only runs as part of a build on your build box.
  • Add a profile to your root pom to containing the configuration you want enabled on continuous build:
<profiles>
    <profile>
        <id>continuous</id>
        <properties>
            <enforcer.skip>false</enforcer.skip>
        </properties>
    </profile>
</profiles>
  • Perform a build with the continuous profile enabled ensuring violations are only found when this profile is enabled.
  • It is determined by architecture that commons-logging is ok to be used in development but is illegal on the continuous box.
  • Configure your continuous profile to exclude commons-logging for the continuous profile only.
  • Ensure you configured this correctly by enabling the continuous profile in your jar project's eclipse properties.
    • Right Click on your jar project->Properties
    • Then find Maven->Active Maven Profiles
    • Verify commons-logging disappears as a dependency when the continuous profile is enabled
    • Experiment doing builds on the jar project. Notice that Run As->Maven install will execute using the project's profile you set in the project's Properties.

Lab 3 Solution

Get Adobe Flash player