springboot打成jar包和war包的两种方式,并引入第三方jar包!

springboot打成jar包和war包的两种方式,并引入第三方jar包!

首先把需要引入的第三方jar包引入到本地项目中,在引用的模块下加一个lib文件夹

一、打成jar包

1.修改pom文件里的打包方式

<packaging>jar</packaging>

2.引入第三方依赖包

<!--本地sdk的第三方包-->
<dependency>
    <groupId>com.alipay</groupId>
    <artifactId>alipay-sdk-java-DEV20171211110402</artifactId>
    <version>1.0</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/alipay-sdk-java-DEV20171211110402.jar</systemPath>
</dependency>

3..修改pom文件中的build

<build>
        <defaultGoal>clean package</defaultGoal>
        <finalName>${project.artifactId}</finalName>
        <plugins>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <executable>true</executable>
                    <includeSystemScope>true</includeSystemScope><!--把第三方jar包打包进来-->
                    <mainClass>com.bosssoft.MpayMercPlatformWebApplication</mainClass><!--主程序启动类-->
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal><!-- 可以把依赖的包都打包到生成的Jar包中 -->
                        </goals>
                    </execution>
                </executions>
            </plugin>

        </plugins>

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/**</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>

    </build>

二、打成war包

1.修改pom文件的打包方式

<packaging>jar</packaging>

2.排除内嵌的tomact

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <!--排除内嵌的tomcat,打成war包时开启,开发使用时需要注释掉-->
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>

3.修改主程序启动类

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class MpayMercPlatformWebApplication extends SpringBootServletInitializer {

    /**
     * 工程打成war需要继承SpringBootServletInitializer,重写configure方法
     * @param application
     * @return
     */
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(MpayMercPlatformWebApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(MpayMercPlatformWebApplication.class, args);
    }

}

4.修改build文件,添加plgin插件

<!--打成war包时开启-->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <configuration>
        <!--<resourceEncoding>UTF-8</resourceEncoding>-->
        <!-- 释放将项目的类文件打成jar放到lib目录中。 打成jar的好处是:只修改class时,可以只更新jar。<archive></archive> -->
        <attachClasses>true</attachClasses><!-- 把class打包jar作为附件 -->
        <archiveClasses>true</archiveClasses><!-- 把class打包jar -->
        <webResources>
            <resource>
                <directory>src/main/resources</directory>
                <targetPath>WEB-INF/classes</targetPath>
                <excludes>
                    <exclude>**/*.class</exclude>
                </excludes>
            </resource>
            <!--引入第三方包-->
            <resource>
                <directory>${project.basedir}/lib</directory>
                <targetPath>WEB-INF/lib/</targetPath>
                <includes>
                    <include>**/*.jar</include>
                </includes>
            </resource>
        </webResources>
        <packagingExcludes>
            WEB-INF/lib/*-classes.jar
        </packagingExcludes>
    </configuration>
</plugin>
posted on 2020-10-19 15:35  有梦可有为  阅读(859)  评论(0编辑  收藏  举报