Maven pom文件基本结构二(maven build)

 

一.分类

在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>。

二. 配置说明

1.基本元素

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

执行build任务时,如果没有指定目标,将使用的默认值。

如上配置:在命令行中执行mvn,则相当于执行mvn install

directorybuild目标文件的存放目录,默认在${basedir}/target目录
finalNamebuild目标文件的名称,默认情况为${artifactId}-${version}
filter

定义*.properties文件,包含一个properties列表,该列表会应用到支持filter的resources中。

也就是说,定义在filter的文件中的name=value键值对,会在build时代替${name}值应用到resources中。

maven的默认filter文件夹为${basedir}/src/main/filters

Maven多环境配置 filter

在开发过程中我们不可避免的会遇到需要在多种环境下进行部署,比如开发,测试,生产每个阶段都需要对应使用不同的配置(数据库,日志等等信息)。

而有了filter我们就可以根据不同环境匹配传入相应的参数,更灵活直观的使用参数,可以大大提升我们的效率。

首先需要在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}是一个变量,表示当前使用的环境。

我们这里有两种方式为${env}这个变量传入值

1)通过maven命令直接传入

-D代表(Properties属性)

mvn package -D env=dev

这里我们是使用-D 这个命令告诉env这个变量传入的参数是dev

2)通过profile定义传入

这是通过在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>prod</id> 
    <properties> 
      <env>prod</env> 
    </properties> 
  </profile> 
</profiles>

使用下面的命令P代表(Profiles配置文件) 

mvn package -P test

在<profiles>指定的<id>中,可以通过-P进行传递或者赋值。

其中properties里面表示默认的变量值,使用maven命令不设定-P时默认使用这个dev。

在测试和部署上线时分别通过maven命令-P传入当前的profile id,这样maven就会将env变量设置为对应的值,从而导致使用不同的filter文件来对resources下的文件进行过滤替换。

例如:当调用maven package时传入-Pdev(因为我们将dev设置为默认,所以也可以不传)参数,则会使用filter-dev.properties中的内容来替换resources目录中的配置文件,具体到我们的项目就是db.properties,内容如下:

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

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

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

 这样在编译结束后

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

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>  
resources一个resources元素的列表。每一个都描述与项目关联的文件是什么和在哪里
targetPath

指定build后的resource存放的文件夹,默认是basedir。

通常被打包在jar中的resources的目标路径是META-INF

filteringtrue/false,表示为这个resource,filter是否激活
directory定义resource文件所在的文件夹,默认为${basedir}/src/main/resources
includes指定哪些文件将被匹配,以*作为通配符
excludes指定哪些文件将被忽略
testResources定义和resource类似,只不过在test时使用

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>

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Maven POM (Project Object Model) 文件Maven 项目的核心配置文件,其中包含了项目中所有的依赖、插件、构建配置等信息。在 Maven 打包过程中,POM 文件的正确配置非常重要。以下是一个基本Maven 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.example</groupId> <artifactId>my-app</artifactId> <version>1.0-SNAPSHOT</version> <!-- 依赖声明 --> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> <!-- 插件声明 --> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.1.2</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <mainClass>com.example.App</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build> </project> ``` 在上述 POM 文件中,`<groupId>`、`<artifactId>` 和 `<version>` 标签定义了项目的基本信息。`<dependencies>` 标签用于声明项目所依赖的其他库,`<build>` 标签下的 `<plugins>` 标签用于声明项目构建时所需要的插件。 在打包过程中,可以使用以下命令: ``` mvn package ``` 该命令会在当前项目根目录下生成一个名为 `my-app-1.0-SNAPSHOT.jar` 的可执行 JAR 文件,其中包含了项目的所有依赖和代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值