首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

Maven应用入门

2012-06-28 
Maven使用入门【Apache Maven is a software project management and comprehension tool. Based on the co

Maven使用入门

Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information.

?

一、安装

?

要下载Maven可以去:http://maven.apache.org/download.html

?

下载完以后解压到某个目录,例如:D:/Maven3

?

然后最后是添加一下环境变量,例如Windows,可以添加Maven_HOME,指定到安装目录,然后在path变量中添加 %Maven_HOME%\bin

?

这样mvn命令行就可以使用了。

?

二、配置

?

默认的配置文件在 %Maven_HOME%\conf 目录下,默认的本地存储路径为:~/.m2/repository 目录下。默认情况下配置文件是一个全局可用的,如果你想针对个人用户单独设置一个配置,可以将 %Maven_HOME%\conf?下的settings.xml文件拷到 ~/.m2 目录下,然后修改对应的配置,例如我想把默认的本地存储路径修改为 D:/M3Repository,就需要在settings.xml文件中添加一条:

<localRepository>D:\M3Repository</localRepository>

?

更多资料,请参考http://maven.apache.org/。

?

三、POM 文件

?

和Ant的build.xml类似,Maven也有一个操作对象文件,也就是上面英文中提到的工程对象模型(project object model)(pom)文件,也即pom.xml;所以运行mvn时需要的信息都在pom.xml中定义好,然后直接运行对应的mvn命令即可。

?

四、Maven库

?

Maven最大的方便性之一,我觉得就是Maven库了,Maven库可以分为远程库和本地库,上面实现的localRepository就是一个本地库。每次当添加一个新的依赖包到pom.xml中,maven会先在本地库查找,如果找到,则直接引用本地库中的包,否则则从远程库上下载的lib包都是放到这个本地库里,再添加到引用中。可以在pom里定义多个远程库,后面会介绍的Archiva就是一个用来创建和管理库的开源项目。

?

下载和配置引用包的整个过程由maven自动完成,不需要个人的干预。要查找某个包,可以登陆:http://mvnrepository.com/。然后查找。

?

?

五、纬度

?

以上已经知道,maven有一个库的概念。于是问题就来了:Maven怎么从库中定位特定的引用包?Maven通过一种特殊的结构来定义每个包,以便能方便的查找到特定的包:

?

/<groupId>/<artifactId>/<version>/<artifactId>-<version>.<packaging>

?

具体的这些字段的意义可以访问:http://maven.apache.org/pom.html。

简单来说groupId类似于包名,artifactId类似于类名,version定位到类的某个特定版本,比如说用svn管理的某个版本。packaging表示打包类型。通过这种分层的结构,就可以很方便的定位的特定的库包。所以groupId, artifactId, version和packaging是使用maven中的关键。

?

六、使用

?

有了以上的知道,就可以开始使用maven。首先需要提供一个pom文件,可以通过以下命令,让maven自动生成一个:

mvn archetype:generate

?

然后根据提示一步一步的添加必要的信息,也可以一次性的提供所需的信息:

?

mvn archetype:create -DgroupId=com.liugang -DartifactId=demo ...

?

然后在本地就会生成一个名称为demo的文件夹,结构为:

?

Maven应用入门

demo下还会生成一个对应pom.xml文件。

?

Maven工程会有一个默认的路径结构:

src/main/java:主代码文件夹src/test/java:测试代码文件夹src/main/resources:资源文件夹src/test/resources:测试资源文件夹

更多的,可以参考:http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html

?

当然这些默认配置也可以很方便在pom.xml文件中修改。

?

OK,这样一个Maven工程就生成了,可以开始写代码了。

?

七、生命阶段

?

不同于Ant的task依赖关键,Maven是通过运行一些生命周期过程来完成特定任务,每个生命过程阶段可以称之为阶段(phase),例如常见的阶段有:initialize, compile, test, install, deploy等等。

?

更详细的介绍,请看:http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

?

执行每个阶段的时候,都会将该阶段以上的所有阶段运行一遍,例如如果运行initialize阶段,则会先运行validate阶段。

?

八、运行

?

可以有三种从maven运行java程序的方式:

    从命令行运行:mvn?exec:java?-Dexec.mainClass="com.vineetmanohar.module.Main"?-Dexec.args="arg0?arg1?arg2" 从阶段(phase)运行:
    view plaincopy to clipboardprint?<build>   <plugins>    <plugin>     <groupId>org.codehaus.mojo</groupId>     <artifactId>exec-maven-plugin</artifactId>     <version>1.1.1</version>     <executions>      <execution>       <phase>test</phase>       <goals>        <goal>java</goal>       </goals>       <configuration>        <mainClass>com.vineetmanohar.module.CodeGenerator</mainClass>        <arguments>         <argument>arg0</argument>         <argument>arg1</argument>        </arguments>       </configuration>      </execution>     </executions>    </plugin>   </plugins>  </build>  
    ?从profile运行:
    view plaincopy to clipboardprint?<profiles>   <profile>    <id>code-generator</id>    <build>     <plugins>      <plugin>       <groupId>org.codehaus.mojo</groupId>       <artifactId>exec-maven-plugin</artifactId>       <version>1.1.1</version>       <executions>        <execution>         <phase>test</phase>         <goals>          <goal>java</goal>         </goals>         <configuration>          <mainClass>com.vineetmanohar.module.CodeGenerator</mainClass>          <arguments>           <argument>arg0</argument>           <argument>arg1</argument>          </arguments>         </configuration>        </execution>       </executions>      </plugin>     </plugins>    </build>   </profile>  </profiles>  ?

更多介绍,可以参考:http://www.vineetmanohar.com/2009/11/3-ways-to-run-java-main-from-maven/

?

关于profile的作用,可以参考:http://maven.apache.org/guides/introduction/introduction-to-profiles.html

?

?

?

热点排行