pom文件中build标签详解

2023-05-16

前言:

   <build >设置,主要用于编译设置
 

1.分类

在Maven的pom.xml文件中,存在如下两种<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">  
  ...  
  <!-- "Project Build" contains elements of the BaseBuild set and the Build set-->  
  <build>...</build>  
   
  <profiles>  
    <profile>  
      <!-- "Profile Build" contains elements of the BaseBuild set only -->  
      <build>...</build>  
    </profile>  
  </profiles>  
</project>    

 

 

说明:

一种<build>被称为Project Build,即是<project>的直接子元素。

另一种<build>被称为Profile Build,即是<profile>的直接子元素。

Profile Build包含了基本的build元素,而Project Build还包含两个特殊的元素,即各种<...Directory>和<extensions>。

2. 配置说明

1.基本元素

示例如下

复制代码


<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

 2. Resources配置

                 用于包含或者排除某些资源文件

    

 


<build>  
        ...  
       <resources>  
          <resource>  
             <targetPath>META-INF/plexus</targetPath>  
             <filtering>true</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时使用

 

  Maven多环境配置实战 filter

   目前在开发一个wap项目,主要有开发、测试和最终部署上线几个阶段,每个阶段对配置(数据库、日志)都有不同的设置。以前都是以开发环境为主,在测试和部署上线时由部署工程师负责修改配置并上线。但是公司并非都有一个项目,我们也不是只负责一个项目,这样的工作方式导致每每上线时大家都心惊胆颤,实在忍受不了折磨,决定研究下maven下如何解决这个问题。找到方案后,不敢独享,将结果向大家介绍下。思路: 
      几个环境中主要的不同可以概括为数据库配置和log日志路径配置以及外部依赖的接口配置不一样,但是我们这里简单起见,假设只考虑数据库配置。 
      这样的话,如果能实现在生成不同的发布包时对资源进行不同的替换就可以达到目的了。经过研究maven,确定了最终方案。最终方案: 
    首先需要在pom文件中确定filter和要filter的资源,这是通过在build节点中添加filter和resource来实现的,示例如下: 
  

 

 


<filters>
    <filter>src/main/filters/filter-${env}.properties</filter> 
    </filters> 
    <resources> 
      <resource> 
        <directory>src/main/resources</directory> 
        <filtering>true</filtering> 
      </resource>    
    </resources>   

 

 

  上述配置表示要对src/main/resources下的资源进行过滤,因为该目录下没有二进制文件,所以没有excluding。过滤时采用的过滤文件为src/main/filters/filter-${env}.properties文件,其中${env}是一个变量,表示当前使用的环境,这是通过在pom文件中通过profile定义的,如下所示: 

 


<properties>                                                                                       
  <env>dev</env> 
  </properties> 
  <profiles> 
  <profile> 
    <id>dev</id> 
    <properties> 
      <env>dev</env> 
    </properties> 
  </profile> 
  <profile> 
    <id>test</id> 
    <properties> 
      <env>test</env> 
    </properties> 
  </profile> 
  <profile> 
    <id>product</id> 
    <properties> 
      <env>product</env> 
    </properties> 
  </profile> 
</profiles>   

 

 


  其中斜体字部分表示缺省的变量值,这样在开发时就不用每次指定这个值。在测试和部署上线时分别通过-P传入当前的profile id,这样maven就会将env变量设置为对应的值,从而导致使用不同的filter文件来对resources下的文件进行过滤替换。 
  例如:当调用maven package时传入-Pdev(因为我们将dev设置为默认,所以也可以不传)参数,则会使用 
    filter-dev.properties中的内容来替换resources目录中的配置文件,具体到我们的项目就是db.properties,内容如下: 


   

   

....... jdbc.connection.url=${xiangmu.jdbc.url} jdbc.connection.username=${xiangmu.jdbc.username} jdbc.connection.password=${xiangmu.jdbc.password} ...............   

 filter-dev.properties文件内容如下: 


   

   

................ xiangmu.jdbc.url=jdbc:mysql://localhost:3306/xiangmu?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8 xiangmu.jdbc.username=root xiangmu.jdbc.password=abcdefg .................   

 这样在编译结束后 


   

   

db.properties的内容就会变为: jdbc.connection.url=jdbc:mysql://localhost:3306/xiangmu?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8 jdbc.connection.username=root jdbc.connection.password=abcdefg  

 

 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>    

 

  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的配置

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

pom文件中build标签详解 的相关文章

  • 是否有从 Maven 到 Bazel 的迁移路径?

    现在巴泽尔 http bazel io http bazel io 已经开源了 是否有一个增量过程可以让我逐渐从 Maven 迁移 一个大型存储库 到 Bazel 我在研究巴泽尔 不 据我们所知 没有这样的过程 我希望 我们一直在运行一些从
  • sbt (play!) 项目与 Maven 父 pom 的集成

    我有一个 Maven 项目 其中包含围绕父 pom 组织的多个 Maven 模块 所有这些模块都打包成 JAR 文件 这些文件是我的 Play 的依赖项 作为 SBT 项目构建的应用程序 MyProject gt pom xml paren
  • Android Studio 2.1.3 中构建错误

    我使用的是android studio 2 1 3 尽管清除了项目并再次重建 重新启动等 我还是收到以下错误 如何解决 错误 任务执行失败 app transformClassesWithJavaResourcesVerifierForDe
  • Visual Studio 2010环境变量

    我在 Windows 中设置了一个名为 SDK 的环境变量 我可以在 csproj 文件中以某种方式使用它吗 就像是
  • 生产准备 Ionic 应用程序的任务

    我想弄清楚是什么best从代码传递到最终可部署 apk ipa 的过程 到目前为止 我有一个使用 Karma Jasmine 的测试套件 它将 TypeScript 转换为 JS 并运行一些单元测试 我通过 gulp 开始这个过程 之后我所
  • Ant 复制文件而不覆盖

    Is there any command in ant to copy files from one folder structure to another without checking the last modified date t
  • Webpack 的 sass-loader 构建时间较慢

    Summary 当我们改用 Webpack 处理 SASS 文件时 我们注意到在某些情况下构建时间变得非常慢 使用以下方法测量构建的不同部分的性能后测速插件 https www npmjs com package speed measure
  • qmake 和 QT_INSTALL_PREFIX。如何为 Qt 库选择新位置?

    我是 qmake 的新手 我正在尝试构建一个现有的应用程序 Qt 最初安装在 usr local lib Qt 4 3 5 中 qmake query QT INSTALL PREFIX 返回该路径 我已将 Qt 库移动到另一个位置 生成的
  • ClassCastException:ApiVersionImpl 无法转换为 java.lang.Integer

    我有 android gradle 项目 当我尝试启动应用程序时出现以下异常 ClassCastException com android build gradle internal model ApiVersionImpl cannot
  • 致命错误:CALL_AND_RETRY_LAST 分配失败 - JavaScript 堆内存不足错误

    我运行时收到此错误ng 构建 prod 92 块资产优化 4136 0155D210 443646 ms 标记 扫描 703 5 770 3 gt 703 6 759 8 MB 2162 2 0 0 ms 自标记开始以来 0 0 ms 0
  • 停止 ant 脚本而不导致构建失败

    在我的 ant 脚本中 我想在满足条件时退出 停止执行构建 而不会失败 我尝试过使用
  • 颠覆和混合修订:破坏构建的秘诀?

    在使用 TFS 一段时间后 我刚刚回到 subversion 一般来说我已经很退出了 有一件事情我记得不一样 我不记得能够从过时的工作副本中提交 或者也许我的记忆力让我无法理解 过时 的定义 我认为 过时 意味着自从我上次更新工作副本以来
  • Maven:从构建中排除测试

    我在项目的 src test java 文件夹中有一些类用作测试 当我使用标准 Maven 编译插件运行 Maven 时 这些项目被编译成 class 文件 并包含在打包编译代码的 jar 中 在运行 Maven 和构建我的版本之前 我已经
  • 使用 Jenkins API 促进构建

    给定一个具有不同升级作业的 Jenkins 构建作业 即 将构建升级到不同的环境 如何使用 Jenkins API 触发特定构建的特定升级作业 综合不同来源的答案得出 Username Username APItoken 12345 Cre
  • 将构建参数传递给 .wxs 文件以动态构建 wix 安装程序

    我是一名学生开发人员 我已经为我现在工作的公司构建了几个安装程序 所以我对WIX还是比较熟悉的 我们最近决定拥有一个构建服务器来自动构建我们的解决方案 它构建调试和发布以及混淆 和非混淆 项目 你真的不需要理解这些 您需要了解的是 我有相同
  • 无法解决dll之间的冲突

    我在构建中收到类似于以下内容的警告墙 No way to resolve conflict between Newtonsoft Json Version 7 0 0 0 and Newtonsoft Json Version 6 0 0
  • CMake:如何最好地构建多个(可选)子项目?

    想象一个包含多个组件的整体项目 basic io web app a app b app c 现在 假设 web 依赖于 io 而 io 又依赖于 basic 所有这些东西都在一个存储库中 并且有一个 CMakeLists txt 将它们构
  • 为arm构建WebRTC

    我想为我的带有arm926ej s处理器的小机器构建webrtc 安装 depot tools 后 我执行了以下步骤 gclient config http webrtc googlecode com svn trunk gclient s
  • 使用 msbuild 复制所有文件和文件夹

    只是想知道是否有人可以帮助我编写一些我正在尝试编写的 msbuild 脚本 我想要做的是使用 msbuild 将所有文件和子文件夹从一个文件夹复制到另一个文件夹 ProjectName gt Source gt Tools gt Viewe
  • “make install”将库安装在 /usr/lib 而不是 /usr/lib64

    我正在尝试在 64 位 CentOS 7 2 上构建并安装一个库 为了这个目的我正在跑步 cmake DCMAKE BUILD TYPE Release DCMAKE INSTALL PREFIX usr DCMAKE C COMPILER

随机推荐