Maven的安装、配置及使用入门

1、命令:
(1)mvn clean compile    (编译到target/目录,compile插件支持Java5的编译,surefile负责执行测试的插件,jar插件执行打包)
(2)maven执行测试:mvn clean test\
(3)打包与运行:maven clean package
(4)其他的maven项目用已经打好的JAR包(maven clean install)

总结:
Maven最主要的命令?
maven clean compile
maven clean test
maven clean package
maven clean install

在运行时指定本地仓库位置:mvn clean install -Dmaven.repo.local=/home/juven/myrepo/



2、GreenMail测试邮件服务器的端口:25



3、maven坐标
Maven 坐标有groupId,artifactId,packaging,version,classifier
  1. <groupId>com.x</groupId>  
  2. <artifactId>helloworld-1</artifactId>  
  3. <packaging>jar</packaging>  
  4. <version>0.0.1-SNAPSHOT</version>  

     (1).groupId,定义当前maven项目隶属的实际项目。首先,maven项目和实际项目不一定是一对一的关系。比如SpringFramework这一实际项目,其对应的maven项目会有很  多,如: spring-core,spring-context等。这是由于maven中模块的概念,因此,一个实际项目往往会被划分成很多模块。其次,groupId不应该对应项目隶属的组织或公司。原因很简单,一个组织下会有很多个实际项目,如果groupId只定义到组织级别,而后面我们会看到,artifactId只能定义maven项目(模块),那么实际项目这个层将难以定义。最后,groupId的表示方式与java包名的表示方式类似,通常与域名反向一一对应。    

     (2).artifactId,该元素定义实际项目中的一个maven项目(模块),推荐的做法是使用实际项目名称作为artifactId前缀,这样做的好处是方便寻找实际构件。在默认情况下,maven生成的构件,其文件名会以artifactId作为开头,如:helloworld-1-0.0.1-SNAPSHOT.jar,使用实际项目名称作为前缀之后,就能方便从一个lib文件夹中找到某个项目的一组构件。。

     (3).version,该元素定义maven项目当前所处的版本,如:helloworld-1-0.0.1-SNAPSHOT.jar的版本是0.0.1。需要注意的是,maven定义了一套完整的版本规范,以及快照(SNAPSHOT)的概念。

     (4).packaging, 定义了maven项目的打包方式。默认值为jar,可取值:ejb / ejb3 /jar / par / rar / war

     (5).classifier,该元素用来帮助定义构建输出的一些附属构件。附属构件与主构件对应,如上例中的主构件是: nexus-indexer-2.0.0.jar,该项目可能还会通过使用一些插件生成如:helloworld-1-0.0.1-SNAPSHOT-javadoc.jar、helloworld-1-0.0.1-SNAPSHOT-sources.jar这样一些附属构件,其包含了java文档和源代码。这时候,javadoc和sources就是这两个附属构件的classifier。这样,附属构件也就拥有了自己唯一的坐标。

 项目构件的文件名是与坐标相对应, 规则为: artifactId-version[-classifier].packaging,[-classifier]为可选。




4、Maven exclusions(排除依赖)
<dependency>  
 <groupId>org.springframework</groupId>  

    <artifactId>spring-webmvc</artifactId>  

    <version>2.5.6</version>  

    <exclusions>  

        <exclusion>  

            <groupId>org.springframework</groupId>  

            <artifactId>spring-beans</artifactId>  

        </exclusion>  

        <exclusion>  

            <groupId>org.springframework</groupId>  

            <artifactId>spring-context</artifactId>  

        </exclusion>  

        <exclusion>  

            <groupId>org.springframework</groupId>  

            <artifactId>spring-core</artifactId>  

        </exclusion>  

        <exclusion>  

            <groupId>org.springframework</groupId>  

            <artifactId>spring-web</artifactId>  

        </exclusion>  

    </exclusions>  

</dependency>  

    maven排除依赖jar处理,但是这样在某些时候会造成一些不可预测的异常。比如上面的程序,根据maven介绍,上面的结构会排除对spring-beans,spring-context,spring-core,spring-web   2.5.6版本的(类似的JAR包)依赖,但是如果出现类似下面的配置:

<dependency>  

    <groupId>org.apache.activemq</groupId>  

    <artifactId>activemq-pool</artifactId>  

    <version>5.2.0</version>  

</dependency> 

    activemq-pool会依赖spring-beans,spring-core等jar包,由于上面的配置已经不允许出现2.5.6版本的类似 jar包,所以当前配置的依赖也不会去引用那些jar包,但是它会试着去引用更低版本或者更高版本的jar,从而导致jar包冲突。


 5、 maven仓库配置

1.两类仓库

1.1本地仓库(local repository)

可以自定义本地仓库的位置,修改${user.home}/.m2/settings.xml :

Xml代码 
  1. <settings>  
  2.   ...  
  3.   <localRepository>D:\java\repository</localRepository>  
  4.   ...  
  5. </settings>  

你还可以在运行时指定本地仓库位置:

mvn clean install -Dmaven.repo.local=/home/juven/myrepo/



1.2远程仓库(Remote repositories)

可以使用访问协议 such as file:// and http://运行maven所需jar包都是从本地仓库,引用在本地仓库没有时将触发从远处次仓库下载,并保存到本地。


2.Maven仓库概览

 

2.1 没有Maven仓库下的开发环境


 

2.2 具有团队内部远程仓库的开发环境

 

下面将就图2.2构建一个团队共享的maven repository

3.Maven仓库工具---Artifactory

3.1下载最新Artifactory,下面就我下载的Artifactory2.6.1配置作简单介绍。

1.双击artifactory.bat即可启动artifactory服务。访问地址http://localhost:8081/artifactory/可以看到服务的管理界面。

 2.也可以在Tomcat部署Artifactory. 

    Startup the Servlet Container VM with -Dartifactory.home=$ARTIFACTORY_HOME, pointing to the location of your Artifactory home folder (If you do not specify this property it will default to${user.home}/.artifactory).

Alternatively, you can also set an ARTIFACTORY_HOME environment variable to point to your Artifactory home folder.
      Make Sure the folder is writable  by the user running the Servlet Container.
Artifactory will try to create the folder on startup if it does not exist.

Make sure the Artifactory configuration file $ARTIFACTORY_HOME/etc/artifactory.config.xml and the log4j configuration file $ARTIFACTORY_HOME/etc/log4j.properties exist in their respective locations.

Deploy the artifactory.war file into the container.



3.2.maven的远程仓库地址设置

默认的远程仓库

我安装了maven-2.0.10,我可以找到这个文件:${M2_HOME}/lib/maven-2.0.10-uber.jar ,打开该文件,能找到超级POM:\org\apache\maven\project\pom-4.0.0.xml ,它是所有Maven POM的父POM,所有Maven项目继承该配置,你可以在这个POM中发现如下配置:

Xml代码   收藏代码
  1. <repositories>  
  2.   <repository>  
  3.     <id>central</id>  
  4.     <name>Maven Repository Switchboard</name>  
  5.     <layout>default</layout>  
  6.     <url>http://repo1.maven.org/maven2</url>  
  7.     <snapshots>  
  8.       <enabled>false</enabled>  
  9.     </snapshots>  
  10.   </repository>  
  11. </repositories>  

上为超级POM配置了ID为central的远程仓库,如果pom.xml中未配置仓库,默认的将使用这个central的超级仓库。

3.2.1在POM中配置远程仓库

使用artifactory在POM中配置其它的远程仓库。


[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <project>  
  2.   .....  
  3. <repositories>  
  4.     <repository>  
  5.       <id>my-repo</id>  
  6.       <name>my repository</name>  
  7.       <url>http://localhost:8080/artifactory/my-repo/</url>  
  8.       <releases>  
  9.         <enabled>true</enabled>  
  10.       </releases>  
  11.       <snapshots>  
  12.         <enabled>true</enabled>  
  13.       </snapshots>  
  14.     </repository>  
  15.   </repositories>  
  16.   
  17.   <pluginRepositories>  
  18.     <pluginRepository>  
  19.       <id>my-repo</id>  
  20.       <name>my repository</name>  
  21.       <url>http://localhost:8080/artifactory/my-repo/</url>  
  22.       <releases>  
  23.         <enabled>true</enabled>  
  24.       </releases>  
  25.       <snapshots>  
  26.         <enabled>true</enabled>  
  27.       </snapshots>  
  28.     </pluginRepository>  
  29.     <pluginRepository>  
  30.       <name>oss.sonatype.org</name>  
  31.       <id>oss.sonatype.org</id>  
  32.       <url>http://oss.sonatype.org/content/groups/public</url>  
  33.     </pluginRepository>  
  34.   </pluginRepositories>  
  35.   ...  
  36. </project>  



3.2.2在settings.xml中配置远程仓库

除了在pox.xml配置仓库,setting文件可以作为一个全局的配置,但并不是简单的将POM中的<repositories>及<pluginRepositories>元素复制到settings.xml中就可以,setting.xml不直接支持 这两个元素。但我们还是有一个并不复杂的解决方案,就是利用profile,如下:

Xml代码   收藏代码
  1. <settings>  
  2.   ...  
  3.   <profiles>  
  4.     <profile>  
  5.       <id>dev</id>  
  6.       <!-- repositories and pluginRepositories here-->  
  7.     </profile>  
  8.   </profiles>  
  9.   <activeProfiles>  
  10.     <activeProfile>dev</activeProfile>  
  11.   </activeProfiles>  
  12.   ...  
  13. </settings>  

这里我们定义一个id为dev的profile,将所有repositories以及pluginRepositories元素放到这个profile中,然后,使用<activeProfiles>元素自动激活该profile。这样,你就不用再为每个POM重复配置仓库。



3.3上传构件至远程仓库

我们需要配置POM的distributionManagement来指定Maven分发构件的位置,如下:

Xml代码   收藏代码
  1. <project>    
  2.   ...    
  3.   <distributionManagement>    
  4.     <repository>    
  5.       <id>my-repo</id>    
  6.       <name>Release Repository</name>    
  7.       <url>http://localhost:8081/artifactory/my-repo</url>    
  8.     </repository>    
  9.    
  10.   ...    
  11. </project>    


一般来说,分发构件到远程仓库需要认证,如果你没有配置任何认证信息,你往往会得到401错误。这个时候,如下在settings.xml中配置认证信息:

Xml代码   收藏代码
  1. <settings>    
  2.   ...    
  3.   <servers>    
  4.     <server>    
  5.       <id>my-repo</id>    
  6.       <username>admin</username>    
  7.       <password>password</password>    
  8.     </server>       
  9.   ...    
  10. </settings>  

需要注意的是,settings.xml中server元素下id的值必须与POM中repository或snapshotRepository下id的值完全一致。将认证信息放到settings下而非POM中,是因为POM往往是它人可见的,而settings.xml是本地的。




1. 创建Maven的普通java项目: 
   mvn archetype:create 
   -DgroupId=packageName 
   -DartifactId=projectName  
2. 创建Maven的Web项目:   
    mvn archetype:create 
    -DgroupId=packageName    
    -DartifactId=webappName 
    -DarchetypeArtifactId=maven-archetype-webapp    
3. 编译源代码: mvn compile 
4. 编译测试代码:mvn test-compile    
5. 运行测试:mvn test   
6. 产生site:mvn site   
7. 打包:mvn package   
8. 在本地Repository中安装jar:mvn install 
9. 清除产生的项目:mvn clean   
10. 生成eclipse项目:mvn eclipse:eclipse  
11. 生成idea项目:mvn idea:idea  
12. 组合使用goal命令,如只打包不测试:mvn -Dtest package   
13. 编译测试的内容:mvn test-compile  
14. 只打jar包: mvn jar:jar  
15. 只测试而不编译,也不测试编译:mvn test -skipping compile -skipping test-compile 
      ( -skipping 的灵活运用,当然也可以用于其他组合命令)  
16. 清除eclipse的一些系统设置:mvn eclipse:clean  

ps: 

一般使用情况是这样,首先通过cvs或svn下载代码到本机,然后执行mvn eclipse:eclipse生成ecllipse项目文件,然后导入到eclipse就行了;修改代码后执行mvn compile或mvn test检验,也可以下载eclipse的maven插件。 

mvn -version/-v  显示版本信息 
mvn archetype:generate        创建mvn项目 
mvn archetype:create -DgroupId=com.oreilly -DartifactId=my-app   创建mvn项目 

mvn package            生成target目录,编译、测试代码,生成测试报告,生成jar/war文件 
mvn jetty:run            运行项目于jetty上, 
mvn compile                    编译 
mvn test                    编译并测试 
mvn clean                    清空生成的文件 
mvn site                    生成项目相关信息的网站 
mvn -Dwtpversion=1.0 eclipse:eclipse        生成Wtp插件的Web项目 
mvn -Dwtpversion=1.0 eclipse:clean        清除Eclipse项目的配置信息(Web项目) 
mvn eclipse:eclipse                将项目转化为Eclipse项目 

在应用程序用使用多个存储库 
<repositories>    
    <repository>      
        <id>Ibiblio</id>      
        <name>Ibiblio</name>      
        <url>http://www.ibiblio.org/maven/</url>    
    </repository>    
    <repository>      
        <id>PlanetMirror</id>      
        <name>Planet Mirror</name>      
        <url>http://public.planetmirror.com/pub/maven/</url>    
    </repository>  
</repositories> 


mvn deploy:deploy-file -DgroupId=com -DartifactId=client -Dversion=0.1.0 -Dpackaging=jar -Dfile=d:\client-0.1.0.jar -DrepositoryId=maven-repository-inner -Durl=ftp://xxxxxxx/opt/maven/repository/ 


发布第三方Jar到本地库中: 

mvn install:install-file -DgroupId=com -DartifactId=client -Dversion=0.1.0 -Dpackaging=jar -Dfile=d:\client-0.1.0.jar 


-DdownloadSources=true 

-DdownloadJavadocs=true 

mvn -e            显示详细错误 信息. 

mvn validate        验证工程是否正确,所有需要的资源是否可用。 
mvn test-compile    编译项目测试代码。 。 
mvn integration-test     在集成测试可以运行的环境中处理和发布包。 
mvn verify        运行任何检查,验证包是否有效且达到质量标准。     
mvn generate-sources    产生应用需要的任何额外的源代码,如xdoclet。 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/lifxue/archive/2009/10/14/4662902.aspx 

常用命令: 
mvn -v 显示版本 
mvn help:describe -Dplugin=help 使用 help 插件的  describe 目标来输出 Maven Help 插件的信息。 
mvn help:describe -Dplugin=help -Dfull 使用Help 插件输出完整的带有参数的目标列 
mvn help:describe -Dplugin=compiler -Dmojo=compile -Dfull 获取单个目标的信息,设置  mojo 参数和  plugin 参数。此命令列出了Compiler 插件的compile 目标的所有信息 
mvn help:describe -Dplugin=exec -Dfull 列出所有 Maven Exec 插件可用的目标 
mvn help:effective-pom 看这个“有效的 (effective)”POM,它暴露了 Maven的默认设置 

mvn archetype:create -DgroupId=org.sonatype.mavenbook.ch03 -DartifactId=simple -DpackageName=org.sonatype.mavenbook 创建Maven的普通java项目,在命令行使用Maven Archetype 插件 
mvn exec:java -Dexec.mainClass=org.sonatype.mavenbook.weather.Main Exec 插件让我们能够在不往 classpath 载入适当的依赖的情况下,运行这个程序 
mvn dependency:resolve 打印出已解决依赖的列表 
mvn dependency:tree 打印整个依赖树 

mvn install -X 想要查看完整的依赖踪迹,包含那些因为冲突或者其它原因而被拒绝引入的构件,打开 Maven 的调试标记运行 
mvn install -Dmaven.test.skip=true 给任何目标添加maven.test.skip 属性就能跳过测试 
mvn install assembly:assembly 构建装配Maven Assembly 插件是一个用来创建你应用程序特有分发包的插件 

mvn jetty:run 调用 Jetty 插件的 Run 目标在 Jetty Servlet 容器中启动 web 应用 
mvn compile 编译你的项目 
mvn clean install 删除再编译 

mvn hibernate3:hbm2ddl 使用 Hibernate3 插件构造数据库



7、插件管理

  父模块中的配置如下
[html]  view plain copy
  1. <build>  
  2.     <pluginManagement>  
  3.         <plugins>  
  4.             <plugin>  
  5.                 <groupId>org.apache.maven.plugins</groupId>  
  6.                 <artifactId>maven-source-plugin</artifactId>  
  7.                 <executions>  
  8.                     <execution>  
  9.                         <id>attach-sources</id>  
  10.                         <phase>verify</phase>  
  11.                         <goals>  
  12.                             <goal>jar-no-fork</goal>  
  13.                         </goals>  
  14.                     </execution>  
  15.                 </executions>  
  16.             </plugin>  
  17.               
  18.         </plugins>  
  19.     </pluginManagement>  
  20. </build>  


子模块配置如下

[html]  view plain copy
  1. <build>  
  2.     <pluginManagement>  
  3.         <plugins>  
  4.             <plugin>  
  5.                 <groupId>org.apache.maven.plugins</groupId>  
  6.                 <artifactId>maven-source-plugin</artifactId>  
  7.             </plugin>  
  8.         </plugins>  
  9.     </pluginManagement>  
  10. </build>  
     

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值