SpringBoot Maven打包根据环境排除文件

需求背景

最近在项目上有个需求,要求把生产环境中的某些文件下掉,测试环境要保留,文件不能删,所以就在打包的时候做处理,项目中是jsp文件,废话不多说

项目结构

SpringBoot 2.6.1、JDK 1.8

SpringBoot Maven打包根据环境排除文件_第1张图片

  • dev:开发环境
  • lst:测试环境
  • procloud:生产环境

项目pom.xml文件配置(maven)

< build> 标签

SpringBoot Maven打包根据环境排除文件_第2张图片

< profiles>标签(多环境需要)

profiles 下的子标签 profile,每个子标签对应一个环境

测试环境打包保留index.jsp

SpringBoot Maven打包根据环境排除文件_第3张图片

生产环境打包排除index.jsp

SpringBoot Maven打包根据环境排除文件_第4张图片

最终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">
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.1.6.RELEASEversion>
        <relativePath /> 
    parent>

    <modelVersion>4.0.0modelVersion>

    <artifactId>iss-managerartifactId>
    <name>iss-managername>

    <dependencies>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>jstlartifactId>
        dependency>

        
        <dependency>
            <groupId>org.apache.tomcat.embedgroupId>
            <artifactId>tomcat-embed-jasperartifactId>
            
        dependency>
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>javax.servlet-apiartifactId>
        dependency>
        <dependency>
            <groupId>javax.servlet.jspgroupId>
            <artifactId>javax.servlet.jsp-apiartifactId>
            <version>2.3.1version>
        dependency>
    dependencies>

    <build>
        <finalName>iss-managerfinalName>
        <resources>
            <resource>
                <directory>src/main/resources/directory>
                <filtering>truefiltering>
                <includes>
                    <include>**/*.propertiesinclude>
                    <include>**/*.xmlinclude>
                    <include>**/*.yamlinclude>
                includes>
            resource>
            <resource>
                <directory>src/main/resources/directory>
                <filtering>falsefiltering>
                <includes>
                    <include>static/include>
                    <include>templates/include>
                includes>
            resource>
        resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <version>1.4.2.RELEASEversion>
                <configuration>
                    <executable>trueexecutable>
                configuration>
            plugin>
            <plugin>
                <artifactId>maven-resources-pluginartifactId>
                <configuration>
                    <encoding>utf-8encoding>
                    <useDefaultDelimiters>trueuseDefaultDelimiters>
                configuration>
            plugin>
        plugins>
    build>

    <profiles>
        <profile>
            
            <id>lstid>
            <properties>
                <profileActive>lstprofileActive>
            properties>

            <build>
                
                <resources>
                    <resource>
                        <directory>src/main/webappdirectory>
                        <includes>
                            <include>**/*.*include>
                        includes>
                        <filtering>truefiltering>
                        <targetPath>META-INF/resourcestargetPath>
                        <excludes>
                            <exclude>**/*.woffexclude>
                            <exclude>**/*.woff2exclude>
                            <exclude>**/*.ttfexclude>
                            <exclude>**/*.eotexclude>
                            <exclude>**/*.svgexclude>
                            <exclude>**/*.docxexclude>
                        excludes>
                    resource>
                    <resource>
                        <directory>src/main/webappdirectory>
                        <includes>
                            <include>**/*.*include>
                            <include>**/*.woffinclude>
                            <include>**/*.woff2include>
                            <include>**/*.ttfinclude>
                            <include>**/*.eotinclude>
                            <include>**/*.svginclude>
                        includes>
                        <filtering>falsefiltering>
                        <targetPath>META-INF/resourcestargetPath>
                    resource>
                resources>
            build>
        profile>

        <profile>
            
            <id>procloudid>
            <properties>
                <profileActive>procloudprofileActive>
            properties>
            <build>
                
                <resources>
                    <resource>
                        <directory>src/main/webappdirectory>
                        <includes>
                            <include>**/*.*include>
                        includes>
                        <filtering>truefiltering>
                        <targetPath>META-INF/resourcestargetPath>
                        <excludes>
                            <exclude>**/*.woffexclude>
                            <exclude>**/*.woff2exclude>
                            <exclude>**/*.ttfexclude>
                            <exclude>**/*.eotexclude>
                            <exclude>**/*.svgexclude>
                            <exclude>**/*.docxexclude>
                            
                            <exclude>**/index.jspexclude>
                        excludes>
                    resource>
                    <resource>
                        <directory>src/main/webappdirectory>
                        <includes>
                            <include>**/*.*include>
                            <include>**/*.woffinclude>
                            <include>**/*.woff2include>
                            <include>**/*.ttfinclude>
                            <include>**/*.eotinclude>
                            <include>**/*.svginclude>
                        includes>
                        <filtering>falsefiltering>
                        <targetPath>META-INF/resourcestargetPath>
                        <excludes>
                            
                            <exclude>**/index.jspexclude>
                        excludes>
                    resource>
                resources>
            build>
        profile>
    profiles>
project>

最后

本人新手,主要用于记录问题,最终是实现了,希望有更好的大佬方案,学习一下!感谢

不积跬步,无以至千里。

你可能感兴趣的:(spring,boot,maven,java)