`
tphaofang
  • 浏览: 18505 次
  • 性别: 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
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics