1、安装
看我之前写的 《Maven安装及配置 (windows)》
2、创建工程2.1 创建
命令:
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
解释:
2.2 工程目录分析
window:tree命令
linux:
my-app
|-- pom.xml 项目的POM( project's Project Object Model)
`-- src 项目源代码
|-- main
| `-- java
| `-- com
| `-- mycompany
| `-- app
| `-- App.java
`-- test 测试源代码
`-- java
`-- com
`-- mycompany
`-- app
2.3 打包
mvn package
2.4 运行程序:
java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
// 说明:
java -cp <路径> 全限定类名
java -classpath <路径> 全限定类名
路径 <=> class文件的查找目录、zip/jar文件
如果有多个的话在window中用“;” 分隔,linux中用“:”
3、运行MAVEN工具
3.1 maven命令
很难全部列出,下面列出最常见的默认的生命周期命令。
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 deployedpackage:
把编译的代码按照指定格式找包,如:jar包。
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 runverify:
运行所有检查来验证包是否有效且达到质量标准。
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:
在集成或者发布环境下执行,复制最终版本包到远程的repository,与其他的开发者或者工程可以共享。
done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.
3.2 有两个在Maven生命周期的中需要注意的命令不在上面列表中:
There are two other Maven lifecycles of note beyond the default list above.
clean:
清理之前生成的东西。
cleans up artifacts created by prior buildssite:
为项目生成此站点文档。
generates site documentation for this project3.3 注意:
有一个有趣的事情,命令和目标可以被顺序执行。
An interesting thing to note is that phases and goals may be executed in sequence.
如:
mvn clean dependency:copy-dependencies package
作用:此命令会清理、复制依赖、打包这个工程。生成工程的站点文档。
mvn site
作用:这个命令基于工程的POM的信息生成一个网站文档。你可在target/site下找到生成的文档。结论Conclusion:
要想深入了解看: http://maven.apache.org/guides/getting-started/index.html
此文章可能因个人能力有限会有问题会不段完善,同时欢迎指正,谢谢!