zoukankan      html  css  js  c++  java
  • SpringBoot多环境分离resources和lib进行打包

    为了更便捷的在本地进行开发,偶尔的又会涉及到测试和生产打包,每个环境下的配置不尽相同,需要配置多环境的配置文件,避免打包时还要自己特意去修改配置文件

    SpringBoot项目的默认配置文件在main/resources下的application.properties且通过配置项 spring.profiles.active 来设置多环境属性,所以可以就此文件创建多个环境的application.properties,只是spring.profiles.active区分开特定的环境值,再通过maven进行配置打包

    首先创建各环境下的配置文件,目录是这样

    dev为开发环境,spring.profiles.active=dev

    test为测试环境,spring.profiles.active=test

    prod为生产环境,spring.profiles.active=prod

    pom.xml文件添加

    <profiles>
        <!-- 开发环境 -->
        <profile>
            <id>dev</id>
            <properties>
                <package.environment>dev</package.environment>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <!-- 测试环境 -->
        <profile>
            <id>test</id>
            <properties>
                <package.environment>test</package.environment>
            </properties>
        </profile>
        <!-- 生产环境 -->
        <profile>
            <id>prod</id>
            <properties>
                <package.environment>prod</package.environment>
            </properties>
        </profile>
    </profiles>

    拆分resources、lib打包zip文件

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <excludes>
                    <exclude>dev/*</exclude>
                    <exclude>test/*</exclude>
                    <exclude>prod/*</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources/${package.environment}</directory>
            </resource>
        </resources>
        <plugins>
            <!-- 分离lib -->
            <plugin>
                <!--把依赖的jar包复制出来放到编译后的target/lib目录,并且在打包时候排除内部依赖-->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!-- 分离资源文件 -->
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <resources>
                                <resource>
                                    <directory>src/main/resources</directory>
                                </resource>
                            </resources>
                            <outputDirectory>${project.build.directory}/resources</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!--打包jar-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <!-- 指定资源文件目录,与打包的jar文件同级目录 -->
                        <manifestEntries>
                            <Class-Path>resources/</Class-Path>
                        </manifestEntries>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>com.schy.transfer.api.ApiApplication</mainClass>
                        </manifest>
                    </archive>
                    <!-- 打包时忽略的文件(也就是不打进jar包里的文件),本例将resources下的.yml、.properties文件全部排除 -->
                    <excludes>
                        <exclude>**/*.xml</exclude>
                        <exclude>**/*.properties</exclude>
                    </excludes>
                </configuration>
            </plugin>
            <!-- spring boot repackage -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.0.1.RELEASE</version>
                <configuration>
                    <layout>ZIP</layout>
                    <includes>
                        <include>
                            <groupId>non-exists</groupId>
                            <artifactId>non-exists</artifactId>
                        </include>
                    </includes>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!--打包为压缩文件-->
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <finalName>${project.artifactId}-${project.version}-${package.environment}</finalName>
                            <appendAssemblyId>false</appendAssemblyId>
                            <descriptors>
                                <descriptor>src/assembly/assembly.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    View Code

     assembly插件配置

    <assembly
            xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
        <id>transfer-api-assembly</id>
        <formats>
            <format>zip</format>
        </formats>
        <includeBaseDirectory>false</includeBaseDirectory>
        <fileSets>
            <fileSet>
                <directory>target/classes</directory>
                <outputDirectory>/resources</outputDirectory>
                <includes>
                    <include>*.properties</include>
                    <include>*.xml</include>
                </includes>
                <fileMode>0755</fileMode>
            </fileSet>
            <fileSet>
                <directory>target/lib</directory>
                <outputDirectory>/lib</outputDirectory>
                <excludes>
                    <exclude>${project.groupId}:${project.artifactId}</exclude>
                </excludes>
                <fileMode>0755</fileMode>
            </fileSet>
            <fileSet>
                <directory>target</directory>
                <outputDirectory>/</outputDirectory>
                <includes>
                    <include>${project.artifactId}-*.jar</include>
                </includes>
                <fileMode>0755</fileMode>
            </fileSet>
        </fileSets>
    </assembly>
    View Code

    最后使用命令给开发环境进行打包

    mvn clean install -Dmaven.test.skip=true -Pdev

    打包的文件结构为

    使用 java -jar test-1.0.jar 运行即可

  • 相关阅读:
    微信支付开发(12) 认清微信支付v2和v3
    教爸爸妈妈用微信
    微信公众平台开发(103) 四六级成绩查询
    微信支付开发(11) Native支付
    微信公众平台开放设备接入能力
    微信公众平台开发(102) 模板消息
    微信支付开发(10) 全网发布
    微信小店开发(3) 自定义菜单加入维权
    openssl下载
    使用PHP QR Code生成二维码
  • 原文地址:https://www.cnblogs.com/cnsevennight/p/12600316.html
Copyright © 2011-2022 走看看