maven项目多环境打包问题

1.xxx-api是基于springboot的模块

2.配置文件

application.properties
spring.profiles.active=@activeEnv@

application-dev.properties

server.servlet.context-path=/api/
server.port=8080
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
# springboot return json date TimeZone
spring.jackson.time-zone=GMT+8

application-prod.properties

server.servlet.context-path=/api/
server.port=8080
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
# springboot return json date TimeZone
spring.jackson.time-zone=GMT+8

logback-spring.xml(springboot 默认的日志框架)

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="120 seconds"> <!--debug="true" -->
    <property name="log.base" value="${mvn.log.dir}"/>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>
                [%level] %date{yyyy-MM-dd HH:mm:ss.SSS} %logger{96}:[%line] - %msg%n
            </pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <appender name="LOGFILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${log.base}/diablo-api.log</file>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>INFO</level>
        </filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${log.base}/diablo-api.%d{yyyy-MM-dd_HH}.%i.log</fileNamePattern>
            <maxHistory>168</maxHistory>
            <maxFileSize>200MB</maxFileSize>
        </rollingPolicy>
        <encoder>
            <pattern>
                [%level] %date{yyyy-MM-dd HH:mm:ss.SSS} %logger{96}:[%line] - %msg%n
            </pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>
    <!-- worker server logback config end -->

    <root level="INFO">
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="LOGFILE"/>
    </root>

</configuration>

3.pom文件

<project>
<profiles>
        <!-- 开发环境 -->
        <profile>
            <id>dev</id>
            <activation>
                <!-- 开发环境,默认激活 -->
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <!-- activeEnv属性关联到application.properties中的spring.profiles.active值 -->
                <activeEnv>dev</activeEnv>
                <mvn.log.dir>/tmp/diablo/logs</mvn.log.dir>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <activeEnv>prod</activeEnv>
                <mvn.log.dir>/export/Logs/diablo-api</mvn.log.dir>
            </properties>
        </profile>
    </profiles>
    <dependencies>
    </dependencies>
    <build>
        <plugins>
            <!-- springboot自带的打包插件,设置mainClass方便本地启动类main方法启动的时候,可以动态匹配到profile -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>springboot模块的启动类的全路径名</mainClass>
                    <layout>JAR</layout>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <configuration>
                    <filesets>
                        <fileset>
                            <directory>src/main/resources/public</directory>
                        </fileset>
                    </filesets>
                </configuration>
            </plugin>
            <!-- copy jar包,配置文件,脚本到目标目录上 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <useDefaultDelimiters>true</useDefaultDelimiters><!--  这是重点-->
                </configuration>
                <executions>
                    <!-- copy jar包到target/build目录 -->
                    <execution>
                        <id>copy-fatjar</id>
                        <phase>install</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/build</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${project.build.directory}</directory>
                                    <include>${project.build.finalName}.${project.packaging}</include>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                    <!-- copy bin/start.sh 及bin/stop.sh文件到target/build/bin目录下 -->
                    <execution>
                        <id>copy-bin</id>
                        <phase>install</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/build/bin</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>src/main/bin</directory>
                                    <include>start.sh</include>
                                    <include>stop.sh</include>
                                    <filtering>true</filtering>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                    <execution>
                        <id>copy Vue.js frontend content third</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${basedir}/target/classes</outputDirectory>
                            <overwrite>true</overwrite>
                            <resources>
                                <resource>
                                    <directory>${basedir}/src/main</directory>
                                    <includes>
                                        <include>*</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- maven jar 打包插件,configuration中可以有过滤标签,将配置文件排除到jar包外 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                     <!-- 可以配置打包的时候jar包里不携带配置文件 -->
                     <!--                    <excludes>-->
<!--                        <exclude>application-dev.properties</exclude>-->
<!--                        <exclude>spring/*.xml</exclude>-->
<!--                    </excludes>-->
                </configuration>
            </plugin>
        </plugins>
        <!-- 添加这段配置是因为logback-spring.xml文件中引用了${mvn.log.dir}变量,需要动态从pom文件中加载生效 -->
        <resources>
            <resource>
                <directory>${project.basedir}/src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
</project>
posted @ 2020-09-01 20:01  SpecialSpeculator  阅读(350)  评论(0编辑  收藏  举报