您现在的位置是:首页 > 文章详情

springboot结合maven打包发布

日期:2018-11-23点击:310

本篇分享如何使用maven便利我们打springboot的发布包;我这里使用的是idea开发工具,首先创建了多个module的项目结构,如图:
image
要对多个module的项目做打包,一般情况都是在父级pom中配置打包的插件,其他module的pom不需要特别的配置,当配置完成后,点击idea中maven工具的package,就能执行一系列打包操作;
image
这里先使用maven-jar-plugin插件,在父级pom中添加配置如下:

<!--通过maven-jar-plugin插件打jar包--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <!--main入口--> <mainClass>com.platform.WebApplication</mainClass> </manifest> </archive> <!--包含的配置文件--> <includes> <!--<include>*.yml</include>--> <!--<include>*.properties</include>--> <!--<include>templates/**</include>--> <!--<include>static/**</include>--> <!--<include>*.xml</include>--> </includes> <excludes> </excludes> </configuration> </plugin>

上面的配置我们需要注意以下几个节点:

  • mainClass:我们需要指定main入口,当然这不是必须的,如果同一个project中有多个main入口,那打包的时候才需要,仅仅就一个main入口这个其实忽略;
  • classpathPrefix:指定加入classpath中依赖包所在的前缀文件夹名
  • addClasspath:依赖包放加入到classpath中,默认true
  • includes:需要包含在jar中的文件,一般不配置(注意:如果配置路径不合适,可能会吧class排除掉)
  • excludes:如果是要做jar包外部配置文件的话,这里需要用excludes排除这些配置文件一起打包在jar中

使用maven-jar-plugin插件针对项目工程来打包,这个时候通过maven的package命令打包,能看到jar中有一个lib文件夹(默认),其中包含了工程项目中所引入的第三方依赖包,通过java -jar xxx.jar能看到jar成功启动:
image
在规范的项目中,一般有dev,test,uat,pro等环境,针对这些个环境需要有不同的配置,springboot中可以通过application-dev|test|...yml来区分不同的配置,仅仅需要在默认的application.yml中加入spring.profiles.active=dev|test...就行了;

这种方式有个不便的地方,比如本地调试或发布上线都需要来回修改active的值(当然通过jar启动时,设置命令行active参数也可以),不是很方便;下面采用在pom中配置profiles,然后通过在idea界面上鼠标点击选择启动所用的配置;

首先,在main层创建配置文件目录如下结构:
image
为了区分测试,这里对不同环境配置文件设置了server.port来指定不同端口(dev:3082,pro:3182)
然后,在父级pom中配置如下profiles信息:

 <profiles> <profile> <id>dev</id> <!--默认运行配置--> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <activeProfile>dev</activeProfile> </properties> </profile> <profile> <id>test</id> <properties> <activeProfile>test</activeProfile> </properties> </profile> <profile> <id>uat</id> <properties> <activeProfile>uat</activeProfile> </properties> </profile> <profile> <id>pro</id> <properties> <activeProfile>pro</activeProfile> </properties> </profile> </profiles>

节点说明:

  • activeByDefault:设置为默认运行配置
  • activeProfile:所选择的启动配置,它的值对应上面创建profiles下面的dev|test|pro文件夹

然后,在pom中的build增加resources节点配置:

<resources> <!--指定所使用的配置文件目录--> <resource> <directory>src/main/profiles/${activeProfile}</directory> </resource> </resources>

此刻我们的配置就完成了,正常情况下idea上maven模块能看到这样的图面:
image
这个时候仅仅只需要我们勾选这些个按钮就行了,不管是调试还是最后打包,都按照这个来获取所需的配置文件。

原文链接:https://yq.aliyun.com/articles/672822
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章