DevOps - Module 3
DevOps - Module 3
DevOps - Module 3
Software build tools are used to automate the creation of executable applications from source
code. They script or automate a variety of tasks, such as downloading dependencies, compiling
and packaging code, running tests, and deployment. They can be triggered through the
command line, inside an IDE, or by continuous integration tools after checking the code out of a
repository and onto a build machine. Different build systems exist for different languages.
Usually, build systems use either a domain-specific language or XML to specify a build.
There are three types of build processes that enable developers to get their software builds
done:
1. Continuous Integration (CI) builds: In this practice, developers merge their code to a
centralized mainline several times a day. This prevents integration problems with incompatible
or buggy code when all of the developers attempt to check in at the last minute prior to a
release. While deployments involve simply preparing the code so it can be run, CI builds involve
running the code against a test suite.
2. Gated check-in builds: Also known as gated commits, this is a software integration pattern
where developers must request a gated commit from the CI server before committing the
changes to the shared mainline. That way, they can observe whether the changes break the
build before actually committing them.
3. Nightly builds: Also referred to as scheduled builds, the name pretty much describes the
process. Builds are done at a scheduled time on a daily basis. This process usually occurs at
night so that the code can be compiled by the build computer during off hours, then tested by
developers the following morning.
In this section, we will generally be referring to CI builds and, to a lesser extent, gated check-in
builds.
Modern build automation tools don’t just compile code; they enable workflows by obtaining
source code, deploying executables to be tested, and optimizing complex build processes.
There are numerous build tools with all sorts of trade-offs between them, but usually they are
based on a specific language. For an example, Maven and Ant are XML-based tools, Grunt is
JSON-based, and sbt is Scala-based.
Build tool features might include: running ad-hoc commands, ordering the execution of
commands based on dependencies, parallelizing different commands, overseeing file changes
and running commands based on those changes, using external processes such as compilers,
allowing configuration and re-configuration, and downloading dependencies.
Build automation tools are crucial for moving towards a continuous delivery (CD) model. They
allow you to build your project with the click of a button, which reduces errors from manually
running steps, and also increases the consistency of the process. Automated builds make sure
that the whole team knows almost immediately when something goes wrong, and who is
responsible for the error. They are especially important in environments with multiple
interdependent projects, helping to ensure that people don’t break anything when working
concurrently on different projects. In addition, outside groups (such as marketing and beta
customer sites) can pick a stable build and use it while the product or feature is still in progress.
When combined with a solid source control system, build tools allow developers to maintain an
archive of builds so that they can backtrack and identify when a bug first appeared in the
system. This improves communication and transparency in an organization. Some build tools
can even capture metrics for code coverage, code complexity, and features complete that help
developers to control code quality and track efficiency.
Add testing to the equation, and you’ll gain the ability to always know the state of your software.
That way, you can safely decide whether or not to deploy to production without worrying about
unforeseen bugs.
Leiningen
Leiningen is a build tool for Clojure. It serves as an adapter between the command line
and Clojure, making it easier for you to run Clojure functions.
Packer
HashiCorp Packer is a free and open source tool for creating identical machine images
for multiple platforms from a single source configuration. Its advantages include: faster
infrastructure deployment, multi-provider portability, improved stability, and greater
testability.
Gulp
Gulp is a JavaScript task runnner that automates painful or time-consuming tasks in your
development workflow, such as: bundling and minifying libraries and stylesheets,
running unit tests, and running code analysis. Its purpose and features are very similar
to those of Grunt.
NAnt
NAnt is a free and open source build automation tool similar to Apache Ant, but targeted
at the .NET environment instead of Java. It can perform tasks such as compiling source
code and resource files into assemblies, running unit tests, configuring build-specific
settings, and more.
Grunt
Grunt is a JavaScript task runner that automates repetitive tasks like minification,
compilation, unit testing, and linting. It also has a large ecosystem of plugins that extend
base functionality.
Ant
Apache Ant is one of the more venerable Java-based build automation tools, originating
from the Apache Tomcat project in 2000. Its strengths are portability, flexibility, and
simplicity. It does not impose coding conventions or project structures, and its build files
can easily be transferred to other platforms because they inherit the Java platform’s
independence. By the same token, Ant files can be difficult to understand and grasp
immediately.
Ant does not have built-in support for dependency management; rather, Ant’s
dependency management is handled by Apache Ivy, a sub-project that integrates with
Ant.
Maven
Apache Maven is a dependency management and build automation tool from the
Apache Software Foundation. Based on the concept of a project object model (POM),
Maven can manage a project’s build, reporting and documentation from a central piece
of information. It uses XML to configure projects and Java to write extensions.
Maven emerged as a result of various frustrations with Ant. Like Ant, it uses XML for
project configuration, but with more conventions and predefined commands that provide
a framework in which to work. It also has built-in dependency management, which Ant
lacks.
Docker
Docker containers are the preferred replacement for Virtual Machines (VMs), given that
they boot faster, perform better, and consume less memory resources. Docker
Containers are also able to share a single kernel and share application libraries.
Gradle
Gradle Build Tool is an open source build automation system designed for multi-project
builds. It supports Maven and Ivy repositories for retrieving dependencies so that you
can reuse artifacts of existing build systems.
Essentially, Gradle was built to combine the best parts of established tools like Ant and
Maven, but with a Groovy-based Domain Specific Language (DSL) instead of XML for
declaring project configuration. DSL tends to be easier to read, more succinct, and also
more powerful.
SBT
Simple Build Tool (SBT) by Scala is an open-source build tool for Java and Scala
projects. Its features include incremental compilation and an interactive shell.
What is Mavan
https://maven.apache.org/plugins/index.html
Introduction
Maven, a Yiddish word meaning accumulator of knowledge, began as an attempt to simplify the
build processes in the Jakarta Turbine project. There were several projects, each with their own
Ant build files, that were all slightly different. JARs were checked into CVS. We wanted a
standard way to build the projects, a clear definition of what the project consisted of, an easy
way to publish project information, and a way to share JARs across several projects.
The result is a tool that can now be used for building and managing any Java-based project. We
hope that we have created something that will make the day-to-day work of Java developers
easier and generally help with the comprehension of any Java-based project.
Maven’s Objectives
Maven’s primary goal is to allow a developer to comprehend the complete state of a
development effort in the shortest period of time. In order to attain this goal, Maven deals with
several areas of concern:
Third party code analysis products also provide Maven plugins that add their reports to the
standard information given by Maven.
Providing guidelines for best practices development
Maven aims to gather current principles for best practices development and make it easy to
guide a project in that direction.
For example, specification, execution, and reporting of unit tests are part of the normal build
cycle using Maven. Current unit testing best practices were used as guidelines:
Maven also assists in project workflow such as release and issue management.
Maven also suggests some guidelines on how to layout your project’s directory structure. Once
you learn the layout, you can easily navigate other projects that use Maven.
While takes an opinionated approach to project layout, some projects may not fit with this
structure for historical reasons. While Maven is designed to be flexible to the needs of different
projects, it cannot cater to every situation without compromising its objectives.
If your project has an unusual build structure that cannot be reorganized, you may have to forgo
some features or the use of Maven altogether.
What is Maven Not?
You might have heard some of the following things about Maven:
While Maven does these things, as you can read above in the “What is Maven?” section, these
are not the only features Maven has, and its objectives are quite different.
Feature Summary
The following are the key features of Maven in a nutshell:
Simple project setup that follows best practices - get a new project or module started
in seconds
Consistent usage across all projects - means no ramp up time for new developers
coming onto a project
Superior dependency management including automatic updating, dependency
closures (also known as transitive dependencies)
Able to easily work with multiple projects at the same time
A large and growing repository of libraries and metadata to use out of the box, and
arrangements in place with the largest Open Source projects for real-time availability
of their latest releases
Extensible, with the ability to easily write plugins in Java or scripting languages
Instant access to new features with little or no extra configuration
Ant tasks for dependency management and deployment outside of Maven
Model based builds: Maven is able to build any number of projects into predefined
output types such as a JAR, WAR, or distribution based on metadata about the
project, without the need to do any scripting in most cases.
Coherent site of project information: Using the same metadata as for the build
process, Maven is able to generate a web site or PDF including any documentation
you care to add, and adds to that standard reports about the state of development of
the project. Examples of this information can be seen at the bottom of the left-hand
navigation of this site under the "Project Information" and "Project Reports"
submenus.
Release management and distribution publication: Without much additional
configuration, Maven will integrate with your source control system (such as
Subversion or Git) and manage the release of a project based on a certain tag. It can
also publish this to a distribution location for use by other projects. Maven is able to
publish individual outputs such as a JAR, an archive including other dependencies
and documentation, or as a source distribution.
Dependency management: Maven encourages the use of a central repository of
JARs and other dependencies. Maven comes with a mechanism that your project's
clients can use to download any JARs required for building your project from a
central JAR repository much like Perl's CPAN. This allows users of Maven to reuse
JARs across projects and encourages communication between projects to ensure
that backward compatibility issues are dealt with.
Maven Plugins
https://maven.apache.org/guides/introduction/introduction-to-plugins.html#:~:text=Plugins
%20are%20the%20central%20feature,Project%20Object%20Model%20(POM).
Maven consists of a core engine which provides basic project-processing capabilities and build-
process management, and a host of plugins which are used to execute the actual build tasks.
What is a Plugin?
"Maven" is really just a core framework for a collection of Maven Plugins. In other words, plugins
are where much of the real action is performed, plugins are used to: create jar files, create war
files, compile code, unit test code, create project documentation, and on and on. Almost any
action that you can think of performing on a project is implemented as a Maven plugin.
Plugins are the central feature of Maven that allow for the reuse of common build logic across
multiple projects. They do this by executing an "action" (i.e. creating a WAR file or compiling unit
tests) in the context of a project's description - the Project Object Model (POM). Plugin behavior
can be customized through a set of unique parameters which are exposed by a description of
each plugin goal (or Mojo).
One of the simplest plugins in Maven is the Clean Plugin. The Maven Clean plugin (maven-
clean-plugin) is responsible for removing the target directory of a Maven project. When you run
"mvn clean", Maven executes the "clean" goal as defined in the Clean plug-in, and the target
directory is removed. The Clean plugin defines a parameter which can be used to customize
plugin behavior, this parameter is called outputDirectory and it defaults to $
{project.build.directory}.
What is a Mojo (And Why the H--- is it Named 'Mojo')?
A Mojo is really just a goal in Maven, and plug-ins consist of any number of goals (Mojos).
Mojos can be defined as annotated Java classes or Beanshell script. A Mojo specifies metadata
about a goal: a goal name, which phase of the lifecycle it fits into, and the parameters it is
expecting.
MOJO is a play on POJO (Plain-old-Java-object), substituting "Maven" for "Plain". Mojo is also
an interesting word (see definition). From Wikipedia, a "mojo" is defined as: "...a small bag worn
by a person under the clothes (also known as a mojo hand). Such bags were thought to have
supernatural powers, such as protecting from evil, bringing good luck, etc."
In Maven, there are two kinds of plugins, build and reporting:
All plugins should have minimal required information: groupId , artifactId and version .
https://maven.apache.org/plugins/index.html
Available Plugins
Maven is - at its heart - a plugin execution framework; all work is done by plugins. Looking for a
specific goal to execute? This page lists the core plugins and others. There are the build and the
reporting plugins:
Build plugins will be executed during the build and they should be configured in
the <build/> element from the POM.
Reporting plugins will be executed during the site generation and they should be
configured in the <reporting/> element from the POM. Because the result of a
Reporting plugin is part of the generated site, Reporting plugins should be both
internationalized and localized. You can read more about the localization of our
plugins and how you can help.
install B 3.0.0- Install the built artifact into the local repository.
M1
resources B 3.2.0 Copy the resources to the output directory for including in the JAR.
verifier B 1.1 Useful for integration tests - verifies the existence of certain conditions.
ejb B 3.1.0 Build an EJB (and optional client) from the current project.
app- B 3.1.0 Build a JavaEE application client from the current project.
client/acr
shade B 3.2.4 Build an Uber-JAR from the current project, including dependencies.
source B 3.2.1 Build a source-JAR from the current project.
changes B+R 2.12.1 Generate a report from an issue tracker or a change document.
antrun B 3.0.0 Run a set of ant tasks from a phase of the build.
enforcer B 3.0.0- Environmental constraint checking (Maven Version, JDK etc), User
M3 Custom Rule Execution.
help B 3.2.0 Get information about the working environment for the project.
invoker B+R 3.2.2 Run a set of Maven projects and verify the output.
patch B 1.2 Use the gnu patch tool to apply patch files to source code.
plugin B+R 3.6.1 Create a Maven plugin descriptor for any mojos found in the source tree,
to include in the JAR.
release B 3.0.0- Release the current project - updating the POM and tagging in the SCM.
M4
remote- B 1.7.0 Copy remote resources to the output directory for inclusion in the
resources artifact.
scripting B 3.0.0 The Maven Scripting Plugin wraps the Scripting API according to
JSR223.
wrapper B 3.0.2 Download and unpack the maven wrapper distribution (works only with
Maven 4)
Plugin (see complete Description
list with version)
animal-sniffer Build signatures of APIs (JDK for example) and checks your classes against them.
versions Manage versions of your project, its modules, dependencies and plugins.
Misc
A number of other projects provide their own Maven plugins. This includes:
jetty Jetty Project Jetty Run a Jetty container for rapid webapp development.
Apache Apache Run an Apache Tomcat container for rapid webapp development.
Tomcat Tomcat
Project
OWASP OWASP Run OWASP Dependency-Check, a utility that identifies project dependencie
dependency Dependency- publicly disclosed, vulnerabilities.
check Project
-check
1. mvn archetype:generate
Provided Archetypes
Maven provides several Archetype artifacts:
maven-archetype-site An archetype to generate a sample Maven site which demonstrates some of the sup
and FML and demonstrates how to i18n your site.
For more information on these archetypes, please refer to the Maven Archetype Bundles page.
What makes up an Archetype?
Archetypes are packaged up in a JAR and they consist of the archetype metadata which
describes the contents of archetype, and a set of Velocity templates which make up the
prototype project. If you would like to know how to make your own archetypes, please refer to
our Guide to creating archetypes.
1. <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
3. <modelVersion>4.0.0</modelVersion>
4.
5. <groupId>my.groupId</groupId>
6. <artifactId>my-archetype-id</artifactId>
7. <version>1.0-SNAPSHOT</version>
8. <packaging>maven-archetype</packaging>
9.
10. <build>
11. <extensions>
12. <extension>
13. <groupId>org.apache.maven.archetype</groupId>
14. <artifactId>archetype-packaging</artifactId>
15. <version>3.1.1</version>
16. </extension>
17. </extensions>
18. </build>
19. </project>
All you need to specify is a groupId , artifactId and version . These three parameters will be
needed later for invoking the archetype via archetype:generate from the commandline.
1. <archetype-descriptor
2. xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-
descriptor/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3. xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-
plugin/archetype-descriptor/1.1.0 https://maven.apache.org/xsd/archetype-
descriptor-1.1.0.xsd"
4. name="quickstart">
5. <fileSets>
6. <fileSet filtered="true" packaged="true">
7. <directory>src/main/java</directory>
8. </fileSet>
9. <fileSet>
10. <directory>src/test/java</directory>
11. </fileSet>
12. </fileSets>
13. </archetype-descriptor>
The attribute name tag should be the same as the artifactId in the archetype pom.xml .
The boolean attribute partial show if this archetype is representing a full Maven project or only
parts.
The requiredProperties , fileSets and modules tags represent the differents parts of the
project:
At this point one can only specify individual files to be created but not empty directories.
Thus the quickstart archetype shown above defines the following directory structure:
archetype
|-- pom.xml
`-- src
`-- main
`-- resources
|-- META-INF
| `-- maven
| `--archetype-metadata.xml
`-- archetype-resources
|-- pom.xml
`-- src
|-- main
| `-- java
| `-- App.java
`-- test
`-- java
`-- AppTest.java
3. Create the prototype files and the prototype pom.xml
1. <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
3. <modelVersion>4.0.0</modelVersion>
4.
5. <groupId>${groupId}</groupId>
6. <artifactId>${artifactId}</artifactId>
7. <version>${version}</version>
8. <packaging>jar</packaging>
9.
10. <name>${artifactId}</name>
11. <url>http://www.myorganization.org</url>
12.
13. <dependencies>
14. <dependency>
15. <groupId>junit</groupId>
16. <artifactId>junit</artifactId>
17. <version>4.12</version>
18. <scope>test</scope>
19. </dependency>
20. </dependencies>
21. </project>
1. mvn install
Now that you have created an archetype, you can try it on your local system by using the
following command. In this command, you need to specify the full information about the
archetype you want to use (its groupId , its artifactId , its version ) and the information about
the new project you want to create ( artifactId and groupId ). Don't forget to include the version
of your archetype (if you don't include the version, you archetype creation may fail with a
message that version:RELEASE was not found)
1. mvn archetype:generate \
2. -DarchetypeGroupId=<archetype-groupId> \
3. -DarchetypeArtifactId=<archetype-artifactId> \
4. -DarchetypeVersion=<archetype-version> \
5. -DgroupId=<my.groupid> \
6. -DartifactId=<my-artifactId>
Once you are happy with the state of your archetype, you can deploy (or submit it to Maven
Central) it as any other artifact and the archetype will then be available to any user of Maven.
Alternative way to start creating your Archetype
Instead of manually creating the directory structure needed for an archetype, simply use
1. mvn archetype:generate
4. -DarchetypeGroupId=org.apache.maven.archetypes
5. -DarchetypeArtifactId=maven-archetype-archetype
Afterwhich, you can now customize the contents of the archetype-resources directory,
and archetype-metadata.xml , then, proceed to Step#4 (Install the archetype and run the
archetype plugin).
Project Object Model (POM)
https://www.tutorialspoint.com/maven/maven_pom.htm
POM stands for Project Object Model. It is fundamental unit of work in Maven. It is an XML file
that resides in the base directory of the project as pom.xml.
The POM contains information about the project and various configuration detail used by
Maven to build the project(s).
POM also contains the goals and plugins. While executing a task or goal, Maven looks for the
POM in the current directory. It reads the POM, gets the needed configuration information, and
then executes the goal. Some of the configuration that can be specified in the POM are
following −
project dependencies
plugins
goals
build profiles
project version
developers
mailing list
Before creating a POM, we should first decide the project group (groupId), its name (artifactId)
and its version as these attributes help in uniquely identifying the project in repository.
POM Example
<groupId>com.companyname.project-group</groupId>
<artifactId>project</artifactId>
<version>1.0</version>
</project>
It should be noted that there should be a single POM file for each project.
All POM files require the project element and three mandatory fields: groupId,
artifactId, version.
Projects notation in repository is groupId:artifactId:version.
Minimal requirements for a POM −
1
Project root
This is project root tag. You need to specify the basic schema settings such as
apache schema and w3.org specification.
2
Model version
Model version should be 4.0.0.
3
groupId
This is an Id of project's group. This is generally unique amongst an
organization or a project. For example, a banking group com.company.bank
has all bank related projects.
4
artifactId
This is an Id of the project. This is generally name of the project. For example,
consumer-banking. Along with the groupId, the artifactId defines the artifact's
location within the repository.
5
version
This is the version of the project. Along with the groupId, It is used within an
artifact's repository to separate versions from each other. For example −
com.company.bank:consumer-banking:1.0
com.company.bank:consumer-banking:1.1.
Super POM
The Super POM is Maven’s default POM. All POMs inherit from a parent or default (despite
explicitly defined or not). This base POM is known as the Super POM, and contains values
inherited by default.
Maven use the effective POM (configuration from super pom plus project configuration) to
execute relevant goal. It helps developers to specify minimum configuration detail in his/her
pom.xml. Although configurations can be overridden easily.
An easy way to look at the default configurations of the super POM is by running the following
command: mvn help:effective-pom
https://maven.apache.org/guides/introduction/introduction-to-the-pom.html
What is a POM?
A Project Object Model or POM is the fundamental unit of work in Maven. It is an XML file that
contains information about the project and configuration details used by Maven to build the
project. It contains default values for most projects. Examples for this is the build directory,
which is target ; the source directory, which is src/main/java ; the test source directory, which
is src/test/java ; and so on. When executing a task or goal, Maven looks for the POM in the
current directory. It reads the POM, gets the needed configuration information, then executes
the goal.
Some of the configuration that can be specified in the POM are the project dependencies, the
plugins or goals that can be executed, the build profiles, and so on. Other information such as
the project version, description, developers, mailing lists and such can also be specified.
Super POM
The Super POM is Maven's default POM. All POMs extend the Super POM unless explicitly set,
meaning the configuration specified in the Super POM is inherited by the POMs you created for
your projects.
You can see the Super POM for Maven 3.6.3 in Maven Core reference documentation.
Minimal POM
The minimum requirement for a POM are the following:
project root
modelVersion - should be set to 4.0.0
groupId - the id of the project's group.
artifactId - the id of the artifact (project)
version - the version of the artifact under the specified group
Here's an example:
1. <project>
2. <modelVersion>4.0.0</modelVersion>
3.
4. <groupId>com.mycompany.app</groupId>
5. <artifactId>my-app</artifactId>
6. <version>1</version>
7. </project>
A POM requires that its groupId, artifactId, and version be configured. These three values form
the project's fully qualified artifact name. This is in the form of <groupId>:<artifactId>:<version>.
As for the example above, its fully qualified artifact name is "com.mycompany.app:my-app:1".
Also, as mentioned in the first section, if the configuration details are not specified, Maven will
use their defaults. One of these default values is the packaging type. Every Maven project has a
packaging type. If it is not specified in the POM, then the default value "jar" would be used.
Furthermore, you can see that in the minimal POM the repositories were not specified. If you
build your project using the minimal POM, it would inherit the repositories configuration in the
Super POM. Therefore when Maven sees the dependencies in the minimal POM, it would know
that these dependencies will be downloaded
from https://repo.maven.apache.org/maven2 which was specified in the Super POM.
Project Inheritance
Elements in the POM that are merged are the following:
dependencies
developers and contributors
plugin lists (including reports)
plugin executions with matching ids
plugin configuration
resources
The Super POM is one example of project inheritance, however you can also introduce your
own parent POMs by specifying the parent element in the POM, as demonstrated in the
following examples.
Example 1
The Scenario
As an example, let us reuse our previous artifact, com.mycompany.app:my-app:1. And let us
introduce another artifact, com.mycompany.app:my-module:1.
1. <project>
2. <modelVersion>4.0.0</modelVersion>
3.
4. <groupId>com.mycompany.app</groupId>
5. <artifactId>my-module</artifactId>
6. <version>1</version>
7. </project>
|-- my-module
| `-- pom.xml
`-- pom.xml
The Solution
Now, if we were to turn com.mycompany.app:my-app:1 into a parent artifact of
com.mycompany.app:my-module:1,we will have to modify com.mycompany.app:my-module:1's
POM to the following configuration:
com.mycompany.app:my-module:1's POM
1. <project>
2. <modelVersion>4.0.0</modelVersion>
3.
4. <parent>
5. <groupId>com.mycompany.app</groupId>
6. <artifactId>my-app</artifactId>
7. <version>1</version>
8. </parent>
9.
10. <groupId>com.mycompany.app</groupId>
11. <artifactId>my-module</artifactId>
12. <version>1</version>
13. </project>
Notice that we now have an added section, the parent section. This section allows us to specify
which artifact is the parent of our POM. And we do so by specifying the fully qualified artifact
name of the parent POM. With this setup, our module can now inherit some of the properties of
our parent POM.
Alternatively, if you want the groupId or the version of your modules to be the same as their
parents, you can remove the groupId or the version identity of your module in its POM.
1. <project>
2. <modelVersion>4.0.0</modelVersion>
3.
4. <parent>
5. <groupId>com.mycompany.app</groupId>
6. <artifactId>my-app</artifactId>
7. <version>1</version>
8. </parent>
9.
10. <artifactId>my-module</artifactId>
11. </project>
This allows the module to inherit the groupId or the version of its parent POM.
Example 2
The Scenario
However, that would work if the parent project was already installed in our local repository or
was in that specific directory structure (parent pom.xml is one directory higher than that of the
module's pom.xml ).
But what if the parent is not yet installed and if the directory structure is as in the following
example?
|-- my-module
| `-- pom.xml
`-- parent
`-- pom.xml
The Solution
To address this directory structure (or any other directory structure), we would have to add
the <relativePath> element to our parent section.
1. <project>
2. <modelVersion>4.0.0</modelVersion>
3.
4. <parent>
5. <groupId>com.mycompany.app</groupId>
6. <artifactId>my-app</artifactId>
7. <version>1</version>
8. <relativePath>../parent/pom.xml</relativePath>
9. </parent>
10.
11. <artifactId>my-module</artifactId>
12. </project>
As the name suggests, it's the relative path from the module's pom.xml to the
parent's pom.xml .
Project Aggregation
Project Aggregation is similar to Project Inheritance. But instead of specifying the parent POM
from the module, it specifies the modules from the parent POM. By doing so, the parent project
now knows its modules, and if a Maven command is invoked against the parent project, that
Maven command will then be executed to the parent's modules as well. To do Project
Aggregation, you must do the following:
Example 3
The Scenario
Given the previous original artifact POMs and directory structure:
com.mycompany.app:my-app:1's POM
1. <project>
2. <modelVersion>4.0.0</modelVersion>
3.
4. <groupId>com.mycompany.app</groupId>
5. <artifactId>my-app</artifactId>
6. <version>1</version>
7. </project>
com.mycompany.app:my-module:1's POM
1. <project>
2. <modelVersion>4.0.0</modelVersion>
3.
4. <groupId>com.mycompany.app</groupId>
5. <artifactId>my-module</artifactId>
6. <version>1</version>
7. </project>
directory structure
|-- my-module
| `-- pom.xml
`-- pom.xml
The Solution
If we are to aggregate my-module into my-app, we would only have to modify my-app.
1. <project>
2. <modelVersion>4.0.0</modelVersion>
3.
4. <groupId>com.mycompany.app</groupId>
5. <artifactId>my-app</artifactId>
6. <version>1</version>
7. <packaging>pom</packaging>
8.
9. <modules>
10. <module>my-module</module>
11. </modules>
12. </project>
In the revised com.mycompany.app:my-app:1, the packaging section and the modules sections
were added. For the packaging, its value was set to "pom", and for the modules section, we
have the element <module>my-module</module> . The value of <module> is the relative path
from the com.mycompany.app:my-app:1 to com.mycompany.app:my-module:1's POM (by
practice, we use the module's artifactId as the module directory's name).
Now, whenever a Maven command processes com.mycompany.app:my-app:1, that same
Maven command would be ran against com.mycompany.app:my-module:1 as well.
Furthermore, some commands (goals specifically) handle project aggregation differently.
Example 4
The Scenario
But what if we change the directory structure to the following:
|-- my-module
| `-- pom.xml
`-- parent
`-- pom.xml
How would the parent POM specify its modules?
The Solution
The answer? - the same way as Example 3, by specifying the path to the module.
1. <project>
2. <modelVersion>4.0.0</modelVersion>
3.
4. <groupId>com.mycompany.app</groupId>
5. <artifactId>my-app</artifactId>
6. <version>1</version>
7. <packaging>pom</packaging>
8.
9. <modules>
10. <module>../my-module</module>
11. </modules>
12. </project>
Example 5
The Scenario
Given the previous original artifact POMs again,
com.mycompany.app:my-app:1's POM
1. <project>
2. <modelVersion>4.0.0</modelVersion>
3.
4. <groupId>com.mycompany.app</groupId>
5. <artifactId>my-app</artifactId>
6. <version>1</version>
7. </project>
com.mycompany.app:my-module:1's POM
1. <project>
2. <modelVersion>4.0.0</modelVersion>
3.
4. <groupId>com.mycompany.app</groupId>
5. <artifactId>my-module</artifactId>
6. <version>1</version>
7. </project>
|-- my-module
| `-- pom.xml
`-- parent
`-- pom.xml
The Solution
To do both project inheritance and aggregation, you only have to apply all three rules.
com.mycompany.app:my-app:1's POM
1. <project>
2. <modelVersion>4.0.0</modelVersion>
3.
4. <groupId>com.mycompany.app</groupId>
5. <artifactId>my-app</artifactId>
6. <version>1</version>
7. <packaging>pom</packaging>
8.
9. <modules>
10. <module>../my-module</module>
11. </modules>
12. </project>
com.mycompany.app:my-module:1's POM
1. <project>
2. <modelVersion>4.0.0</modelVersion>
3.
4. <parent>
5. <groupId>com.mycompany.app</groupId>
6. <artifactId>my-app</artifactId>
7. <version>1</version>
8. <relativePath>../parent/pom.xml</relativePath>
9. </parent>
10.
11. <artifactId>my-module</artifactId>
12. </project>
NOTE: Profile inheritance the same inheritance strategy as used for the POM itself.
Project Interpolation and Variables
One of the practices that Maven encourages is don't repeat yourself. However, there are
circumstances where you will need to use the same value in several different locations. To
assist in ensuring the value is only specified once, Maven allows you to use both your own and
pre-defined variables in the POM.
For example, to access the project.version variable, you would reference it like so:
1. <version>${project.version}</version>
One factor to note is that these variables are processed after inheritance as outlined above.
This means that if a parent project uses a variable, then its definition in the child, not the parent,
will be the one eventually used.
Available Variables
Project Model Variables
Any field of the model that is a single value element can be referenced as a variable. For
example, ${project.groupId} , ${project.version} , ${project.build.sourceDirectory} and so on.
Refer to the POM reference to see a full list of properties.
These variables are all referenced by the prefix " project. ". You may also see references
with pom. as the prefix, or the prefix omitted entirely - these forms are now deprecated and
should not be used.
Special Variables
project.baseUri The directory that the current project resides in, represented as an URI. Sin
maven.build.timestamp The timestamp that denotes the start of the build (UTC). Since Maven 2.1.0
1. <project>
2. ...
3. <properties>
4. <maven.build.timestamp.format>yyyy-MM-
dd'T'HH:mm:ss'Z'</maven.build.timestamp.format>
5. </properties>
6. ...
7. </project>
The format pattern has to comply with the rules given in the API documentation
for SimpleDateFormat. If the property is not present, the format defaults to the value already
given in the example.
Properties
You are also able to reference any properties defined in the project as a variable. Consider the
following example:
1. <project>
2. ...
3. <properties>
4. <mavenVersion>3.0</mavenVersion>
5. </properties>
6.
7. <dependencies>
8. <dependency>
9. <groupId>org.apache.maven</groupId>
10. <artifactId>maven-artifact</artifactId>
11. <version>${mavenVersion}</version>
12. </dependency>
13. <dependency>
14. <groupId>org.apache.maven</groupId>
15. <artifactId>maven-core</artifactId>
16. <version>${mavenVersion}</version>
17. </dependency>
18. </dependencies>
19. ...
20. </project>
https://www.tutorialspoint.com/maven/maven_repositories.htm
local
central
remote
Local Repository
Maven local repository is a folder location on your machine. It gets created when you run any
maven command for the first time.
Maven local repository keeps your project's all dependencies (library jars, plugin jars etc.).
When you run a Maven build, then Maven automatically downloads all the dependency jars
into the local repository. It helps to avoid references to dependencies stored on remote
machine every time a project is build.
Maven local repository by default get created by Maven in %USER_HOME% directory. To
override the default location, mention another path in Maven settings.xml file available at
%M2_HOME%\conf directory.
https://maven.apache.org/archives/maven-1.x/using/scm.html
Working with your source control system in Maven is quite simple. Maven can provide the
following services:
Common tasks such as checking out code, tagging the current code, and performing an
update
Generating reports on the recent usage history of source control
Providing links to a web view of the source control, and if desired publishing that
information to the project site
Support is provided by the Maven SCM subproject. This provides support for the following
SCM systems:
CVS
Subversion
ClearCase (partial)
Perforce (partial)
StarTeam (partial)
Visual SourceSafe (planned)
It is possible to perform several common tasks with Maven. These can be particularly useful
for automation in your development environment, or in continuous integration.
There are also goals that provide assistance with making releases from your SCM.
For more information about common tasks and SCM based releases, see the SCM Plugin
Documentation. This contains a complete guide to the scm: goals and related properties.
For general information on making releases with Maven, please see Making Releases.
Generating Reports
Maven provides three different reports on SCM usage that can be included in your project
site. These are provided by the Change Log, File Activity and Developer Activity plugins.
These are included in your project site by default, however if you would like to explicitly select
them for your project, add any or all of the following lines to project.xml inside
the <reports/> section:
<report>maven-changelog-plugin</report>
<report>maven-file-activity-plugin</report>
<report>maven-developer-activity-plugin</report>