1.简介
本文将介绍如何使用Eclipse的Maven插件来创建一简单的Maven项目,以及在Eclipse中运行Maven命令。
2.步骤
1)打开新建项目对话框,选择Maven Project(File->New->Object->Maven->Maven Project)
2)不好选择Simple Project,从而可以选择项目的archetype
3)选择名为quickstart的Archetype,点击Next
Archetype:项目骨架,项目的目录和文件结构
4)输入Group Id,Artifact Id信息,Version默认为0.0.1-SNAPSHOT,Package会根据Group Id和Artifact Id自动生成
Group Id:项目所属的项目组
Artifact Id:项目在项目组中的ID
Version:项目的版本号,后缀SNASHOT为快照的意思,表示版本不稳定
5)由于通过quickstart来创建项目时会默认生成一个App.java和一个AppTest.java文件,我们可以直接运行Maven命令
选中项目->Run As->Maven Test
运行结果:
Java代码
- [INFO] Scanning for projects...
- [INFO]
- [INFO] ------------------------------------------------------------------------
- [INFO] Building firstmvn 0.0.1-SNAPSHOT
- [INFO] ------------------------------------------------------------------------
- [INFO]
- [INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ firstmvn ---
- [debug] execute contextualize
- [INFO] Using ‘UTF-8‘ encoding to copy filtered resources.
- [INFO] <span style="color: #ff0000;">skip non existing resourceDirectory F:\workspace\firstmvn\src\main\resources</span>
- [INFO]
- [INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ firstmvn ---
- [INFO] <span style="color: #ff0000;">Nothing to compile - all classes are up to date</span>
- [INFO]
- [INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ firstmvn ---
- [debug] execute contextualize
- [INFO] Using ‘UTF-8‘ encoding to copy filtered resources.
- [INFO]<span style="color: #ff0000;"> skip non existing resourceDirectory F:\workspace\firstmvn\src\test\resources</span>
- [INFO]
- [INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ firstmvn ---
- [INFO]<span style="color: #ff0000;"> Nothing to compile - all classes are up to date</span>
- [INFO]
- [INFO] --- maven-surefire-plugin:2.10:test (default-test) @ firstmvn ---
- [INFO] Surefire report directory: F:\workspace\firstmvn\target\surefire-reports
- -------------------------------------------------------
- T E S T S
- -------------------------------------------------------
- Running com.siyuan.firstmvn.AppTest
- Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.012 sec
- Results :
- Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
- [INFO] ------------------------------------------------------------------------
- [INFO] BUILD SUCCESS
- [INFO] ------------------------------------------------------------------------
- [INFO] Total time: 1.762s
- [INFO] Finished at: Tue Sep 18 22:54:57 CST 2012
- [INFO] Final Memory: 4M/72M
- [INFO] ------------------------------------------------------------------------
注:第一次运行Maven可能会有更多额外的Maven插件下载信息
3.项目结构介绍
firstmvn
|--src
|--main
|--java
|--resources
|--test
|--java
|--resources
|--target
|-pom.xml
SRC:源文件目录
TARGET:编译目录
pom.xml: Project Object Model项目对象模型,定义了项目的基本信息,用于描述项目如何构建,声明依赖等等。
Xml代码
- <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>com.siyuan</groupId>
- <artifactId>firstmvn</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
- <name>firstmvn</name>
- <url>http://maven.apache.org</url>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
- </project>
重要的元素:
groupId,artifactId,version,packaging
这四个元素组成了Maven的坐标,它相当为项目在Maven世界中的ID。
其中packaging默认为jar,可以为jar,pom,war等。
dependencies
项目依赖,由1到N个dependency组成,每个dependency包含了一个坐标和依赖范围scope
其中scope默认为compile,表示该依赖对主代码和测试代码都有效,
test则表示该依赖只对测试有效,即测试代码中可以使用Junit-3.8.1.jar中的内容,主代码不能使用,
其余scope将在之后介绍。