构建第一个Maven应用

        Maven安装成功之后我们动手来构建一个Maven应用,并通过这个应用更进一步的了解Maven的细节。
        1.打开eclipse,在菜单栏中点击File->New->Other,然后在界面中找到"Maven project",点击下一步进入下一个页面:


构建第一个Maven应用
 

        2.第二个界面可以选择是否创建一个示例项目,这里直接下一步:


构建第一个Maven应用
 

        3.选择"原型",也就是选择项目的类型,我们选择"mavent-archetype-quickstart",下一步:


构建第一个Maven应用
  

        4.进入到Maven项目创建的最后一步,也是最重要的一步,如图所示:


构建第一个Maven应用
 

        想必此时很多不了解Maven的朋友会“无从下手”,因为我们不了解上面这些配置的含义,其实并不复杂。
        GroupID:是项目组织唯一的标识符,实际对应JAVA的包的结构,是main目录里java的目录结构(下面我们会讲解Maven目录结构)。

        ArtifactID:就是项目的唯一的标识符,实际对应项目的名称,就是项目根目录的名称。

        GroupId中填写:com.hello.maven;

        ArtifactID中填写:helloMaven;

        在最后的package会自动根据前面填写的内容变化:com.hello.maven.helloMaven;

 

        5.点击"Finsh"后eclipse会自动创建项目,以下就是项目的目录结构:


构建第一个Maven应用
 

 

        在这里要重点说下Maven的目录结构,在Maven官网上推荐了一套“标准”的目录结构:
        原文地址:http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html

        原文大致意思:Maven提倡使用一个共同的标准目录结构,使开发人员能在熟悉了一个Maven工程后,对其他的Maven工程也能清晰了解。Maven推荐大家尽可能的遵守这样的目录结构,这样可以快速掌握其他项目结构。

        以下是标准的目录结构,并且也是创建项目所产生的目录结构,如下:



构建第一个Maven应用
 
 

        在工程根目录中是描述文件pom.xml即Maven构建文件,另外还包括提供给最终用户的文件,如,README.txt, LICENSE.txt等等。
        工程根目录还包括两个子目录:src,target。顶级目录下可能出现的其他目录仅仅是CVS或.svn和其他多模块工程的工程目录,最好不要再有其他目录。
        Target目录是所有工程编译构建的输出目录。
        Src目录包含所有工程的源码文件,配置文件,资源文件等等。它下面的子目录一般包含main(主要的工程源文件),test(测试文件),site(项目站点文件)。

 

        接下来我们就开始仔细分析下Maven工程与普通的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">
  <!-- 指明POM使用的对象模型的版本,此值很少改动 -->
  <modelVersion>4.0.0</modelVersion>
  <!-- groupId -->
  <groupId>com.hello.maven</groupId>
  <!-- artifactId -->
  <artifactId>helloMaven</artifactId>
  <!-- 项目产品版本 -->
  <version>0.0.1-SNAPSHOT</version>
  <!-- 打包方式 -->
  <packaging>jar</packaging>
  <!-- 项目显示名称 -->
  <name>helloMaven</name>
  <!-- 项目站点 -->
  <url>http://286.iteye.com/</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>

 

        通过前面Ant的学习,pom.xml文件的内容并不难理解,只是有些语法语义上的差别。

        接下来我们在pom.xml配置文件中加入<build>来进行构建项目,代码如下:

<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">
  <!-- 指明POM使用的对象模型的版本,此值很少改动 -->
  <modelVersion>4.0.0</modelVersion>
  <!-- groupId -->
  <groupId>com.hello.maven</groupId>
  <!-- artifactId -->
  <artifactId>helloMaven</artifactId>
  <!-- 项目产品版本 -->
  <version>0.0.1-SNAPSHOT</version>
  <!-- 打包方式 -->
  <packaging>jar</packaging>
  <!-- 项目显示名称 -->
  <name>helloMaven</name>
  <!-- 项目站点 -->
  <url>http://286.iteye.com/</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>
  <!-- 这里采用默认编译器 -->
  <build>
  	<defaultGoal>compile</defaultGoal>
  </build>
</project>

 

        进入项目根目录,即pom.xml所在目录,在控制台状态下运行Maven"clean"命令:
mvn clean

运行结果:
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building helloMaven 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ helloMaven ---
[INFO] Deleting D:\maven_project\helloMaven\target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.686s
[INFO] Finished at: Mon Jul 29 11:59:12 CST 2013
[INFO] Final Memory: 3M/122M
[INFO] ------------------------------------------------------------------------
 
        我们继续尝试Maven的"test"命令,结果如下:
mvn test

运行结果:
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building helloMaven 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ helloMaven ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\maven_project\helloMaven\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ helloMaven ---
[INFO] Compiling 1 source file to D:\maven_project\helloMaven\target\classes
[INFO] 
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ helloMaven ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\maven_project\helloMaven\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ helloMaven ---
[INFO] Compiling 1 source file to D:\maven_project\helloMaven\target\test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ helloMaven ---
[INFO] Surefire report directory: D:\maven_project\helloMaven\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.hello.maven.helloMaven.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.01 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.712s
[INFO] Finished at: Mon Jul 29 12:35:06 CST 2013
[INFO] Final Memory: 10M/122M
[INFO] ------------------------------------------------------------------------
 
        Maven的build命令:
mvn build

运行结果:
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building helloMaven 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ helloMaven ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\maven_project\helloMaven\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ helloMaven ---
[INFO] Nothing to compile - all classes are up to date
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.679s
[INFO] Finished at: Mon Jul 29 13:29:27 CST 2013
[INFO] Final Memory: 3M/122M
[INFO] ------------------------------------------------------------------------
 
        Maven的install命令:
mvn install

运行结果:
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building helloMaven 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ helloMaven ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\maven_project\helloMaven\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ helloMaven ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ helloMaven ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\maven_project\helloMaven\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ helloMaven ---
[INFO] Compiling 1 source file to D:\maven_project\helloMaven\target\test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ helloMaven ---
[INFO] Surefire report directory: D:\maven_project\helloMaven\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.hello.maven.helloMaven.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.024 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
[INFO] --- maven-jar-plugin:2.3.2:jar (default-jar) @ helloMaven ---
[INFO] Building jar: D:\maven_project\helloMaven\target\helloMaven-0.0.1-SNAPSHOT.jar
[INFO] 
[INFO] --- maven-install-plugin:2.3.1:install (default-install) @ helloMaven ---
[INFO] Installing D:\maven_project\helloMaven\target\helloMaven-0.0.1-SNAPSHOT.jar to C:\Users\maoshou\.m2\repository\com\hello\maven\helloMaven\0.0.1-SNAPSHOT\helloMaven-0.0.1-SNAPSHOT.jar
[INFO] Installing D:\maven_project\helloMaven\pom.xml to C:\Users\maoshou\.m2\repository\com\hello\maven\helloMaven\0.0.1-SNAPSHOT\helloMaven-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.289s
[INFO] Finished at: Mon Jul 29 13:32:56 CST 2013
[INFO] Final Memory: 8M/122M
[INFO] ------------------------------------------------------------------------
 
        运行完mvn install命令之后,target目录下已经多了很多文件及jar包,说明我们的maven项目已经构建完成,下一篇我们会研究maven的相关配置信息及含义。

你可能感兴趣的:(maven)