1、maven笔记(一):构建项目
1.在E:\下新建mvn目录,由于mvn默认搜索路径为src/main/java/目录下,所以我们在mvn下新建目录/src/main/java/com/zmp/mvn/helloworld/HelloWorld.java,最终目录结构如下:
"E:\mvn\src\main\java\com\zmp\mvn\helloworld\HelloWorld.java"
HelloWorld.java代码如下:
package com.zmp.mvn.helloworld;
public class HelloWorld {
public static String sayHello() {
return "Hello Maven";
}
public static void main(String[] args) {
HelloWorld h = new HelloWorld();
System.out.println(h.sayHello());
}
}
public class HelloWorld {
public static String sayHello() {
return "Hello Maven";
}
public static void main(String[] args) {
HelloWorld h = new HelloWorld();
System.out.println(h.sayHello());
}
}
2.在E:\mvn\下新建pom.xml:
pom.xml
<!-- 文档的版本和编码方式 -->
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org./POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<!--POM模型的版本,对于Maven2.0和3.0来说,它只能是4.0.0 -->
<modelVersion>4.0.0</modelVersion>
<!-- groupId,artifactId和version这三个元素定义了项目的坐标,在maven的世界里,
任何pom,jar或者war都是基于这些坐标区分的 -->
<!-- 定义了项目属于哪个组,跟公司或者个人存在关联 -->
<groupId>com.zmp.mvn</groupId>
<!-- 定义了当前maven项目在组中的唯一id -->
<artifactId>hello-world</artifactId>
<!-- 当前项目版本,SNAPSHOT为快照的意思,说明项目还在开发当中,是不稳定的版本 -->
<version>1.0-SNAPSHOT</version>
<!-- 项目名称,比较友好,不是必须的,但方便信息交流 -->
<name>MAVEN HELLO WORLD PROJECT</name>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org./POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<!--POM模型的版本,对于Maven2.0和3.0来说,它只能是4.0.0 -->
<modelVersion>4.0.0</modelVersion>
<!-- groupId,artifactId和version这三个元素定义了项目的坐标,在maven的世界里,
任何pom,jar或者war都是基于这些坐标区分的 -->
<!-- 定义了项目属于哪个组,跟公司或者个人存在关联 -->
<groupId>com.zmp.mvn</groupId>
<!-- 定义了当前maven项目在组中的唯一id -->
<artifactId>hello-world</artifactId>
<!-- 当前项目版本,SNAPSHOT为快照的意思,说明项目还在开发当中,是不稳定的版本 -->
<version>1.0-SNAPSHOT</version>
<!-- 项目名称,比较友好,不是必须的,但方便信息交流 -->
<name>MAVEN HELLO WORLD PROJECT</name>
</project>
3.进入E:\mvn\目录下,如图:
输出的内容如下:
[INFO] Scanning for projects...
[INFO]
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building MAVEN HELLO WORLD PROJECT 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello-world ---
[INFO] Deleting E:\mvn\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-world ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory E:\mvn\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ hello-world ---
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to E:\mvn\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.024 s
[INFO] Finished at: 2014-04-16T10:35:47+08:00
[INFO] Final Memory: 7M/103M
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building MAVEN HELLO WORLD PROJECT 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello-world ---
[INFO] Deleting E:\mvn\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-world ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory E:\mvn\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ hello-world ---
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to E:\mvn\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.024 s
[INFO] Finished at: 2014-04-16T10:35:47+08:00
[INFO] Final Memory: 7M/103M
[INFO] ------------------------------------------------------------------------
4.在mvn目录下多了个target文件夹:
即构建项目成功,所编译的类在\target\classes\目录下
5.用到的命令
clean(指清理target文件夹,默认删除该文件夹及文件夹下所有文件,如果target文件夹存在的话)
compile(指编译项目主代码)