美文网首页
maven filter/profile

maven filter/profile

作者: 艾比ab | 来源:发表于2020-03-19 17:02 被阅读0次

    maven 命令:clean package -Dmaven.test.skip=true -P deta

    1.命令很简单是:清class文件,打包构建,跳过测试,注意最后一个 -P product,-P maven 会激活项目下的pom.xml配置的<profiles>标签下id为deta

    <profiles>
        <profile>
          <!-- 本地开发环境 -->
          <id>dev</id>
          <properties>
            <profiles.active>dev</profiles.active>
          </properties>
          <activation>
            <!-- 设置默认激活这个配置 -->
            <activeByDefault>true</activeByDefault>
          </activation>
        </profile>
        <profile>
          <!-- 发布环境 -->
          <id>release</id>
          <properties>
            <profiles.active>release</profiles.active>
          </properties>
        </profile>
        <profile>
          <!-- 测试环境 -->
          <id>beta</id>
          <properties>
            <profiles.active>beta</profiles.active>
          </properties>
        </profile>
      </profiles>
    

    2.使用占位符上面配置定义的${profiles.active}进行资源过滤

    <build>
        <resources>
          <resource><!--不参与打包和替换的资源文件-->
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
              <include>**/*.yml</include>
              <include>**/*.yaml</include>
              <include>**/*.properties</include>
              <include>**/*.xml</include>
            </includes>
          </resource>
          <resource><!--不参与打包和替换的资源文件-->
            <directory>src/main/resources</directory>
            <filtering>false</filtering>
            <excludes>
              <exclude>**/*.pdf</exclude>
              <exclude>**/*.txt</exclude>
            </excludes>
          </resource>
        </resources>
        <filters>
          <filter>src/main/resources/config/${profiles.active}/log-profile.properties</filter>
        </filters>
    

    需要自定义配置filter插件的分隔符${}。否则默认会使用@作为分隔符。

    <plugin><!-- 需要自定义 delimiter-->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.6</version>
            <configuration>
              <encoding>UTF-8</encoding>
              <delimiters>
                <delimiter>${*}</delimiter>
              </delimiters>
              <useDefaultDelimiters>false</useDefaultDelimiters>
            </configuration>
            </plugin>
    

    总结
    激活profile其实就是激活<profile>标签的id,给properties的属性赋值。
    通过filters标签的源文件,对sources 的 input文件集进行内容替换。
    注意:需要自定义分隔符。

    整个pom配置参考

    <?xml version="1.0" encoding="UTF-8"?>
    
    <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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
      </parent>
    
      <groupId>com.aibi</groupId>
      <artifactId>springboot-demo</artifactId>
      <version>1.0-SNAPSHOT</version>
      <name>springboot-demo</name>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
        </dependency>
    <!--    <dependency>-->
    <!--      <groupId>junit</groupId>-->
    <!--      <artifactId>junit</artifactId>-->
    <!--      <version>4.11</version>-->
    <!--      <scope>test</scope>-->
    <!--    </dependency>-->
      </dependencies>
    
      <profiles>
        <profile>
          <!-- 本地开发环境 -->
          <id>dev</id>
          <properties>
            <profiles.active>dev</profiles.active>
          </properties>
          <activation>
            <!-- 设置默认激活这个配置 -->
            <activeByDefault>true</activeByDefault>
          </activation>
        </profile>
        <profile>
          <!-- 发布环境 -->
          <id>release</id>
          <properties>
            <profiles.active>release</profiles.active>
          </properties>
        </profile>
        <profile>
          <!-- 测试环境 -->
          <id>beta</id>
          <properties>
            <profiles.active>beta</profiles.active>
          </properties>
        </profile>
      </profiles>
    
    
      <build>
        <resources>
          <resource><!--不参与打包和替换的资源文件-->
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
              <include>**/*.yml</include>
              <include>**/*.yaml</include>
              <include>**/*.properties</include>
              <include>**/*.xml</include>
            </includes>
          </resource>
          <resource><!--不参与打包和替换的资源文件-->
            <directory>src/main/resources</directory>
            <filtering>false</filtering>
            <excludes>
              <exclude>**/*.pdf</exclude>
              <exclude>**/*.txt</exclude>
            </excludes>
          </resource>
        </resources>
        <filters>
          <filter>src/main/resources/config/${profiles.active}/log-profile.properties</filter>
        </filters>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
          <plugins>
            <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
    
            <plugin><!-- 需要自定义 delimiter-->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.6</version>
            <configuration>
              <encoding>UTF-8</encoding>
              <delimiters>
                <delimiter>${*}</delimiter>
              </delimiters>
              <useDefaultDelimiters>false</useDefaultDelimiters>
            </configuration>
            </plugin>
    
            <plugin>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
          </plugins>
        </pluginManagement>
      </build>
    </project>
    
    

    总结

    只替换,不重新编译技巧
    最后,排查中还有一个小技巧,不要用mvn install或者mvn package这种操作来重新运行,因为会很慢很耗时,直接用mvn clean resources:resources就可以了,它只复制&替换配置目录下的文件,非常地快

    参考资料
    https://blog.csdn.net/haiyuntai/article/details/53260191

    https://blog.csdn.net/weixin_34259232/article/details/91757071

    相关文章

      网友评论

          本文标题:maven filter/profile

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