Vous êtes sur la page 1sur 15

Maven

Build tool
Similar to ant in this sense
Quality Management
Used to run tests and generate reports
Common activities
Multiple jars
Dependencies and versions
Project structure
Build, publish and deploy
Maven POM
Project Object Model
Describes a Project
Name & Version
Artifact Type
Repositories
Dependencies
Plugins
Profiles (Alternate Build Configurations)
Project Identifier (GAV)
Maven identifies a project using
GroupId
ArtifactId
Version (Snapshot for in-development version)
Describes a Project
Name & Version
Artifact Type
Source Code Locations
Dependencies
Plugins
Profiles (Alternate Build Configurations)
Project Identifier (GAV)
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<artifactId>maven-training</artifactId>
<groupId>org.lds.training</groupId>
<version>1.0</version>
<packaging>jar</packaging>
</project>
Packaging element tells maven how to build the project
Examples are jar, war, ear, custom
Default is jar
Project Inheritance
POM files can inherit configurations
groupId, version
Project Config
Dependencies
Plugin Configurations
etc

<?xml version="1.0" encoding="UTF-8"?>
<project>
<parent>
<artifactId>maven-training-parent</artifactId>
<groupId>org.lds.training</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>maven-training</artifactId>
<packaging>jar</packaging>
</project>
Standard Directory Layout
target: Default work directory
src: All project source files go in this directory
src/main: All sources that go into primary artifact
src/test: All sources contributing to testing project
src/main/java: All java source files
src/main/webapp: All web source files
src/main/resources: All non compiled source files
src/test/java: All java test source files
src/test/resources: All non compiled test source files
Maven Build Lifecycle
Maven build follows a lifecycle
Some common phases
generate-sources
compile
test
package
install
deploy
clean
Maven repositories
Dependencies are download from repository via http
Downloaded dependencies are cached in a local repository
Usually found in ~/.m2/repository
Repository follows a simple folder structure.
{groupId}/{artifactId}/{version}/{artifactId}-{version}.jar
groupId . is replaced with /
By default, maven uses the following repository
http://repo1.maven.org/maven2


Defining Repositories
Repositories are defined in pom or settings
Repositories can be inherited from parent
Repositories are identified by id

<project>
...
<repositories>
<repository>
<id>lds-main</id>
<name>Uworx Nexus</name>
<url>http://10.210.40.37:8081/nexus/content</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</snapshots>
</repository>
</repositories>
</project>
Dependencies
Register dependencies in pom.xml
Concept of transitive dependencies
Can be controlled using exclusions or optional
declarations
Only compile and runtime scopes are transitive
Dependencies consist of
G.A.V (GroupId, ArtifactId, Version)
Scope (compile, test, provided)
If version is not supplied, maven downloads the
default version.

Adding dependencies
<project>
...
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Dependencies can be made optional so that they
are not resolved transitively
Dependency exclusions
Exclusions exclude transitive dependencies
<project>
...
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.0.5.RELEASE</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
Settings.xml
Contains configurations that
Should not be bundled to any specific project
Should not be distributed to an audience (user specific
settings)
Found at location
~/.m2/settings.xml
Contains configurations such as:
Remote repositories
Profiles
Archetypes
Maven project templating toolkit
Quickly generate projects
Best practices employed by your organization

Vous aimerez peut-être aussi