Lab-4 (Jenkins and Maven Configuration)
Lab-4 (Jenkins and Maven Configuration)
Lab-4 (Jenkins and Maven Configuration)
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.
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.
The src/main/java directory contains the project source code and the
src/test/java directory contains the test source.
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:
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:
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/
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.