安装配置略
setting.xml文件使用默认
示例程序
1 新建hello-world文件夹
2 在该文件夹中创建pom.xml文件
1
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 4.0.0
6
7
8 ...
9 ...
10 ...
11 ...
12 ...
13 ...
14 ...
15 ...
16 ...
17
18
19 ...
20 ...
21
22
23 ...
24 ...
25 ...
26 ...
27 ...
28 ...
29 ...
30 ...
31
32
33 ...
34 ...
35 ...
36 ...
37 ...
38 ...
39 ...
40 ...
41 ...
42
示例代码如下:
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
cn.yue.mvn
hello-worlds
0.0.1-SNAPSHOT
hello-worlds
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:\workspace3\hello-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:\workspace3\hello-world\src\main\re
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:\workspace3\hello-world\target\classes
[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中添加依赖
junit
junit
4.7
test
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:\workspace3\hello-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:\workspace3\hello-world\target
[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:\workspace3\hello-world\src\main\re
sources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ hello-worlds
---
[INFO] Compiling 1 source file to F:\workspace3\hello-world\target\classes
[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:\workspace3\hello-world\src\test\re
sources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ hello
-worlds ---
[INFO] Compiling 1 source file to F:\workspace3\hello-world\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ hello-worlds ---
[INFO] Surefire report directory: F:\workspace3\hello-world\target\surefire-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:\workspace3\hello-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:\workspace3\hello-world\target
[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:\workspace3\hello-world\src\main\re
sources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ hello-worlds
---
[INFO] Compiling 1 source file to F:\workspace3\hello-world\target\classes
[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:\workspace3\hello-world\src\test\re
sources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ hello
-worlds ---
[INFO] Compiling 1 source file to F:\workspace3\hello-world\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ hello-worlds ---
[INFO] Surefire report directory: F:\workspace3\hello-world\target\surefire-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:\workspace3\hello-world\target\hello-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:\workspace3\hello-world\target>dir
驱动器 F 中的卷是 工作
卷的序列号是 0002-6355
F:\workspace3\hello-world\target 的目录
2014/08/27 11:24
.
2014/08/27 11:24 ..
2014/08/27 11:24 classes
2014/08/27 11:24 2,432 hello-worlds-0.0.1-SNAPSHOT.jar
2014/08/27 11:24 maven-archiver
2014/08/27 11:24 surefire
2014/08/27 11:24 surefire-reports
2014/08/27 11:24 test-classes
1 个文件 2,432 字节
7 个目录 135,869,345,792 可用字节
==================如何在其他maven项 目中直接引用================
F:\workspace3\hello-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:\workspace3\hello-world\target
[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:\workspace3\hello-world\src\main\re
sources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ hello-worlds
---
[INFO] Compiling 1 source file to F:\workspace3\hello-world\target\classes
[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:\workspace3\hello-world\src\test\re
sources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ hello
-worlds ---
[INFO] Compiling 1 source file to F:\workspace3\hello-world\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ hello-worlds ---
[INFO] Surefire report directory: F:\workspace3\hello-world\target\surefire-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:\workspace3\hello-world\target\hello-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:\workspace3\hello-world\target\hello-worlds-0.0.1-SNAPSHOT.j
ar to C:\Users\acer-pc\.m2\repository\cn\yue\mvn\hello-worlds\0.0.1-SNAPSHOT\hel
lo-worlds-0.0.1-SNAPSHOT.jar
[INFO] Installing F:\workspace3\hello-world\pom.xml to C:\Users\acer-pc\.m2\repo
sitory\cn\yue\mvn\hello-worlds\0.0.1-SNAPSHOT\hello-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中添加如下内容
org.apache.maven.plugins
maven-shade-plugin
1.2.1
package
shade
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
cn.yue.mvn.HelloWorld
重新编安装
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中添加要执行的命令
========================================================================================
------------------------- 坐标和依赖----------------------------
========================================================================================
坐标的定义
cn.yue.mvn
hello-world
1.0-SNAPSHOT
jar
junit
junit
3.8.1
test
================依赖范围============
compile 编译 (默认)
test 测试
provided 已提供
runtime 运行时
system 系统
import 导入依赖
示例代码如下:
javax.sql
jdbc-stdext
2.0
system
${java.home}/lib/rt.jar
查看当前项目的已解析依赖
mvn dependency:list
查看当前项目的依赖树
mvn dependency:tree
mvn dependency:analyze
=======================================================================================
-----------------------------使用maven构建web应用-------------------------
=======================================================================================
打包war程序
借助jetty-maven-plugin快速开发和web模块
显示的指定打包方式
4.0.0
cn.yue.mvn
0.0.1-SNAPSHOT
hello-world
pom
类和资源文件
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
org.mortbay.jetty
jetty-maven-plugin
7.1.6.v20100715
10
/test
如果想直接运行jetty-maven-plugin,还需要在setting.xml文件下添加如下:
org.mortbay.jetty
此时可以使用mvn jetty:run运行jetty,默认监听的端口为:8080
如果想修改端口号,使用以下命令:
mvn jetty:run -Djetty.port=9999
======================================使用cargo实现知动布署============================
布署到web容器
cargo支持两种布置方式:
1 standalone模式
修改pom.xml配置文件
org.codehaus.cargo
cargo-maven-plugin
1.0
tomcat6x
d:\cmd\apache-tomcat
standalone
${project.build.directory}/tomcat6x
2 existing模式