Spring Boot项目打包部署

1.pom.xml

真实环境中,有时需要把spring boot的项目打包成war包,并部署到tomcat下。此时需要如下配置pom.xml

 version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>modelVersion>

    <artifactId>artifactId>
    <packaging>warpackaging>

    <name>name>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.0.2.RELEASEversion>
        <relativePath/>
    parent>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-starter-loggingartifactId>
                exclusion>
            exclusions>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-log4j2artifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
        <resources>
            <resource>
                <directory>src/main/resourcesdirectory>
                <excludes>
                    <exclude>application*.propertiesexclude>
                excludes>
            resource>
            <resource>      
                
                <directory>src/main/resourcesdirectory>
                <filtering>truefiltering>
                <includes>
                    <include>application.propertiesinclude>
                    <include>application-${profileActive}.propertiesinclude>
                includes>
            resource>
        resources>
    build>

    <profiles>
        <profile>
            <id>devid>
            <activation>
                <activeByDefault>trueactiveByDefault>
            activation>
            <properties>
                <profileActive>devprofileActive>
            properties>
        profile>

        <profile>
            <id>prodid>
            <properties>
                <profileActive>prodprofileActive>
            properties>
            <dependencies>
                <dependency>        
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-starter-tomcatartifactId>
                    <scope>providedscope>
                dependency>
            dependencies>
        profile>
    profiles>
project>

2. application.properties


spring.profiles.active=@profileActive@
# ..

3. 在idea中运行时:

如果用idea中的

运行的话,则必须设置

Spring Boot项目打包部署_第1张图片

为dev或者别的可用的profile。

或者如果用右边栏的profile

则可以直接用spring-boot maven插件运行

Spring Boot项目打包部署_第2张图片

你可能感兴趣的:(Spring Boot项目打包部署)