LDSTechForumProjects

Introduction to Maven

« Back to Java Stack Training

This training provides some basic knowledge about Maven and how it works. Because Java Stack 3 makes fewer 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 use Maven sufficient for basic development tasks.

Prerequisites

  • Must have previously completed the Introduction to the Java Stack 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 Introduction

  • Overview of what Maven is.
  • Discuss Maven Features.
  • Identify Maven resources.

The pom.xml File and Maven Project Structure

  • What is a Maven module
  • pom.xml basics
  • Cover packaging types
  • Overview Maven project structure conventions
  • Multi Module Projects/Inheritence
  • Introduce the Maven build lifecycle

Project Dependencies

  • How to add a Dependency
  • Discuss Maven Repositories
  • Introduce transitive and optional dependencies
  • Discuss dependency exclusion
  • Cover the Dependency Management concept

Slide Deck

Media:IntroductionToMaven.ppt

Introduction Presentation

Get Adobe Flash player

Lab 1 Learning Resources

Presentation The Maven Project

Get Adobe Flash player

Lab 2 Create a Maven Project

Create a Maven Project

  • Create a Maven project that produces a jar. GAV=org.lds.training:maven-training:1.0
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <artifactId>maven-training</artifactId>
    <groupId>org.lds.training</groupId>
    <packaging>jar</packaging>
    <version>1.0</version>
</project>
  • Import this project into the LDSTech IDE.
    • File->Import...
    • Maven->Existing Maven Projects then click Next
    • In Root Directory: specify the directory of the module you created.
    • Click "Finish".

Perform a Maven Build

  • Build this project
    • Right Click on project
    • Select Run As->Maven package
    • Peruse your Maven project build log
  • Find your created module in your target directory.
  • Clean your target directory by invoking a Maven clean.

Configuring java source directory

  • Add a Java class to your project.
    • Create the src/main/java directories in your project.
    • Update your Eclipse project configuration. Right click on the project and select Maven->Update Project Configuration. Note! You may need to resync eclipse with the file system by right clicking on the project->Refresh.
    • Notice how a java source folder was added to your Eclipse project under "src/main/java".
    • In the java source directory add a HelloWorld.java file like the following:
public class HelloWorld {
    public static void main(String[] args) throws Exception {
        System.out.println("Hello World!!!!");
    }
}

Create a Multi module project

  • Turn your project into a multi-module project
    • Create a parent pom using packaging type pom. GAV=org.lds.training:maven-training-parent:1.0
    • Make your jar project a child of this parent project
      • Organize the directory structure correctly.
parent-folder-->pom.xml
                maven-training-->pom.xml
                                 src-->main-->java-->HelloWorld.java
      • Add a modules entry into your parent project.
<modules>
    <module>maven-training</module>
</modules>
      • Add a parent element to your child project.
<parent>
    <artifactId>maven-training-parent</artifactId>
    <groupId>org.lds.training</groupId>
    <version>1.0</version>
</parent>
    • Inherit the groupId and version of your child project from the parent by deleting the respective elements from your child project pom.
  • Run a package build from the parent project and make sure everything is still building correctly.

Create a web module

  • Create a web project as another child of the parent project. GAV=org.lds.training:maven-training-web:1.0 Be sure to use packaging type war.
    • Don't forget to add a module entry for this new project in the parent.
    • Since war projects require a web.xml create the following file and directories src/main/webapp/WEB-INF/web.xml
  • Re-import the new multi-module project into eclipse.
    • Delete the project from eclipse.
      • Right Click on project.
      • Select "Delete"
      • Don't check Delete project contents on disk
      • Select OK
    • Re-import your project following the instructions above.
  • Build your multi-module project by following steps above but do it against your parent project so that all modules get built.
  • View the artifacts built in your target directories.

Presentation Lab 2 Solution

Get Adobe Flash player

Presentation Maven Dependencies

Get Adobe Flash player

Lab 3 Manage Maven Dependencies

Adding a Dependency

  • Add a dependency on log4j to your web project by adding the following to your web project pom.
<dependencies>
    <dependency>
        <groupId>log4j</groupId>
	<artifactId>log4j</artifactId>
	<version>1.2.16</version>
    </dependency>
</dependencies>
  • Notice how your eclipse project dependencies automatically changed
    • View your web project->Java Resources->Maven Dependencies
    • If you don't see the dependency try updating your project by selecting your web project->Maven->Update Project Configuration
  • Build your project and install it in your local maven repository by invoking Run As->Maven install.
  • Find your built artifacts in your local maven repository.
  • Open your built .war with a zip viewer and notice the new dependency in the artifact's /WEB-INF/lib directory.
  • Add a dependency on your jar project (maven-training) to your war project (maven-training-web).
  • Notice your java libraries changed again. View your web project->Java Resources->Web App Libraries.

Transitive Dependencies

  • Add a dependency on spring-core to your jar project: GAV=org.springframework:spring-core:3.0.5.RELEASE
  • Notice how dependencies were transitively to your war project.
  • Exclude commons-logging
    • Add an exclusion to spring-core dependency by adding the following inside the dependency element.
<exclusions>
    <exclusion>
        <groupId>commons-logging</groupId>
    	<artifactId>commons-logging</artifactId>
    </exclusion>
</exclusions>
  • Notice how the commons-logging dependency has disappeared from the projects.

Dependency Management

  • Add a spring-core dependency to dependencyManagement section to your parent pom.
<dependencyManagement>
    <dependencies>
        <dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-core</artifactId>
	    <version>3.0.5.RELEASE</version>
	</dependency>
    </dependencies>
</dependencyManagement>
  • Delete spring-core version element from child pom.
  • Notice how the dependency version is resolved from the parent pom.
  • Move the commons-logging exclusion to the dependencyManagement section.
  • Notice how commons-logging is still excluded.

Presentation Lab 3 Solution and Conclusion

Get Adobe Flash player

« Back to Java Stack Training
This page was last modified on 14 February 2013, at 11:59.

Note: Content found in this wiki may not always reflect official Church information. See Terms of Use.