maven

  • maven安装配置
    1. 下载解压maven压缩包,并配置相关环境变量,并将%M2_HOME%\bin添加到PATH,运行mvn -v确认安装成功
      M2_HOME=C:\Program Files\apache-maven-3.0.4,指向maven解压安装目录(指向新版maven就可直接升级)
      MAVEN_OPTS=-Xms128m -Xmx512m,maven运行需要多一点内存(java默认的内存不够)
    2. 配置本地maven仓库:maven会将依赖jar包下载到本地,可以将本地仓库位置配置到C:盘之外的地方
      复制${M2_HOME}/conf/settings.xml到${user.dir}/.m2/settings.xml,不直接修改M2_HOME下文件以方便maven升级
      修改localRepository元素,如<localRepository>D:\Hongwei\project\data\m2repos</localRepository>
    3. 插件下载或安装网址:
      http://download.eclipse.org/technology/m2e/releases
      http://m2eclipse.sonatype.org/sites/m2e,核心模块
      http://m2eclipse.sonatype.org/sites/m2e-extras,额外组件方便与其他工具如SVN集成
      配置maven插件:不使用内嵌的maven,它与命令行构建不一致(mvn package)
    4. 配置代理:ping repo1.maven.org不通,telnet proxy port可行,配置proxies元素
      <proxies>
        <proxy><id>my-proxy</id><active>true</active>
        <protocol>http</protocol><host>ip</host><port>3128</port>
       <username>user</username><password>pwd</password><nonProxyHosts>repos.mycom.com|*.google.com</nonProxyHosts>
        </proxy>
      </proxies>
    5. 镜像仓库请求到本地public:修改settings.xml配置,maven内置了central仓库访问,也可以在pom配置repository元素访问其他仓库,但最好是将所有仓库请求都镜像到私服public,
      <mirrors><mirror>
      <id>nexus</id><name>local</name><url>http://localhost:8079/nexus/content/groups/public/</url>
        <mirrorOf>central</mirrorOf>
      </mirror></mirrors>
      mirrorOf可能值:*匹配所有远程仓库,repo1,repo2匹配两个仓库,*,!repo1排除某个仓库,external:*排除file://协议依赖
    6. 部署项目模块:将项目模块部署install到私服以供其他模块依赖,此时注意快照版依赖将即时更新,而稳定版依赖将本地缓存
      <distributionManagement>
       <repository><id>nexus</id><name>local</name><url>http://localhost:8079/nexus/content/groups/public/</url>
        </repository><snapshotRepository>
       <id>nexus</id><name>local</name><url>http://localhost:8079/nexus/content/groups/public/</url>
      </snapshotRepository></distributionManagement>
      验证信息需要配置settings.xml,因为pom会发布出去不利于密码保护
      <servers><server><id>nexus</id><username>admin</username><password>admin123</password></server></servers>
  • maven私服安装:Nexus当前最流行的Maven仓库管理软件
    1. 下载安装:解压至/usr/local并建立软链接
      http://www.sonatype.org/downloads/nexus-2.0.4-bundle.tar.gz
      tar xvzf nexus-2.0.2-bundle.tgz /usr/local
      ln -s nexus-2.0.2 nexus
    2. 配置端口及仓库路径:${nexus}/conf/nexus.properties,私服仓库保存在${nexus}/work
      application-port=8079
      nexus-work=${bundleBasedir}/work
    3. 运行nexus私服并查看日志:
      /usr/local/nexus/bin/nexus start
      tail -f logs/wrapper.log
    4. 访问nexus并管理远程仓库:
      http://localhost:8079/nexus,默认登录账户admin/admin123,在这里还查找maven构件或上传私有构件
      nexus自带public、thirdparty、releases、central等仓库,类型有hosted本地、proxy代理、group组合
      其他公开远程仓库,http://download.java.net/maven/2http://repository.jboss.com/mvaven2
      本地代理仓库proxy到内网私服的public,修改本地public顺序为local、central
      让maven使用本地public仓库:http://localhost:8079/nexus/content/groups/public/
    5. 上传本地jar到私服仓库:例如jta.jar
      thirdparty可用来上传本地jar
      mvn install:install-file -DgroupId=javax.transaction -DartifactId=jta -Dversion=1.1 -Dpackaging=jar -Dfile=c:\path\to\jar\jta-1.1-classes.jar
    6. 搜索构件:内网私服可以搜索已索引的构件,而外网搜索服务能提供更多的构建搜索
      Sonatype Nexus:http://repository.sonatype.org,提供关键字、类名、坐标、校验和等搜索
      Jarvana:http://www.jarvana.com/jarvana,支持依赖声明片段、文档浏览
      MVNbrowser:http://www.mvnbrowser.com,支持构件的依赖关系
      MVNrepository:http://mvnrepository.com,支持构件各版本大小变化图表
  • maven基础知识
    1. 构件坐标:maven使用自己的坐标系统定位所有构件,同时也要求maven项目有自己的坐标
      groupId公司项目(org.sonatype.nexus,公司可能有多个项目),
      artifactId项目模块(nexus-indexer,可能多个项目都有core模块所以加上项目前缀),
      version版本(快照版SNAPSHOT不稳定),
      packaging打包,默认为jar
      classifier附属构件(不能直接定义,由插件帮助生成javadoc、sources等),
      目标构件名为artifactId-version[-classifier].packaging,如nexus-indexer-2.0.0-javadoc.jar
    2. 依赖范围
      compile,编译需要,测试和运行也需要,如spring-core.jar
      test,仅测试需要,如junit.jar
      provided,仅编译和测试需要,运行时无效,如servlet-api.jar已有容器提供
      runtime,仅运行时需要,如jdbc-connector.jar
      system,使用systemPath指定系统路径,不利于构建的可移植性,如tools.jar
    3. 依赖关系
      传递性依赖,当前项目依赖的构件,可能还对其他构件有依赖,maven会自动计算出所有的依赖关系
      依赖调解,第一原则是路径最近者优先,第二原则是声明顺序优先,maven会自动使用构件的稳定版本(非SNAPSHOT快照版)
      可选依赖,比如某库支持mysql和postgresql但只能使用其一,此时某库对两种driver的依赖是可选的,项目中必须引入其中一种
      排除依赖,可能某库依赖某构件的2.0版,而项目明确需要3.0版本,可排除某库对某构件2.0版的依赖并显示引入其3.0版
      <denpendency>
        <groupId>com.some.project</groupId>
        <artifactId>project-model.jar</artifact>
        <version>1.0.0</version>
        <exclusions><exclusion>
          <groupId>com.other.project</groupId><artifactId>project-model.jar</artifactId>
        </exclusion></exclusions>
      </denpendency>
      <denpendency>
        <groupId>com.other.project</groupId><artifactId>project-model.jar</artifactId>
        <version>3.0</version>
      </denpendency>
      归类依赖,spring-core、spring-beans等其实是来自同一项目的多个模块,它们依赖的版本通常是相同的,此时可使用版本变量以方便spring升级
      <properties>
        <spring.version>2.5.6</spring.version>
      </properties>
      <denpendency>
        <groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${spring.version></version>
      </denpendency>
      优化依赖,mvn denpendency:analyze,Used undeclared dependencies需要显示声明,Unused declared denpencies可能不需要声明(如spring-core等是spring-context-support的基础也最好声明版本)
    4. 聚合与继承:聚合用于同时构建过个模块(反应堆还可以灵活剪裁),继承用于给子模块提供通用的配置(如源码插件版本等)
      聚合模块使用modules声明目标模块
      <modules>
      <module>../account-email</module>
      <module>../account-persist</module>
      </modules>
      剪裁反应堆,默认mvn clean install会构建整个反应堆链,而剪裁有利于多模块项目的快速构建
      mvn clean install -pl account-email,account-persist,构建指定的模块列表project list
      mvn clean install -pl account-email -am,构建指定模块及其依赖also make
      mvn clean install -pl account-parent -amd,构建指定模块及依赖于它的模块also make denpendents
      mvn clean install -rf account-email,构建反应堆中指定模块之后的模块resume-from
      子模块使用parent声明父模块,通常子模块是父模块的子目录时relativePath=../pom.xml
      <parent>
      <groupId>com.soyinke.account</groupId>
      <artifactId>account-parent</artifactId>
      <version>1.0.0-SNAPSHOT</version>
      <relativePath>../account-parent/pom.xml</relativePath>
      </parent>
      父模块可声明而不引入插件版本,统一源码及目标版本等
      <properties>
        <spring.version>2.5.6</spring.version>
        <junit.version>4.7.1</junit.version>
        <maven.compiler.source>1.6</maven.compiler.source>
      </properties>
      <denpendencyManagement>
        <denpendency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${spring.version}</version></denpendency>
       <denpendency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version></denpendency>
      </denpendencyManagement>
      子模块直接引用依赖构件即可,或完整引用特有的依赖构件模块
      <denpencies>
        <denpendency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId></denpendency>
        <denpendency><groupId>junit</groupId><artifactId>junit</artifactId></denpendency>
      </denpencies>
    5. 生命周期lifecycle、阶段phase、插件目标plugin:goal
      • 默认有三套生命周期,各自有独立的阶段顺序
        clean=pre-clean clean post-clean,清理项目
        site=pre-site site post-site site-deploy,站点信息
        default=validate initialize generate-sources process-sources generate-resources process-resources compile process-classes generate-test-sources process-test-resources test-compile process-test-classes test prepare-package package pre-integration-test integration-test post-integration-test verify install deploy
      • 常用构建命令:
        1. mvn -h;mvn help:describe -Dcmd=install -Ddetail; mvn help:help -Ddetail; mvn help:describe -Dplugin=groupId:artifactId[:version] 
          mvn assembly:help -Ddetail -Dgoal=,命令目标帮助
        2. mvn dependency:analyze,分析依赖包
        3. mvn deploy -pl ../soyinke-parent,指定发布项目包
        4. mvn package -Dmaven.test.skip=true,跳过测试打包
        5. mvn install,加入本地依赖库;mvn install:install-file -DgroupId= -DartifactId -Dversion -Dpackaging -Dfile=,加入jar包到本地依赖库
      • 阶段与插件的目标绑定,而插件可能有多个目标,maven内置了许多阶段与目标的绑定,用户也可以按打包源码source.jar的方式自定义这种绑定
        查看插件信息mvn help:describe -Dplugin=groupId:artifactId -Ddetail
        目标前缀help代表了org.apache.maven.plugins:maven-help-plugin:2.1
        <settings><pluginGroups>
                <pluginGroup>org.mortbay.jetty</pluginGroup>
        </pluginGroups></settings>
        插件仓库元数据:配置前缀
        <metadata><plugins><plugin>
        <name>Maven Clean Plugin</name><prefix>clean</prefix><artifactId>maven-clean-plugin</artifactId>
        </plugin></plugins></metadata>
    6. 创建骨架
      • 骨架组成
        描述符:archetype.xml,/src/main/resources/META-INF/maven/,包含骨架文件列表
      • 原型文件:/src/main/resources/archetype-resources/,骨架插件要复制的文件
      • 原型pom:/src/main/resources/archetype-resources/pom.xml
      • 骨架项目pom:/pom.xml
  • maven项目管理:
    1. 基本配置:约定优于配置,超级POM中包含许多约定。
      编码属性project.build.sourceEncoding=UTF-8
      代码版本属性maven.compiler.source=maven.compiler.target=1.6,实际是配置compiler插件
      跳过测试maven.test.skip=true
    2. 常见阶段:
      compile,编译
      package,根据jar或war打包
      install,安装到本地m2repos,本地
      deploy,上传到nexus仓库,内网
    3. 高级操作:
      • 发布到仓库:mvn deploy
        打开nexus上的hosted类型仓库releases的发布策略depoyment policy=allow redeploy
        复制发布配置distributionManagement元素到项目pom,可以在父模块配置供子模块继承
        在${maven}/conf/settings.xml中添加servers元素,可以在nexus/Security/Users重置密码。
        <servers>  
            <server> 
            <id>snapshots</id> 
            <username>deployment</username>
            <password>deploy</password>
            </server> 
            <server> 
            <id>releases</id> 
            <username>deployment</username>
            <password>deploy</password>
            </server> 
        </servers>
      • 打包可执行jar,配置shade插件,生成主类信息
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <executions>
                <execution>
                    <phrase>package</phrase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer implementation="org.apache.mave.plugins.shade.resource.ManifestResurceTransformer">
                                <mainClass>com.HelloWorld</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
      • 打包源码sources.jar、文档javadoc.jar
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>2.2</version>
            <executions>
                <execution>
                    <id>attach-sources</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>jar-no-fork</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
      • 运行主类并传递参数:
        mvn exec:java -Dexec.mainClass=package.class -Dexec.args="arg1 arg2"
  • maven插件目标
    1. maven-help-plugin,mvn help:help
      • describe,描述插件或目标,-Ddetail -Dgoal=describe -Dplugin=prefix|groupId:artifactId[:version] -Dcmd=phrase|prefix:goal
      • system,输出环境变量和系统变量
    2. maven-clean-plugin,mvn clean:help
      • clean,清空输出目录
    3. maven-resources-plugin,mvn resources:help
      • [test-]resources,复制资源
    4. maven-compiler-plugin,mvn compiler:help
      • [test-]compile,编译源码
    5. maven-dependency-plugin,mvn dependency:help
      • analyze[-only],分析依赖使用情况(默认触发test-compile)
      • tree|resolve,树形显示或计算所有依赖
    6. maven-surefire-plugin, mvn surefire:help
      • test,运行测试
    7. maven-antrun-plugin,mvn antrun:help
      • run,执行ant构建任务,-Dtasks提供target任务列表(另外:ant:ant,插件maven-ant-plugin可以生成对应的ant构建文件)
    8. maven-jar-plugin, mvn jar:help
      • [test-]jar,打包字节码
    9. maven-war-plugin, mvn war:help
      • exploded,集合依赖包到WebContent\WEB-INF\lib
      • war,打包网站项目war
    10. mvn-install-plugin,mvn install:help
      • install,发布构件到本地库
      • install-file,发布三方jar包到本地库
    11. maven-deploy-plugin,mvn deploy:help
      • deploy,发布构件到远程库(通常如果有javadoc、sources等也会发布)
      • deploy-file,发布三方jar包到远程库
    12. maven-source-plugin, mvn source:help
      • [test-]jar[-no-fork],打包源码(默认触发compile)
    13. maven-javadoc-plugin,mvn javadoc:help
      • [test-]javadoc,在site目录下生成apidocs源码文档
      • [test-]jar,生成target\apidocs文档并打包
    14. maven-site-plugin,mvn site:help
      • site,生成站点内容
      • deploy,发布站点到远程
      • jar|run,打包或运行站点(jetty,-Dport设定端口)
    15. maven-release-plugin,mvn release:help
      • branch,分支代码库(-DbranchBase非标准目录trunk/tags/branchs时需要,-DbranchName分支名,-Dusername+password账户)
      • prepare+clean,准备或清理
      • perform+rollback,执行或回滚发布(workingDirectory=target/checkout)
    16. maven-eclipse-plugin,mvn eclipse:help
      • eclipse+clean,生成或清理项目信息
      • myeclipse[-clean],生成或清理myeclipse项目信息
    17.  
  • maven相关问题:
    1. Missing artifact com.sun:tools:jar:1.5.0
      解决一:添加tools.jar到依赖库,在pom.xml中添加依赖项
      mvn install:install-file -DgroupId=com.sun -DartifactId=tools -Dversion=1.5.0 -Dpackaging=jar -Dfile="C:\Program Files\Java\jdk1.6.0_22\lib\tools.jar"
      项目中添加依赖
      <dependency> 
           <groupId>com.sun</groupId> 
           <artifactId>tools</artifactId> 
           <version>1.5.0</version>
          <scope>provided</scope>
      </dependency>
      解决二:指定运行eclipse的java虚拟机路径,在eclipse.ini的openFile下面添加
      -vm
      C:/Program Files/Java/jdk1.6.0_22/bin/javaw.exe




posted @ 2012-09-24 16:18  xlongwei  阅读(882)  评论(0编辑  收藏  举报
xlongwei