Maven
Maven
Maven - Overview
What is Maven?
Maven is a project management and comprehension tool that provides developers a
complete build lifecycle framework. Development team can automate the project's build
infrastructure in almost no time as Maven uses a standard directory layout and a default
build lifecycle.
In case of multiple development teams environment, Maven can set-up the way to work as
per standards in a very short time. As most of the project setups are simple and reusable,
Maven makes life of developer easy while creating reports, checks, build and testing
automation setups.
Builds
Documentation
Reporting
Dependencies
SCMs
Releases
Distribution
Mailing list
To summarize, Maven simplifies and standardizes the project build process. It handles
compilation, distribution, documentation, team collaboration and other tasks seamlessly.
Maven increases reusability and takes care of most of the build related tasks.
Maven Evolution
Maven was originally designed to simplify building processes in Jakarta Turbine project.
There were several projects and each project contained slightly different ANT build files.
JARs were checked into CVS.
Apache group then developed Maven which can build multiple projects together, publish
projects information, deploy projects, share JARs across several projects and help in
collaboration of teams.
Page 2 of 96
Explore our latest online courses and learn new skills at your own pace. Enroll and
become a certified expert to boost your career.
Objective
The primary goal of Maven is to provide developer with the following −
Maven project structure and contents are declared in an xml file, pom.xml, referred as
Project Object Model (POM), which is the fundamental unit of the entire Maven system. In
later chapters, we will explain POM in detail.
Developers do not have to mention each and every configuration detail. Maven provides
sensible default behavior for projects. When a Maven project is created, Maven creates
default project structure. Developer is only required to place files accordingly and he/she
need not to define any configuration in pom.xml.
As an example, following table shows the default values for project source code files,
resource files and other configurations. Assuming, ${basedir} denotes the project
location −
Item Default
Resources ${basedir}/src/main/resources
Tests ${basedir}/src/test
In order to build the project, Maven provides developers with options to mention life-cycle
goals and project dependencies (that rely on Maven plugin capabilities and on its default
conventions). Much of the project management and build related tasks are maintained by
Maven plugins.
Page 3 of 96
Developers can build any given Maven project without the need to understand how the
individual plugins work. We will discuss Maven Plugins in detail in the later chapters.
Features of Maven
Coherent site of project information − Using the same metadata as per the
build process, maven is able to generate a website and a PDF including complete
documentation.
Backward Compatibility − You can easily port the multiple modules of a project
into Maven 3 from older versions of Maven. It can support the older versions also.
Automatic parent versioning − No need to specify the parent in the sub module
for maintenance.
Parallel builds − It analyzes the project dependency graph and enables you to
build schedule modules in parallel. Using this, you can achieve the performance
improvements of 20-50%.
Better Error and Integrity Reporting − Maven improved error reporting, and it
provides you with a link to the Maven wiki page where you will get full description
of the error.
OS Task Command
Page 4 of 96
OS Output
If you do not have Java installed on your system, then download the Java Software
Development Kit (SDK) from the following link http://www.oracle.com. We are assuming
Java 11.0.11 as the installed version for this tutorial.
OS Output
OS Output
Page 5 of 96
Verify Java installation using the command java -version as explained above.
OS Archive name
Windows apache-maven-3.8.4-bin.zip
Linux apache-maven-3.8.4-bin.tar.gz
Mac apache-maven-3.8.4-bin.tar.gz
Linux /usr/local/apache-maven
Mac /usr/local/apache-maven
OS Output
OS Output
Windows Append the string ;%M2% to the end of the system variable, Path.
OS Task Command
Finally, verify the output of the above commands, which should be as follows −
OS Output
Maven - POM
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>
Page 8 of 96
<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.
Project root
1 This is project root tag. You need to specify the basic schema settings such as
apache schema and w3.org specification.
Model version
2
Model version should be 4.0.0.
groupId
This is an Id of project's group. This is generally unique amongst an
3
organization or a project. For example, a banking group com.company.bank
has all bank related projects.
artifactId
This is an Id of the project. This is generally name of the project. For example,
4
consumer-banking. Along with the groupId, the artifactId defines the artifact's
location within the repository.
version
This is the version of the project. Along with the groupId, It is used within an
5 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
Page 9 of 96
An easy way to look at the default configurations of the super POM is by running the
following command: mvn help:effective-pom
Create a pom.xml in any directory on your computer.Use the content of above mentioned
example pom.
Now open command console, go the folder containing pom.xml and execute the following
mvn command.
C:\MVN\project>mvn help:effective-pom
C:\MVN>mvn help:effective-pom
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------< com.companyname.project-group:project >----------------
[INFO] Building project 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-help-plugin:3.2.0:effective-pom (default-cli) @ project ---
[INFO]
Effective POMs, after inheritance, interpolation, and profiles are applied:
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.261 s
[INFO] Finished at: 2021-12-10T19:54:53+05:30
[INFO] ------------------------------------------------------------------------
C:\MVN>
Effective POM displayed as result in console, after inheritance, interpolation, and profiles
are applied.
<resource>
<directory>C:\MVN\src\main\resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>C:\MVN\src\test\resources</directory>
</testResource>
</testResources>
<directory>C:\MVN\target</directory>
<finalName>project-1.0</finalName>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
</plugin>
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>default-clean</id>
<phase>clean</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
Page 12 of 96
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>default-testResources</id>
<phase>process-test-resources</phase>
<goals>
<goal>testResources</goal>
</goals>
</execution>
<execution>
<id>default-resources</id>
<phase>process-resources</phase>
<goals>
<goal>resources</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>default-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<executions>
<execution>
<id>default-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
Page 13 of 96
<id>default-testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<executions>
<execution>
<id>default-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>default-install</id>
<phase>install</phase>
<goals>
<goal>install</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>default-deploy</id>
<phase>deploy</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
Page 14 of 96
</executions>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.3</version>
<executions>
<execution>
<id>default-site</id>
<phase>site</phase>
<goals>
<goal>site</goal>
</goals>
<configuration>
<outputDirectory>C:\MVN\target\site</outputDirectory>
<reportPlugins>
<reportPlugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</a
</reportPlugin>
</reportPlugins>
</configuration>
</execution>
<execution>
<id>default-deploy</id>
<phase>site-deploy</phase>
<goals>
<goal>deploy</goal>
</goals>
<configuration>
<outputDirectory>C:\MVN\target\site</outputDirectory>
<reportPlugins>
<reportPlugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</a
</reportPlugin>
</reportPlugins>
</configuration>
</execution>
</executions>
<configuration>
<outputDirectory>C:\MVN\target\site</outputDirectory>
<reportPlugins>
<reportPlugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifac
Page 15 of 96
</reportPlugin>
</reportPlugins>
</configuration>
</plugin>
</plugins>
</build>
<reporting>
<outputDirectory>C:\MVN\target\site</outputDirectory>
</reporting>
</project>
In above pom.xml, you can see the default project source folders structure, output
directory, plug-ins required, repositories, reporting directory, which Maven will be using
while executing the desired goals.
Maven pom.xml is also not required to be written manually. Maven provides numerous
archetype plugins to create projects, which in order, create the project structure and
pom.xml
prepare-
resource copying Resource copying can be customized in this phase.
resources
There are always pre and post phases to register goals, which must run prior to, or after
a particular phase.
When Maven starts building a project, it steps through a defined sequence of phases and
executes goals, which are registered with each phase.
clean
default(or build)
site
A goal represents a specific task which contributes to the building and managing of a
project. It may be bound to zero or more build phases. A goal not bound to any build
phase could be executed outside of the build lifecycle by direct invocation.
The order of execution depends on the order in which the goal(s) and the build phase(s)
are invoked. For example, consider the command below. The clean and package
arguments are build phases while the dependency:copy-dependencies is a goal.
Here the clean phase will be executed first, followed by the dependency:copy-
dependencies goal, and finally package phase will be executed.
Clean Lifecycle
When we execute mvn post-clean command, Maven invokes the clean lifecycle consisting
of the following phases.
pre-clean
clean
post-clean
Maven clean goal (clean:clean) is bound to the clean phase in the clean lifecycle. Its
clean:cleangoal deletes the output of a build by deleting the build directory. Thus, when
mvn clean command executes, Maven deletes the build directory.
We can customize this behavior by mentioning goals in any of the above phases of clean
life cycle.
Page 17 of 96
<execution>
<id>id.clean</id>
<phase>clean</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>clean phase</echo>
</tasks>
</configuration>
Page 18 of 96
</execution>
<execution>
<id>id.post-clean</id>
<phase>post-clean</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>post-clean phase</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Now open command console, go to the folder containing pom.xml and execute the
following mvn command.
C:\MVN\project>mvn post-clean
Maven will start processing and displaying all the phases of clean life cycle.
C:\MVN>mvn post-clean
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------< com.companyname.projectgroup:project >----------------
[INFO] Building project 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-antrun-plugin:1.1:run (id.pre-clean) @ project ---
[INFO] Executing tasks
[echo] pre-clean phase
[INFO] Executed tasks
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ project ---
[INFO]
[INFO] --- maven-antrun-plugin:1.1:run (id.clean) @ project ---
Page 19 of 96
C:\MVN>
You can try tuning mvn clean command, which will display pre-clean and clean. Nothing
will be executed for post-clean phase.
validate
1 Validates whether project is correct and all necessary information is available
to complete the build process.
initialize
2
Initializes build state, for example set properties.
generate-sources
3
Generate any source code to be included in compilation phase.
process-sources
4
Process the source code, for example, filter any value.
generate-resources
5
Generate resources to be included in the package.
6 process-resources
Page 20 of 96
Copy and process the resources into the destination directory, ready for
packaging phase.
compile
7
Compile the source code of the project.
process-classes
8 Post-process the generated files from compilation, for example to do bytecode
enhancement/optimization on Java classes.
generate-test-sources
9
Generate any test source code to be included in compilation phase.
process-test-sources
10
Process the test source code, for example, filter any values.
test-compile
11
Compile the test source code into the test destination directory.
process-test-classes
12
Process the generated files from test code file compilation.
test
13
Run tests using a suitable unit testing framework (Junit is one).
prepare-package
14 Perform any operations necessary to prepare a package before the actual
packaging.
package
15 Take the compiled code and package it in its distributable format, such as a
JAR, WAR, or EAR file.
pre-integration-test
16 Perform actions required before integration tests are executed. For example,
setting up the required environment.
integration-test
17 Process and deploy the package if necessary into an environment where
integration tests can be run.
post-integration-test
18 Perform actions required after integration tests have been executed. For
example, cleaning up the environment.
verify
19
Run any check-ups to verify the package is valid and meets quality criteria.
Page 21 of 96
install
20 Install the package into the local repository, which can be used as a
dependency in other projects locally.
deploy
21 Copies the final package to the remote repository for sharing with other
developers and projects.
There are few important concepts related to Maven Lifecycles, which are worth to mention
−
When a phase is called via Maven command, for example mvn compile, only
phases up to and including that phase will execute.
<tasks>
<echo>validate phase</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>id.compile</id>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>compile phase</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>id.test</id>
<phase>test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>test phase</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>id.package</id>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>package phase</echo>
</tasks>
</configuration>
</execution>
Page 23 of 96
<execution>
<id>id.deploy</id>
<phase>deploy</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>deploy phase</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Now open command console, go the folder containing pom.xml and execute the following
mvn command.
C:\MVN\project>mvn compile
Maven will start processing and display phases of build life cycle up to the compile phase.
C:\MVN>mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------< com.companyname.projectgroup:project >----------------
[INFO] Building project 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-antrun-plugin:1.1:run (id.validate) @ project ---
[INFO] Executing tasks
[echo] validate phase
[INFO] Executed tasks
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ project ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. bu
[INFO] skip non existing resourceDirectory C:\MVN\src\main\resources
[INFO]
Page 24 of 96
C:\MVN>
Site Lifecycle
Maven Site plugin is generally used to create fresh documentation to create reports,
deploy site, etc. It has the following phases −
pre-site
site
post-site
site-deploy
In the following example, we will attach maven-antrun-plugin:run goal to all the phases
of Site lifecycle. This will allow us to echo text messages displaying the phases of the
lifecycle.
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.7</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>2.9</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>id.pre-site</id>
<phase>pre-site</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>pre-site phase</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>id.site</id>
<phase>site</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>site phase</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>id.post-site</id>
<phase>post-site</phase>
Page 26 of 96
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>post-site phase</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>id.site-deploy</id>
<phase>site-deploy</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>site-deploy phase</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Now open the command console, go the folder containing pom.xml and execute the
following mvn command.
C:\MVN\project>mvn site
Maven will start processing and displaying the phases of site life cycle up to site phase.
C:\MVN>mvn site
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------< com.companyname.projectgroup:project >----------------
[INFO] Building project 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
Page 27 of 96
C:\MVN>
Profiles are specified in pom.xml file using its activeProfiles/profiles elements and are
triggered in variety of ways. Profiles modify the POM at build time, and are used to give
parameters different target environments (for example, the path of the database server in
the development, testing, and production environments).
Profile Activation
A Maven Build Profile can be activated in various ways.
Present/missing files.
env.properties
1
default configuration used if no profile is mentioned.
Page 29 of 96
env.test.properties
2
test configuration when test profile is used.
env.prod.properties
3
production configuration when prod profile is used.
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Now open the command console, go to the folder containing pom.xml and execute the
following mvn command. Pass the profile name as argument using -P option.
Maven will start processing and displaying the result of test build profile.
C:\MVN>
Add another profile element to profiles element of pom.xml (copy existing profile
element and paste it where profile elements ends).
Again repeat the above three steps, update id to prod and task section for
env.prod.properties.
Now open the command console, go to the folder containing pom.xml and execute the
following mvn commands. Pass the profile names as argument using -P option.
Add test profile as an active profile using active Profiles node as shown below in example.
Page 32 of 96
Now open command console, go to the folder containing pom.xml and execute the
following mvn command. Do not pass the profile name using -P option. Maven will display
result of test profile being an active profile.
C:\MVN\project>mvn test
The test profile will trigger when the system property "env" is specified with the value
"test". Create an environment variable "env" and set its value as "test".
<profile>
<id>test</id>
<activation>
<property>
<name>env</name>
<value>test</value>
</property>
</activation>
</profile>
Let's open command console, go to the folder containing pom.xml and execute the
following mvn command.
Page 33 of 96
C:\MVN\project>mvn test
<profile>
<id>test</id>
<activation>
<os>
<name>Windows XP</name>
<family>Windows</family>
<arch>x86</arch>
<version>5.1.2600</version>
</os>
</activation>
</profile>
Now open command console, go to the folder containing pom.xml and execute the
following mvn commands. Do not pass the profile name using -P option. Maven will
display result of test profile being an active profile.
C:\MVN\project>mvn test
<profile>
<id>test</id>
<activation>
<file>
<missing>target/generated-sources/axistools/wsdl2java/
com/companyname/group</missing>
</file>
</activation>
</profile>
Page 34 of 96
Now open the command console, go to the folder containing pom.xml and execute the
following mvn commands. Do not pass the profile name using -P option. Maven will
display result of test profile being an active profile.
C:\MVN\project>mvn test
Maven - Repositories
What is a Maven Repository?
In Maven terminology, a repository is a directory where all the project jars, library jar,
plugins or any other project specific artifacts are stored and can be used by Maven easily.
Maven repository are of three types. The following illustration will give an idea regarding
these three types.
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.
Page 35 of 96
When you run Maven command, Maven will download dependencies to your custom path.
Central Repository
Maven central repository is repository provided by Maven community. It contains a large
number of commonly used libraries.
When Maven does not find any dependency in local repository, it starts searching in central
repository using following URL − https://repo1.maven.org/maven2/
To browse the content of central maven repository, maven community has provided a URL
− https://search.maven.org/#browse. Using this library, a developer can search all the
available libraries in central repository.
Remote Repository
Sometimes, Maven does not find a mentioned dependency in central repository as well. It
then stops the build process and output error message to console. To prevent such
situation, Maven provides concept of Remote Repository, which is developer's own
custom repository containing required libraries or other project jars.
For example, using below mentioned POM.xml, Maven will download dependency (not
available in central repository) from Remote Repositories mentioned in the same pom.xml.
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.companyname.projectgroup</groupId>
<artifactId>project</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>com.companyname.common-lib</groupId>
<artifactId>common-lib</artifactId>
<version>1.0.0</version>
</dependency>
<dependencies>
<repositories>
<repository>
<id>companyname.lib1</id>
<url>http://download.companyname.org/maven2/lib1</url>
</repository>
<repository>
<id>companyname.lib2</id>
<url>http://download.companyname.org/maven2/lib2</url>
</repository>
</repositories>
</project>
Step 1 − Search dependency in local repository, if not found, move to step 2 else
perform the further processing.
Step 3 − If a remote repository has not been mentioned, Maven simply stops the
processing and throws error (Unable to find dependency).
Maven - Plugins
Page 37 of 96
A plugin generally provides a set of goals, which can be executed using the following
syntax −
mvn [plugin-name]:[goal-name]
For example, a Java project can be compiled with the maven-compiler-plugin's compile-
goal by running the following command.
mvn compiler:compile
Plugin Types
Maven provided the following two types of Plugins −
Build plugins
1 They execute during the build process and should be configured in the
<build/> element of pom.xml.
Reporting plugins
2 They execute during the site generation process and they should be configured
in the <reporting/> element of the pom.xml.
clean
1
Cleans up target after the build. Deletes the target directory.
compiler
2
Compiles Java source files.
surefire
3
Runs the JUnit unit tests. Creates test reports.
jar
4
Builds a JAR file from the current project.
war
5
Builds a WAR file from the current project.
javadoc
6
Generates Javadoc for the project.
antrun
7
Runs a set of ant tasks from any phase mentioned of the build.
Example
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>clean phase</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Next, open the command console and go to the folder containing pom.xml and execute the
following mvn command.
C:\MVN\project>mvn clean
Maven will start processing and displaying the clean phase of clean life cycle.
C:\MVN>mvn clean
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------< com.companyname.projectgroup:project >----------------
[INFO] Building project 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ project ---
[INFO] Deleting C:\MVN\target
[INFO]
[INFO] --- maven-antrun-plugin:1.1:run (id.clean) @ project ---
[INFO] Executing tasks
[echo] clean phase
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.266 s
[INFO] Finished at: 2021-12-13T13:58:10+05:30
[INFO] ------------------------------------------------------------------------
Page 40 of 96
C:\MVN>
You can define phase from where plugin should starts its processing using its phase
element. We've used clean phase.
You can configure tasks to be executed by binding them to goals of plugin. We've
bound echo task with run goal of maven-antrun-plugin.
Maven will then download the plugin if not available in local repository and start its
processing.
Let's open the command console, go to the C:\MVN directory and execute the following
mvn command. Make sure that C:\MVN directory is empty before running the command.
C:\MVN>mvn archetype:generate
-DgroupId = com.companyname.bank
-DartifactId = consumerBanking
-DarchetypeArtifactId = maven-archetype-quickstart
-DinteractiveMode = false
Maven will start processing and will create the complete java application project structure.
C:\MVN>
Now go to C:/MVN directory. You'll see a java application project created, named consumer
Banking (as specified in artifactId). Maven uses a standard directory layout as shown
below −
Page 42 of 96
Using the above example, we can understand the following key concepts −
consumerBanking
1
contains src folder and pom.xml
src/main/java
2 contains java code files under the package structure
(com/companyName/bank).
src/main/test
3 contains test java code files under the package structure
(com/companyName/bank).
src/main/resources
4 it contains images/properties files (In above example, we need to create this
structure manually).
If you observe, you will find that Maven also created a sample Java Source file and Java
Test file. Open C:\MVN\consumerBanking\src\main\java\com\companyname\bank folder,
you will see App.java.
package com.companyname.bank;
/**
* Hello world!
*
*/
public class App {
public static void main( String[] args ){
System.out.println( "Hello World!" );
}
}
package com.companyname.bank;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
Page 43 of 96
/**
* @return the suite of tests being tested
*/
public static Test suite() {
return new TestSuite( AppTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp() {
assertTrue( true );
}
}
Developers are required to place their files as mentioned in table above and Maven
handles all the build related complexities.
In the next chapter, we'll discuss how to build and test the project using maven Build and
Test Project.
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.companyname.projectgroup</groupId>
<artifactId>project</artifactId>
<version>1.0</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
</dependency>
</dependencies>
</project>
Here you can see, Maven already added Junit as test framework. By default, Maven adds a
source file App.java and a test file AppTest.java in its default directory structure, as
discussed in the previous chapter.
Let's open the command console, go the C:\MVN\consumerBanking directory and execute
the following mvn command.
-------------------------------------------------------
TESTS
-------------------------------------------------------
Running com.companyname.bank.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.028 sec
Results :
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ consumerBanking ---
[INFO] Building jar: C:\MVN\consumerBanking\target\consumerBanking-1.0-SNAPSHOT.j
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.663 s
[INFO] Finished at: 2021-12-13T17:34:27+05:30
[INFO] ------------------------------------------------------------------------
C:\MVN\consumerBanking>
You've built your project and created final jar file, following are the key learning concepts
−
Page 46 of 96
We give maven two goals, first to clean the target directory (clean) and then
package the project build output as jar (package).
Maven compiles the source code file(s) and then tests the source code file(s).
>java com.companyname.bank.App
Hello World!
package com.companyname.bank;
package com.companyname.bank;
/**
* Hello world!
*
*/
Page 47 of 96
Hello World!
For example, let us do the following changes to the project created in ‘Creating Java
Project’ chapter.
Copy any jar into the lib folder. We've used ldapjdk.jar, which is a helper library
for LDAP operations.
Here you are having your own library, specific to the project, which is an usual case and it
contains jars, which may not be available in any repository for maven to download from. If
your code is using this library with Maven, then Maven build will fail as it cannot download
or refer to this library during compilation phase.
To handle the situation, let's add this external dependency to maven pom.xml using the
following way.
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ldapjdk</groupId>
<artifactId>ldapjdk</artifactId>
<scope>system</scope>
<version>1.0</version>
<systemPath>${basedir}\src\lib\ldapjdk.jar</systemPath>
</dependency>
Page 49 of 96
</dependencies>
</project>
Look at the second dependency element under dependencies in the above example, which
clears the following key concepts about External Dependency.
Hope now you are clear about external dependencies and you will be able to specify
external dependencies in your Maven project.
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.7</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>2.9</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
C:\MVN\consumerBanking>mvn site
Your project documentation is now ready. Maven has created a site within the target
directory.
Format
Description Reference
Name
mvn archetype:generate
What is Archetype?
Archetype is a Maven plugin whose task is to create a project structure as per its template.
We are going to use quickstart archetype plugin to create a simple java application here.
C:\MVN>mvn archetype:generate
Maven will start processing and will ask to choose the required archetype.
C:\MVN>mvn archetype:generate
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.2.0:generate (default-cli) > generate-sources @
[INFO]
[INFO] <<< maven-archetype-plugin:3.2.0:generate (default-cli) < generate-sources @
Page 53 of 96
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:3.2.0:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.arch
Choose archetype:
1: remote -> am.ik.archetype:elm-spring-boot-blank-archetype (Blank multi project for S
2: remote -> am.ik.archetype:graalvm-blank-archetype (Blank project for GraalVM)
...
3021: remote -> za.co.absa.hyperdrive:component-archetype_2.12 (-)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 1
Maven will ask for the project detail. Enter project detail as asked. Press Enter if the
default value is provided. You can override them by entering your own value.
Maven will ask for the project detail confirmation. Press enter or press Y.
version: 1.0-SNAPSHOT
package: com.companyname.insurance
Y:
Now Maven will start creating the project structure and will display the following −
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: maven-archetype
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.companyname.insurance
[INFO] Parameter: artifactId, Value: health
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: com.companyname.insurance
[INFO] Parameter: packageInPathFormat, Value: com/companyname/insurance
[INFO] Parameter: package, Value: com.companyname.insurance
[INFO] Parameter: groupId, Value: com.companyname.insurance
[INFO] Parameter: artifactId, Value: health
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Project created from Archetype in dir: C:\MVN\health
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 04:44 min
[INFO] Finished at: 2021-12-13T18:52:59+05:30
[INFO] ------------------------------------------------------------------------
Created Project
Now go to C:\ > MVN directory. You'll see a java application project created, named
health, which was given as artifactId at the time of project creation. Maven will create a
standard directory layout for the project as shown below −
Page 55 of 96
Created POM.xml
Maven generates a POM.xml file for the project as listed below −
Created App.java
Page 56 of 96
Maven generates sample java source file, App.java for the project as listed below −
Location: C:\ > MVN > health > src > main > java > com > companyname >
insurance > App.java.
package com.companyname.insurance;
/**
* Hello world!
*
*/
public class App {
public static void main( String[] args ) {
System.out.println( "Hello World!" );
}
}
Created AppTest.java
Maven generates sample java source test file, AppTest.java for the project as listed below
−
Location: C:\ > MVN > health > src > test > java > com > companyname >
insurance > AppTest.java.
package com.companyname.insurance;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest extends TestCase {
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName ) {
super( testName );
}
/**
Page 57 of 96
Now you can see the power of Maven. You can create any kind of project using single
command in maven and can kick-start your development.
Different Archetypes
maven-archetype-archetype
1
An archetype, which contains a sample archetype.
maven-archetype-j2ee-simple
2
An archetype, which contains a simplified sample J2EE application.
maven-archetype-mojo
3
An archetype, which contains a sample a sample Maven plugin.
maven-archetype-plugin
4
An archetype, which contains a sample Maven plugin.
maven-archetype-plugin-site
5
An archetype, which contains a sample Maven plugin site.
maven-archetype-portlet
6
An archetype, which contains a sample JSR-268 Portlet.
maven-archetype-quickstart
7
An archetype, which contains a sample Maven project.
maven-archetype-simple
8
An archetype, which contains a simple Maven project.
9 maven-archetype-site
An archetype, which contains a sample Maven site to demonstrates some of
the supported document types like APT, XDoc, and FML and demonstrates how
Page 58 of 96
maven-archetype-site-simple
10
An archetype, which contains a sample Maven site.
maven-archetype-webapp
11
An archetype, which contains a sample Maven Webapp project.
Maven - Snapshots
A large software application generally consists of multiple modules and it is common
scenario where multiple teams are working on different modules of same application. For
example, consider a team is working on the front end of the application as app-ui project
(app-ui.jar:1.0) and they are using data-service project (data-service.jar:1.0).
Now it may happen that team working on data-service is undergoing bug fixing or
enhancements at rapid pace and they are releasing the library to remote repository almost
every other day.
Now if data-service team uploads a new version every other day, then following problems
will arise −
data-service team should tell app-ui team every time when they have released an
updated code.
app-ui team required to update their pom.xml regularly to get the updated version.
What is SNAPSHOT?
SNAPSHOT is a special version that indicates a current development copy. Unlike regular
versions, Maven checks for a new SNAPSHOT version in a remote repository for every
build.
Now data-service team will release SNAPSHOT of its updated code every time to
repository, say data-service: 1.0-SNAPSHOT, replacing an older SNAPSHOT jar.
Snapshot vs Version
In case of Version, if Maven once downloaded the mentioned version, say data-
service:1.0, it will never try to download a newer 1.0 available in repository. To download
the updated code, data-service version is be upgraded to 1.1.
In case of SNAPSHOT, Maven will automatically fetch the latest SNAPSHOT (data-
service:1.0-SNAPSHOT) every time app-ui team build their project.
Page 59 of 96
app-ui pom.xml
app-ui project is using 1.0-SNAPSHOT of data-service.
data-service pom.xml
data-service project is releasing 1.0-SNAPSHOT for every minor change.
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
Let's open the command console, go to the C:\ > MVN > app-ui directory and execute
the following mvn command.
Maven will start building the project after downloading the latest SNAPSHOT of data-
service.
--------------------------------------------------
TESTS
--------------------------------------------------
Running com.companyname.bank.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.027 sec
Results :
Example
Consider a team is developing a project bus-core-api on which two other projects app-
web-ui and app-desktop-ui are dependent.
<version>1.0-SNAPSHOT</version>
<scope>system</scope>
<systemPath>C:\MVN\bus_core_api\target\bus_core_api-1.0-SNAPSHOT.j
</dependency>
</dependencies>
</project>
bus-core-api project −
Now, teams of app-web-ui and app-desktop-ui projects require that their build process
should kick off whenever bus-core-api project changes.
Using snapshot, ensures that the latest bus-core-api project should be used but to meet
the above requirement we need to do something extra.
Use a Continuous Integration (CI) Server like Hudson to manage build automation
automatically.
Using Maven
Update bus-core-api project pom.xml.
<artifactId>bus-core-api</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<artifactId>maven-invoker-plugin</artifactId>
<version>1.6</version>
<configuration>
<debug>true</debug>
<pomIncludes>
<pomInclude>app-web-ui/pom.xml</pomInclude>
<pomInclude>app-desktop-ui/pom.xml</pomInclude>
</pomIncludes>
</configuration>
<executions>
<execution>
<id>build</id>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<build>
</project>
Let's open the command console, go to the C:\ > MVN > bus-core-api directory and
execute the following mvn command.
bus-core-ui-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------
Once bus-core-api build is successful, Maven will start building the app-web-ui project.
[INFO] ------------------------------------------------------------------
[INFO] Building app-web-ui
[INFO] task-segment: [package]
[INFO] ------------------------------------------------------------------
...
[INFO] [jar:jar {execution: default-jar}]
[INFO] Building jar: C:\MVN\app-web-ui\target\
app-web-ui-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------
Once app-web-ui build is successful, Maven will start building the app-desktop-ui
project.
[INFO] ------------------------------------------------------------------
[INFO] Building app-desktop-ui
[INFO] task-segment: [package]
[INFO] ------------------------------------------------------------------
...
[INFO] [jar:jar {execution: default-jar}]
[INFO] Building jar: C:\MVN\app-desktop-ui\target\
app-desktop-ui-1.0-SNAPSHOT.jar
[INFO] -------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] -------------------------------------------------------------------
Hudson considers each project build as job. Once a project code is checked-in to SVN (or
any Source Management Tool mapped to Hudson), Hudson starts its build job and once
this job gets completed, it start other dependent jobs (other dependent projects)
automatically.
In the above example, when bus-core-ui source code is updated in SVN, Hudson starts
its build. Once build is successful, Hudson looks for dependent projects automatically, and
starts building app-web-ui and app-desktop-ui projects.
Maven helps to avoid such requirements to discover all the libraries required. Maven does
so by reading project files (pom.xml) of dependencies, figure out their dependencies and
so on.
We only need to define direct dependency in each project pom. Maven handles the rest
automatically.
Page 67 of 96
With transitive dependencies, the graph of included libraries can quickly grow to a large
extent. Cases can arise when there are duplicate libraries. Maven provides few features to
control extent of transitive dependencies.
Dependency mediation
Determines what version of a dependency is to be used when multiple versions
1
of an artifact are encountered. If two dependency versions are at the same
depth in the dependency tree, the first declared dependency will be used.
Dependency management
Directly specify the versions of artifacts to be used when they are encountered
2 in transitive dependencies. For an example project C can include B as a
dependency in its dependency Management section and directly control which
version of B is to be used when it is ever referenced.
Dependency scope
3
Includes dependencies as per the current stage of the build.
Excluded dependencies
Any transitive dependency can be excluded using "exclusion" element. As
4
example, A depends upon B and B depends upon C, then A can mark C as
excluded.
Optional dependencies
Any transitive dependency can be marked as optional using "optional" element.
5
As example, A depends upon B and B depends upon C. Now B marked C as
optional. Then A will not use C.
Dependency Scope
Transitive Dependencies Discovery can be restricted using various Dependency Scope as
mentioned below.
compile
1 This scope indicates that dependency is available in classpath of project. It is
default scope.
provided
2 This scope indicates that dependency is to be provided by JDK or web-
Server/Container at runtime.
3 runtime
Page 68 of 96
This scope indicates that dependency is not required for compilation, but is
required during execution.
test
4 This scope indicates that the dependency is only available for the test
compilation and execution phases.
system
5
This scope indicates that you have to provide the system path.
import
This scope is only used when dependency is of type pom. This scope indicates
6
that the specified POM should be replaced with the dependencies in that POM's
<dependencyManagement> section.
Dependency Management
Usually, we have a set of project under a common project. In such case, we can create a
common pom having all the common dependencies and then make this pom, the parent of
sub-project's poms. Following example will help you understand this concept.
App-UI-WAR
<groupId>com.companyname.groupname</groupId>
<artifactId>App-UI-WAR</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>com.companyname.groupname</groupId>
<artifactId>App-Core-lib</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>com.companyname.groupname</groupId>
<artifactId>App-Data-lib</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>
App-Core-lib
App-Data-lib
<parent>
<artifactId>Root</artifactId>
<groupId>com.companyname.groupname</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.companyname.groupname</groupId>
<artifactId>App-Data-lib</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
</project>
Root
</dependencies>
</project>
Now when we build App-UI-WAR project, Maven will discover all the dependencies by
traversing the dependency graph and build the application.
Common dependencies can be placed at single place using concept of parent pom.
Dependencies of App-Data-lib and App-Core-lib project are listed in Root project
(See the packaging type of Root. It is POM).
Check-in the code from all project in progress into the SVN (version control
system) or source code repository and tag it.
Store the build output either WAR or EAR file to a common network location.
Get the file from network and deploy the file to the production site.
Updated the documentation with date and updated version number of the
application.
Problem Statement
There are normally multiple people involved in the above mentioned deployment process.
One team may handle check-in of code, other may handle build and so on. It is very likely
that any step may be missed out due to manual efforts involved and owing to multi-team
environment. For example, older build may not be replaced on network machine and
deployment team deployed the older build again.
Solution
Automate the deployment process by combining the following −
</configuration>
</plugin>
</plugins>
</build>
</project>
SCM
1
Configures the SVN location from where Maven will check out the source code.
Repositories
2 Location where built WAR/EAR/JAR or any other artifact will be stored after
code build is successful.
Plugin
3
maven-release-plugin is configured to automate the deployment process.
mvn release:clean
It cleans the workspace in case the last release process was not successful.
mvn release:rollback
Rollback the changes done to workspace code and configuration in case the last release
process was not successful.
mvn release:prepare
Changes the version of the application and removes SNAPSHOT from the version to
make release.
Page 74 of 96
Increment the version number and append SNAPSHOT for future release.
mvn release:perform
Checks out the code using the previously defined tag and run the Maven deploy goal, to
deploy the war or built artifact to repository.
Let's open the command console, go to the C:\ > MVN >bus-core-api directory and
execute the following mvn command.
>mvn release:prepare
Maven will start building the project. Once build is successful run the following mvn
command.
>mvn release:perform
Once build is successful you can verify the uploaded JAR file in your repository.
C:\MVN>mvn archetype:generate
-DgroupId = com.companyname.automobile
-DartifactId = trucks
Page 75 of 96
-DarchetypeArtifactId = maven-archetype-webapp
-DinteractiveMode = false
Maven will start processing and will create the complete web based java application project
structure as follows −
Now go to C:/MVN directory. You'll see a java application project created, named trucks
(as specified in artifactId) as specified in the following snapshot. The following directory
structure is generally used for web applications −
Page 76 of 96
Maven uses a standard directory layout. Using the above example, we can understand the
following key concepts −
trucks
1
contains src folder and pom.xml.
src/main/webapp
2
contains index.jsp and WEB-INF folder.
src/main/webapp/WEB-INF
3
contains web.xml
src/main/resources
4
it contains images/properties files.
POM.xml
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>trucks</finalName>
</build>
</project>
If you observe, you will find that Maven also created a sample JSP Source file.
Open C:\ > MVN > trucks > src > main > webapp > folder to see index.jsp with the
following code −
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
You can view the output of Maven commands inside the Eclipse, using its own
console.
It does the dependency management for Eclipse build path based on Maven's
pom.xml.
It automatic downloads the required dependencies and sources from the remote
Maven repositories.
It provides wizards for creating new Maven projects, pom.xml and to enable Maven
support on existing projects
Eclipse URL
Following example will help you to leverage benefits of integrating Eclipse and maven.
Open Eclipse.
Select Project location, where a project was created using Maven. We've created a
Java Project consumer Banking in the previous chapters. Go to ‘Creating Java
Project’ chapter, to see how to create a project using Maven.
Now, have a look at consumer Banking project properties. You can see that Eclipse has
added Maven dependencies to java build path.
Page 82 of 96
Maven will start building the project. You can see the output in Eclipse Console as follows
−
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. bu
[INFO] skip non existing resourceDirectory C:\MVN\consumerBanking\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ consumerBanking ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is
[INFO] Compiling 1 source file to C:\MVN\consumerBanking\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ consum
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. bu
[INFO] skip non existing resourceDirectory C:\MVN\consumerBanking\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ consumerBan
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is
[INFO] Compiling 1 source file to C:\MVN\consumerBanking\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ consumerBanking ---
[INFO] Surefire report directory: C:\MVN\consumerBanking\target\surefire-reports
-------------------------------------------------------
TESTS
-------------------------------------------------------
Running com.companyname.bank.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.028 sec
Results :
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ consumerBanking ---
[INFO] Building jar: C:\MVN\consumerBanking\target\consumerBanking-1.0-SNAPSHOT.j
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.663 s
[INFO] Finished at: 2021-12-13T17:34:27+05:30
[INFO] ------------------------------------------------------------------------
Now, right click on App.java. Select Run As option. Then select Java Application.
Page 84 of 96
Hello World!
Maven - NetBeans
NetBeans 6.7 and newer has in-built support for Maven. In case of previous version,
Maven plugin is available in plugin Manager. We are using NetBeans 6.9 in this example.
You can view the output of Maven commands inside the NetBeans using its own
console.
NetBeans provides a Maven Repository browser that enables you to view your local
repository and registered external Maven repositories.
Following example will help you to leverage benefits of integrating NetBeans and Maven.
Open NetBeans.
Select Project location, where a project was created using Maven. We've created a
Java Project consumerBanking. Go to ‘Creating Java Project’ chapter, to see how to
create a project using Maven.
Now, you can see the maven project in NetBeans. Have a look at consumerBanking project
Libraries and Test Libraries. You can see that NetBeans has added Maven dependencies to
its build path.
Page 86 of 96
Maven will start building the project. You can see the output in NetBeans Console as
follows −
------------------------------------------------------------------------
[clean:clean]
[resources:resources]
[WARNING] Using platform encoding (Cp1252 actually)
to copy filtered resources, i.e. build is platform dependent!
skip non existing resourceDirectory C:\MVN\consumerBanking\src\main\resourc
[compiler:compile]
Compiling 2 source files to C:\MVN\consumerBanking\target\classes
[resources:testResources]
[WARNING] Using platform encoding (Cp1252 actually)
to copy filtered resources, i.e. build is platform dependent!
skip non existing resourceDirectory C:\MVN\consumerBanking\src\test\resourc
[compiler:testCompile]
Compiling 1 source file to C:\MVN\consumerBanking\target\test-classes
[surefire:test]
Surefire report directory: C:\MVN\consumerBanking\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.companyname.bank.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.023 sec
Results :
[jar:jar]
Building jar: C:\MVN\consumerBanking\target\consumerBanking-1.0-SNAPSHOT.ja
[install:install]
Installing C:\MVN\consumerBanking\target\consumerBanking-1.0-SNAPSHOT.jar
to C:\Users\GB3824\.m2\repository\com\companyname\bank\consumerBanking\
1.0-SNAPSHOT\consumerBanking-1.0-SNAPSHOT.jar
------------------------------------------------------------------------
BUILD SUCCESSFUL
------------------------------------------------------------------------
Total time: 9 seconds
Finished at: Thu Jul 19 12:57:28 IST 2012
Final Memory: 16M/85M
------------------------------------------------------------------------
Now, right click on App.java. Select Run File as option. You will see the result in the
NetBeans Console.
You can view the output of Maven commands inside the IntelliJ IDEA using its own
console.
Page 90 of 96
IntelliJ IDEA resolves Maven dependencies from its workspace without installing to
local Maven repository (requires dependency project be in same workspace).
IntelliJ IDEA automatically downloads the required dependencies and sources from
the remote Maven repositories.
IntelliJ IDEA provides wizards for creating new Maven projects, pom.xml.
Following example will help you to leverage benefits of integrating IntelliJ IDEA and Maven.
Select Project location, where a project was created using Maven. We have created
a Java Project consumerBanking. Go to ‘Creating Java Project' chapter, to see how
to create a project using Maven.
Page 92 of 96
Now, you can see the maven project in IntelliJ IDEA. Have a look at
consumerBanking project external libraries. You can see that IntelliJ IDEA has
added Maven dependencies to its build path under Maven section.
Page 94 of 96
"C:\Program Files\Java\jdk1.6.0_21\bin\java"
-Didea.launcher.port=7533
"-Didea.launcher.bin.path=
Page 96 of 96
C:\Program Files\Java\jdk1.6.0_21\jre\lib\deploy.jar;
C:\Program Files\Java\jdk1.6.0_21\jre\lib\javaws.jar;
C:\Program Files\Java\jdk1.6.0_21\jre\lib\jce.jar;
C:\Program Files\Java\jdk1.6.0_21\jre\lib\jsse.jar;
C:\Program Files\Java\jdk1.6.0_21\jre\lib\management-agent.jar;
C:\Program Files\Java\jdk1.6.0_21\jre\lib\plugin.jar;
C:\Program Files\Java\jdk1.6.0_21\jre\lib\resources.jar;
C:\Program Files\Java\jdk1.6.0_21\jre\lib\rt.jar;
C:\Program Files\Java\jdk1.6.0_21\jre\lib\ext\dnsns.jar;
C:\Program Files\Java\jdk1.6.0_21\jre\lib\ext\localedata.jar;
C:\Program Files\Java\jdk1.6.0_21\jre\lib\ext\sunjce_provider.jar;
C:\Program Files\Java\jdk1.6.0_21\jre\lib\ext\sunmscapi.jar;
C:\Program Files\Java\jdk1.6.0_21\jre\lib\ext\sunpkcs11.jar
C:\MVN\consumerBanking\target\classes;
C:\Program Files\JetBrains\
IntelliJ IDEA Community Edition 11.1.2\lib\idea_rt.jar"
com.intellij.rt.execution.application.AppMain com.companyname.bank.App
Hello World!