Maven Basics

From: http://tutorials.jenkov.com/maven/maven-tutorial.html

# Introduction
Maven is built around the pom.xml file. In Maven, how to build your project is predefined in the Maven Build Life Cycles, Phases and Goals. The POM file describes *what to build*, but most often *not how to build it*. How to build it is up to the Maven build phases and goals.

# Minimal POM file
Here is a minimal POM file:


4.0.0
com.jenkov
java-web-crawler
1.0.0

this outputs MAVEN_REPO/com/jenkov/java-web-crawler/1.0.0/java-web-crawler-1.0.0.jar

# Super POM
You can make a POM file explicitly inherit from another POM file. That way you can change the settings across all inheriting POM’s via their common super POM. You specify the super POM at the top of a POM file like this:


4.0.0

org.codehaus.mojo
my-parent
2.0
../my-parent

my-project

mvn help:effective-pom shows the combined pom of parent and current

# Running Maven

When executing the mvn command you pass the name of a build life cycle, phase or goal to it, which Maven then executes.

syntax: mvn phase:goal

# Directory Structure
You must follow the maven directory structure

“`
– src
– main
– java
– resources
– webapp
– test
– java
– resources
– target
“`

more here: Introduction to the Maven Standard Directory Layout

# Dependencies

“`
4.0.0
com.jenkov.crawler
java-web-crawler
1.0.0



org.jsoup
jsoup
1.7.1


junit
junit
4.8.1
test


“`

## external dependencies

“`

mydependency
mydependency
system
1.0
${basedir}\war\WEB-INF\lib\mydependency.jar

“`

## snapshot dependencies

Snapshot dependencies are dependencies (JAR files) which are under development.

`1.0-SNAPSHOT`

# Repositories

maven tries to pull dependencies from local, central, and remote repo

## Local Repositores

it’s defined in ~/.m2/settings.xml



d:\data\java\products\maven\repository

## Central Repositories

Maintained by the maven community

## Remote Repositories

A remote repository is often used for hosting projects internal to your organization, which are shared by multiple projects.



jenkov.code
http://maven.jenkov.com/maven2/lib

# Maven Build Cycles

When Maven builds a software project it follows a build life cycle. The build life cycle is divided into build phases, and the build phases are divided into build goals.
Since you cannot execute the default life cycle directly, you need to execute a build phase or goal from the default life cycle.

1. default
validate Validates that the project is correct and all necessary information is available. This also makes sure the dependencies are downloaded.
compile Compiles the source code of the project.
test Runs the tests against the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed.
package Packs the compiled code in its distributable format, such as a JAR.
install Install the package into the local repository, for use as a dependency in other projects locally.
deploy Copies the final package to the remote repository for sharing with other developers and projects.
2. clean
3. site

# Profiles

Profiles let you use differvent build settings

About 逸飞

后端工程师

发表评论

电子邮件地址不会被公开。 必填项已用*标注