`
tphaofang
  • 浏览: 18922 次
  • 性别: Icon_minigender_1
  • 来自: 湖南
社区版块
存档分类
最新评论

maven安装配置

 
阅读更多

1.下载并配置maven,
  到http://maven.apache.org下载maven的最新版本,并解压到某一目录(我本机是E:\资料收藏\maven\apache-maven-3.0.5-bin\apache-maven-3.0.5);
2.配置环境变量(可做可不做,主要为了能在dos下运行mvn命令)
      a.配置系统环境变量:
          增加 MVN_HOME=E:\资料收藏\maven\apache-maven-3.0.5-bin\apache-maven-3.0.5
          PATH 里面加上%MVN_HOME%\bin
     
      d.在命令行上输入 : mvn -version; 回车,如看到下面信息表示安装成功:Apache Maven 3.0.5 (r01de14724cdef164cd33c7c8c2fe155faf9602da; 2013-02-19 21:51:28+0800) 
  3.eclipse插件安装
      Update site是http://m2eclipse.sonatype.org/sites/m2e,全选安装就好了,重启eclipse.
      这样就安装了eclipse集成的maven插件,但建议使用在第一步下载maven,做法如下:进入Preferences——》maven——》Installations,点击“Add”添加maven的解压路径。 
      

 
   4.maven配置, 如上图,use setting选择我们下载的maven目录中的settings.xml;

    localRepository(本地资源库保存的位置):d:/nexus/.m2/repository
     以下是我的setting配置:需要建立一个私服,下面会讲到 
     <?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

  <localRepository>d:/nexus/.m2/repository</localRepository>

  <interactiveMode>true</interactiveMode>

  <offline>false</offline>

   <!--测试服务器-->
    <server>
            <id>tomcat7</id>
            <username>admin</username>
            <password>admin</password>
    </server>
     <server>
            <id>tomcat6</id>
            <username>admin</username>
            <password>admin</password>
    </server>
   
    <!--发布服务器-->
    <server>
            <id>releases</id>
            <username>admin</username>
            <password>admin</password>
    </server>
     <server>
            <id>snapshots</id>
            <username>admin</username>
            <password>admin</password>
    </server>
  
  <mirrors>
     <!--evelyn local private nexus-->
     <mirror>
      <id>nexus-public</id>
      <mirrorOf>nexus-public</mirrorOf>
      <name>crm_sales for this Mirror.</name>
      <url>http://127.0.0.1:8081/nexus/content/groups/public</url>
    </mirror>
  </mirrors>
 
   <profiles>
    <profile>
    <id>nexus</id>
    <repositories>
              <repository>
                       <id>nexus</id>
                       <name>local private nexus</name>
                       <url>http://127.0.0.1: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://127.0.0.1:8081/nexus/content/groups/public</url>
                        <releases><enabled>true</enabled></releases>
                        <snapshots><enabled>true</enabled></snapshots>
             </pluginRepository>
    </pluginRepositories>
    </profile>
  </profiles>

  <activeProfiles>
    <activeProfile>nexus</activeProfile>
  </activeProfiles>
</settings>
其中上面标注为红色的,需要修改为自己的一个本地目录,以后所有的jar包都会放在这个下面.

5.关于maven工程pom.xml文件的一些概念
   a. 坐标
     Maven把项目作为构件,每个构件定义一个坐标,由于区分其他的构件,配置依赖时只需加入这个坐标,maven会先到本地仓库查找该构件,找不到就到远程仓库查找。
     比如:我搭的storm项目
        <groupId>test</groupId>
        <artifactId>storm</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
       
     坐标如下:
         GroupId:定义当前maven项目所属的组织机构。
         ArtifactID:构件的ID,一个project、模块的id。
         Version:版本号。
         Packaging:打包的方式。默认是jar。Web project需要选择war。这里选择默认方式

    b.资源仓库的配置repositories,把我们需要的资源的仓库地址配在这里,默认是中央仓库,我这里用了私服和第三方特殊资源库
       <repositories>
                <repository>
                        <id>nexus</id>
                        <name>local private nexus</name>
                        <url>http://127.0.0.1:8081/nexus/content/groups/public</url>
                        <releases>
                                <enabled>true</enabled>
                        </releases>
                        <snapshots>
                                <enabled>true</enabled>
                        </snapshots>
                </repository>
                <repository>
                        <id>clojars.org</id>
                        <url>http://clojars.org/repo</url>
                </repository>
        </repositories>
    c.依赖jar包的配置<dependencies>中的<dependency> 配置的jar包是传递依赖的,比如
               <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-beans</artifactId>
                        <version>3.2.2.RELEASE</version>
                        <scope></scope>
                </dependency>
       它会把spring-beans所依赖的包都下载下来,其中<scope>标签是配置maven依赖的作用域,一般有以下几种:
          compile(编译范围)
                compile是默认的范围;如果没有提供一个范围,那该依赖的范围就是编译范围。编译范围依赖在所有的classpath中可用,同时它们也会被打包。
          provided(已提供范围)
                provided依赖只有在当JDK或者一个容器已提供该依赖之后才使用。例如,如果你开发了一个web应用,你可能在编译classpath中需要可用的Servlet API来编译一个servlet,但            是你不会想要在打包好的WAR中包含这个Servlet API;这个Servlet API JAR由你的应用服务器或者servlet容器提供。已提供范围的依赖在编译classpath(不是运行时)可用。它们不            是传递性的,也不会被打包。
          runtime(运行时范围)
                runtime依赖在运行和测试系统的时候需要,但在编译的时候不需要。比如,你可能在编译的时候只需要JDBC API JAR,而只有在运行的时候才需要JDBC驱动实现。
          test(测试范围)
                test范围依赖 在一般的 编译和运行时都不需要,它们只有在测试编译和测试运行阶段可用。
          system(系统范围)
                system范围依赖与provided类似,但是你必须显式的提供一个对于本地系统中JAR文件的路径。这么做是为了允许基于本地对象编译,而这些对象是系统类库的一部分。这样的构            件应该是一直可用的,Maven也不会在仓库中去寻找它。 如果你将一个依赖范围设置成系统范围,你必须同时提供一个systemPath元素 。注意该范围是不推荐使用的(你应该一直尽量            去从公共或定制的Maven仓库中引用依赖)。
    d. maven插件的配置plugins,我这里基本上包含了开发中常见的插件配置 
       
        <plugins>
                        <!-- 清除target中的内容 -->
                        <plugin>
                                <artifactId>maven-clean-plugin</artifactId>
                                <version>2.5</version>
                                <configuration>
                                        <!--忽略错误-->
                                        <failOnError>false</failOnError>
                                </configuration>

                        </plugin>
                        <!-- 支持多个源代码的目录 -->
                        <plugin>
                                <groupId>org.codehaus.mojo</groupId>
                                <artifactId>build-helper-maven-plugin</artifactId>
                                <version>1.1</version>
                                <executions>
                                        <execution>
                                                <id>add-source</id>
                                                <phase>generate-sources</phase>
                                                <goals>
                                                        <goal>add-source</goal>
                                                </goals>
                                                <configuration>
                                                        <sources>
                                                                <source>src/main/java</source>
                                                                <source>src/main/resources</source>
                                                                <source>src/army/java</source>
                                                        </sources>
                                                </configuration>
                                        </execution>
                                </executions>
                        </plugin>


                        <!-- test插件, 仅测试名称为*Test的类,使用支持分组测试的surefire-junit47 driver -->
                        <plugin>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-surefire-plugin</artifactId>
                                <version>2.14.1</version>
                                <configuration>
                                        <classpathDependencyExcludes>
                                                <!-- exclude code absent api -->
                                                <classpathDependencyExclude>javax.faces:javax.faces-api</classpathDependencyExclude>
                                        </classpathDependencyExcludes>
                                        <excludes>
                                                <exclude>**/BaseTest.java</exclude>
                                        </excludes>
                                        <argLine>-Xmx256M</argLine>
                                        <testFailureIgnore>true</testFailureIgnore>
                                </configuration>
                                <dependencies>
                                        <dependency>
                                                <groupId>org.apache.maven.surefire</groupId>
                                                <artifactId>surefire-junit47</artifactId>
                                                <version>2.14.1</version>
                                        </dependency>
                                </dependencies>

                        </plugin>
                        <!-- 打包jar文件时,配置文件给排除在外 -->
                        <plugin>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-jar-plugin</artifactId>
                                <executions>
                                        <execution>
                                                <phase>package</phase>
                                                <goals>
                                                        <goal>jar</goal>
                                                </goals>
                                                <configuration>
                                                        <classifier>noconfig</classifier>
                                                        <excludes>
                                                                <exclude>*.xml</exclude>
                                                                <exclude>*.properties</exclude>
                                                        </excludes>
                                                </configuration>
                                        </execution>
                                </executions>
                        </plugin>

                        <!-- 拷贝依赖的jar包到lib目录 -->
                        <plugin>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-dependency-plugin</artifactId>
                                <version>2.8</version>
                                <executions>
                                        <execution>
                                                <id>copy-dependencies</id>
                                                <phase>package</phase>
                                                <goals>
                                                        <goal>copy-dependencies</goal>
                                                </goals>
                                                <configuration>
                                                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                                                        <overWriteReleases>false</overWriteReleases>
                                                        <overWriteSnapshots>false</overWriteSnapshots>
                                                        <overWriteIfNewer>true</overWriteIfNewer>
                                                        <includeScope>runtime</includeScope>

                                                </configuration>
                                        </execution>
                                </executions>
                        </plugin>

                        <!-- 资源文件单独打包. -->
                        <plugin>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-resources-plugin</artifactId>
                                <version>2.6</version>
                                <executions>
                                        <execution>
                                                <id>copy-resources</id>
                                                <!-- here the phase you need -->
                                                <phase>validate</phase>
                                                <goals>
                                                        <goal>copy-resources</goal>
                                                </goals>
                                                <configuration>
                                                        <encoding>UTF-8</encoding>
                                                        <outputDirectory>${basedir}/target/config</outputDirectory>
                                                        <resources>
                                                                <resource>
                                                                        <directory>src/main/resources</directory>
                                                                        <filtering>true</filtering>
                                                                </resource>
                                                        </resources>
                                                </configuration>
                                        </execution>
                                </executions>
                        </plugin>

                        <!-- 生成source -->
                        <plugin>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-source-plugin</artifactId>
                                <version>2.2.1</version>
                                <configuration>
                                        <attach>true</attach>
                                        <encoding>UTF-8</encoding>
                                </configuration>
                                <executions>
                                        <execution>
                                                <id>attach-sources</id>
                                                <goals>
                                                        <goal>jar</goal>
                                                </goals>
                                        </execution>
                                </executions>
                        </plugin>
                        <!-- 生成doc -->
                        <plugin>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-javadoc-plugin</artifactId>
                                <version>2.7</version>
                                <executions>
                                        <execution>
                                                <id>attach-javadocs</id>
                                                <goals>
                                                        <goal>jar</goal>
                                                </goals>
                                        </execution>
                                </executions>
                        </plugin>
                        <!-- 生成类和依赖一起的Jar包 -->
                        <plugin>
                                <artifactId>maven-assembly-plugin</artifactId>
                                <configuration>
                                        <descriptorRefs>
                                                <descriptorRef>jar-with-dependencies</descriptorRef>
                                        </descriptorRefs>
                                        <archive>
                                                <manifest>
                                                        <mainClass></mainClass>
                                                </manifest>
                                        </archive>
                                </configuration>
                                <executions>
                                        <execution>
                                                <id>make-assembly</id>
                                                <phase>package</phase>
                                                <goals>
                                                        <goal>single</goal>
                                                </goals>
                                        </execution>
                                </executions>

                        </plugin>
                       <!-- 编译插件 -->
                        <plugin>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-compiler-plugin</artifactId>
                                <configuration>
                                        <defaultLibBundleDir>lib</defaultLibBundleDir>
                                        <source>1.6</source>
                                        <target>1.6</target>
                                        <encoding>UTF-8</encoding>
                                </configuration>
                        </plugin>

                        <!-- eclipse插件, 设定wtp版本 -->
                        <plugin>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-eclipse-plugin</artifactId>
                                <version>2.9</version>
                                <configuration>
                                        <downloadSources>true</downloadSources>
                                        <downloadJavadocs>false</downloadJavadocs>
                                        <wtpversion>2.0</wtpversion>
                                        <!-- 增加设置项目encoding的文件 -->
                                        <additionalConfig>
                                                <file>
                                                        <name>.settings/org.eclipse.core.resources.prefs</name>
                                                        <content>
                                                        <![CDATA[eclipse.preferences.version=1${line.separator}encoding/<project>=${project.build.sourceEncoding}${line.separator}]]>
                                                        </content>
                                                </file>
                                        </additionalConfig>
                                </configuration>
                        </plugin>
                </plugins>



6.使用nexus-2.4.0-09配置maven私服
  a.在nexus官网下载安装包:http://nexus.sonatype.org/downloads/ 下载后解压
  b.在解压文件E:\资料收藏\maven\nexus-2.4.0-09-bundle\nexus-2.4.0-09\bin\jsw\windows-x86-32目录 下,双击install-nexus.bat,添加windows服务 
   

 
    设置手动或自动,双击设置
  c.双击nexus.bat,会启动内置的jetty服务器,在浏览器打开地址:http://127.0.0.1:8081/nexus/index.html,user:admin,password admin123登录nexus服务器
  d.设置组资源仓库,它可以包含多个资源仓库,比如下图:左边列表中的仓库就是各种仓库的组合,可以添加和移除 
    

 
   
  e.在setting.xml中配置私服
     <mirrors>
     <!--evelyn local private nexus-->
     <mirror>
      <id>nexus-public</id>
      <mirrorOf>nexus-public</mirrorOf>
      <name>crm_sales for this Mirror.</name>
      <url>http://127.0.0.1:8081/nexus/content/groups/public</url>
    </mirror>
  </mirrors>
 
   <profiles>
    <profile>
    <id>nexus</id>
    <repositories>
              <repository>
                       <id>nexus</id>
                       <name>local private nexus</name>
                       <url>http://127.0.0.1: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://127.0.0.1:8081/nexus/content/groups/public</url>
                        <releases><enabled>true</enabled></releases>
                        <snapshots><enabled>true</enabled></snapshots>
             </pluginRepository>
    </pluginRepositories>
    </profile>
  </profiles>

  <activeProfiles>
    <activeProfile>nexus</activeProfile>
  </activeProfiles>




 

  • 大小: 61.2 KB
  • 大小: 10.4 KB
  • 大小: 65.5 KB
分享到:
评论

相关推荐

    Maven安装配置外加Myeclipse上完美运行图解

    Maven 安装配置指南 本资源为 Maven 安装配置的详细指南,旨在帮助用户快速搭建 Maven 开发环境,并在 MyEclipse 中进行完美运行。以下是 Maven 安装配置的详细步骤: Step 1:下载 Maven 首先,需要下载 Maven ...

    Maven安装配置说明

    将上述配置保存后,确保替换原有的Maven配置文件,以保证新配置生效。 #### Eclipse中Maven的配置 **2.1 打开Eclipse的首选项设置** - 在Eclipse中,选择`Window &gt; Preferences`打开首选项设置窗口。 **2.2 找到...

    maven 安装配置手册

    ### Maven安装配置手册知识点概述 #### 一、Maven概览 Maven是一个项目管理和综合工具,主要用于Java项目。它的设计理念是“约定优于配置”,即通过预设的规则和标准来减少配置工作量,使开发者能够更加专注于业务...

    Maven安装配置.doc

    《全面解析Maven安装配置与使用》 Maven,作为Java开发中的构建工具,极大地简化了项目的构建、管理和依赖管理过程。本文将详细介绍Maven的安装配置步骤,并提供Eclipse集成Maven的方法,以及创建和配置Maven项目的...

    Maven安装配置教程.docx

    Maven 安装配置教程 Maven 是一个基于项目对象模型(POM)的项目管理工具,由 Apache 软件基金会开发。Maven 的主要功能是帮助开发者们构建、测试和部署项目。下面是 Maven 安装配置教程的详细知识点: 一、Maven ...

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

    "Maven的安装、配置及使用入门" 在本章节中,我们将学习如何安装、配置和使用Maven。Maven是一个基于项目对象模型(Project Object Model,POM)的软件项目管理和构建自动化工具。它可以帮助开发者管理项目的构建、...

    JDK和Maven安装配置总结.docx

    ### JDK和Maven安装配置详解 #### 一、JDK安装与配置 ##### 1. JDK 安装步骤 JDK (Java Development Kit) 的安装通常较为简单直观,大多数情况下,只需要按照安装向导提示进行即可。对于 JDK 1.5(即 Java 5)的...

    Maven安装配置(Myeclipse).rar_maven

    ** Maven安装配置在Myeclipse中的详细步骤及注意事项 ** Maven是一款强大的项目管理工具,它可以帮助Java开发者管理和构建项目,自动解决项目的依赖关系。在Myeclipse集成开发环境中,集成Maven可以使得项目管理...

    Maven安装配置教程.pdf

    【Maven安装配置教程】 Maven是一个强大的Java项目管理和构建工具,它可以帮助开发者管理项目依赖、构建工件、执行测试以及生成文档。本教程将详细介绍如何安装和配置Maven,以及如何在MyEclipse集成环境中使用...

    Maven安装配置教程 (2).docx

    **Maven安装配置教程** Maven是一个强大的Java项目管理和依赖管理工具,它简化了构建、编译、测试和部署的过程。以下是一个详细的Maven安装配置教程: 1. **下载Maven** 要安装Maven,首先需要访问官方网站...

    JDK和Maven安装配置分析与总结.zip

    - **优化Maven配置**:选择靠近的镜像源可提高下载速度,合理设置本地仓库避免磁盘空间浪费。 - **使用Maven profiles**:针对不同环境(开发、测试、生产)配置不同的profile,便于管理。 - **理解Maven生命周期和...

    Maven安装配置教程 (2).pdf

    **Maven安装配置教程** **一、Maven的下载与安装** Maven是Java开发中的一个项目管理和构建工具,它简化了项目的构建、依赖管理以及报告生成。要安装Maven,首先需要从官方网站&lt;http://maven.apache.org/download....

    Maven安装配置教程及仓库、POM坐标系、Maven工程、继承与聚合

    ### Maven安装配置教程及仓库、POM坐标系、Maven工程、继承与聚合 #### Maven安装与配置 在开始之前,我们首先需要了解Maven是什么。Maven是Apache的一个项目,是一个项目管理和综合工具,主要用来帮助Java项目...

    实验2 Maven安装配置.docx

    实验2 Maven安装配置.docx

    maven安装配置文档

    maven安装的文档,描述了maven的安装,环境变量配置,myeclipse配置。简单易懂,完整实用。

    maven安装配置(新版)pdf

    maven安装配置(新版) 1.去 Apache 官网下载Maven 2.创建安装目录并解压安装包 损览与源又档一致,卜载高11 在某个非C盘盘符(今后的本地仓库会建在这里,仓库会很大,所以不能放在c盘)的 根目录下新建一个文件夹叫...

Global site tag (gtag.js) - Google Analytics