0% found this document useful (0 votes)
10 views27 pages

Apache Maven

The document provides an overview of Apache Maven, a build automation tool for Java projects, detailing its project lifecycle, configuration, and key components such as POM, dependencies, and plugins. It explains how to create a simple project, manage dependencies, and utilize various plugins for tasks like compilation and testing. Additionally, it covers the use of properties and profiles to enhance project configuration and maintainability.

Uploaded by

eramchegyt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views27 pages

Apache Maven

The document provides an overview of Apache Maven, a build automation tool for Java projects, detailing its project lifecycle, configuration, and key components such as POM, dependencies, and plugins. It explains how to create a simple project, manage dependencies, and utilize various plugins for tasks like compilation and testing. Additionally, it covers the use of properties and profiles to enhance project configuration and maintainability.

Uploaded by

eramchegyt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Apache

Maven
IT Academy
Agenda
• What is Maven, Project lifecycle
• Getting, installing, configuring Maven
• POM, GAV, Archetype
• The pom.xml contents
• dependencies
• properties
• exclusions
• profiles
• Some useful plugins
What is Apache Maven
• Apache Maven is a build automation tool used
primarily for Java projects.
• Maven addresses two aspects of building software:
1) it describes how software is built
2) it describes its dependencies
Get it!
Configure
Windows
Settings: %MAVEN_HOME%\conf\settings.xml
Repository Location: UserProfile\.m2\

Linux
Settings: /usr/local/maven/conf/settings.xml
Repository Location: ~/.m2/

Use your own temporary settings for maven:


mvn --settings=[PATH_TO_SETTINGS_FILE]
Adding a Local repository location:
mvn -Dmaven.repo.local=/path/to/local/repo
Create a Simple Project
mvn archetype:generate
-DgroupId=[myGroup]
-DartifactId=[myArtifact]
-Dversion=1.0-SNAPSHOT
-DarchetypeArtifactId=maven-archetype-
artifact

OR
mvn archetype:generate
Arche-who? O_o
• Archetype is a Maven project templating toolkit.
• An archetype is defined as an original pattern or model from which
all other things of the same kind are made.
Path Conventions
Repositories
• Local repository:
 copy on local computer which is a
cache of the remote downloads;
 may contain project-local build
artifacts as well;
 located in (by default)
UserProfile/.m2/repository
Project Object Model (POM)
• Describes a project
 Name and Version
 Artifact Type
 Source Code Locations
 Dependencies
 Plugins
 Profiles (alternate build configs.)

• Uses XML by default


Project Name (GAV)
• Maven uniquely identifies a project using:
groupId: project grouping identifier (no spaces or colons)
 usually loosely based on Java package

artfiactId: name of project (no spaces or colons)


version: version of project
 format {Major}.{Minor}.{Maintenance}
 add -SNAPSHOT to identify in development
Project Name (GAV)
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.softserve.itacademy</groupId>
<artifactId>HelloMaven</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Maven project</name>
<description>This is a maven project</description>
<packaging>jar</packaging>
<properties>
<java.version>1.8</java.version>
<properties>
</project>
Dependencies
• The Dependency is another archive (JAR, ZIP, and so on) which
current project needs in order to compile, build, test, and/or to
run.
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
<scope>compile</scope>
</dependency>
</dependencies>
Dependency Scope
• compile (default)
• provided (JDK or the container provides it)
• runtime (only required for execution, not for compilation)
• test (only required for testing, not for normal use)
• system (you provide it locally, not looked up in a repo)

<scope>system</scope>
<systemPath>${home}/tools.jar</systemPath>
Exclusions
• The Maven picks the "nearest definition“ that means that the version
used will be the closest one to your project in the tree of dependencies.

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.14.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>

</exclusion>
</exclusions>
</dependency>
Build Lifecycle

• The Build Lifecycle is the process


of building and distributing an
artifact.

Separate “clean” and “site” lifecycle


Properties
• The Properties or variables are useful to keep your Maven pom.xml file
more easy to read and maintain.

<properties>
<jdk.version>1.8</jdk.version>
<log4j.version>1.2.16</log4j.version>
</properties>

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>

mvn install -Dmyproperty = value


Profiles
• The Profiles are an alternative set of configuration values which set
or override default values.

<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>

mvn install -Pprofilename


Profile Activation
Operating System based: JDK based:
<activation> <activation>
<os> <jdk>1.4</jdk>
<name>Windows 7</name> </activation>
<family>Windows</family> <activation>
<arch>x64</arch> <jdk>[1.5-1.8)</jdk>
<version>5.1.2600</version> </activation>
</os>
</activation>
Maven Operation Model
• The Plugin-architecture allows
usage of plug-ins for various
tasks: compile, test, build,
deploy, checkstyle, etc.
Plugins
• The maven-compiler-plugin has two goals:

compiler:compile and compiler:testCompile

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
Plugins
• The Assembly Plugin for Maven is primarily intended to allow users to aggregate the
project output along with its dependencies, modules, site documentation, and other files
into a single distributable archive.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.softserve.itacademy.App</mainClass> assembly:singl
</manifest>
</archive> e
</configuration>
</plugin>
Plugins
• The maven-surefire-plugin is used during the test phase of the
build lifecycle to execute the unit tests of an application.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<includes>
<include>**/*Test.java</include>

</includes> surefire:test
<excludes>
<exclude>**/TestToSkip.java</exclude>
</excludes>
</configuration>
</plugin>
Plugins
The maven-checkstyle-plugin generates a report regarding the
code style used by the developers.

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.17</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>check</goal> checkstyle:checkstyle
</goals>
</execution>
</executions>
</plugin>
Plugins
• The maven-dependency-plugin provides the capability to
manipulate artifacts.
• It can copy and/or unpack artifacts from local or remote
repositories to a specified location.

mvn
dependency:analyze
mvn dependency:tree
Plugins
• Generate project files for the most popular IDEs.

idea:ide eclipse:eclips
a e

• Manipulate WAR projects within the popular Servlet containers.

tomcat7:run glassfish:deploy
Thanks for
attention!
IT Academy

You might also like