项目创建过程:创建、编译、测试、打包、安装
概念:生命周期(build lifecycle)、Maven 仓库(repositories)、依赖管理(dependency management)、项目对象模型(Project Object Model)
1、创建一个简单的项目
mvn archetype:create -DgroupId=org.sonatype.mavenbook.ch03 -DartifactId=simple -DpackageName=org.sonatype.mavenbook
(1)Maven Archtype插件创建了一个与artifactId匹配的目录――simple。
(2)每个项目在文件pom.xml里有它的项目对象模型 (POM)。这个文件描述了这个项目,配置了插件,声明了依赖。
(3)在一个Java项目中,Java类放在src/main/java下面,而classpath资源文件放在src/main/
resources下面。
(4)我们项目的测试用例放在src/test下。在这个目录下面,src/test/java存放像使用JUnit或者TestNG这样的Java测试类。目录src/test/resources下存放测试classpath资源文件。
2、构建一个简单的项目
使用Maven Archetype插件创建了一个项目,你会希望构建并打包这个应用。想要构建打包这个应用,在包含pom.xml的目录
下运行mvn install或mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building helloworld 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ helloworld ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /root/.m2/repository/org/apache/maven/helloworld/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ helloworld ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ helloworld ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /root/.m2/repository/org/apache/maven/helloworld/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ helloworld ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ helloworld ---
[INFO] Surefire report directory: /root/.m2/repository/org/apache/maven/helloworld/target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.mycompany.helloworld.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.023 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ helloworld ---
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ helloworld ---
[INFO] Installing /root/.m2/repository/org/apache/maven/helloworld/target/helloworld-1.0-SNAPSHOT.jar to /root/.m2/repository/com/mycompany/helloworld/helloworld/1.0-SNAPSHOT/helloworld-1.0-SNAPSHOT.jar
[INFO] Installing /root/.m2/repository/org/apache/maven/helloworld/pom.xml to /root/.m2/repository/com/mycompany/helloworld/helloworld/1.0-SNAPSHOT/helloworld-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.931 s
[INFO] Finished at: 2015-06-13T18:10:17+08:00
[INFO] Final Memory: 9M/21M
[INFO] ------------------------------------------------------------------------
3、简单的项目对象模型 (Project Object Model)
坐标(coordinates):groupId,artifactId, packaging, version
Maven插件和目标 (Plugins and Goals):archetype:create;一个Maven插件是一个单个或者多个目标的集合。
Maven生命周期 (Lifecycle):Available lifecycle phases are: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-clean, clean, post-clean, pre-site, site, post-site, site-deploy.
mvn install/package 生命周期:
mvn resources:resources \ 对应目录src/main/resources
compiler:compile \ 对应目录src/main/java
resources:testResources \ 对应目录src/test/resources
compiler:testCompile \ 对应目录src/test/java
surefire:test \ 运行所有的测试并且创建那些捕捉详细测试结果的输出文件
jar:jar Jar插件的jar目标绑定到了package 阶段。这个目标把输出目录打包成JAR文件
Maven仓库(Repositories): http://repo1.maven.org/maven2
/<groupId>/<artifactId>/<version>/<artifactId>-<version>.<packaging>
Maven依赖管理 (Dependency Management):在Maven中一个依赖不仅仅是一个JAR。它是一个POM文件,这个POM可能也声明了对其它构件的依赖,叫做传递性依赖
站点生成和报告 (Site Generation and Reporting):在项目目录下运行mvn site;
Site生命周期只关心处理在src/site目录下的site内容,还有生成报告。在这个命令运行过之后,你将会在target/site目录下看到一个项目web站点,即target/site/index.html
3、定制项目
pom.xml添加定制信息,如组织,法律和开发人员信息
......
<licenses>
<license>
<name>Apache 2</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
<comments>A business-friendly OSS license</comments>
</license>
</licenses>
<organization>
<name>Sonatype</name>
<url>http://www.sonatype.com</url>
</organization>
<developers>
<developer>
<id>jason</id>
<name>Jason Van Zyl</name>
<email>[email protected]</email>
<url>http://www.sonatype.com</url>
<organization>Sonatype</organization>
<organizationUrl>http://www.sonatype.com</organizationUrl>
<roles>
<role>developer</role>
</roles>
<timezone>-6</timezone>
</developer>
</developers>
......
pom.xml添加新的依赖
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>