Lesson 37
Introducing Maven

We have been using Maven to build our projects in NetBeans, but we have not discussed the advantages of using Maven over the build management tools that are built into the IDE. The IDE tools work reasonably well for small, individual projects that don't depend on too many external libraries, but they have the following drawbacks for larger, multideveloper projects:

  • Everyone on the team must use NetBeans.
  • All external JAR files must be referenced directly in the project and must be manually copied to each developer's machine.
  • There is no good way to manage the versions of the required external libraries.
  • There is no way to build the project outside of NetBeans. This severely limits the options the team has for automated build machines and for building and deploying the project to QA, performance test, and production environments.

Maven helps address these issues. Maven is a project management framework that provides IDE-independent build and dependency management tools.

WHAT IS MAVEN?

Maven bills itself as a project management framework. It strives to manage a project's build, reporting, and documentation from one place.

Maven's build management is declarative rather than task oriented. It has a built-in lifecycle, so you simply declare what you want to do, not how to do it. Maven also has declarative dependency management, so you tell Maven what libraries (including version numbers) you need, and it will make sure those libraries are available to your code.

Maven does a lot of things, so covering what it can do is a big subject on its own. In this lesson, we will concentrate on using the built-in Maven lifecycle and the dependency management features to make our projects easier to build and to share with our team.

PROJECT OBJECT MODEL

Maven is based on the project object model (POM). The POM is defined in an XML file called pom.xml. This file contains the declarations for all libraries on which the project depends and can contain declarations of the Java version to use and other project-level settings. Listing 37.1 shows a typical POM file.

LISTING 37.1

A Typical Mavin POM.xml File

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
 
  <groupId>com.sg</groupId>
  <artifactId>MeanMedianMode</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>
 
  <name>MeanMedianMode</name>
  <url>http://maven.apache.org</url>.
 
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
 
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>5.6.0</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
 
    <build>
        <finalName>mean-median-mode</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
 

Let's analyze the POM file. First, it is important to remember that this file is generated by NetBeans when you create your project. We'll just look at the parts of the file that you might want to modify.

Project Identification

The following code contains the tags that identify your project to Maven:

<groupId>com.sg</groupId>
<artifactId>MeanMedianMode</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
 
<name>MeanMedianMode</name>

We learned about most of this in the first part of this course, but it is worth covering each of these tags again.

  • groupID: Traditionally, the groupId is the base package of your project, but this is not a requirement. The groupId is meant to identify the organization with which the project is associated.
  • artifactId: The artifactId is the name that will be given to the file into which this project is packaged.
  • SNAPSHOT: The version indicates the version of the project. It defaults to 1.0-SNAPSHOT in NetBeans, but you can set this to any value.
  • packaging: The packaging tag indicates how you would like the project to be packaged. For now, this will always be jar, which stands for Java Archive. If you were doing web applications, then this would be war, which stands for Web Archive.
  • name: The name tag is, essentially, the name of this project. It does not have to match the artifactId discussed earlier.

Dependencies

The following tags indicate the external Java libraries on which your application depends:

<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>5.6.0</version>
    <scope>test</scope>
  </dependency>
</dependencies>

Each library on which we depend has a separate dependency tag nested in the dependencies tag. In this case, we rely on version 5.6.0 of the JUnit library. Note that each dependency entry is described using the same tags used to describe our project to Maven: groupId, artifactId, and version. The scope tag can be used to limit where and how a particular library will be used. In this case, the value test indicates that the JUnit library should be used only when running unit tests but should not be included when packaging or installing the project.

Build Settings

The build tag contains settings affecting how the project will be built.

<build>
  <finalName>mean-median-mode</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

With the build tags, we are interested in the target and source tags of the compiler plugin. The source tag indicates the Java version that should be used for the source code of the project, and the target tag indicates the version of the JVM on which the project should be run.

DEPENDENCY MANAGEMENT

One of the great features of Maven is dependency management. This means that Maven will manage all the external libraries (packaged as JAR files) that you need to use in your program. You may have noticed that we're using the term dependency here in a different way than we did when we talked about the Spring Framework. The Spring Framework helps us manage how the components of our application relate to and depend on each other at the class and object levels through dependency injection. Maven, on the other hand, helps us manage the external Java libraries that we rely on at a project level.

Without Maven, the developer is responsible for manually identifying, downloading, and including the JAR files of all the libraries on which the project depends. The developer is also responsible for storing and managing these JAR files.

When using Maven, the developer is still responsible for identifying the libraries on which the project depends, but Maven automatically fetches all dependencies into a central repository on your machine (located in the ~/.m2 directory). All Maven projects on your machine share this repository, which means that each library is downloaded only once.

As an added bonus, Maven also handles transitive dependencies automatically. For example, if you declare (in your POM) that your project depends on Library A and it turns out that Library A depends on Libraries B and C, Maven will automatically download all three libraries into your local repository. You do not have to specify (or even be aware) that Library A requires Libraries B and C.

MAVEN LIFECYCLE

Maven's project lifecycle is defined but is flexible. You can change it if you need to but, for most projects, the predefined lifecycle is sufficient. The lifecycle consists of several stages, which are known as goals. These goals are simply the kinds of actions (like compiling and running unit tests) that we need to take as we build a software project. NetBeans hides much of this from us, but these goals are run behind the scenes when we ask NetBeans to build and run our applications.

Developers tend to use the following goals extensively:

  • compile: Compiles the project source code
  • test-compile: Compiles the project test source code
  • test: Runs the project unit tests
  • package: Builds and packages the project
  • install: Installs the project package into the local .m2 repository (the project package can then be used in other projects)

SUMMARY

In this lesson, we took a closer look at Maven and discussed its benefits for larger projects. The main takeaways for this lesson are the following:

  • Maven is an IDE independent build management tool.
  • Maven is a declarative framework. It allows you to say what you want to have done without specifying how it will be done.
  • The Maven build is represented in the project object model, which is defined in the pom.xml file.
  • Maven manages the external libraries on which your project depends. This includes management of transitive dependencies.
  • Maven has a predefined lifecycle made up of several stages called goals.
  • Using Maven, instead of the built-in IDE build management tools, gives software teams more flexibility in choosing which IDE (or IDEs) will be used and allows easy integration with automated build and deployment servers.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.54.6