本文环境

  1. IntelliJ 2019.2

1.Maven项目打包

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.5.5</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.nineya.malt.MaltMain</mainClass>   
                    </manifest>        
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>  
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

2.springboot项目打包jar

springboot使用上述那种方法会报错,无法运行。

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>com.cl.graph.SpringbootApplication</mainClass>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>8</source>
                <target>8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

3.springboot项目打包war

本文描述的是gradle方式将springboot项目打包成war的方法。

1.添加apply plugin: 'war',将tomcat的依赖范围修改为providedRuntime

plugins {
    id "org.springframework.boot" version "2.2.5.RELEASE"
    id "io.spring.dependency-management" version "1.0.9.RELEASE"
    id "checkstyle"
    id "java"
    id 'war'    //生成.war包添加
}

dependencies {
    implementation "org.springframework.boot:spring-boot-starter-actuator"
    implementation "org.springframework.boot:spring-boot-starter-data-jpa"
    implementation "org.springframework.boot:spring-boot-starter-web"
    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'   //生成.war包添加
    implementation "org.springframework.boot:spring-boot-starter-undertow"
    implementation "org.springframework.boot:spring-boot-starter-freemarker"
    implementation "com.sun.mail:jakarta.mail"
}

2.主类继承SpringBootServletInitializer,重写configure方法

public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
        // Customize the spring config location
        System.setProperty("spring.config.additional-location", "file:${user.home}/.halo/,file:${user.home}/halo-dev/");

        // Run application
        CONTEXT = SpringApplication.run(Application.class, args);

    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
}

3.执行Gradle选项中Tasks > build > bootWar即可。