zoukankan      html  css  js  c++  java
  • Maven概览

    Maven的核心思想,约定由于配置

    1 Maven坐标

    1.1 本项目的坐标

    • groupId:  必须。项目组名称,定义当前Maven项目所隶属的实际项目,通常与域名反向一一对应,与Java包名表示方式类似
    • artifactId:  必须。项目名称
    • version:   必须。版本
    • packaging:  (可选,没写则为jar)。打包方式,jar、war等
    • classifier   (不能直接定义)。帮助定义构建输出的附属构件

    1.2 项目的依赖的坐标

    • groupId:  必须。依赖的组名称
    • artifactId:  必须。依赖的工程名称
    • version:   必须。版本
    • type:    (可选,没写则为jar),该依赖的类型
    • scope   (可选,没写则为compile),依赖的范围:compile、test、provided、runtime、system、import,用来控制在 编译classpath、测试classpath、运行classpath 三种classpath中依赖是否起作用

        1.compile 默认编译依赖范围。对于编译,测试,运行三种classpath都有效
        2.test:测试依赖范围。只对于测试classpath有效
        3.provided:已提供依赖范围。对于编译,测试的classpath都有效,但对于运行无效。因为容器已经提供,例如servlet-api
        4.runtime:运行时提供。例如:jdbc驱动

     

    • optional  (可选,没写则为必选)标记依赖是否可选,一般不用,因为按面向对象设计单一职责原则,一个类应只有一个职责而非糅合太多功能。
    • exclusions (可选),用来排除依赖的传递性。只需要指定 groupIdartifactId

      当Maven通过这些坐标无法从中心仓库获取该组件时,可以通过下面的方法处理:

    1. 用安装插件安装本地依赖,命令:
      mvn install:install-file -Dfile=non-maven-proj.jar -DgroupId=some.group -DartifactId=non-maven-proj -Dversion=1 -Dpackaging=jar
    2. 创建你自己的仓库,并部署它
    3. 设置依赖scope到system,并定义一个systemPath,但这个不推荐

    2 Maven命令

    • mvn archetype:generate  创建maven项目,只运行该命令会以交互模式运行,让写groupId、artifactId、version等。可以非交互模式运行:
      •   创建普通工程:
        mvn archetype:generate -DgroupId=com.trinea.maven.test -DartifactId=maven-quickstart -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
      • 创建Web工程:
        mvn archetype:generate -DgroupId=com.trinea.maven.web.test -DartifactId=maven-web -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
    • mvn clean    删除target文件夹(清除编译出的类)
    • mvn compile    编译类到target文件夹下
    • mvn test      运行单元测试
    • mvn package    打包(生成的包在本工程内)
    • mvn install     安装(将包安装到本地maven仓库中,供其他项目使用)

        执行test   会自动先调用compile

        执行package 会自动先调用test

        执行install  会自动先调用package

    • mvn dependency:list  列出已解析依赖(Resolved Dependency)
    • mvn dependency:tree  列出依赖树
    • mvn dependency:analyze  依赖分析(分析编译主代码和测试代码需要的依赖,不会分析执行测试和运行时所需的依赖),会列出 未声明但使用了的依赖(Unused declared dependencies)、已声明但未使用的依赖(Used undeclared dependencies)

    3 Maven核心概念

    3.1 Maven的核心概念有 坐标、依赖、仓库、生命周期、插件等。

    生命周期和插件关联密切,生命周期只是进行行为上的抽象定义,其实际执行由相应的插件来完成。生命周期插件的绑定实际上是生命周期阶段与插件目标的绑定。

    • 生命周期有三种:clean、default、site,三者间相互独立,每个周期包含若干个阶段,阶段前后有依赖关系。如clean周期的pre-clean、-clean、post-clean三个阶段。
      1. default周期各阶段:validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy
      2. clean周期各阶段:pre-clean, clean, post-clean

      3. site周期各阶段:pre-site, site, post-site, site-deploy

    • 每个插件都集成有若干同类的功能,一个功能就是该插件的一种目标 。如dependency的目标有analyze、tree等

    三个生命周期主要阶段及其插件目标的默认绑定关系如下(default生命周期的绑定关系与打包类型有关,这里以jar为例):

     

    插件及其目标介绍详见:https://maven.apache.org/plugins/

    4 聚合与继承

    聚合主要为了快速构建项目,继承主要为了消除重复。

    聚合就是在父项目的pom中指定modules,继承就是在子项目pom中指定parent

    聚合时列出的模块不需要考虑顺序,Maven将自己根据依赖关系排序。

    4.1 super pom

    所有pom默认都继承自一个super pom,对apache-maven-3.2.5而言,该文件在 yourmaven/lib/maven-model-builder-3.2.5.jar里的pom.xml,内容如下:

      1 <?xml version="1.0" encoding="UTF-8"?>
      2 
      3 <!--
      4 Licensed to the Apache Software Foundation (ASF) under one
      5 or more contributor license agreements.  See the NOTICE file
      6 distributed with this work for additional information
      7 regarding copyright ownership.  The ASF licenses this file
      8 to you under the Apache License, Version 2.0 (the
      9 "License"); you may not use this file except in compliance
     10 with the License.  You may obtain a copy of the License at
     11 
     12     http://www.apache.org/licenses/LICENSE-2.0
     13 
     14 Unless required by applicable law or agreed to in writing,
     15 software distributed under the License is distributed on an
     16 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     17 KIND, either express or implied.  See the License for the
     18 specific language governing permissions and limitations
     19 under the License.
     20 -->
     21 
     22 <!-- START SNIPPET: superpom -->
     23 <project>
     24   <modelVersion>4.0.0</modelVersion>
     25 
     26   <repositories>
     27     <repository>
     28       <id>central</id>
     29       <name>Central Repository</name>
     30       <url>https://repo.maven.apache.org/maven2</url>
     31       <layout>default</layout>
     32       <snapshots>
     33         <enabled>false</enabled>
     34       </snapshots>
     35     </repository>
     36   </repositories>
     37 
     38   <pluginRepositories>
     39     <pluginRepository>
     40       <id>central</id>
     41       <name>Central Repository</name>
     42       <url>https://repo.maven.apache.org/maven2</url>
     43       <layout>default</layout>
     44       <snapshots>
     45         <enabled>false</enabled>
     46       </snapshots>
     47       <releases>
     48         <updatePolicy>never</updatePolicy>
     49       </releases>
     50     </pluginRepository>
     51   </pluginRepositories>
     52 
     53   <build>
     54     <directory>${project.basedir}/target</directory>
     55     <outputDirectory>${project.build.directory}/classes</outputDirectory>
     56     <finalName>${project.artifactId}-${project.version}</finalName>
     57     <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
     58     <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
     59     <scriptSourceDirectory>${project.basedir}/src/main/scripts</scriptSourceDirectory>
     60     <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
     61     <resources>
     62       <resource>
     63         <directory>${project.basedir}/src/main/resources</directory>
     64       </resource>
     65     </resources>
     66     <testResources>
     67       <testResource>
     68         <directory>${project.basedir}/src/test/resources</directory>
     69       </testResource>
     70     </testResources>
     71     <pluginManagement>
     72       <!-- NOTE: These plugins will be removed from future versions of the super POM -->
     73       <!-- They are kept for the moment as they are very unlikely to conflict with lifecycle mappings (MNG-4453) -->
     74       <plugins>
     75         <plugin>
     76           <artifactId>maven-antrun-plugin</artifactId>
     77           <version>1.3</version>
     78         </plugin>
     79         <plugin>
     80           <artifactId>maven-assembly-plugin</artifactId>
     81           <version>2.2-beta-5</version>
     82         </plugin>
     83         <plugin>
     84           <artifactId>maven-dependency-plugin</artifactId>
     85           <version>2.8</version>
     86         </plugin>
     87         <plugin>
     88           <artifactId>maven-release-plugin</artifactId>
     89           <version>2.3.2</version>
     90         </plugin>
     91       </plugins>
     92     </pluginManagement>
     93   </build>
     94 
     95   <reporting>
     96     <outputDirectory>${project.build.directory}/site</outputDirectory>
     97   </reporting>
     98 
     99   <profiles>
    100     <!-- NOTE: The release profile will be removed from future versions of the super POM -->
    101     <profile>
    102       <id>release-profile</id>
    103 
    104       <activation>
    105         <property>
    106           <name>performRelease</name>
    107           <value>true</value>
    108         </property>
    109       </activation>
    110 
    111       <build>
    112         <plugins>
    113           <plugin>
    114             <inherited>true</inherited>
    115             <artifactId>maven-source-plugin</artifactId>
    116             <executions>
    117               <execution>
    118                 <id>attach-sources</id>
    119                 <goals>
    120                   <goal>jar</goal>
    121                 </goals>
    122               </execution>
    123             </executions>
    124           </plugin>
    125           <plugin>
    126             <inherited>true</inherited>
    127             <artifactId>maven-javadoc-plugin</artifactId>
    128             <executions>
    129               <execution>
    130                 <id>attach-javadocs</id>
    131                 <goals>
    132                   <goal>jar</goal>
    133                 </goals>
    134               </execution>
    135             </executions>
    136           </plugin>
    137           <plugin>
    138             <inherited>true</inherited>
    139             <artifactId>maven-deploy-plugin</artifactId>
    140             <configuration>
    141               <updateReleaseInfo>true</updateReleaseInfo>
    142             </configuration>
    143           </plugin>
    144         </plugins>
    145       </build>
    146     </profile>
    147   </profiles>
    148 
    149 </project>
    150 <!-- END SNIPPET: superpom -->
    View Code

    5 Maven pom.xml结构

     1 <project xmlns="http://maven.apache.org/POM/4.0.0"
     2         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
     4                             http://maven.apache.org/xsd/maven-4.0.0.xsd">
     5     <modelVersion>4.0.0</modelVersion>
     6 
     7     <!-- The Basics -->
     8     <groupId>...</groupId>
     9     <artifactId>...</artifactId>
    10     <version>...</version>
    11     <packaging>...</packaging>
    12     <dependencies>...</dependencies>
    13     <parent>...</parent>
    14     <dependencyManagement>...</dependencyManagement>
    15     <modules>...</modules>
    16     <properties>...</properties>
    17 
    18     <!-- Build Settings -->
    19     <build>...</build>
    20     <reporting>...</reporting>
    21 
    22     <!-- More Project Information -->
    23     <name>...</name>
    24     <description>...</description>
    25     <url>...</url>
    26     <inceptionYear>...</inceptionYear>
    27     <licenses>...</licenses>
    28     <organization>...</organization>
    29     <developers>...</developers>
    30     <contributors>...</contributors>
    31 
    32     <!-- Environment Settings -->
    33     <issueManagement>...</issueManagement>
    34     <ciManagement>...</ciManagement>
    35     <mailingLists>...</mailingLists>
    36     <scm>...</scm>
    37     <prerequisites>...</prerequisites>
    38     <repositories>...</repositories>
    39     <pluginRepositories>...</pluginRepositories>
    40     <distributionManagement>...</distributionManagement>
    41     <profiles>...</profiles>
    42 </project>
    View Code

    参考文献:http://blog.csdn.net/tomato__/article/details/13168191

    6 其他

    maven跳过测试进行编译示例:mvn -DskipTests=true install assembly:single

    高版本的maven要求jdk为1.7或later,对低版本的jdk会报错。

    版本要求:

    maven 3.3要去至少 jdk 1.7

    maven 3.2要去至少jdk 1.6

    maven 3.1要求至少jdk 1.5

    maven 3.0要求至少jdk 1.5

    参考资料:

    1、《Maven实战》

  • 相关阅读:
    CodeForces
    CodeForces
    CodeForces 718C && HDU 3572 && Constellation
    CodeForces
    USACO 5.4 tour的dp解法
    10.22~10.28一周经典题目整理(meeting,BZOJ4377,POJ3659)
    codeforces 724D
    codeforces 724C
    hdu5909 Tree Cutting
    hdu5822 color
  • 原文地址:https://www.cnblogs.com/z-sm/p/5262074.html
Copyright © 2011-2022 走看看