您的位置:首页 > 其它

maven简单应用总结

2016-09-01 09:20 337 查看


下载Maven

下载地址:http://mirrors.hust.edu.cn/apache/maven/maven-3/3.2.5/binaries/apache-maven-3.2.5-bin.zip 
(当前最新版是3.3.9,此版本是3.2.5,为了适配服务器环境,此版本以上需要jdk1.7支持)


配置Maven环境变量

添加变量:M2_HOME,变量值:[your_maven_path]/apache-maven-3.2.5
变量 Path 添加:%M2_HOME%\bin;
运行:mvn -version 查看版本信息,执行成功即配置成功。


什么是Maven仓库

Maven仓库就是放置所有JAR文件(WAR,ZIP,POM等等)的地方,所有Maven项目可以从同一个Maven仓库中获取自己所需要的依赖JAR,这节省了磁盘资源。此外,由于Maven仓库中所有的JAR都有其自己的坐标,该坐标告诉Maven它的组ID,构件ID,版本,打包方式等等,因此Maven项目可以方便的进行依赖版本管理。你也不在需要提交JAR文件到SCM仓库中,你可以建立一个组织层次的Maven仓库,供所有成员使用。

简言之,Maven仓库能帮助我们管理构件(主要是JAR)。


本地仓库 vs 远程仓库

运行Maven的时候,Maven所需要的任何构件都是直接从本地仓库获取的。如果本地仓库没有,它会首先尝试从远程仓库下载构件至本地仓库,然后再使用本地仓库的构件。

比如说,你的项目配置了junit-3.8的依赖,在你运行mvn test 的时候,Maven需要使用junit-3.8的jar文件,它首先根据坐标查找本地仓库,如果找到,就直接使用。如果没有,Maven会检查可用的远程仓库配置,然后逐个尝试这些远程仓库去下载junit-3.8的jar文件,如果远程仓库存在该文件,Maven会将其下载到本地仓库中,继而使用。如果尝试过所有远程仓库之后,Maven还是没能够下载到该文件,它就会报错。

Maven缺省的本地仓库地址为${user.home}/.m2/repository 。也就是说,一个用户会对应的拥有一个本地仓库。

你也可以自定义本地仓库的位置,修改%M2_HOME%\conf\setting.xml :

<!-- localRepository

   | The path to the local repository maven will use to store artifacts.

   |

   | Default: ${user.home}/.m2/repository

   -->

  <localRepository>E:\MvnRepository(你想放置的本机位置)</localRepository>

原始的Maven安装就自带了一个远程仓库——Maven中央仓库(http://repo1.maven.org/maven2),同时它关闭了snapshot版本构件下载的支持。

为了提高下载速度,继而提高构建速度,同时方便管理,我们一般会搭建一个局域网的远程仓库(即私服),这里我们用nexus已经搭建好了私服。
私服地址:http://172.16.16.17:8081/nexus/index.html 
用户名:admin  密码:dniony1


在settings.xml中配置远程仓库(nexus私服)

修改%M2_HOME%\conf\setting.xml,如下:

 </profiles>

        ...

    <profile>

      <id>dev</id>

      <repositories>

         <repository>

            <id>nexus</id>

            <name>local private nexus</name>

            <url>http://172.16.16.17:8081/nexus/content/groups/public/</url>

            <releases>

                <enabled>true</enabled>

            </releases>

            <snapshots>

                <enabled>true</enabled>

            </snapshots>

         </repository>

      </repositories>

      <pluginRepositories>

         <pluginRepository>

            <id>nexus</id>

            <name>local private nexus</name>

            <url>http://172.16.16.17:8081/nexus/content/groups/public/</url>

            <releases>

                <enabled>true</enabled>

            </releases>

            <snapshots>

                <enabled>false</enabled>

            </snapshots>

         </pluginRepository>

      </pluginRepositories>

   </profile>

  </profiles>

 

  <!-- activeProfiles

   | List of profiles that are active for all builds.

   |

  <activeProfiles>

    <activeProfile>alwaysActiveProfile</activeProfile>

    <activeProfile>anotherAlwaysActiveProfile</activeProfile>

  </activeProfiles>

  -->

  <activeProfiles> 

    <activeProfile>dev</activeProfile> 

  </activeProfiles>

 

这里定义一个id为dev的profile,将所有repositories以及pluginRepositories元素放到这个profile中,然后使用<activeProfiles>元素自动激活该profile。
<releases><enabled>true</enabled></releases>告诉Maven可以从这个仓库下载releases版本的构件,而<snapshots><enabled>false</enabled></snapshots>告诉Maven不要从这个仓库下载snapshot版本的构件。禁止从公共仓库下载snapshot构件是推荐的做法,因为这些构件不稳定,且不受你控制,你应该避免使用。但是由于项目需要,我们这里激活了snapshots下载。


镜像

如果你的地理位置附近有一个速度更快的central镜像(私服),或者你想覆盖central仓库配置,或者你想为所有POM使用唯一的一个远程仓库(这个远程仓库代理的所有必要的其它仓库),你可以使用settings.xml中的mirror配置。以上的mirror配置用私服覆盖了Maven所有仓库。
修改%M2_HOME%\conf\setting.xml,如下:

<mirrors>

    <!-- mirror

     | Specifies a repository mirror site to use instead of a given repository. The repository that

     | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used

     | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.

     |

    <mirror>

      <id>mirrorId</id>

      <mirrorOf>repositoryId</mirrorOf>

      <name>Human Readable Name for this Mirror.</name>

      <url>http://my.repository.com/repo/path</url>

    </mirror>

     -->

 <mirror>

   <id>nexus</id>

   <mirrorOf>*</mirrorOf>

   <name>Nexus Public</name>

   <url>http://172.16.16.17:8081/nexus/content/groups/public/</url>

 </mirror>

</mirrors>


Eclipse中Maven插件配置及使用

安装maven插件:当下版本的eclipse都自带了maven插件,此步省略。
配置maven环境:

进入Window -> Preferences -> Maven  -> Installations



在该选项卡add本机配置好的maven并选中即可。

进入Window -> Preferences -> Maven  ->
 User Settings



在该选项卡中为User Settings选择配置好的%M2_HOME%\conf\setting.xml,同时可以看到Local Repository会变成上面配置好的本地仓库地址,然后依次点击Reindex
 Apply  OK按钮。


Maven常用命令

clean清理(删除target目录下编译内容)
compile编译
package打包(根据事先指定的格式,比如jar,进行打包)
install安装(安装到本地代码库)

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: