`

Maven之POM之<build>标签

阅读更多
pom.xml中build标签

pom.xml 中的 build 标签是用于配置 pom 的,
相当于 pom 的 configuration,主要用于:

1、定义(或声明)项目的目录结构
2、使用maven的插件(maven plugins)。



1、分类
根据 POM 4.00 XSD,build 元素可以划分为两种级别的构建:
        1、基本构建:BaseBuild
        2、正常构建:Build
            正常构建包含基本构建,除此之外,还包含其它构建元素。

正常构建:Build 标签也分为两种:
        1、project build
                针对整个项目的所有情况都有效。该配置可以被 profile 全部继承。
        2、profile build
                用于重写覆盖掉 project build 中的配置。
                是 project build 的子集。


<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" 除了包含 BaseBuild 集合外,还包含其它构建元素 -–> 
  <build>…</build>  

  <!–- "Profile Build" 是 "Project Build"s 的子集 -–> 
  <profiles>  
    <profile>
      <build>…</build>  
    </profile>  
  </profiles>  
</project>  




2、配置说明

       (1)基本元素:BaseBuild
<build>  
        <defaultGoal>install</defaultGoal>  
        <directory>${basedir}/target</directory>  
        <finalName>${artifactId}-${version}</finalName>  
        <filters>  
                <filter>filters/filter1.properties</filter>  
        </filters>  
         ...  
</build>  


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


        (2)Resources配置 - 1
                 用于包含或者排除某些资源文件。
                 说明:资源通常不是源代码(也可以是),它们一般不被编译,例如:.xml 文件, .properties文件等。
<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时使用


        (2)Resources配置 - 2
<resources>
   <!-- 
     | 有几个路径,就对应几个 resource 标签 
     | 或:
     | 一个目录,对应一个 resource 标签
   -->
   <resource>
       <directory>
           ${basedir}/src/main/content/META-INF
       </directory>
       <targetPath>../vault-work/META-INF</targetPath>
       <filtering>true</filtering>
   </resource>
   <resource>
       <directory>
           ${basedir}/src/main/content/jcr_root
       </directory>
       <excludes>
           <!-- 用法1:不包括一整个目录-->
           <exclude>apps/ui/**</exclude>  

           <!-- 用法2:不包括某类文件(所有路径下)-->  
           <exclude>**/*.jpg</exclude>

           <!-- 用法3:不包括某个文件(所有路径下)-->
           <exclude>**/.DS_Store</exclude>
       </excludes>
       <targetPath>.</targetPath>
       <filtering>false</filtering>
   </resource>
</resources>



        (3)plugins配置
                  用于指定使用的插件
<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)GAV (groupId, artifactId, version)
                      指定插件的标准坐标
                2)extensions
                      是否加载plugin的extensions,默认为false
                3)inherited
                      true/false,这个plugin是否应用到该pom的孩子pom,默认为true
                4)configuration
                      配置该plugin期望得到的properties
                5)dependencies
                      作为plugin的依赖
                6)executions
                      plugin可以有多个目标,每一个目标都可以有一个分开的配置,可以将一个plugin绑定到不同的阶段
                      假如绑定antrun:run目标到verify阶段

<build>  
    <plugins>  
        <plugin>  
            <artifactId>maven-antrun-plugin</artifactId>  
            <version>1.1</version>  
            <executions>  
                <execution>  
                    <id>echodir</id>  
                    <goals>  
                        <goal>run</goal>  
                    </goals>  
                    <phase>verify</phase>  
                    <inherited>false</inherited>  
                    <configuration>  
                        <tasks>  
                            <echo>Build Dir: ${project.build.directory}</echo>  
                        </tasks>  
                    </configuration>  
                </execution>  
            </executions>  
        </plugin>  
    </plugins>  
</build>  


                          id:标识,用于和其他 execution 区分。
                              当这个阶段执行时,它将以这个形式展示[plugin:goal execution:id]。
                              举例:在这里为: [antrun:run execution:echodir]
                          goals:目标列表
                          phase:目标执行的阶段
                          inherit:子类pom是否继承
                          configuration:在指定目标下的配置

        (4)pluginManagement配置
                   pluginManagement的配置和plugins的配置是一样的,只是用于继承,使得可以在孩子pom中使用。
                   父pom:
<build>  
    ...  
    <pluginManagement>  
        <plugins>  
            <plugin>  
              <groupId>org.apache.maven.plugins</groupId>  
              <artifactId>maven-jar-plugin</artifactId>  
              <version>2.2</version>  
                <executions>  
                    <execution>  
                        <id>pre-process-classes</id>  
                        <phase>compile</phase>  
                        <goals>  
                            <goal>jar</goal>  
                        </goals>  
                        <configuration>  
                            <classifier>pre-process</classifier>  
                        </configuration>  
                    </execution>  
                </executions>  
            </plugin>  
        </plugins>  
    </pluginManagement>  
    ...  
</build>  


则在子pom中,我们只需要配置:
<build>  
    ...  
    <plugins>  
        <plugin>  
            <groupId>org.apache.maven.plugins</groupId>  
            <artifactId>maven-jar-plugin</artifactId>  
        </plugin>  
    </plugins>  
    ...  
</build>  


这样就大大简化了孩子pom的配置




-
转载请注明,
原文出处:http://lixh1986.iteye.com/blog/2382352






引用:
http://blog.csdn.net/cpf2016/article/details/45674377





-
分享到:
评论

相关推荐

    Maven build之pom.xml文件中的Build配置

    本文主要阐述了maven build是用来干什么的,以及对build标签中各子元素配置的作用

    springboot 基础简易实例, maven项目

    &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt; &lt;/plugin&gt; &lt;/plugins&gt; &lt;/build&gt; &lt;/project&gt; --------------------------- src/main/resources/application.yml --------------------------- spring: # 指定...

    maven 配置注意事项

    &lt;localRepository&gt;D:\Program Files\apache-maven-3.3.3\repository&lt;/localRepository&gt; &lt;server&gt;  &lt;id&gt;tomcat7&lt;/id&gt;  &lt;username&gt;admin&lt;/username&gt;  &lt;password&gt;admin&lt;/password&gt; &lt;/server&gt; 3.配置jdk环境(注意是...

    使用Maven管理进行多模块开发案例

    &lt;/modules&gt; packaging节点只能指定为pom,modules节点说明由几个模块组合,上面是把我们经常使用的架构分层模式分成一个个组件进行开发dao-&gt;service-&gt;web层。此pom文档经常还被用来进行一些依赖管理和插件管理,...

    打jar包注意点.docx

    1、先在pom.xml文件中加入&lt;build&gt;&lt;/build&gt;标签 &lt;build&gt; &lt;plugins&gt; &lt;plugin&gt; &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt; &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt; &lt;version&gt;2.3.1&lt;/version&gt; ...

    ssm黑马旅游整合最终版2018

    &lt;project 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"&gt; &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt; ...

    archetype-catalog(2020最新).zip

    将archetype-catalog文件放在maven仓库根目录下,VM Options 配置 -DarchetypeCatalog=internal,但是可能出现项目无法加载Resource文件下的配置文件,需要在pom.xml文件的&lt;build&gt;&lt;/build&gt;标签内添加&lt;resources&gt; ...

    spring和mybatis结合的maven工程

    &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt; &lt;/properties&gt; &lt;dependencies&gt; &lt;!-- 使用spring-core --&gt; &lt;dependency&gt; &lt;groupId&gt;org.springframework&lt;/groupId&gt; &lt;artifactId&gt;...

    elasticsearch学习demo

    &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt; &lt;configuration&gt; &lt;source&gt;1.8&lt;/source&gt; &lt;target&gt;1.8&lt;/target&gt; &lt;/configuration&gt; &lt;/plugin&gt; &lt;/plugins&gt; &lt;/build&gt; &lt;properties&gt; &lt;elasticsearch.version&gt;...

    autoconfig-demo.zip

    手动实现springboot 2.2.5 启动自动装配演示代码 &lt;?xml version="1.0" encoding="UTF-8"?... &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt; &lt;/plugin&gt; &lt;/plugins&gt; &lt;/build&gt; &lt;/project&gt;

    基于MyEclipse搭建maven+springmvc整合图文教程(含源码0

     &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;  &lt;version&gt;2.0.2&lt;/version&gt;  &lt;configuration&gt;  &lt;source&gt;1.5&lt;/source&gt;  &lt;target&gt;1.5&lt;/target&gt;  &lt;/configuration&gt;  &lt;/plugin&gt;  &lt;/plugins&gt;  &lt;/build&gt; 添加...

    hadoop mapreduce wordcount

    &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt; &lt;/properties&gt; &lt;dependencies&gt; &lt;dependency&gt; &lt;groupId&gt;org.apache.hadoop&lt;/groupId&gt; &lt;artifactId&gt;hadoop-common&lt;/artifactId&gt; ...

    Maven 权威指南 非扫描版

    使用 如果Eclipse 安装了Maven插件,选 择pom.xml文件,击右键——&gt;选择 Run As——&gt; Maven build。 三、在eclipse上安装maven插件 1、在线安装 略。 2、离线安装 下载maven的eclipse插件包 解压到eclipse...

    SSM框架整合

    &lt;project 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"&gt; &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt; ...

    t淘淘商城项目 商城项目 视频和源码教程 详细

    &lt;artifactId&gt;maven-resources-plugin&lt;/artifactId&gt; &lt;version&gt;2.7&lt;/version&gt; &lt;configuration&gt; &lt;encoding&gt;UTF-8&lt;/encoding&gt; &lt;/configuration&gt; &lt;/plugin&gt; &lt;!-- java编译插件 --&gt; &lt;plugin&gt; &lt;groupId&gt;org....

    maven的优缺点 项目

    &lt;artifactId&gt;maven-resources-plugin&lt;/artifactId&gt; &lt;configuration&gt; &lt;encoding&gt;UTF-8&lt;/encoding&gt; &lt;/configuration&gt; &lt;/plugin&gt; &lt;/plugins&gt; &lt;/build&gt; &lt;/project&gt; 3.6.生命周期 Maven -build...

    incr-build-plugin:Maven的增量构建

    Maven的增量构建 如果未检测到更改,此Maven扩展可以跳过模块的整个执行。 重要限制和假设 需要Maven 3.1.2+ ... &lt; groupId&gt;be.waines.maven&lt;/ groupId&gt; &lt; artifactId&gt;incremental-build&lt;/ a

    springboot编程训练系统设计与实现.zip

    对于Maven,将以下内容添加到pom.xml文件中: xml复制代码运行&lt;parent&gt; &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt; &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt; &lt;version&gt;2.5.4&lt;/version&gt; &lt;relative...

    flatten-maven-plugin:扁平化Maven插件

    &lt;build&gt; &lt;plugins&gt; &lt;plugin&gt; &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt; &lt;artifactId&gt;flatten-maven-plugin&lt;/artifactId&gt; &lt;!--&lt;version&gt;INSERT LATEST VERSION HERE&lt;/version&gt;--&gt; &lt;executions&gt; &lt;execution...

    deb-maven-plugin:一个用于创建deb包的maven插件

    Deb Maven插件 Maven插件,用于创建.deb程序包,创建控制文件,版权文件以及可复制地复制运行时依赖项。...&lt; build&gt; &lt; plugins&gt; &lt; plugin&gt; &lt; groupId&gt;io.solit.maven&lt;/ groupId&gt; &lt; artifactId&gt;deb-

Global site tag (gtag.js) - Google Analytics