Maven 5分钟学会

Installation安装
Maven is a Java tool, so you must have Java installed in order to proceed.
Maven是Java工具,所以你必须安装Java才能运行。 就是配置Java环境变量

去官方网站下载之: http://maven.apache.org/download.html

我的电脑----属性----高级----环境变量,点击“系统变量”下的新建,输入:变量名 MAVEN_HOME; 变量值d:\Maven204,在系统变量列表中找到系统变量path,打开之,在变量值中追加”;%MAVEN_HOME%\bin”,至此环境变量设置完毕。
        检查一下是否已经完成安装,打开dos窗口,输入mvn –v,如果出现以下信息则表示maven2已经安装成功:
X:〉mvn –v

Creating a Project创建项目
1、选择一个根目录,我的java工作目录是D:\eclipse\workspace
2、打开dos窗口采用cd命令进入D:\eclipse\workspace目录下
3、输入下列指令
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false


注意:刚开始运行总不成功,很郁闷,后来看官方给的答复才明白怎么回事。If you have just installed Maven, it may take a while on the first run. This is because Maven is downloading the most recent artifacts (plugin jars and other files) into your local repository. You may also need to execute the command a couple of times before it succeeds. This is because the remote server may time out before your downloads are complete. Don't worry, there are ways to fix that.
如果刚刚安装Maven,第一次运行可能要花点时间,因为Maven要下载最新的jar或者其他的文件什么的到你本地目录,成功之前你可能需要执行几次上面的命令,因为远程服务器可能超时,呵呵 ,万事开头难,所以要耐心。

但是在公司里面,我试了好多次,但是就是不成功,很是郁闷,后来回家之后一次就成功了,所以感觉可能是公司里面网络有限制,所以下载不完就失败了,其他同学也小心了,防不胜防啊!!

cd my-app

my-app
|-- pom.xml
`-- src
    |-- main
    |   `-- java
    |       `-- com
    |           `-- mycompany
    |               `-- app
    |                   `-- App.java
    `-- test
        `-- java
            `-- com
                `-- mycompany
                    `-- app
                        `-- AppTest.java

The POM(Project Object Model)项目对象模型
The pom.xml file is the core of a project's configuration in Maven. It is a single configuration file that contains the majority of information required to build a project in just the way you want. The POM is huge and can be daunting in its complexity, but it is not necessary to understand all of the intricacies just yet to use it effectively.
POM文件在Maven中是项目配置的核心,它是一个单独的配置文件用你想要的方式包含需要构建项目的主要信息,POM文件的复杂性让我们望而生畏,但是我们没必要理解所有东西,我们只是很方便的用它就好了

Build the Project

mvn package
执行这个命令也会下载很多不知道是干嘛的东西,以下面结束
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Thu Jul 07 21:34:52 CEST 2011
[INFO] Final Memory: 3M/6M
[INFO] ------------------------------------------------------------------------

我们来测试下我们刚才生成的jar包
java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
打印出
Hello World!

恭喜你,成功了,这世界,我来了,Maven World,I Come,哈哈

Maven Phases(Maven 步骤)

validate: mvn validate 验证工程是否正确,所有需要的资源是否可用。
compile: 编译 mvn compile
test: 测试 mvn test 编译并测试
package: mvn package 生成target目录,编译、测试代码,生成测试报告,生成jar/war文件
integration-test: mvn integration-test 在集成测试可以运行的环境中处理和发布包。
verify: mvn verify 运行任何检查,验证包是否有效且达到质量标准。
deploy:

There are two other Maven lifecycles of note beyond the default list above. They are

clean: cleans up artifacts created by prior builds
site: generates site documentation for this project


mvn clean dependency:copy-dependencies package

This command will clean the project, copy dependencies, and package the project (executing all phases up to package, of course).
清除项目命令,复制依赖然后打包项目

Generating the Site 生成站点

mvn site

This phase generates a site based upon information on the project's pom. You can look at the documentation generated under target/site.
这个阶段会更具项目的POM生成一个站点,你可以看一下在target/site下面生成的文档。



猜你喜欢

转载自jiaozhiguang-126-com.iteye.com/blog/1669108