这篇主要介绍打包到线上环境,已经多应用的切换方式,重点在于pom文件配置方式,以及对于Idea的使用上面。
Idea开发环境
通过编辑右上的Startup运行配置,将Startup改名为Startup DEV代表DEV应用,然后复制该运行配置,改名为Startup PROD代表PROD应用。对于非多应用的系统,这里也可以理解为开发环境和线上环境的2种启动方式。至于如何打包成多应用,后面打包部分会介绍到。
DEV应用的启动配置和昨天的部分一样,PROD应用的配置如下:
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsICM38FdsYkRGZkRG9lcvx2bjxiNx8VZ6l2csYHbzIWN5cVZwZkMMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnL2gDNyAjMzETMxEDNwkTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
注意红色圈住的部分。代表启动时,使用的环境。
另外根据dev的配置文件,需要在Startup的resources目录中建立application-prod.yml和logback-prod.xml文件,如果是复制的dev的配置文件,注意替换文件内容中对应的dev改为prod。
接下来需要注意,当启动DEV应用测试时,需要勾选maven中的dev选项,并且不勾选prod选项。因为打包的文件会根据这2个选项生成不同的class文件。同样,如果启动PROD应用时,勾选的部分反过来。
这时,我们可以在Idea中无缝的启动多应用进行调试。
不知道各位是否能理解这其中的一些细节,目前为止,多模块(多应用或多环境)的项目开发环境已经搭建好了,接下来的就是撸码撸功能了。记住功能可以通过Module类型的模块,划分多个。
应用部署
当开发完成,接下来的工作便是打包了。为了保证能理解的比较清楚,请仔细对比Startup中得Pom文件,并且理解每个环节的作用和意义。
1. 首先配置DEV应用的maven的install选项,参考下图:
然后用同样的方式配置PROD应用。最终的Maven启动项如下图:
重点部分见红圈,总体来说,目前的内容多数重点在于对Idea的使用上,然后是对于SpringBoot以及Maven的使用,各位可以多了解下这种开发机制的作用。
打包配置
对于Pom和Maven使用,内容有点多,还请各位看的仔细一点,这里会把Pom里的配置每个都拆开来讲解。
1. 打包的时候保留devtools,用于热更新。
<!-- 打包时保留 devtools -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<excludeDevtools>false</excludeDevtools>
</configuration>
</plugin>
2. 打包时,将引用的第三方包拷贝到指定位置,其中
${release.dir} 发布的根路径
${project.name 发布的应用目录
${release.lib} 部署的第三方包目录
这3个部分配置在Boot的Pom文件<properties>里面
<!-- 拷贝引用的Lib到指定位置 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${release.dir}/${project.name}/${release.lib}/</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
3. 打包成Jar时,将配置文件隔离,即resources下的指定内容拷贝到指定文件夹中。
${basedir} 为项目路径,maven内置配置
<!-- 分离配置文件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<encoding>UTF-8</encoding>
<outputDirectory>${release.dir}</outputDirectory>
<resources>
<resource>
<directory>${basedir}</directory>
<includes>
<include>config/**</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
4. 打包时,不包含配置文件,并生成可执行的jar文件
<!-- 打包类文件,并移除配置文件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>${release.lib}/</classpathPrefix>
<mainClass>com.jees.demo.Startup</mainClass>
<useUniqueVersions>false</useUniqueVersions>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
<excludes>
<exclude>
config/**
</exclude>
</excludes>
</configuration>
</plugin>
5. 打包完成后,拷贝配置及执行文件到指定位置
<!-- 将生成的文件拷贝到指定位置 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>deploy</id>
<phase>install</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<echo message="COPY FILE:${package.name}" />
<copy todir="${release.dir}/${project.name}/config/">
<fileset dir="${basedir}/target/classes/config/"></fileset>
</copy>
<copy file="${basedir}/target/${package.name}" tofile="${release.dir}/${project.name}/${package.name}" />
</target>
</configuration>
</execution>
</executions>
</plugin>
6. 其他配置项
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>${spring.boot.version}</version>
<optional>true</optional>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>jees.demo</groupId>
<artifactId>jees.demo.module</artifactId>
<version>${dev.version}</version>
<scope>${dev.scope}</scope>
</dependency>
</dependencies>
最后总结下多模块的思路:
1. 各个模块以jar形式打包,提供一个程序入口,并通过预设生产环境来切换需要导入的jar。
2. 对于多应用的项目,最好将应用生成目录分开,注意不要导入其他应用的包含的入口或者同类型类或者同接口的实现类,避免在应用使用了不对口的实现类。
3. 熟悉idea中的项目设置,环境配置等知识,避免设置错误导致项目未能正确运行或者打包。
完整的项目见附件:
图片另存为后重命名为zip或rar
喜欢的朋友请关注github的jeesupport项目。
https://github.com/aiyoyoyo/jeesupport