温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用maven打包发布springboot的方法

发布时间:2020-08-20 14:42:02 来源:亿速云 阅读:175 作者:小新 栏目:编程语言

这篇文章将为大家详细讲解有关使用maven打包发布springboot的方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

 

分享如何使用maven便利我们打springboot的发布包;我这里使用的是idea开发工具,首先创建了多个module的项目结构,如图:

使用maven打包发布springboot的方法

要对多个module的项目做打包,一般情况都是在父级pom中配置打包的插件,其他module的pom不需要特别的配置,当配置完成后,点击idea中maven工具的package,就能执行一系列打包操作;

使用maven打包发布springboot的方法

这里先使用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>
        </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成功启动:

使用maven打包发布springboot的方法

在规范的项目中,一般有dev,test,uat,pro等环境,针对这些个环境需要有不同的配置,springboot中可以通过application-dev|test|...yml来区分不同的配置,仅仅需要在默认的application.yml中加入spring.profiles.active=dev|test...就行了;

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

使用maven打包发布springboot的方法

为了区分测试,这里对不同环境配置文件设置了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模块能看到这样的图面:

使用maven打包发布springboot的方法

这个时候仅仅只需要我们勾选这些个按钮就行了,不管是调试还是最后打包,都按照这个来获取所需的配置文件。

以上就是如何使用maven打包发布springboot的详细内容,更多请关注亿速云其它相关文章!

关于使用maven打包发布springboot的方法就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI