maven 的学习

来源:互联网 发布:六轴机器人编程 编辑:程序博客网 时间:2024/06/02 04:18
---------------准备工作-------------

Jdk  1.5以上java开发环境。

Eclipse IDE 一个。

Maven 3.0.3下载地址: http://maven.apache.org/docs/3.0.3/release-notes.html


----//快速搭建步骤

 

第一步:配置maven环境

 

将下载文件解压,然后设置maven环境

如果你配置过jdk的话,这里对你应该不难。如我的maven环境为:F:\maven\apache-maven-3.0.3

我的电脑-----属性----高级-----环境变量-----环境变量-----新建

变量名:M2_HOME

变量值:F:\maven\apache-maven-3.0.3

找到path 

在环境变量值尾部加入:;%M2_HOME%\bin;---前面注意分号

当然,你也可以直接在path 路径下加入:;F:\maven\apache-maven-3.0.3\bin  只是上面的方式更优雅一点。

我新建立

打开命令提示符(开始---运行---cmd,检查我们的java环境和maven环境是否有误。


第二步:修改仓库位置

 

修改我们仓库地址,仓库用于存放我们项目所依赖的所有jar包。

我的仓库路径:F:\maven\repo----这个路径是我自己创建,你可以将路径创建在任何位置。

我们打开…\apache-maven-3.0.3\conf\目录下的setting.xml文件,设置成我们创建的仓库路径

下面我们用一个命令验证一下。打开命令提示符,输入:mvn help:system 

该命令会打印出所有的java系统属性和环境变量。这些信息对我们日常的编程工作很有帮且。

如果运行的过程中没有错误,打开我们仓库(F:\maven\repo)会发现里面多了一些文件。这些文件就是我们从maven的中央仓库下载到本地仓库的。

 

第三步:创建maven项目

创建一个我们自己的项目。

我们通过maven命令行方式创建一个项目


mvn archetype:create -DgroupId=com.chongshi.test -DartifactId=hello -DpackageName=com.chongshi.test -Dversion=1.0

 

因为是第一次构建项目,所有依赖的jar包都要从maven的中央仓库下载,所以需要时间等待。等以后我们的本地仓库中积累了我们常用的jar包后,我们的开发将变得非常规范和方便。^_^!!

 

借助下载jar包的时间,我们来了解一下pom.xml文件。



<projectxmlns="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.chongshi.test</groupId>
      <artifactId>hello</artifactId>
      <version>1.0</version>
   <packaging>jar</packaging>

  <name>hello</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>



Ø project:pom.xml文件中的顶层元素; 
Ø modelVersion:指明POM使用的对象模型的版本。这个值很少改动。
Ø groupId:指明创建项目的组织或者小组的唯一标识。GroupId是项目的关键标识,典型的,此标识以组织的完全限定名来定义。比如,org.apache.maven.plugins是所有Maven插件项目指定的groupId。 

Ø artifactId:指明此项目产生的主要产品的基本名称。项目的主要产品通常为一个JAR文件。第二,象源代码包通常使用artifactId作为最后名称的一部分。典型的产品名称使用这个格式: <artifactId>- <version>. <extension>(比如:myapp-1.0.jar)。 

Ø version:项目产品的版本号。Maven帮助你管理版本,可以经常看到SNAPSHOT这个版本,表明项目处于开发阶段。 

Ø name:项目的显示名称,通常用于maven产生的文档中。 

Ø url:指定项目站点,通常用于maven产生的文档中。 

Ø description:描述此项目,通常用于maven产生的文档中。

 

对于一个项目中只有下面的一部分是是我们需要关注的:

<groupId>com.chongshi.test</groupId>

 <artifactId>hello</artifactId>

 <version>1.0</version>



第四步:编译项目代码

我们的项目已经创建完成。但我们点开目录发现,它并不是我们eclipse所需要的项目目录格式。我们需要把它构建成我们eclipse可以导入的项目。

在命令提示符下进入到我们的创建的项目目录(F:\maven\hello)下,执行:mvn clean compile


Clean 告诉maven清理输入出目录target/,compile告诉maven编译项目主代码。

不要急,我们又需要一段时间来下载,相关jar包。^_^!第一次用maven要学会淡定。

 

项目是编译完了,但项目的目录结构还不是我们想要的eclipse的项目结构,是不能导入到eclipse中的。所以,还需要执行一个命令:mvn eclipse:eclipse

 

命令执行完成后就得我们需要的项目目录了。

 

第五步:导入eclipse工具

 

打开的我们的eclipse工具。

先配置maven仓库路径

Window----Perferences-----java-----Build Path-----Classpath Variables

New一个变量的类路径。


Name :M2_REPO   注意这个名字必须要大写。

Path :F:/maven/repo  点击“Folder…”找到有本地仓库的位置。

 

下面,可以导入我的hello项目了。Eclipse如何导入项目,我这里就不说了,如果你是个java开发人员的话。

 

第六步:包的更新与下载

 

打开项目发现我们junit 是3.8.1的,有点老了。那我想换成4.7的,如何通过maven的方式更换呢。其实,很简单,打开我们项目下的的pom.xml文件。


复制代码
……<dependencies>    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>4.8.1</version>      <scope>test</scope>    </dependency>  </dependencies>……
复制代码

更改变,junit的版本号,然后重新执行:mvn eclipse:eclipse  

Maven 中央仓库地址:http://search.maven.org

 

假如,我们想下载一个struts jar包。在搜索框内搜索strruts ,会要列出中央仓库中的所有struts版本。

列表的格式与我们pom.xml配置文件的格式是对应的。


我们在pom.xml中加入:

<groupId>stuts</groupId>

 <artifactId>struts-scripting</artifactId>

 <version>1.0.1</version>

然后更新项目就可从中央仓库下载我们想要的任意jar包(必须是开源的包)



 如何搭建创建maven项目


http://www.cnblogs.com/leiOOlei/p/3361633.html 第一种


第二种

使用eclipse插件创建一个web project

首先创建一个Maven的Project如下图


我们勾选上Create a simple project (不使用骨架)


这里的Packing 选择 war的形式

由于packing是war包,那么下面也就多出了webapp的目录



由于我们的项目要使用eclipse发布到tomcat下面,这里我们需要先把项目转成dynamic web project

 

在我们的项目上点击右键,选择properties 并找到 Project Facets ,并点击Convert to faceted form…   如下图:


然后勾选Dynamic Web Module 并点击ok  如下图:(3.0只有tomcat7才支持)

接下来观察我们的项目结构,多了一个web content目录


虽然此时我们可以发布到tomcat中,但这不符合maven的结构,我们还要做如下修改

把上图WebContent下面两个目录 META-INF ,WEB-INF 直接剪切到src/main/webapp目录下,并删掉WebContent目录,那么现在的项目结构如下图:


接着重新指定一个web的路径,点击add,选择Folder -- 〉  next


在src下找到webapp目录,然后finish

最后一步,我们要把当前的build path 指向 Maven Dependency, 直接点击add,选择Java Build Path Entries 然后next

然后再点击finish完成

完成后如下图:

至此一个基于maven的webapp就建立好了,并可以直接从eclipse中发布到tomcat中

 

补充:我们需要在src/main/webapp/WEB-INF下面创建一个web.xml



导入我们的Spring mvc依赖jar包

<dependencies>

 

    <dependency>

        <groupId>org.springframework</groupId>

        <artifactId>spring-core</artifactId>

        <version>3.0.7.RELEASE</version>

    </dependency>

  

    <dependency>

        <groupId>org.springframework</groupId>

        <artifactId>spring-web</artifactId>

        <version>3.0.7.RELEASE</version>

    </dependency>

  

    <dependency>

        <groupId>org.springframework</groupId>

        <artifactId>spring-webmvc</artifactId>

        <version>3.0.7.RELEASE</version>

    </dependency>

 

    <dependency>

        <groupId>org.codehaus.jackson</groupId>

        <artifactId>jackson-mapper-asl</artifactId>

        <version>1.7.1</version>

    </dependency>

  

  </dependencies>

 

直接保存,maven就会自动为我们下载所需jar文件



三使用Nexus搭建Maven代理仓库


使用Maven构建和管理项目是非常享受的一件事,我们可以从Maven中央仓库下载所需要的构件(artifact),但实际开发中由于种种原因我们需要在架设一个Maven本地代理仓库,如:不方便访问公网、节省带宽和时间、管理自家的共用artifact等等。本地地理仓库是我自己取的名字,为了不与下文的本地仓库想混淆。



获取构建的流程如下:用户使用Maven构建项目时,首先是要直接从本地仓库获取的,如果本地仓库没有,它会根据setting.xml的设置去首先尝试从远程仓库下载构件至本地仓库,然后再使用本地仓库的构件。如果setting.xml设置的远程仓库是本地代理仓库,则本地代理仓库先尝试从自己的库中获取,如果没有再从远程仓库(比如中央仓库)下载构件至本地仓库。

Nexus 是一个优秀的Maven仓库管理器,还提供了强大的仓库管理功能,构件搜索功能,它基于REST,友好的UI是一个extjs的REST客户端,它占用较少的内存,基于简单文件系统而非数据库。这些优点使其日趋成为最流行的Maven仓库管理器。本文就使用Nexus搭建一个本地代理仓库。

下载和安装

Nexus官方下载地址:http://www.sonatype.org/nexus/go,目前最新的版本是2.7.2。

Nexu安装非常容易,因为它内嵌了Jetty,只要有JRE救能直接运行。解压Nexu包会得到两个目录nexus-2.7.2-03和sonatype-work,sonatype-work是默认仓库目录。运行、安装都是使用nexus-2.7.2-03/bin/nexus.bat文件,它的使用方式:

<ol class="linenums" style="margin: 0px 0px 10px 30px; padding: 0px; border-left-color: rgb(0, 153, 51); border-left-width: 2px; border-left-style: solid;"><li class="L0" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(153, 153, 153);"><span class="typ" style="color: rgb(255, 0, 85);">Usage</span><span class="pun" style="color: rgb(17, 17, 17);">:</span><span class="pln" style="color: rgb(17, 17, 17);">nexus</span><span class="pun" style="color: rgb(17, 17, 17);">.</span><span class="pln" style="color: rgb(17, 17, 17);">bat </span><span class="pun" style="color: rgb(17, 17, 17);">{</span><span class="pln" style="color: rgb(17, 17, 17);"> console </span><span class="pun" style="color: rgb(17, 17, 17);">:</span><span class="pln" style="color: rgb(17, 17, 17);"> start </span><span class="pun" style="color: rgb(17, 17, 17);">:</span><span class="pln" style="color: rgb(17, 17, 17);"> stop </span><span class="pun" style="color: rgb(17, 17, 17);">:</span><span class="pln" style="color: rgb(17, 17, 17);"> restart </span><span class="pun" style="color: rgb(17, 17, 17);">:</span><span class="pln" style="color: rgb(17, 17, 17);"> install </span><span class="pun" style="color: rgb(17, 17, 17);">:</span><span class="pln" style="color: rgb(17, 17, 17);"> uninstall </span><span class="pun" style="color: rgb(17, 17, 17);">}</span></li></ol>
其中console是控制台方式运行,install是以windows service寄存,uninstall是下载windows service,start是运行windows service,stop是停止windows service,restart是重启windows service,。

Nexus默认端口是8081,可以在nexus-2.7.1-01/conf/nexus.properties中修改,启动后就可以通过地址:http://localhost/:8081/nexus 来访问了。界面如下:


管理仓库

管理仓库需要先登录,默认登录用户名/密码是admin/admin123。登录后就可以看到左栏菜单的管理项。


这里,可以管理仓库,配置Nexus系统,管理任务,管理用户,角色,权限,查看系统的RSS源,管理及查看系统日志,等等。

  1. 设置Nexus代理上公网
    如果您的本地代理仓库服务器没有公网权限,Nexus为你留了使用代理的路子,点击右边菜单的“Server”,在右边找到:


添加你的代理服务器即可。


  • 2  仓库管理
    点击左边导航栏的Repositories,界面的主面板会显示所有一个所有仓库及仓库组的列表,你会看到它们的Type字段的值有group,hosted,proxy,virtual。这里我们不关心virtual,只介绍下另外三种类型:

    1. hosted,本地代理仓库,通常我们会部署自己的构件到这一类型的仓库。
    • proxy,代理的远程仓库,它们被用来代理远程的公共仓库,如maven中央仓库。
    • group,仓库组,用来合并多个hosted/proxy仓库,通常我们配置maven依赖仓库组。
    1. 如何管理、添加等操作,Nexus都写的很清楚了,我就不一一赘述了。
    2. 修改setting.xml配置Maven的仓库

    Maven安装后默认的是使用中央仓库,这是为了能让Maven开箱即用。而Maven缺省的本地仓库地址为${user.home}/.m2/repository。也就是说,一个用户会对应的拥有一个本地仓库。你也可以自定义本地仓库的位置,修改${user.home}/.m2/settings.xml。

    首先需要修改Mirrors

    1. <mirrors>

    2. <!-- mirror | Specifies a repository mirror site to use instead of a given
    3. repository. The repository that | this mirror serves has an ID that matches
    4. the mirrorOf element of this mirror. IDs are used | for inheritance and direct
    5. lookup purposes, and must be unique across the set of mirrors. | -->
    6. <mirror>
    7. <id>nexusc</id>
    8. <mirrorOf>*</mirrorOf>
    9. <name>Nexus</name>
    10. <url>http://localhost:8081/nexus/content/groups/public/</url>
    11. </mirror>
    1. </mirrors>

    在执行 Maven 命令的时候, Maven 还需要安装一些插件包,这些插件包的下载地址也让其指向本地代理仓库的地址,修改如下:


    1. 在执行 Maven 命令的时候, Maven 还需要安装一些插件包,这些插件包的下载地址也让其指向本地代理仓库的地址,修改如下:
    <ol class="linenums" style="margin: 0px 0px 10px 30px; padding: 0px; border-left-color: rgb(0, 153, 51); border-left-width: 2px; border-left-style: solid;"><li class="L0" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(153, 153, 153);"><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"><profile></span></li><li class="L1" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(204, 204, 204);"><span class="pln" style="color: rgb(17, 17, 17);"></span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"><id></span><span class="pln" style="color: rgb(17, 17, 17);">jdk-1.4</span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"></id></span></li><li class="L2" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(153, 153, 153);"><span class="pln" style="color: rgb(17, 17, 17);"></span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"><activation></span></li><li class="L3" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(204, 204, 204);"><span class="pln" style="color: rgb(17, 17, 17);"></span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"><jdk></span><span class="pln" style="color: rgb(17, 17, 17);">1.4</span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"></jdk></span></li><li class="L4" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(153, 153, 153);"><span class="pln" style="color: rgb(17, 17, 17);"></span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"></activation></span></li><li class="L5" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(204, 204, 204);"><span class="pln" style="color: rgb(17, 17, 17);"></span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"><repositories></span></li><li class="L6" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(153, 153, 153);"><span class="pln" style="color: rgb(17, 17, 17);"></span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"><repository></span></li><li class="L7" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(204, 204, 204);"><span class="pln" style="color: rgb(17, 17, 17);"></span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"><id></span><span class="pln" style="color: rgb(17, 17, 17);">nexus</span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"></id></span></li><li class="L8" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(153, 153, 153);"><span class="pln" style="color: rgb(17, 17, 17);"></span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"><name></span><span class="pln" style="color: rgb(17, 17, 17);">local private nexus</span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"></name></span></li><li class="L9" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(204, 204, 204);"><span class="pln" style="color: rgb(17, 17, 17);"></span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"><url></span><span class="pln" style="color: rgb(17, 17, 17);">http://localhost:8081/nexus/content/groups/public/</span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"></url></span></li><li class="L0" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(153, 153, 153);"><span class="pln" style="color: rgb(17, 17, 17);"></span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"><releases></span></li><li class="L1" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(204, 204, 204);"><span class="pln" style="color: rgb(17, 17, 17);"></span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"><enabled></span><span class="pln" style="color: rgb(17, 17, 17);">true</span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"></enabled></span></li><li class="L2" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(153, 153, 153);"><span class="pln" style="color: rgb(17, 17, 17);"></span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"></releases></span></li><li class="L3" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(204, 204, 204);"><span class="pln" style="color: rgb(17, 17, 17);"></span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"><snapshots></span></li><li class="L4" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(153, 153, 153);"><span class="pln" style="color: rgb(17, 17, 17);"></span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"><enabled></span><span class="pln" style="color: rgb(17, 17, 17);">false</span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"></enabled></span></li><li class="L5" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(204, 204, 204);"><span class="pln" style="color: rgb(17, 17, 17);"></span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"></snapshots></span></li><li class="L6" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(153, 153, 153);"><span class="pln" style="color: rgb(17, 17, 17);"></span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"></repository></span></li><li class="L7" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(204, 204, 204);"><span class="pln" style="color: rgb(17, 17, 17);"></span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"></repositories></span></li><li class="L8" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(153, 153, 153);"><span class="pln" style="color: rgb(17, 17, 17);"></span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"><pluginRepositories></span></li><li class="L9" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(204, 204, 204);"><span class="pln" style="color: rgb(17, 17, 17);"></span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"><pluginRepository></span></li><li class="L0" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(153, 153, 153);"><span class="pln" style="color: rgb(17, 17, 17);"></span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"><id></span><span class="pln" style="color: rgb(17, 17, 17);">nexus</span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"></id></span></li><li class="L1" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(204, 204, 204);"><span class="pln" style="color: rgb(17, 17, 17);"></span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"><name></span><span class="pln" style="color: rgb(17, 17, 17);">local private nexus</span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"></name></span></li><li class="L2" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(153, 153, 153);"><span class="pln" style="color: rgb(17, 17, 17);"></span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"><url></span><span class="pln" style="color: rgb(17, 17, 17);">http://localhost:8081/nexus/content/groups/public/</span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"></url></span></li><li class="L3" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(204, 204, 204);"><span class="pln" style="color: rgb(17, 17, 17);"></span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"><releases></span></li><li class="L4" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(153, 153, 153);"><span class="pln" style="color: rgb(17, 17, 17);"></span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"><enabled></span><span class="pln" style="color: rgb(17, 17, 17);">true</span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"></enabled></span></li><li class="L5" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(204, 204, 204);"><span class="pln" style="color: rgb(17, 17, 17);"></span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"></releases></span></li><li class="L6" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(153, 153, 153);"><span class="pln" style="color: rgb(17, 17, 17);"></span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"><snapshots></span></li><li class="L7" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(204, 204, 204);"><span class="pln" style="color: rgb(17, 17, 17);"></span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"><enabled></span><span class="pln" style="color: rgb(17, 17, 17);">false</span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"></enabled></span></li><li class="L8" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(153, 153, 153);"><span class="pln" style="color: rgb(17, 17, 17);"></span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"></snapshots></span></li><li class="L9" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(204, 204, 204);"><span class="pln" style="color: rgb(17, 17, 17);"></span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"></pluginRepository></span></li><li class="L0" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(153, 153, 153);"><span class="pln" style="color: rgb(17, 17, 17);"></span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"></pluginRepositories></span></li><li class="L1" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(204, 204, 204);"><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"></profile></span></li></ol>

    部署构件至Nexus


    通过Nexus UI部署

    有时候有个jar文件你无法从公共Maven仓库找到,但是你能从其它得到这个jar文件(甚至是POM),那么你完全可以将这个文件部署到Nexus中,使其成为标准流程的一部分。步骤如下:

    点击左边导航栏的"Repository",在右边的仓库列表中选择一个仓库,如“3rd Party”,然后在页面下方的tab选择“Artifact Upload Artifact(s)”,你会看到构件上传界面。选择你要上传的构件,并指定POM,(或者手工编写GAV等信息),最后点击Upload,该构件就直接被部署到了Nexus的"3rd Party"仓库中。


    

    通过Maven部署
    更常见的用例是:团队在开发一个项目的各个模块,为了让自己开发的模块能够快速让其他人使用,你会想要将snapshot版本的构件部署到Maven仓库中,其他人只需要在POM添加一个对于你开发模块的依赖,就能随时拿到最新的snapshot。
    以下的pom.xml配置和settings.xml能让你通过Maven自动化部署构件:
    pom.xml
    1. <project>
    2. ...
    3. <distributionManagement>
    4. <repository>
    5. <id>nexus-releases</id>
    6. <name>Nexus Release Repository</name>
    7. <url>http://localhost:8081/nexus/content/repositories/releases/</url>
    8. </repository>
    9. <snapshotRepository>
    10. <id>nexus-snapshots</id>
    11. <name>Nexus Snapshot Repository</name>
    12. <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
    13. </snapshotRepository>
    14. </distributionManagement>
    15. ...
    16. </project>
    settings.xml
    <ol class="linenums" style="margin: 0px 0px 10px 30px; padding: 0px; border-left-color: rgb(0, 153, 51); border-left-width: 2px; border-left-style: solid;"><li class="L0" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(153, 153, 153);"><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"><settings></span></li><li class="L1" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(204, 204, 204);"><span class="pln" style="color: rgb(17, 17, 17);">...</span></li><li class="L2" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(153, 153, 153);"><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"><servers></span></li><li class="L3" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(204, 204, 204);"><span class="pln" style="color: rgb(17, 17, 17);">  </span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"><server></span></li><li class="L4" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(153, 153, 153);"><span class="pln" style="color: rgb(17, 17, 17);">    </span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"><id></span><span class="pln" style="color: rgb(17, 17, 17);">nexus-releases</span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"></id></span></li><li class="L5" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(204, 204, 204);"><span class="pln" style="color: rgb(17, 17, 17);">    </span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"><username></span><span class="pln" style="color: rgb(17, 17, 17);">admin</span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"></username></span></li><li class="L6" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(153, 153, 153);"><span class="pln" style="color: rgb(17, 17, 17);">    </span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"><password></span><span class="pln" style="color: rgb(17, 17, 17);">admin123</span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"></password></span></li><li class="L7" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(204, 204, 204);"><span class="pln" style="color: rgb(17, 17, 17);">  </span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"></server></span></li><li class="L8" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(153, 153, 153);"><span class="pln" style="color: rgb(17, 17, 17);">  </span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"><server></span></li><li class="L9" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(204, 204, 204);"><span class="pln" style="color: rgb(17, 17, 17);">    </span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"><id></span><span class="pln" style="color: rgb(17, 17, 17);">nexus-snapshots</span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"></id></span></li><li class="L0" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(153, 153, 153);"><span class="pln" style="color: rgb(17, 17, 17);">    </span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"><username></span><span class="pln" style="color: rgb(17, 17, 17);">admin</span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"></username></span></li><li class="L1" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(204, 204, 204);"><span class="pln" style="color: rgb(17, 17, 17);">    </span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"><password></span><span class="pln" style="color: rgb(17, 17, 17);">admin123</span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"></password></span></li><li class="L2" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(153, 153, 153);"><span class="pln" style="color: rgb(17, 17, 17);">  </span><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"></server></span><span class="pln" style="color: rgb(17, 17, 17);">  </span></li><li class="L3" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(204, 204, 204);"><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"></servers></span></li><li class="L4" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(153, 153, 153);"><span class="pln" style="color: rgb(17, 17, 17);">...</span></li><li class="L5" style="margin: 0px; padding: 0px 0px 0px 10px; color: rgb(204, 204, 204);"><span class="tag" style="margin: 0px 3px; padding: 0px 3px; color: rgb(17, 17, 17); display: inline-block; background-color: rgb(238, 238, 238);"></settings></span></li></ol>
    这里我们配置所有的snapshot版本构件部署到Nexus的Snapshots仓库中, 所有的release构件部署到Nexus的Releases仓库中。由于部署需要登陆,因为我们在settings.xml中配置对应Repository id的用户名和密码。
    然后,在项目目录中执行mvn deploy ,你会看到maven将项目构件部署到Nexus中,浏览Nexus对应的仓库,就可以看到刚才部署的构件。当其他人构建其项目时,Maven就会从Nexus寻找依赖并下载。

    
  • 0 0
    原创粉丝点击