---
title: "maven 快速入门（转载）"
author: "tof.j"
date: "2008-03-26T15:47:22Z"
tags: []
slug: "maven"
draft: false
lastmod: "2008-03-26T15:47:22Z"
id: 176366
url: "https://tof-j.iteye.com/blog/176366"
---

创建快速启动项目  
mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=my-app  
编译  
mvn compile  
测试  
mvn test  
如果只是编译测试源文件，而不启动测试：  
mvn test-compile    
打包  
mvn package  
安装到本地Repository  
mvn install  
Maven会自动查找测试文件，寻找的模式为：  
默认包括的测试文件有：  
\*\*/\*Test.java  
\*\*/Test\*.java  
\*\*/\*TestCase.java  
默认排除的测试文件有：  
\*\*/Abstract\*Test.java  
\*\*/Abstract\*TestCase.java  
创建项目网站  
mvn site  
清理  
mvn clean  
为项目生成IntelliJ IDEA描述符，可以在一个已经存在的IDEA项目上进行，会更新设置而不是从零开始。  
mvn idea:idea  
如何使用插件  
示例如下：  
&lt;build&gt;  
&lt;plugins&gt;  
&lt;plugin&gt;  
&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;  
&lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;  
&lt;configuration&gt;  
&lt;source&gt;1.5&lt;/source&gt;  
&lt;target&gt;1.5&lt;/target&gt;  
&lt;/configuration&gt;  
&lt;/plugin&gt;  
&lt;/plugins&gt;  
&lt;/build&gt;  
如何在Jar中包含资源  
把资源放置在${basedir}/src/main/resources目录中即可。测试用例所需资源的路径是${basedir}/src/test/resources。  
如何过滤资源文件  
有时候资源文件需要构建时刻才能提供的值，为了达到这个目的，在资源文件中添加${&lt;property name&gt;}这样的引用。这些属性可以来自pom.xml，settings.xml，其它的属性文件或是系统属性。  
按照如下方式修改pom.xml：  
&lt;build&gt;  
&lt;resources&gt;  
&lt;resource&gt;  
&lt;directory&gt;src/main/resources&lt;/directory&gt;  
&lt;filtering&gt;true&lt;/filtering&gt;  
&lt;/resource&gt;  
&lt;/resources&gt;  
&lt;/build&gt;  
按如下使用pom.xml中的值：  
\# application.properties  
application.name=${pom.name}  
application.version=${pom.version}  
如果是使用其它的属性文件：  
&lt;build&gt;  
&lt;filters&gt;  
&lt;filter&gt;src/main/filters/filter.properties&lt;/filter&gt;  
&lt;/filters&gt;  
&lt;resources&gt;  
&lt;resource&gt;  
&lt;directory&gt;src/main/resources&lt;/directory&gt;  
&lt;filtering&gt;true&lt;/filtering&gt;  
&lt;/resource&gt;  
&lt;/resources&gt;  
&lt;/build&gt;  
也可以把这些属性写在pom.xml中：  
&lt;properties&gt;  
&lt;my.filter.value&gt;hello&lt;/my.filter.value&gt;  
&lt;/properties&gt;  
同样可以是Java的系统属性，或是通过-D传入命令行参数。

如何使用外部依赖  
在pom.xml中的dependencies一节中列出了所需的全部外部依赖。为了定义外部依赖，需要定义至少4个内容：groupId, artifactId, version, 和 scope。Scope可以是test，compile或runtime。Maven会自动从一个远程的Repository下载所需的依赖。  
如何部署jar到自己的远程repository  
在pom.xml中添加如下内容：  
&lt;distributionManagement&gt;  
&lt;repository&gt;  
&lt;id&gt;mycompany-repository&lt;/id&gt;  
&lt;name&gt;MyCompany Repository&lt;/name&gt;  
&lt;url&gt;scp://repository.mycompany.com/repository/maven2&lt;/url&gt;  
&lt;/repository&gt;  
&lt;/distributionManagement&gt;  
同样在用户的settings.xml中也要定义服务器：  
&lt;settings&gt;  
&lt;servers&gt;  
&lt;server&gt;  
&lt;id&gt;mycompany-repository&lt;/id&gt;  
&lt;username&gt;jvanzyl&lt;/username&gt;  
&lt;!-- Default value is ~/.ssh/id\_dsa --&gt;  
&lt;privateKey&gt;/path/to/identity&lt;/privateKey&gt; (default is ~/.ssh/id\_dsa)  
&lt;passphrase&gt;my\_key\_passphrase&lt;/passphrase&gt;  
&lt;/server&gt;  
&lt;/servers&gt;  
&lt;/settings&gt;