zoukankan      html  css  js  c++  java
  • Maven 的基本配置与使用

    什么是Maven

    Maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具。
    发文时,绝大多数开发人员都把 Ant 当作 Java 编程项目的标准构建工具。遗憾的是,Ant 的项目管理工具(作为 make的替代工具)不能满足绝大多数开发人员的需要。通过检查 Ant 构建文件,很难发现项目的相关性信息和其它信息(如开发人员/拥有者、版本或站点主页)。
    Maven 除了以程序构建能力为特色之外,还提供 Ant 所缺少的高级项目管理工具。由于 Maven 的缺省构建规则有较高的可重用性,所以常常用两三行 Maven 构建脚本就可以构建简单的项目,而使用 Ant 则需要十几行。事实上,由于 Maven 的面向项目的方法,许多 Apache Jakarta 项目发文时使用 Maven,而且公司项目采用 Maven 的比例在持续增长。
    Maven这个单词来自于意第绪语,意为知识的积累,最早在 Jakata Turbine项目中它开始被用来试图简化构建过程。当时有很多项目,它们的Ant build文件仅有细微的差别,而JAR文件都由CVS来维护。于是Maven创始者开始了Maven这个项目,该项目的清晰定义包括,一种很方便的发布项目信息的方式,以及一种在多个项目中共享JAR的方式。(摘录百度百科)
    http://baike.baidu.com/view/336103.htm

    安装Maven

    Window
    #检查JDK
    echo %JAVA_HOME%
    C:Program FilesJavajdk1.8.0_05
    java -version
    java version "1.8.0_05"
    Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
    Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)
    #环境变量
    M2_HOME  E:Javaapache-maven-3.2.1
    Path %M2_HOME%in
    #检查配置
    mvn -v
    Apache Maven 3.2.1 (ea8b2b07643dbb1b84b6d16e1f08391b666bc1e9; 2014-02-15T01:37:5
    2+08:00)
    Maven home: E:Javaapache-maven-3.2.1
    Java version: 1.8.0_05, vendor: Oracle Corporation
    Java home: C:Program FilesJavajdk1.8.0_05jre
    Default locale: zh_CN, platform encoding: GBK
    OS name: "windows 7", version: "6.1", arch: "amd64", family: "dos"

    Liunx
    #导入环境变量
    $ export M2_HOME=/opt/apache-maven
    $ export PATH=$PATH:$M2_HOME/bin
    #检查配置
    mvn -v
    #修改仓库位置
    conf目录下的setting.xml文件,设置成自己创建的仓库路径

    <!--
    localRepository | The path to the local repository maven will use to store artifacts. | Default: ${user.home}/.m2/repository <localRepository>/path/to/local/repo</localRepository> -->

    #配置http代理
    如果网络有特殊限制。通过http代理连接,在conf目录下的setting.xml文件配置proxy

    Maven坐标

    Maven坐标为各种构件引入了秩序,任务一个构件都必须明确定义自己的坐标,而一组Maven坐标是通过一些元素
       定义的,他们是 groupId,artifactId,version,packaging,classifier

    <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <packaging>jar</packaging> <version>3.0.6.RELEASE</version>

    groupId: 定义当前Maven项目隶属的实际项目。
            1:Maven项目和实际项目不一定是一对一的关系,比如SpringFramework这一实际项目,这是
                 由于Maven中模块的概念,因为一个实际项目往往被划分成很多模块
            2:groupId不应该对应项目隶属的组织或公司。原因很简单,一个组织下会有很多实际项目
            3:groupId的表示方式与Java包名的表示方式类似,通常与域名反向一一对应
    artifactId
            定义实际项目中的一个Maven项目(模块),推荐的做法是使用实际项目名称作为artifactId的前缀。
            这样做的好处是方便寻找实际构件,在默认情况下,Maven生成的构件,其文件名会以artifactId作为
            开头。如spring-core-3.0.6.RELEASE.jar 
    version
            定义Maven项目当前所处的版本,如3.0.6.RELEASE
    packaging  定义Maven项目的打包方式。
                 1:打包方式通常与所生成构件的文件扩展名对应,如上例中的packaging为jar,最终的文件名为spring-core-3.0.6.RELEASE.jar
                    而使用war打包方式的Maven项目,最终生成的构件会有一个.war文件,不过这不是绝对的。
                 2:打包方式会影响构建的生命周期,比如jar打包和war打包使用不同的命令
                 3:当不定义packaging的时候,Maven会使用默认值jar
    classifier
                用来帮助定义构建输出的一些附属构件。附属构件与主构件对应,
                如上例中的主构件是spring-core-3.0.6.RELEASE.jar,该项目可能还会通过使用一些插件生成如
                spring-core-3.0.6.RELEASE-sources.jar,spring-core-3.0.6.RELEASE-doc.jar这样的附件构件,
                其中包含了Java源代码和文档,这时候,javadoc和sources就是这两个附属构件的classifier.
                这样附属也就拥有了自己唯一的坐标

    上述5个元素中,groupId,artifactId,version是必须定义的,packaging是可可选的(默认为jar),而classifier是不能直接定义的。同时,项目构件的文件名是与坐标相对应的,一般的规则为artifactId-version[-classifier].packaging[-classifier]表示可选

    创建Maven项目

    #创建一个简单Maven项目
    mvn archetype:generate
        -DgroupId=com.mycompany.app
        -DartifactId=my-app-simple
        -DarchetypeArtifactId=maven-archetype-quickstart
        -Dversion=1.0
    #创建Maven的Web项目   
    mvn archetype:create
        -DgroupId=packageName
        -DartifactId=webappName
        -DarchetypeArtifactId=maven-archetype-webapp
        -Dversion=1.0

    #创建项目(命令行中构建的选择不能换行)
    mvn archetype:create -DgroupId=com.homeinns.web -DartifactId=homeinns-web -DarchetypeArtifactId=maven-archetype-webapp -Dversion=1.0

    #构建成eclipse项目
    cd homeinns-web
    mvn eclipse:eclipse

    mvn archetype:create 创建Maven项目
    mvn compile 编译源代码
    mvn deploy 发布项目
    mvn test-compile 编译测试源代码
    mvn test 运行应用程序中的单元测试
    mvn site 生成项目相关信息的网站
    mvn dependency:tree 查看maven的依赖树结构
    mvn clean 清除项目目录中的生成结果
    mvn package 根据项目生成的jar
    mvn install 在本地Repository中安装jar
    mvn eclipse:eclipse 构建成eclipse工程
    mvn eclipse:clean  清除eclipse结构
    mvnjetty:run  启动jetty服务
    mvntomcat:run 启动tomcat服务
    mvn clean package -Dmaven.test.skip=true:清除以前的包后重新打包,跳过测试类
    mvn dependency:copy-dependencies  导出项目依赖的jar包
    mvn dependency:copy-dependencies -DoutputDirectory=lib 导出依赖的jar包到lib文件夹

    导入eclipse工程

    An internal error occurred during: "Updating Maven Project". Unsupported IClasspathEntry kind=4
    http://stackoverflow.com/questions/10564684/how-to-fix-error-updating-maven-project-unsupported-iclasspathentry-kind-4
    如果没有/src/main/java目录配置一下工程JDK目录

    image

    如果提示如下错误,设置下java的编译版本
    Java compiler level does not match the version of the installed Java project facet.xxx Unknown Faceted Project Problem (Java Version Mismatch)

    image

    Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin: 2.3.2 :compile ( default -compile) on project oecp: Compilation failure

    因为eclipse默认是运行在JRE上面的,maven的一些功能要求使用JDK,所以要在eclipse中的配置文件eclipse.ini中加入

    -vm C:/Program Files/Java/jdk1.8.0_05/bin/javaw.exe

    或者指定自己JDK

    image

    目录结构

    image

    src/main/java : java源文件存放位置
    src/main/resource : resource资源位置
    src/test/java : 测试代码源位置

    对象模型(POM)

    <project xmlns="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/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.homeinns.web</groupId>
      <artifactId>homeinns-web</artifactId>
      <packaging>war</packaging>
      <version>1.0-SNAPSHOT</version>
      <name>homeinns-web Maven Webapp</name>
      <url>http://maven.apache.org</url>
      <dependencies>
          <!--junit-->
          <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>4.11</version>
              <scope>test</scope>
          </dependency>
      </dependencies>
      <build>
        <finalName>homeinns-web</finalName>
      </build>
    </project>

    junit依赖范围

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

    注意:当依赖范围是test的时候,该依赖只会被加入到测试代码的classpath中,也就是说,对于项目主代码,
    该依赖是没有任何作用的。JUnit是单元测试框架,只有在测试的时候才需要,因此使用该依赖范围

    使用Jetty容器热部署

    <?xml version="1.0" encoding="utf-8"?>
    <project xmlns="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/maven-v4_0_0.xsd">  
      <modelVersion>4.0.0</modelVersion>  
      <groupId>com.homeinns.web</groupId>  
      <artifactId>homeinns-web</artifactId>  
      <packaging>war</packaging>  
      <version>1.0</version>  
      <name>homeinns-web Maven Webapp</name>  
      <url>http://maven.apache.org</url>  
      <dependencies> 
        <dependency> 
          <groupId>junit</groupId>  
          <artifactId>junit</artifactId>  
          <version>3.8.1</version>  
          <scope>test</scope> 
        </dependency>  
        <dependency> 
          <groupId>org.mortbay.jetty</groupId>  
          <artifactId>jsp-api-2.0</artifactId>  
          <version>6.1.14</version> 
        </dependency>  
        <dependency> 
          <groupId>tomcat</groupId>  
          <artifactId>jasper-compiler-jdt</artifactId>  
          <version>5.5.15</version> 
        </dependency>  
        <dependency> 
          <groupId>tomcat</groupId>  
          <artifactId>jasper-compiler</artifactId>  
          <version>5.5.15</version> 
        </dependency>  
        <dependency> 
          <groupId>tomcat</groupId>  
          <artifactId>jasper-runtime</artifactId>  
          <version>5.5.15</version> 
        </dependency>  
        <dependency> 
          <groupId>org.mortbay.jetty</groupId>  
          <artifactId>jsp-2.1</artifactId>  
          <version>6.1.14</version>  
          <scope>test</scope>  
          <exclusions> 
            <exclusion> 
              <groupId>org.mortbay.jetty</groupId>  
              <artifactId>jsp-api-2.1</artifactId> 
            </exclusion>  
            <exclusion> 
              <groupId>org.mortbay.jetty</groupId>  
              <artifactId>start</artifactId> 
            </exclusion>  
            <exclusion> 
              <groupId>org.mortbay.jetty</groupId>  
              <artifactId>jetty-annotations</artifactId> 
            </exclusion> 
          </exclusions> 
        </dependency> 
      </dependencies>  
      <build> 
        <finalName>homeinns-web</finalName>  
        <plugins> 
          <!-- jetty插件 -->  
          <plugin> 
            <groupId>org.mortbay.jetty</groupId>  
            <artifactId>maven-jetty-plugin</artifactId>  
            <version>6.1.10</version>  
            <configuration> 
              <scanIntervalSeconds>10</scanIntervalSeconds>  
              <stopKey>foo</stopKey>  
              <stopPort>9999</stopPort> 
            </configuration>  
            <executions> 
              <execution> 
                <id>start-jetty</id>  
                <phase>pre-integration-test</phase>  
                <goals> 
                  <goal>run</goal> 
                </goals>  
                <configuration> 
                  <scanIntervalSeconds>0</scanIntervalSeconds>  
                  <daemon>true</daemon> 
                </configuration> 
              </execution>  
              <execution> 
                <id>stop-jetty</id>  
                <phase>post-integration-test</phase>  
                <goals> 
                  <goal>stop</goal> 
                </goals> 
              </execution> 
            </executions> 
          </plugin> 
        </plugins> 
      </build> 
    </project>

     

    #启动Jetty

    mvn jetty:run
    #停止服务
    CTRL+C

    http://docs.codehaus.org/display/JETTY/Maven+Jetty+Plugin

    Maven 中央仓库

    http://search.maven.org/
    http://mvnrepository.com/

    阿里云 MAVEN

    阿里云的 Maven 库:http://maven.aliyun.com

    在本地库 .m2 的目录下 settings.xml 添加配置:

    <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
                              https://maven.apache.org/xsd/settings-1.0.0.xsd">
    
          <mirrors>
            <mirror>  
                <id>alimaven</id>  
                <name>aliyun maven</name>  
                <url>http://maven.aliyun.com/nexus/content/groups/public/</url>  
                <mirrorOf>central</mirrorOf>          
            </mirror>  
          </mirrors>
    </settings>
    复制代码
    <repositories>
        <repository>
          <id>central</id>
          <name>Central Repository</name>
          <url>http://maven.aliyun.com/nexus/content/repositories/central</url>
          <layout>default</layout>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
        </repository>
      </repositories>

    Refer:
    http://maven.apache.org/
    http://www.yiibai.com/maven/
    http://tangyanbo.iteye.com/blog/1503782
    Building Java Projects with Maven
    http://spring.io/guides/gs/maven

    http://maven.aliyun.com/mvn/view

  • 相关阅读:
    深度医疗(1)
    ENVI 5.X 影像处理入门实战教程-视频课程
    linux C++通讯架构实战 卷1-视频教程
    2 分钟把握 Envoy 的脉络,适应新场景的 envoy 有哪些不同?能做什么?
    Kubernetes Ingress诡异的502、503、504等奇葩问题(二)
    Kubernetes Ingress诡异的502、503、504等奇葩问题(一)
    Docker容器数据管理(数据卷&数据卷容器)
    SQL Server表水平分区
    从Asp .net到Asp core (第二篇)《Asp Core 的生命周期》
    从Asp .net到Asp core (第一篇)《回顾Asp .net生命周期与管道机制》
  • 原文地址:https://www.cnblogs.com/Irving/p/3720752.html
Copyright © 2011-2022 走看看