Maven小小入门
一、快速demo
快速构建项目骨架:
敲打命令 mvn archetype:generate
接着很长的输出,很多著名的项目archetype
按enter选择默认的 mvn-archetype-quickstart
继续选择默认
命名groupId:com.sequence.mvndemo
命名artifactId:hello-world
版本可默认
Package包:com.sequence.mvndemo(默认)
命令完成后的项目文档结构:
hello-world
|-- pom.xml
`-- src
|-- main
| `-- java
| `-- com
| `-- sequence
| `-- mvndemo
| `-- App.java
`-- test
`-- java
`-- com
`-- sequence
`-- mvndemo
`-- AppTest.java
自动生成的pom.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.sequence.mvndemo</groupId>
<artifactId>hello-world</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>hello-world</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>
项目构建完成。
基本命令:
a) 编译mvn compile
b) 运行测试 mvn test
test: test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
c) 安装到本地库 mvn install
install: install the package into the local repository, for use as a dependency in other projects locally
d) 打包 mvn package
package: take the compiled code and package it in its distributable format, such as a JAR.
e) 清除之前build 的artifacts :mvn clean
clean: cleans up artifacts created by prior builds,This will remove the target directory with all the build data before starting so that it is fresh.
f) 在target/site 目录下生成项目文档:mvn site
site: generates site documentation for this project
g) 运行jar包
h) 构建成eclipse 项目:mvn eclipse:eclipse
i) 构建成idea项目: mvn idea:idea
文档结构及文件overview
1. ${basedir}/src/main/java 项目代码
${basedir}/src/test/java 测试代码
${basedir}/target 目标目录
- Pom.xml: pom作为项目对象模型。通过xml表示maven项目,使用pom.xml来实现。主要描述了项目:包括配置文件;开发者需要遵循的规则,缺陷管理系统,组织和licenses,项目的url,项目的依赖性,以及其他所有的项目相关因素。
项目基本信息
依赖
Build 插件<plugins> <resources>
……
举例:
1)配置插件,使maven用jdk1.5来编译
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
2)JUnit依赖
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
3. Setting.xml
全局:$M2_HOME/conf/settings.xml
个人:${user.home}/.m2/settings.xml
localRepository:表示本地库的保存位置,也就是maven2主要的jar保存位置,默认在${user.dir}/.m2/repository,如果需要另外设置,就换成其他的路径。
offline:如果不想每次编译,都去查找远程中心库,那就设置为true。当然前提是你已经下载了必须的依赖包。
设置本地资源库:
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>${user.home}/.m2/repository</localRepository>
<interactiveMode>true</interactiveMode>
<usePluginRegistry>false</usePluginRegistry>
<offline>false</offline>
...
</settings>
Mirrors :表示镜像库,指定库的镜像,用于增加其他库
<mirrors>
<mirror>
<id>planetmirror.com</id>
<name>PlanetMirror Australia</name>
<url>http://downloads.planetmirror.com/pub/maven2</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
id,name:唯一的标志,用于区别镜像
url:镜像的url
mirrorOf:此镜像指向的服务id
posted on 2011-07-05 11:31 SeQuence.Choi 阅读(334) 评论(0) 编辑 收藏 举报