首先, 下载并安装好Maven,然后按下面的指导一步步执行. 在命令行环境下面输入下面的指令:
Windows command代码
- mvn –version
mvn –version
你应该能看到类似下面这样的一些关于Maven的版本信息:
Console output代码
- Apache Maven 3.0.3 (r1075438; 2011–02–28 18:31:09+0100)
- Maven home: D:\apache-maven-3.0.3\bin\..
- Java version: 1.6.0_25, vendor: Sun Microsystems Inc.
- Java home: E:\Program Files\Java\jdk1.6.0_25\jre
- Default locale: nl_NL, platform encoding: Cp1252
- OS name: “windows 7”, version: “6.1”, arch: “amd64”, family: “windows”
Apache Maven 3.0.3 (r1075438; 2011-02-28 18:31:09+0100)
Maven home: D:\apache-maven-3.0.3\bin\..
Java version: 1.6.0_25, vendor: Sun Microsystems Inc.
Java home: E:\Program Files\Java\jdk1.6.0_25\jre
Default locale: nl_NL, platform encoding: Cp1252
OS name: “windows 7”, version: “6.1”, arch: “amd64”, family: “windows”
鉴于你的网络配置,你可能需要一些其它的配置,你可以到Apache的官方网站上看更详细的配置。http://maven.apache.org/guides/mini/guide-configuring-maven.html不过入门阶段,默认的配置也就够了。
Windows command代码
- mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
如果你刚刚安装Maven,这个命令执行的时间会比较长,因为它会到网上做一些更新,如果你网速不好,那么时间就更久了。所以你就多念几声“阿弥陀佛”,保佑它顺利执行完。如果你人品好的话,那么很快,你就可以在当前文件夹下面看到这么一堆东西了。你可以执行Tree 命令看看生成的目录结构。
Windows command代码
- Tree . (仔细看,tree 后面有个小点哦,表示当前目录。)
Tree . (仔细看,tree 后面有个小点哦,表示当前目录。)
Console output代码
- my-app
- |– pom.xml (这个其实是最终于的产物。)
- `– src
- |– main
- | `– java
- | `– com
- | `– mycompany
- | `– app
- | `– App.java (这个是生存的SourceCode)
- `– test
- `– java
- `– com
- `– mycompany
- `– app
- `– AppTest.java(这个是测试的Code,是不是感觉超牛B,超变态)
my-app
|– pom.xml (这个其实是最终于的产物。)
`– src
|– main
| `– java
| `– com
| `– mycompany
| `– app
| `– App.java (这个是生存的SourceCode)
`– test
`– java
`– com
`– mycompany
`– app
`– AppTest.java(这个是测试的Code,是不是感觉超牛B,超变态)
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”>
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.mycompany.app</groupId>
- <artifactId>my-app</artifactId>
- <packaging>jar</packaging>
- <version>1.0-SNAPSHOT</version>
- <name>Maven Quick Start Archetype</name>
- <url>http://maven.apache.org</url>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.8.2</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
- </project>
<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”>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>Maven Quick Start Archetype</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Windows command代码
- cd my-app
- mvn package
cd my-app
mvn package
执行这个指令是帮你把项目打包,当然所以打包依赖的任务也都会自动被执行到。这根Ant大同小异。 你可以在控制台看到这样的信息。
Console output代码
- …
- [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] ————————————————————————
…
[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] ————————————————————————
和前面你执行的那个长长的命令行不同(archetype:generate),你可能已经注意到这个命令行很短,后面跟了个参数 package,这个参数和前面不一样,它不是什么插件,也不是什么目标任务。Maven给它起了个新名字(phase),我们暂时翻译成状态。可以理解为项目的一中状态。当你输入这个指令后,Maven帮你做了下面这些事情:
- 1.validate
- 2.generate-sources
- 3.process-sources
- 4.generate-resources
- 5.process-resources
- 6.compile
执行 Tree . 命令,你可以看到它生存了新的目录。
Console output代码
- ├─src
- │ ├─main
- │ │ └─Java
- │ │ └─com
- │ │ └─mycompany
- │ │ └─app
- │ └─test
- │ └─java
- │ └─com
- │ └─mycompany
- │ └─app
- └─target
- ├─classes
- │ └─com
- │ └─mycompany
- │ └─app
- ├─maven-archiver
- ├─surefire-reports
- └─test-classes
- └─com
- └─mycompany
- └─app
├─src
│ ├─main
│ │ └─java
│ │ └─com
│ │ └─mycompany
│ │ └─app
│ └─test
│ └─java
│ └─com
│ └─mycompany
│ └─app
└─target
├─classes
│ └─com
│ └─mycompany
│ └─app
├─maven-archiver
├─surefire-reports
└─test-classes
└─com
└─mycompany
└─app
其中还打了个jar包 my-app-1.0-SNAPSHOT.jar
- 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.
- clean: cleans up artifacts created by prior builds
- site: generates site documentation for this project
对于上面的这些指令,我们建议你每个都执行一下,看看什么效果。对你了解Maven的功能很有帮助。
Windows command代码
- mvn site
mvn site
这个比较有意思,会帮你的Project生存一个站点。
This phase generates a site based upon information on the project’s pom. You can look at the documentation generated under target/site.
如果你有兴趣的话,可以到官网了解更多的信息。
http://maven.apache.org/guides/getting-started/index.html