Lab-4 (Jenkins and Maven Configuration)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Jenkins and Maven

In this lab, you will get a chance to work with Jenkins and Maven. Maven is a
popular build system for the JVM ecosystem and performs a wide array of
useful build and project management tasks. By configuring Jenkins to build
your maven projects you can automate much to the project creation process,
allowing Jenkins to benefit from Maven’s convention over configuration. You
may find that Maven works so well with Jenkins that you decide to adopt it for
some of your non-Maven project.

Like Jenkins, Maven is a Java tool and various versions of Maven require various
versions of Java. Recent versions of Maven require Java 1.7 so our lab system is
ready to use Maven.

This lab begins by creating a simple Maven project at the command line. Once
we have a basic Maven project working we can create a Jenkins Project to build
it automatically.

1. Create a simple Maven Project

To begin we will create a directory in the user home directory to our maven
project:

Next, we will use Maven to create a starter project. Archetypes are the equivalent
of Maven project templates. Using the Maven archetype:generate goal and the
appropriate project archetype we can create a fairly robust starter project. Use the
following command to generate a Maven quick start project:
Command: mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -
DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
[INFO] Parameter: basedir, Value: /home/ubuntu/maventest1
[INFO] Parameter: package, Value: com.mycompany.app
[INFO] Parameter: groupId, Value: com.mycompany.app
[INFO] Parameter: artifactId, Value: my-app
[INFO] Parameter: packageName, Value: com.mycompany.app
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: /home/ubuntu/maventest1/my-app
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.462 s
[INFO] Finished at: 2018-01-21T01:57:31+00:00
[INFO] Final Memory: 14M/51M
[INFO] ----------------------------------------------------------------------

Because we have just installed Maven this will take a minute because Maven
must download the most recent artifacts (plugin jars and other files) into your
local repository (~/.m2). Once the local repository cache is well populated new
project creation will be almost instantaneous.

You will notice that the generate goal created a directory with the same name
given as the artifactId.

Change into that directory.


Under this directory you will notice the Project Object Model file (pom.xml) and
the following standard

Maven project structure:

The src/main/java directory contains the project source code and the
src/test/java directory contains the test source.

The pom.xml file is the core of a project's configuration in Maven. It is a single


configuration file that contains the majority of information required to build a
project in just the way you want. The POM is huge and can be daunting in its
complexity, but it is not necessary to understand all of the intricacies just yet to use
it effectively. This project's POM looks like this:

cat pom.xml
 What type of packaging do you think this POM will produce?
 Does this project have dependencies?

o If so on what?
o What version?
o When would the dependency be in force?
You executed the Maven goal archetype:generate, and passed in various
parameters to that goal. The “archetype” is the plugin that contains the goal
“generate”. If you are familiar with Ant, you may conceive of this as similar to a
task. This goal created a simple project based upon an archetype. A Maven plugin
is essentially a collection of goals with a general common purpose. For example
the jboss-maven-plugin, has the purpose: "deal with various jboss items".
STEP2. Build the Maven Project
Now that we have our Maven project constructed we can build it. Execute the
Maven “package” phase:

Command: mvn package


[INFO] Building jar: /home/ubuntu/maventest1/my-app/target/my-app-1.0-
SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.711 s
[INFO] Finished at: 2018-01-21T02:16:08+00:00
[INFO] Final Memory: 17M/42M
[INFO] ------------------------------------------------------------------------

Again, because this is your first Maven build, several packages will need to be
downloaded into the local Maven repository (you can run the command again if
you like, it will complete in 1-2 seconds the second time). The mvn package
command prints out various action logs ending with Build Success.
Unlike the first command executed (archetype:generate) you may notice the
second is simply a single word - package. Rather than a goal, this is a phase. A
phase is a step in the build lifecycle, which is an ordered sequence of phases.
When a phase is given, Maven will execute every phase in the sequence up to and
including the one defined. For example, if we execute the compile phase, the
phases that actually get executed are:
 validate
 generate-sources
 process-sources
 generate-resources
 process-resources
 compile
Test the newly compiled and packaged JAR with the following command:

ubuntu@ip-172-31-88-146:~/maventest1/my-app$ java -cp


target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
Hello World!
Press Enter
STEP3- Executing Other Maven Project Phases
Although hardly a comprehensive list, these are the most common default lifecycle phases
executed in
Maven:
 validate: validate the project is correct and all necessary information is available
 compile: compile the source code of the project
 test: test the compiled source code using a suitable unit testing framework
 package: take the compiled code and package it in its distributable format, such as a JAR.
 integration-test: process and deploy the package if necessary into an environment
where integration tests can be run
 verify: run any checks to verify the package is valid and meets quality criteria
 install: install the package into the local repository, supports dependencies in other
projects
 deploy: done in an integration or release environment, copies the final package to the
remote repository for sharing with other developers and projects.
There are two other Maven lifecycles of note beyond the default list above. They are:
 clean: cleans up artifacts created by prior builds
 site: generates site documentation for this project

Phases are actually mapped to underlying goals. The specific goals executed per phase is
dependent upon the packaging type of the project. For example, package executes jar:jar if the
project type is a JAR, and war:war if the project type is a WAR.

An interesting thing to note is that phases and goals may be executed in sequence.
Try this Command:
 mvn clean dependency:copy-dependencies package
This is a useful combination of phases and goals for Jenkins builds. This command
will clean the project, copy dependencies, and package the project (executing all
phases up to package, of course). In this way you ensure the dependencies are
downloaded fresh every time. Now try just clean and package:
Enter this command- mvn clean package
One thing you might note is that everything that happens during a build is done by
a plugin. Maven itself essentially does nothing but run plugins that execute goals
underlying phases in a lifecycle.

Next try executing the site phase from the site lifecycle: mvn site

This phase generates a site based upon information on the project's pom.
You can look at the documentation generated under target/site.
For more information on Maven check out the Maven Getting Started Guide:
https://maven.apache.org/guides/getting-started/

4. Creating a Jenkins Maven Project


Now that we have a working Maven project we can configure Jenkins to build
it. Open a browser and point it at your Jenkins Server.

Click New Item in the left side navigation to create a new Maven project.
Enter a name for your project and ensure Maven project is selected then click OK.
When the project configuration screen comes up click Save without making any
changes. Run a build:

The build will fail but it will create the project and the workspace directory.
Before Jenkins can build the project, we need to copy all of the project files to the
new project directory. Normally Jenkins would use a source code control
checkout command to populate this directory, we will try that in a later lab.
Now copy the Maven project to the Jenkins project workspace:

Commands:
 cp –r . ~/.jenkins/workspace/mavenproj1/
 ls –l ~/.jenkins/workspace/mavenproj1/

Now we can configure the Jenkins project to execute goals or phases of our
project. Open the project configuration page and enter the “clean” and
“package” phases.

Save and Build your Maven project


When the build completes refresh your project page. As you can see your Maven
project built successfully and we now have Test results. Explore the test results
links.

5. Adding JavaDoc with the Site lifecycle


Again Open the project configuration page or Edit your Maven project
configuration to execute the “site” phase after the other two phases you already
have.

Next build your project. Now refresh your project page:


You should see your test results trend begin to build and your new site
documentation link. Take a minute to browser the documentation. Once you click
the “Maven-generated site” link you will see boiler plate information about the
“my-app” application and a navigation bar to the left which you can use to pull up
various pages generated to describe your program:

Congratulation you have finished the Maven lab.

You might also like