美文网首页
12.跟我学SpringBoot-多环境打包

12.跟我学SpringBoot-多环境打包

作者: 孔垂云 | 来源:发表于2017-12-04 00:46 被阅读0次

SpringBoot工程的核心是基础配置文件,这一块涉及的参数会非常多,而且开发、测试、生产各不一样,为此就需要设置多环境打包。这一节将讲解如何进行多环境配置文件打包,和SSH工程不太一样。

1.先建立一个工程,及对应配置文件

工程目录.png

/src/resources下面共有三个配置文件,其中application-test.yml和application-dev.yml为测试和开发的配置文件,根据实际打包进不同的文件,application.yml为打包环境选择文件,配置内容为:

spring:
 profiles:
   active: @environment@

为设置的环境变量

2.修改pom.xml

 <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <environment>dev</environment>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <environment>test</environment>
            </properties>
        </profile>
    </profiles>

这一段是设置当前工程的所有配置文件,并指定哪个是默认激活的。

设置编译配置

<build>
        <finalName>chapter11_profiles</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.5.2.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal><!--可以把依赖的包都打包到生成的Jar包中 -->
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>${maven-jar-plugin.version}</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>com.critc.ProfilesStartApplication</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/lib</directory>
                <targetPath>BOOT-INF/lib/</targetPath>
                <includes>
                    <include>**/*.jar</include>
                </includes>
            </resource>
            <resource>
                <filtering>true</filtering>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>application-dev.yml</exclude>
                    <exclude>application-test.yml</exclude>
                </excludes>
            </resource>
            <resource>
                <filtering>true</filtering>
                <directory>src/main/resources</directory>
                <includes>
                    <include>application-${environment}.yml</include>
                    <include>application.yml</include>
                </includes>
            </resource>
        </resources>
    </build>

这一节设置所有编译参数。
几个主要确定的地方:
1、设置编译的jdk版本
2、设置spring-boot-maven-plugin,利用该插件进行打包
3、设置启动类
4、设置应用的资源文件及拷贝的jar包路径。

所有都设置完毕后执行如下命令:

执行打包.png
这样将会在工程的target目录下生成chapter11_profiles.jar

源码下载

本例子详细源码

相关文章

  • 12.跟我学SpringBoot-多环境打包

    SpringBoot工程的核心是基础配置文件,这一块涉及的参数会非常多,而且开发、测试、生产各不一样,为此就需要设...

  • Maven 多环境打包以及聚合打包(一)

    说明 本文只是一个总结参考文章:Maven 插件 - 打包时多环境配置文件设置 Maven 多环境打包 mav...

  • iOS多环境打包

    为了调试,我们需要经常打包测试。以前总是一套环境走天下,或者就是每次在代码的全局变量里面修改环境,搞来搞去感觉忒麻...

  • MAVEN 多环境打包

    这里的profile和Spring里面的profile是一样的道理,首先对默认的文件进行打包,然后读取默认激活的p...

  • Maven多环境打包

    场景 在应用部署的时候,往往遇到需要发布到不同环境的情况,而每个环境的数据库信息、密钥信息等可能会存在差异。举个例...

  • maven 多环境打包

    目录结构 在pom.xml中添加如下profile的配置: 当加入这些配置后就能在右侧看到目录如下:image.p...

  • vue多环境打包

    方法1 node属性process.argv 改属性为node执行命令获取到的命令集合 如打包命令为npm bui...

  • 跟我学区块链(一) 环境搭建

    跟我学区块链(一) 环境搭建

  • Gradle多版本/多环境打包

    本文开发环境: Android Studio 3.1.4 Build #AI-173.4907809, built...

  • Android多环境配置打包

    需求 打包多个app,App名称,应用图标,包名,启动页都不一样,如果打包一个,然后又替换一下资源,再打下一个,真...

网友评论

      本文标题:12.跟我学SpringBoot-多环境打包

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