Home >Java >javaTutorial >How to use maven to package and release springboot

How to use maven to package and release springboot

不言
不言forward
2018-11-24 16:51:032672browse

The content of this article is about how to use maven to package and publish springboot. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

This article shares how to use maven to facilitate us to create springboot release packages; I use the idea development tool here, and first create a project structure of multiple modules, as shown in the figure:

To package projects of multiple modules, the packaged plug-ins are generally configured in the parent pom. The pom of other modules does not require special configuration. When the configuration is completed, click The package of the maven tool in idea can perform a series of packaging operations;

Here first use the maven-jar-plugin plug-in and add the following configuration in the parent 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>

We need to pay attention to the following nodes in the above configuration:

  • mainClass: We need to specify the main entrance, of course This is not necessary. If there are multiple main entries in the same project, it is only needed when packaging. Only one main entry is actually ignored;

  • classpathPrefix: Specify to add to the classpath The prefix folder name where the dependent package is located

  • addClasspath: the dependent package is added to the classpath, the default is true

  • includes: needs to be included in the jar The files in are generally not configured (note: if the configuration path is inappropriate, the class may be excluded)

  • excludes: If you are making an external configuration file for the jar package, you need to do this here Use excludes to exclude these configuration files and package them in the jar together.

Use the maven-jar-plugin plug-in to package the project. At this time, you can package it through the maven package command and you can see the jar. There is a lib folder (default), which contains third-party dependency packages introduced in the project. Through java -jar xxx.jar, you can see that the jar is successfully started:

In standard projects, there are generally dev, test, uat, pro and other environments. Different configurations are required for these environments. In springboot, different configurations can be distinguished by application-dev|test|...yml. Just add spring.profiles.active=dev|test... to the default application.yml;

This method has inconveniences, such as local debugging or publishing and online need to be modified back and forth. The active value (of course, when starting through jar, you can also set the command line active parameter) is not very convenient; below, configure profiles in the pom, and then select the configuration used for startup by clicking on the idea interface; first, in the main layer Create the configuration file directory with the following structure:

In order to distinguish the tests, server.port is set for different environment configuration files to specify different ports (dev: 3082, pro: 3182)
Then, configure the following profiles information in the parent pom:

<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>

Node description:

  • activeByDefault :Set as the default running configuration

  • activeProfile: The selected startup configuration, its value corresponds to the dev|test|pro folder under the profiles created above

Then, add the resources node configuration to the build in the pom:

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

At this moment our configuration is complete. Under normal circumstances, the maven module on the idea can see such a picture. Surface:

At this time, we only need to check these buttons. Whether it is debugging or final packaging, follow this to obtain the required configuration files.

The above is the detailed content of How to use maven to package and release springboot. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete