美文网首页
Maven用法整理

Maven用法整理

作者: 菊地尤里 | 来源:发表于2019-07-18 15:43 被阅读0次

1.继承

当被继承项目与继承项目的目录结构是父子关系的时候,写法如下

<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">  
  <parent>  
    <groupId>com.tiantian.mavenTest</groupId>  
    <artifactId>projectA</artifactId>  
    <version>1.0-SNAPSHOT</version>  
  </parent>  
  <modelVersion>4.0.0</modelVersion>  
  <groupId>com.tiantian.mavenTest</groupId>  
  <artifactId>projectB</artifactId>  
  <packaging>jar</packaging>  
  <version>1.0-SNAPSHOT</version>  
</project>  

被继承项目与继承项目的目录结构不是父子关系的时候
我们就需要在子项目的pom.xml文件定义中的parent元素下再加上一个relativePath元素的定义,用以描述父项目的pom.xml文件相对于子项目的pom.xml文件的位置

<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">  
  <parent>  
    <groupId>com.tiantian.mavenTest</groupId>  
    <artifactId>projectA</artifactId>  
    <version>1.0-SNAPSHOT</version>  
    <relativePath>../projectA/pom.xml</relativePath>  
  </parent>  
  <modelVersion>4.0.0</modelVersion>  
  <groupId>com.tiantian.mavenTest</groupId>  
  <artifactId>projectB</artifactId>  
  <packaging>jar</packaging>  
  <version>1.0-SNAPSHOT</version>  
</project>  

2.聚合

1.修改被聚合项目的pom.xml中的packaging元素的值为pom

  1. 在被聚合项目的pom.xml中的modules元素下指定它的子模块项目
<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/xsd/maven-4.0.0.xsd">  
  <modelVersion>4.0.0</modelVersion>  
  <groupId>com.tiantian.mavenTest</groupId>  
  <artifactId>projectA</artifactId>  
  <version>1.0-SNAPSHOT</version>  
  <packaging>pom</packaging>  
  <modules>  
       <module>projectB</module>  
  </modules>  
</project>  

3.聚合与继承同时进行

projectA的pom.xml应该这样定义

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/xsd/maven-4.0.0.xsd">  
  <modelVersion>4.0.0</modelVersion>  
  <groupId>com.tiantian.mavenTest</groupId>  
  <artifactId>projectA</artifactId>  
  <version>1.0-SNAPSHOT</version>  
  <packaging>pom</packaging>  
  <modules>  
       <module>../projectB</module>  
  </modules>  
</project>  

projectB的pom.xml应该这样定义

<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/xsd/maven-4.0.0.xsd">  
  <modelVersion>4.0.0</modelVersion>  
  <parent>  
       <groupId>com.tiantian.mavenTest</groupId>  
       <artifactId>projectA</artifactId>  
       <version>1.0-SNAPSHOT</version>  
       <relativePath>../projectA/pom.xml</relativePath>  
  </parent>  
  <groupId>com.tiantian.mavenTest</groupId>  
  <artifactId>projectB</artifactId>  
  <version>1.0-SNAPSHOT</version>  
  <packaging>jar</packaging>  
</project>  

4.依赖

在dependency元素中我们主要通过依赖项目的groupId、artifactId和version来定义所依赖的项目。

<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/xsd/maven-4.0.0.xsd">  
  <modelVersion>4.0.0</modelVersion>  
  <groupId>com.tiantian.mavenTest</groupId>  
  <artifactId>projectB</artifactId>  
  <version>1.0-SNAPSHOT</version>  
  <packaging>jar</packaging>  
   
  <dependencies>  
    <dependency>  
      <groupId>junit</groupId>  
      <artifactId>junit</artifactId>  
      <version>3.8.1</version>  
      <scope>test</scope>  
              <optional>true</optional>  
    </dependency>  
  </dependencies>  
</project>  

还可以指定以下元素

type:对应于依赖项目的packaging类型,默认是jar

scope:表示依赖项目的一个作用范围。scope的主要取值范围如下

compile:这是它的默认值,这种类型很容易让人产生误解,以为只有在编译的时候才是需要的,其实这种类型表示所有的情况都是有用的,包括编译和运行时。而且这种类型的依赖性是可以传递的

provided:这个跟compile很类似,但是它表示你期望这个依赖项目在运行时由JDK或者容器来提供。这种类型表示该依赖只有在测试和编译的情况下才有效,在运行时将由JDK或者容器提供。这种类型的依赖性是不可传递的。


runtime:这种类型表示该依赖在编译的时候不是必须的,只有在运行的时候才是必须的。


exclusions:考虑这样一种情况,我们的projectA依赖于projectB,然后projectB又依赖于projectC,但是在projectA里面我们不需要projectB依赖的projectC,那么这个时候我们就可以在依赖projectB的时候使用exclusions元素下面的exclusion排除projectC


system:这种类型跟provided类似,唯一不同的就是这种类型的依赖我们要自己提供jar包,这需要与另一个元素systemPath来结合使用。systemPath将指向我们系统上的jar包的路径,而且必须是给定的绝对路径。


systemPath:上面已经说过了这个元素是在scope的值为system的时候用于指定依赖的jar包在系统上的位置的,而且是绝对路径。该元素必须在依赖的 jar包的scope为system时才能使用,否则Maven将报错。


optional:当该项目本身作为其他项目的一个依赖时标记该依赖为可选项。假设现在projectA有一个依赖性projectB,我们把projectB这个依赖项设为optional,这表示projectB在projectA的运行时不一定会用到。这个时候如果我们有另一个项目projectC,它依赖于projectA,那么这个时候因为projectB对于projectA是可选的,所以Maven在建立projectC的时候就不会安装projectB,这个时候如果projectC确实需要使用到projectB,那么它就可以定义自己对projectB的依赖。当一个依赖是可选的时候,我们把optional元素的值设为true,否则就不设置optional元素。

<dependencies>  
     <dependency>  
            <groupId>com.tiantian.mavenTest</groupId>  
            <artifactId>projectB</artifactId>  
            <version>1.0-SNAPSHOT</version>  
            <exclusions>  
                   <exclusion>  
                          <groupId>com.tiantian.mavenTest</groupId>  
                          <artifactId>projectC</artifactId>  
                   </exclusion>  
            </exclusions>  
     </dependency>  
</dependencies>  

5.属性

在pom.xml文件中我们可以使用${propertyName}的形式引用属性

  <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

6.<DependencyManagement>标签

如果dependencies里面的dependency自己没有声明version元素,那么maven就会到dependencyManagement里面去找有没有对该artifactId和groupId进行过版本声明,如果有,就继承该版本并下载包。
如果没有就会报错,告诉我们必须为dependency声明一个version!

//只是对版本进行管理,不会实际引入jar  
<dependencyManagement>  
     <dependencies>  
           <dependency>  
               <groupId>org.springframework</groupId>  
               <artifactId>spring-core</artifactId>  
               <version>3.2.7</version>  
           </dependency>  
   </dependencies>  
</dependencyManagement>  
 
//会实际下载jar包  
<dependencies>  
      <dependency>  
               <groupId>org.springframework</groupId>  
               <artifactId>spring-core</artifactId>  
      </dependency>  
</dependencies>

pluginManagement用法类似

7.build标签

(1)全局配置(project build)
针对整个项目的所有情况都有效
(2)配置(profile build)
针对不同的profile配置

<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">
  …
  <!– "Project Build" contains more elements than just the BaseBuild set –>
  <build>…</build>
  <profiles>
    <profile>
      <!– "Profile Build" contains a subset of "Project Build"s elements –>
      <build>…</build>
    </profile>
  </profiles>
</project>

8.配置说明

<build>
        <defaultGoal>install</defaultGoal>
        <directory>${basedir}/target</directory>
        <finalName>${artifactId}-${version}</finalName>
        <filters>
                <filter>filters/filter1.properties</filter>
        </filters>
         ...
</build>

1)defaultGoal
执行build任务时,如果没有指定目标,将使用的默认值。如上配置:在命令行中执行mvn,则相当于执行mvn install
2)directory
build目标文件的存放目录,默认在{basedir}/target目录 3)finalName build目标文件的名称,默认情况为{artifactId}-{version} 4)filter 定义*.properties文件,包含一个properties列表,该列表会应用到支持filter的resources中。 也就是说,定义在filter的文件中的name=value键值对,会在build时代替{name}值应用到resources中。
maven的默认filter文件夹为${basedir}/src/main/filters

Resources配置

<build>
        ...
       <resources>
                  <resource>
                        <targetPath>META-INF/plexus</targetPath>
                        <filtering>false</filtering>
            <directory>${basedir}/src/main/plexus</directory>
            <includes>
                <include>configuration.xml</include>
            </includes>
            <excludes>
                <exclude>**/*.properties</exclude>
            </excludes>
         </resource>
    </resources>
    <testResources>
        ...
    </testResources>
    ...
</build>

1)resources
一个resources元素的列表。每一个都描述与项目关联的文件是什么和在哪里

2)targetPath
指定build后的resource存放的文件夹,默认是basedir。
通常被打包在jar中的resources的目标路径是META-INF
3)filtering
true/false,表示为这个resource,filter是否激活
4)directory
定义resource文件所在的文件夹,默认为${basedir}/src/main/resources
5)includes
指定哪些文件将被匹配,以*作为通配符
6)excludes
指定哪些文件将被忽略
7)testResources
定义和resource类似,只不过在test时使用

plugin配置

<build>
    ...
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.0</version>
            <extensions>false</extensions>
            <inherited>true</inherited>
            <configuration>
                <classifier>test</classifier>
            </configuration>
            <dependencies>...</dependencies>
            <executions>...</executions>
        </plugin>
    </plugins>
</build>

1)extensions
是否加载plugin的extensions,默认为false
2)inherited
true/false,这个plugin是否应用到该pom的孩子pom,默认为true

多环境配置

 <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <env>dev</env>
                <!--<redis_host>127.0.0.1</redis_host>-->
            </properties>
        </profile>
        <profile>
            <id>pre</id>
            <properties>
                <env>pre</env>
            </properties>
        </profile>
        <profile>
            <id>pro</id>
            <properties>
                <env>pro</env>
            </properties>
        </profile>
    </profiles>
        <!-- 使用指定的filter进行过滤,在执行mvn命令的时候带上-Ppro就代表生产环境,就会加载生产环境的properties,-Pdev就代表开发环境(默认) -->
        <filters>
            <filter>src/main/resources/properties/env/${env}.properties</filter>
        </filters>
        <!-- 资源文件位置src/main/resources/,这下面的资源文件的${}会全部被替换成filter中的标签内容。
                 directory指定的value会作为classes的资源跟目录,
                 比如指定:src/main/resources/,则classes下会出现jdbc等包,
                 若指定:src/main/resources/jdbc/,则classes下直接出现jdbc包下的文件,不会额外出现jdbc等其他包结构。因为他把jdbc作为了根目录
         -->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <!--
                      exclude可以排除指定文件,支持通配符 ,匹配项不会生成到classes目录下,路径是以directory开始的
                      在这里就是directory(src/main/resources/)/properties/env/pro.properties
                 -->
                <!--<excludes>-->
                <!--<exclude>properties/env/pro.properties</exclude>-->
                <!--</excludes>-->
            </resource>
        </resources>

打包命令:
mvn clean package -Pdev(-Ppre,-Ppro)

相关文章

网友评论

      本文标题:Maven用法整理

      本文链接:https://www.haomeiwen.com/subject/pwmllctx.html