安装配置略
setting.xml文件使用默认
示例程序
1 新建hello-world文件夹
2 在该文件夹中创建pom.xml文件
1 <project xmlns="http://maven.apache.org/POM/4.0.0"
2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
4 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5 <modelVersion>4.0.0</modelVersion>
6
7 <!-- 基本设置 -->
8 <groupId>...</groupId>
9 <artifactId>...</artifactId>
10 <version>...</version>
11 <packaging>...</packaging>
12 <dependencies>...</dependencies>
13 <parent>...</parent>
14 <dependencyManagement>...</dependencyManagement>
15 <modules>...</modules>
16 <properties>...</properties>
17
18 <!-- 构建过程的设置 -->
19 <build>...</build>
20 <reporting>...</reporting>
21
22 <!-- 项目信息设置 -->
23 <name>...</name>
24 <description>...</description>
25 <url>...</url>
26 <inceptionYear>...</inceptionYear>
27 <licenses>...</licenses>
28 <organization>...</organization>
29 <developers>...</developers>
30 <contributors>...</contributors>
31
32 <!-- 环境设置 -->
33 <issueManagement>...</issueManagement>
34 <ciManagement>...</ciManagement>
35 <mailingLists>...</mailingLists>
36 <scm>...</scm>
37 <prerequisites>...</prerequisites>
38 <repositories>...</repositories>
39 <pluginRepositories>...</pluginRepositories>
40 <distributionManagement>...</distributionManagement>
41 <profiles>...</profiles>
42 </project>
示例代码如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>cn.yue.mvn</groupId>
<artifactId>hello-worlds</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hello-worlds</name>
</project>
3 创建源码目录
src/main/java/包名
HelloWorld.java文件内容如下;
package cn.yue.mvn;
/**
* 测试mvn
*
* @version
*
* @author retacn
*
* @time 2014-8-27
*/
public class HelloWorld {
public String sayHello() {
return "hello maven";
}
public static void main(String[] args) {
System.out.println(new HelloWorld().sayHello());
}
}
4 编译执行
F:workspace3hello-world>mvn clean compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building hello-worlds 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: http://192.168.1.79:8081/nexus/content/groups/public/org/apache/mav
en/plugins/maven-resources-plugin/2.5/maven-resources-plugin-2.5.pom
Downloaded: http://192.168.1.79:8081/nexus/content/groups/public/org/apache/mave
n/plugins/maven-resources-plugin/2.5/maven-resources-plugin-2.5.pom (7 KB at 2.3
KB/sec)
Downloading: http://192.168.1.79:8081/nexus/content/groups/public/org/apache/mav
en/plugins/maven-plugins/19/maven-plugins-19.pom
Downloaded: http://192.168.1.79:8081/nexus/content/groups/public/org/apache/mave
n/plugins/maven-plugins/19/maven-plugins-19.pom (11 KB at 25.5 KB/sec)
Downloading: http://192.168.1.79:8081/nexus/content/groups/public/org/apache/mav
en/maven-parent/19/maven-parent-19.pom
Downloaded: http://192.168.1.79:8081/nexus/content/groups/public/org/apache/mave
n/maven-parent/19/maven-parent-19.pom (25 KB at 41.2 KB/sec)
Downloading: http://192.168.1.79:8081/nexus/content/groups/public/org/apache/mav
en/plugins/maven-resources-plugin/2.5/maven-resources-plugin-2.5.jar
Downloaded: http://192.168.1.79:8081/nexus/content/groups/public/org/apache/mave
n/plugins/maven-resources-plugin/2.5/maven-resources-plugin-2.5.jar (26 KB at 54
3.7 KB/sec)
[INFO]
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ hello-worlds ---
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ hello-worl
ds ---
Downloading: http://192.168.1.79:8081/nexus/content/groups/public/org/apache/mav
en/shared/maven-filtering/1.0/maven-filtering-1.0.pom
Downloaded: http://192.168.1.79:8081/nexus/content/groups/public/org/apache/mave
n/shared/maven-filtering/1.0/maven-filtering-1.0.pom (6 KB at 14.4 KB/sec)
Downloading: http://192.168.1.79:8081/nexus/content/groups/public/org/apache/mav
en/shared/maven-shared-components/16/maven-shared-components-16.pom
Downloaded: http://192.168.1.79:8081/nexus/content/groups/public/org/apache/mave
n/shared/maven-shared-components/16/maven-shared-components-16.pom (9 KB at 20.4
KB/sec)
Downloading: http://192.168.1.79:8081/nexus/content/groups/public/org/apache/mav
en/shared/maven-filtering/1.0/maven-filtering-1.0.jar
Downloaded: http://192.168.1.79:8081/nexus/content/groups/public/org/apache/mave
n/shared/maven-filtering/1.0/maven-filtering-1.0.jar (42 KB at 65.5 KB/sec)
[debug] execute contextualize
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e
. build is platform dependent!
[INFO] skip non existing resourceDirectory F:workspace3hello-worldsrcmain
e
sources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ hello-worlds
---
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. buil
d is platform dependent!
[INFO] Compiling 1 source file to F:workspace3hello-world argetclasses
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.251s
[INFO] Finished at: Wed Aug 27 09:35:42 CST 2014
[INFO] Final Memory: 12M/94M
[INFO] ------------------------------------------------------------------------
5 编写测试代码
在pom.xml中添加依赖
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
</dependencies>
6 源码文件为
package cn.yue.mvn;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
/**
* mvn测试类
*
* @version
*
* @author retacn
*
* @time 2014-8-27
*/
public class HelloWorldTest {
@Test
public void testSayHello() {
HelloWorld helloworld=new HelloWorld();
String result=helloworld.sayHello();
assertEquals("hello maven",result);
}
}
7 编译测试
F:workspace3hello-world>mvn clean test
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for
cn.yue.mvn:hello-worlds:jar:0.0.1-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-comp
iler-plugin is missing. @ line 5, column 12
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten t
he stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support buildin
g such malformed projects.
[WARNING]
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building hello-worlds 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ hello-worlds ---
[INFO] Deleting F:workspace3hello-world arget
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ hello-worl
ds ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory F:workspace3hello-worldsrcmain
e
sources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ hello-worlds
---
[INFO] Compiling 1 source file to F:workspace3hello-world argetclasses
[INFO]
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ he
llo-worlds ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory F:workspace3hello-worldsrc est
e
sources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ hello
-worlds ---
[INFO] Compiling 1 source file to F:workspace3hello-world arget est-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ hello-worlds ---
[INFO] Surefire report directory: F:workspace3hello-world argetsurefire-repo
rts
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running cn.yue.mvn.HelloWorldTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.06 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.715s
[INFO] Finished at: Wed Aug 27 11:19:30 CST 2014
[INFO] Final Memory: 16M/152M
[INFO] ------------------------------------------------------------------------
8 打包和运行
F:workspace3hello-world>mvn clean package
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for
cn.yue.mvn:hello-worlds:jar:0.0.1-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-comp
iler-plugin is missing. @ line 5, column 12
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten t
he stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support buildin
g such malformed projects.
[WARNING]
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building hello-worlds 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ hello-worlds ---
[INFO] Deleting F:workspace3hello-world arget
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ hello-worl
ds ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory F:workspace3hello-worldsrcmain
e
sources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ hello-worlds
---
[INFO] Compiling 1 source file to F:workspace3hello-world argetclasses
[INFO]
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ he
llo-worlds ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory F:workspace3hello-worldsrc est
e
sources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ hello
-worlds ---
[INFO] Compiling 1 source file to F:workspace3hello-world arget est-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ hello-worlds ---
[INFO] Surefire report directory: F:workspace3hello-world argetsurefire-repo
rts
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running cn.yue.mvn.HelloWorldTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.063 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.3.2:jar (default-jar) @ hello-worlds ---
[INFO] Building jar: F:workspace3hello-world argethello-worlds-0.0.1-SNAPSHO
T.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.980s
[INFO] Finished at: Wed Aug 27 11:24:23 CST 2014
[INFO] Final Memory: 17M/150M
[INFO] ------------------------------------------------------------------------
结果会保存在target目录中
F:workspace3hello-world arget>dir
驱动器 F 中的卷是 工作
卷的序列号是 0002-6355
F:workspace3hello-world arget 的目录
2014/08/27 11:24 <DIR> .
2014/08/27 11:24 <DIR> ..
2014/08/27 11:24 <DIR> classes
2014/08/27 11:24 2,432 hello-worlds-0.0.1-SNAPSHOT.jar
2014/08/27 11:24 <DIR> maven-archiver
2014/08/27 11:24 <DIR> surefire
2014/08/27 11:24 <DIR> surefire-reports
2014/08/27 11:24 <DIR> test-classes
1 个文件 2,432 字节
7 个目录 135,869,345,792 可用字节
==================如何在其他maven项 目中直接引用================
F:workspace3hello-world>mvn clean install
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for
cn.yue.mvn:hello-worlds:jar:0.0.1-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-comp
iler-plugin is missing. @ line 5, column 12
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten t
he stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support buildin
g such malformed projects.
[WARNING]
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building hello-worlds 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ hello-worlds ---
[INFO] Deleting F:workspace3hello-world arget
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ hello-worl
ds ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory F:workspace3hello-worldsrcmain
e
sources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ hello-worlds
---
[INFO] Compiling 1 source file to F:workspace3hello-world argetclasses
[INFO]
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ he
llo-worlds ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory F:workspace3hello-worldsrc est
e
sources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ hello
-worlds ---
[INFO] Compiling 1 source file to F:workspace3hello-world arget est-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ hello-worlds ---
[INFO] Surefire report directory: F:workspace3hello-world argetsurefire-repo
rts
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running cn.yue.mvn.HelloWorldTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.063 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.3.2:jar (default-jar) @ hello-worlds ---
[INFO] Building jar: F:workspace3hello-world argethello-worlds-0.0.1-SNAPSHO
T.jar
[INFO]
[INFO] --- maven-install-plugin:2.3.1:install (default-install) @ hello-worlds -
--
Downloading: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-dige
st/1.0/plexus-digest-1.0.pom
Downloaded: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-diges
t/1.0/plexus-digest-1.0.pom (2 KB at 1.4 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-comp
onents/1.1.7/plexus-components-1.1.7.pom
Downloaded: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-compo
nents/1.1.7/plexus-components-1.1.7.pom (5 KB at 13.0 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-dige
st/1.0/plexus-digest-1.0.jar
Downloaded: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-diges
t/1.0/plexus-digest-1.0.jar (12 KB at 21.2 KB/sec)
[INFO] Installing F:workspace3hello-world argethello-worlds-0.0.1-SNAPSHOT.j
ar to C:Usersacer-pc.m2
epositorycnyuemvnhello-worlds .0.1-SNAPSHOThel
lo-worlds-0.0.1-SNAPSHOT.jar
[INFO] Installing F:workspace3hello-worldpom.xml to C:Usersacer-pc.m2
epo
sitorycnyuemvnhello-worlds .0.1-SNAPSHOThello-worlds-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.006s
[INFO] Finished at: Wed Aug 27 11:34:13 CST 2014
[INFO] Final Memory: 16M/152M
[INFO] ------------------------------------------------------------------------
===============生成可执行的jar文件===============
如果程序可执行在jar文件 的META-ING/MANIFFEST.MF中会年到main-class一行
需要通过maven-shade-plugin配置该插件
在pom.xml中添加如下内容
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>cn.yue.mvn.HelloWorld</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
重新编安装
mvn clena insall
会在target文件夹下生成以下两个文件
hello-worlds-0.0.1-SNAPSHOT.jar //可直接运行的jar ,META-ING/MANIFFEST.MF中会看到main-class一行
original-hello-worlds-0.0.1-SNAPSHOT.jar //原始的jar
=========================使用archetype生成项目骨架=========================
到任意目录使用mvn archetype:generate 生成项目骨架
Define value for property 'groupId': : cn.yue.mvn
Define value for property 'artifactId': : hello-world
[INFO] Using property: version = 1.0-SNAPSHOT
Define value for property 'package': cn.yue.mvn: : cn.yue.mvn
Confirm properties configuration:
groupId: cn.yue.mvn
artifactId: hello-world
version: 1.0-SNAPSHOT
package: cn.yue.mvn
Y: : y
可以看到该目录下会生成所要的项目的骨架
==================================m2eclipse的基本使用==================================
右击项目选择要执行的命令,如果没有可以通过 maven build中coals中添加要执行的命令
========================================================================================
------------------------- 坐标和依赖----------------------------
========================================================================================
坐标的定义
<!--隶属的实际项目-->
<groupId>cn.yue.mvn</groupId>
<!--项目的一具模块-->
<artifactId>hello-world</artifactId>
<!--当前所处的版本-->
<version>1.0-SNAPSHOT</version>
<!--项目的打包方式-->
<packaging>jar</packaging>
<!--classifier构建输出一些附属构建-->
<!--依赖位置,定义依赖的坐标-->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
================依赖范围============
compile 编译 (默认)
test 测试
provided 已提供
runtime 运行时
system 系统
import 导入依赖
示例代码如下:
<dependency>
<groupId>javax.sql</groupId>
<artifactId>jdbc-stdext</artifactId>
<version>2.0</version>
<scope>system</scope>
<systemPath>${java.home}/lib/rt.jar</systemPath>
</dependency>
查看当前项目的已解析依赖
mvn dependency:list
查看当前项目的依赖树
mvn dependency:tree
mvn dependency:analyze
=======================================================================================
-----------------------------使用maven构建web应用-------------------------
=======================================================================================
打包war程序
借助jetty-maven-plugin快速开发和web模块
显示的指定打包方式
<modelVersion>4.0.0</modelVersion>
<groupId>cn.yue.mvn</groupId>
<version>0.0.1-SNAPSHOT</version>
<artifactId>hello-world</artifactId>
<!--显示的指定打包方式pom或是war-->
<packaging>pom</packaging>
类和资源文件
src/main/java
src/main/resource
测试类和资源文件
src/test/java
src/test/resource
web的资源目录
src/test/webapp基必须包含WEB-INF子目录和web.xml文件
=============================使用jetty-maven-plugn进行测试===============
配置jetty-maven-plugin
<build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>7.1.6.v20100715</version>
<configuration>
<!--扫描项目变更时间间隔-->
<sacnintervalSeconds>10</sacnintervalSeconds>
<webAppConfig>
<contextPath>/test</contextPath>
</webAppConfig>
</configuration>
</plugin>
</plugins>
</build>
如果想直接运行jetty-maven-plugin,还需要在setting.xml文件下添加如下:
<pluginGroups>
<pluginGroup>org.mortbay.jetty</pluginGroup>
</pluginGroups>
此时可以使用mvn jetty:run运行jetty,默认监听的端口为:8080
如果想修改端口号,使用以下命令:
mvn jetty:run -Djetty.port=9999
======================================使用cargo实现知动布署============================
布署到web容器
cargo支持两种布置方式:
1 standalone模式
修改pom.xml配置文件
<build>
<plugins>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven-plugin</artifactId>
<version>1.0</version>
<configuration>
<containner>
<containerId>tomcat6x</containerId>
<!-- 指定tomcat的根目录 -->
<home>d:cmdapache-tomcat</home>
</containner>
<configuration>
<type>standalone</type>
<home>${project.build.directory}/tomcat6x</home>
</configuration>
</configuration>
</plugin>
</plugins>
</build>
2 existing模式