Maven入门教程

听说Maven这种项目周期管理软件已经很长时间了,因为之前做的项目都不允许上网,所以没办法使用maven进行jar包下载。现在跳槽后直接使用专线链接互联网,终于有了使用maven的机会。因此就简单写一写maven的入门资料。

首先需要下载maven(因为我在windows使用eclipse进行coding,所以我下载maven的windows版本)。下载后直接安装,然后需要配置三个环境变量:

1、MAVEN_HOME=安装路径(e.g C:\apache-maven-3.0)

2、MAVEN=%MAVEN_HOME%\bin

3、在PATH变量中添加 %MAVEN_HOME%

OK。在cmd中输入 mvn -v 。如果看到

++++++++++++++++++++++++++++++++++++++++++++++++

C:\Users\Eric>mvn -version
Apache Maven 3.0.3 (r1075438; 2011-03-01 01:31:09+0800)
Maven home: D:\Program Files (x86)\apache-maven-3.0.3
Java version: 1.7.0, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.7.0\jre
Default locale: zh_CN, platform encoding: GBK
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"

++++++++++++++++++++++++++++++++++++++++++++++++

说明安装成功。

下面开始部署第一个maven project。在eclipse中新建一个maven project。

这是配置的pom.xml内容

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 3   <modelVersion>4.0.0</modelVersion>
 4 
 5   <groupId>org.andy.maven</groupId>
 6   <artifactId>MavenTest</artifactId>
 7   <version>0.0.1-SNAPSHOT</version>
 8   <packaging>jar</packaging>
 9 
10   <name>MavenTest</name>
11   <url>http://maven.apache.org</url>
12 
13   <properties>
14     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15   </properties>
16 
17   <dependencies>
18     <dependency>
19       <groupId>junit</groupId>
20       <artifactId>junit</artifactId>
21       <version>3.8.1</version>
22       <scope>test</scope>
23     </dependency>
24   </dependencies>
25 </project>

我们编写一个JAVA类

 1 package org.andy.maven.MavenTest;
 2 
 3 public class MyMavenTest {
 4 
 5     public String getString(String s){
 6         System.out.println("In the getString the para is "+s);
 7         return "SUCCESS";
 8     }
 9     
10     public static void main(String[] args) {
11         // TODO Auto-generated method stub
12         System.out.println("In the main is OK");
13     }
14 
15 }

然后在pom.xml上面点击右键,选择run as->Maven build 。在Goals 中输入 clean compile (表示先清空,然后再重新编译).

 1 [INFO] Scanning for projects...
 2 [INFO]                                                                         
 3 [INFO] ------------------------------------------------------------------------
 4 [INFO] Building MavenTest 0.0.1-SNAPSHOT
 5 [INFO] ------------------------------------------------------------------------
 6 [INFO] 
 7 [INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ MavenTest ---
 8 [INFO] Deleting D:\scala-workspace\MavenTest\target
 9 [INFO] 
10 [INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ MavenTest ---
11 [debug] execute contextualize
12 [INFO] Using 'UTF-8' encoding to copy filtered resources.
13 [INFO] skip non existing resourceDirectory D:\scala-workspace\MavenTest\src\main\resources
14 [INFO] 
15 [INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ MavenTest ---
16 [INFO] Compiling 1 source file to D:\scala-workspace\MavenTest\target\classes
17 [INFO] ------------------------------------------------------------------------
18 [INFO] BUILD SUCCESS
19 [INFO] ------------------------------------------------------------------------
20 [INFO] Total time: 2.027s
21 [INFO] Finished at: Mon Jul 21 21:52:08 CST 2014
22 [INFO] Final Memory: 9M/23M
23 [INFO] ------------------------------------------------------------------------

18行表示maven 编译成功。

eclipse中maven命令解释:

Maven Clean -- 清空当前项目

Maven Build -- 执行maven命令

Maven Install -- 生成jar包

Update Project -- 如果修改了pom里面的依赖信息(e.g 版本号,增加/删除jar包等),更新当前项目

posted on 2014-07-21 22:01  vikings`s blog  阅读(321)  评论(0编辑  收藏  举报

导航