码迷,mamicode.com
首页 > 其他好文 > 详细

maven项目多环境打包问题

时间:2020-09-14 19:22:29      阅读:67      评论:0      收藏:0      [点我收藏+]

标签:app   动态   serve   main方法   pre   encoding   ons   port   排除   

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

<?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>
                </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>

maven项目多环境打包问题

标签:app   动态   serve   main方法   pre   encoding   ons   port   排除   

原文地址:https://www.cnblogs.com/PythonOrg/p/13598263.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!