Apache Maven(一):快速入门
Maven 是什么?
Maven 是一个项目管理和整合工具。Maven 为开发者提供了一套完整的构建生命周期框架。开发团队几乎不用花多少时间就能够自动完成工程的基础构建配置,因为 Maven 使用了一个标准的目录结构和一个默认的构建生命周期。
在有多个开发团队环境的情况下,Maven 能够在很短的时间内使得每项工作都按照标准进行。因为大部分的工程配置操作都非常简单并且可复用,在创建报告、检查、构建和测试自动配置时,Maven 可以让开发者的工作变得更简单。
Maven 安装
这里介绍一下在Windows 系统上安装Maven。注:Maven 3.3+ 需要JDK 1.7或更高版本才能执行
Maven 的下载地址:官方下载
我们将下载下来的zip文件,解压到本地磁盘目录,然后配置环境变量。
环境变量配置(windows7为例):
1.添加M2_HOME 和 MAVEN_HOME 环境变量。注:Maven 说只是添加 M2_HOME , 但一些项目仍引用 Maven 的文件夹 MAVEN_HOME, 因此,为了安全也把它添加进去
2.更新Path变量的变量值。在最后追加
命令:mvn -v 或 mvn -version: 查看Maven版本信息,并确定是否安装成。
C:\Users\Administrator>mvn -v
Apache Maven 3.5.3 (3383c37e1f9e9b3bc3df5050c29c8aff9f295297; 2018-02-25T03:49:0
5+08:00)
Maven home: D:\apache-maven-3.5.3\bin\..
Java version: 1.8.0_161, vendor: Oracle Corporation
Java home: D:\DevSoft\jdk1.8.0_161
Default locale: zh_CN, platform encoding: GBK
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"
如果你看到类似消息,说明 Apache Maven 在 Windows 上已安装成功。
第一个Maven项目
可以在项目目录下执行如下命令:会在你的项目路径下生成一个名称为hello的Maven项目。
mvn archetype:generate -DgroupId=com.cnblogs.hello -DartifactId=hello -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
hello目录结构如下图:
所有的源代码放在文件夹 /src/main/java/, 所有的单元测试代码放入 /src/test/java/。
pom.xml文件内容:该文件是在Maven项目中的核心配置文件。它是一个单独的配置文件,其中包含以您构建项目所需的大部分信息。后面我会详细介绍这个文件。
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cnblogs.hello</groupId>
<artifactId>hello</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>hello</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
接下来执行命令:mvn package 将项目进行打包处理。注:这个命令必须在hello项目木木下执行即:D:\MavenWork\hello目录
D:\MavenWork\hello>mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< com.cnblogs.hello:hello >-----------------------
[INFO] Building hello 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e
. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\MavenWork\hello\src\main\resources [INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hello ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. buil
d is platform dependent!
[INFO] Compiling source file to D:\MavenWork\hello\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ he
llo ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e
. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\MavenWork\hello\src\test\resources [INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ hello -
--
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. buil
d is platform dependent!
[INFO] Compiling source file to D:\MavenWork\hello\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.:test (default-test) @ hello ---
[INFO] Surefire report directory: D:\MavenWork\hello\target\surefire-reports
Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/o
rg/apache/maven/surefire/surefire-junit3/2.12./surefire-junit3-2.12..pom
Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/or
g/apache/maven/surefire/surefire-junit3/2.12./surefire-junit3-2.12..pom (1.7 k
B at B/s)
Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/o
rg/apache/maven/surefire/surefire-providers/2.12./surefire-providers-2.12..pom Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/or
g/apache/maven/surefire/surefire-providers/2.12./surefire-providers-2.12..pom
(2.3 kB at 1.0 kB/s)
Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/o
rg/apache/maven/surefire/surefire-junit3/2.12./surefire-junit3-2.12..jar
Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/or
g/apache/maven/surefire/surefire-junit3/2.12./surefire-junit3-2.12..jar ( kB
at kB/s) -------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.cnblogs.hello.AppTest
Tests run: , Failures: , Errors: , Skipped: , Time elapsed: 0.004 sec Results : Tests run: , Failures: , Errors: , Skipped: [INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ hello ---
[INFO] Building jar: D:\MavenWork\hello\target\hello-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10.482 s
[INFO] Finished at: --15T14::+:
[INFO] ------------------------------------------------------------------------
最后执行java命令进行测试打包生成的jar
java -cp target/hello-1.0-SNAPSHOT.jar com.cnblogs.hello.App
结果打印如下:
Hello World!
最后生成站点,执行命令:mvn site
mvn site
执行命令会会根据项目的信息生成一个网站。您可以查看target/site下生成的文档。
通过快速入门我可以简单了解Maven的一些功能,这只是一个快速入门指南。后续几章会有更全面的详细介绍。
最新文章
- Atitit 智能云网络摄像机的前世今生与历史 优点 &#160;密码默认888888
- Java常见问题
- HTML中为何P标签内不可包含DIV标签?
- settimeout如何调用方法的时候,传递参数
- oracle Redhat64 安装
- 【Hadoop代码笔记】Hadoop作业提交之JobTracker接收作业提交
- Linux基本命令之逻辑测试二
- iOS-OC-基础-NSArray常用方法
- CentOS7.2设置静态IP
- hack查询地址
- win10安装elementary os双系统
- 阿里架构师带你深入浅出jvm
- HashMap源码解读(jdk1.8)
- 喜怨交加C++
- Elasticsearch 5.0Head插件
- 使用 Docker 搭建 Java Web 运行环境(转)
- C#Mvc地址栏传值
- 并发研究之Java内存模型(Java Memory Model)
- 教你一招:windows批处理中实现延时的办法
- linux小倒腾