转载

Maven学习7: 多环境构建

1. 灵活构建

1.1 三大特性

  • 属性
  • 资源过滤
  • Profile

1.2 属性

  1. 自定义属性

    • 用户可以在项目的pom文件的<properties>元素中自定义maven属性。
    • 在pom文件的其他地方,使用${属性名称}引用该属性。
    • 意义在于消除重复 ,示例代码如下。

      <project> <properties> <springframework.groupId>org.springframework</springframework.groupId> <springframework.version>5.2.1.RELEASE</springframework.version> </properties> <dependencies> <dependency> <groupId>${springframework.groupId}</groupId> <artifactId>spring-web</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>${springframework.groupId}</groupId> <artifactId>spring-aop</artifactId> <version>${springframework.version}</version> </dependency> </dependencies> </project>
  2. 内置属性

    两个常用的内置属性:

    ${basedir} ${version} 
  3. pom属性

    用户可以使用该类属性引用pom文件中对应元素的值。

    常用的pom属性如下:

    ${project.build.sourceDirectory} ${project.build.testSourceDirectory} ${project.build.directory} ${project.outputDirectory} ${project.testOutputDirectory} ${project.groupId} ${project.artifactId} ${project.version} ${project.build.finalName} 
  4. settings属性

    用户可以使用该类属性引用settings文件中对应元素的值。

    • ${settings.localRepository} :用户本地仓库
  5. Java系统属性

    mvn help:system ${user.home} 
  6. 环境变量属性

    mvn help:system ${env.JAVA_HOME} 

1.3 资源过滤

  1. 遇到的问题

    • 多环境配置:项目不同的环境对应着不同的数据库配置,手动更改这些配置往往比较低效。
  2. 解决

    1. 使用maven属性将这些会发生变化的部分提取出来。
    2. 然后开启资源目录的过滤功能,就可以解决这一问题。
  3. 示例

    • 数据库配置

      # src/main/resources/application.properties database.jdbc.driverClass=${db.driver} database.jdbc.connectionUrl=${db.url} database.jdbc.username=${db.user} database.jdbc.password=${db.pw}
    • 开启资源过滤

      <project> <properties> <db.driver>com.mysql.jdbc.Driver</db.driver> <db.url>jdbc:mysql//localhost:3306/test</db.url> <db.user>dev-user</db.user> <db.pw>dev-pwd</db.pw> </properties> <build> <resources> <resource> <directory>${project.basedir}/src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build> </project>
    • 编译代码mvn compile

      • 查看主代码编译输出目录下的资源文件,已经得到了替换。
      database.jdbc.driverClass=com.mysql.jdbc.Driver database.jdbc.connectionUrl=jdbc:mysql//localhost:3306/test database.jdbc.username=aaa database.jdbc.password=aaa-pwd

1.4 maven profile

  1. 针对不同环境的profile

    • 示例:基于开发和测试环境的profile

      <project> <profiles> <profile> <id>dev</id> <properties> <db.driver>com.mysql.jdbc.Driver</db.driver> <db.url>jdbc:mysql//localhost:3306/test</db.url> <db.user>dev</db.user> <db.pw>dev-pwd</db.pw> </properties> </profile> <profile> <id>test</id> <properties> <db.driver>com.mysql.jdbc.Driver</db.driver> <db.url>jdbc:mysql//localhost:3306/test</db.url> <db.user>test</db.user> <db.pw>test-pwd</db.pw> </properties> </profile> </profiles> </project>
  2. 激活profile

    1. 命令行激活

      • 使用maven命令行参数-P 加上profile的id ,多个id之间逗号分隔
      • 如:mvn clean compile -Pdev
    2. 默认激活

      • 配置profile > activation > activationByDefault 元素的值为true ,该profile默认激活

        <project> <profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <db.driver>com.mysql.jdbc.Driver</db.driver> <db.url>jdbc:mysql//localhost:3306/test</db.url> <db.user>dev</db.user> <db.pw>dev-pwd</db.pw> </properties> </profile> </profiles> </project>
    3. 系统属性激活

      • mvn ... -D属性xx=属性xx的值
      • 指定的属性存在,激活profile
      • 指定的属性存在,并且值等于x时,激活profile

        <project> <profiles> <profile> <id>dev</id> <activation> <property> <name>test</name> <value>x</value> </property> </activation> <properties> <db.driver>com.mysql.jdbc.Driver</db.driver> <db.url>jdbc:mysql//localhost:3306/test</db.url> <db.user>dev</db.user> <db.pw>dev-pwd</db.pw> </properties> </profile> </profiles> </project>
    4. 文件存在与否激活

      <project> <profiles> <profile> <id>dev</id> <activation> <file> <exists>x.properties</exists> <missing>y.properties</missing> </file> </activation> <properties> <db.driver>com.mysql.jdbc.Driver</db.driver> <db.url>jdbc:mysql//localhost:3306/test</db.url> <db.user>dev</db.user> <db.pw>dev-pwd</db.pw> </properties> </profile> </profiles> </project>
  3. 查看当前激活的profile

    • mvn help:active-profiles
  4. 列出所有的profile

    • mvn help:all-profiles
原文  https://segmentfault.com/a/1190000021491675
正文到此结束
Loading...