- 创建一个指定目录:例如:D:\maven,运行cmd并进入该目录,执行如下脚本本:
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
如果刚安装maven,初次运行时,可能会花费一些时间。因为maven会下载最近的构件(artifacts:构件?)到本地仓库,该构件包括(plugin jars and other files)。由于远程服务器可能会下载超时,所以可能需要执行多次以上命令,直到执行成功。 如下图: 在maven目录下生成一个与artifactId同名的目录,进行该目录cd my-app
运行tree -F 可查看该目录结构: 以上为maven 创建的 main/java 目录:存放源码 test/java目录:存放测试源码 pox.xml:POM(Project Object Model) - POM 在maven中,pox.xml文件是项目配置的核心。这个配置文件包含的所需build项目的大多数信息。POM是巨大而且非常复杂,无需了解所有的配置。以上项目的POM:
4.0.0 com.mycompany.app my-app jar 1.0-SNAPSHOT my-app http://maven.apache.org junit junit 3.8.1 test - Build项目 在my-app目录下,运行:
mvn package
将打印如下信息:Unlike the first command executed (archetype:generate) you may notice the second is simply a single word - package. Rather than a goal, this is a phase. A phase is a step in the build lifecycle, which is an ordered sequence of phases. When a phase is given, Maven will execute every phase in the sequence up to and including the one defined. For example, if we execute the compile phase, the phases that actually get executed are: 1.validate 2.generate-sources 3.process-sources 4.generate-resources 5.process-resources 6.compile
test最近编译和打包的jar:java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
输出:Hello World!
- Maven 阶段(Phases) 虽然没乎没有一个全面的列表,这些都是最常见的默认执行的生命周期阶段: validate: 验证项目是正确的并且所有的必要信息是有效的 compile:编译项目的源码 test:测试程序源代,使用合适的的单元测试仪框架,这些测试代码应该不需要打包或部署 package:编译源码并按分派格式打包,如打包成jar文件 integration-test:过程和部署包如果有必要到环境中,集成测试可以运行 verify:运行任务检查,验证包的有效性和符合质量标准 install:安装包到本地仓库,作为你本机的基他项目前的依赖项 deploy:在集成电路或发布的环境下,复制最终的包到远程仓库,共享给其他的开发者和项目。 由于英语水平不高,以免造成理解上的错误,将原文附上:
◾ validate: validate the project is correct and all necessary information is available ◾ compile: compile the source code of the project ◾ test: test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed ◾ package: take the compiled code and package it in its distributable format, such as a JAR. ◾ integration-test: process and deploy the package if necessary into an environment where integration tests can be run ◾ verify: run any checks to verify the package is valid and meets quality criteria ◾install: install the package into the local repository, for use as a dependency in other projects locally ◾deploy: done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.
还有另外两个Maven lifecycles: clean:清理之前建立的artifacts site:生成站点文档为该项目 阶段实际上是被映射到相关的目标(underlying goals),挂靠每个阶段的具体目标依赖于项目的包类型。 For example, package executes jar:jar if the project type is a JAR, and war:war if the project type is - you guessed it - a WAR.