<springboot项目>利用maven打包将资源文件分离

问题:

传统的springboot项目打包完成之后,将项目上传到服务器执行java -jar命令运行
问题:每次上传的jar包文件太大了,非常的耗时
为了解决这个问题:想到一种方案,利用maven插件将springboot的第三方jar、编译的资源文件以及配置文件分别打包,这样的话就可以解决每次上传文件大耗时的问题

解决方案:

  • 第三方jar独立打包
  • 编译的资源文件打成想要的jar包
  • resource下的资源文件单独打包

实现

在pom中配置maven打包插件:

resources部分模块:

    <build>
        <finalName>xxx项目名称xxxfinalName>
        <plugins>
            
            <plugin>
                
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-dependency-pluginartifactId>
                <executions>
                    <execution>
                        <id>copy-dependenciesid>
                        <phase>prepare-packagephase>
                        <goals>
                            <goal>copy-dependenciesgoal>
                        goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/liboutputDirectory>
                            <overWriteReleases>falseoverWriteReleases>
                            <overWriteSnapshots>falseoverWriteSnapshots>
                            <overWriteIfNewer>trueoverWriteIfNewer>
                        configuration>
                    execution>
                executions>
            plugin>
            
            <plugin>
                <artifactId>maven-resources-pluginartifactId>
                <executions>
                    <execution>
                        <id>copy-resourcesid>
                        <phase>packagephase>
                        <goals>
                            <goal>copy-resourcesgoal>
                        goals>
                        <configuration>
                            <resources>
                                <resource>
                                    <directory>src/main/resourcesdirectory>
                                resource>
                            resources>
                            <outputDirectory>${project.build.directory}/resourcesoutputDirectory>
                        configuration>
                    execution>
                executions>
            plugin>
            
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-jar-pluginartifactId>
                <configuration>
                    <archive>
                        
                        <manifestEntries>
                            <Class-Path>resources/Class-Path>
                        manifestEntries>
                        <manifest>
                            <addClasspath>trueaddClasspath>
                            <classpathPrefix>lib/classpathPrefix>
                            <mainClass>com.xxx.xxx.xxxmainClass>
                        manifest>
                    archive>
                    
                    <excludes>
                        <exclude>**/*.ymlexclude>

                        <exclude>**/*.dbexclude>
                    excludes>
                configuration>
            plugin>
            
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <configuration>
                
                    <layout>ZIPlayout>
                    <includes>
                        <include>
                            <groupId>non-existsgroupId>
                            <artifactId>non-existsartifactId>
                        include>
                    includes>
                configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackagegoal>
                        goals>
                    execution>
                executions>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <version>3.2version>
                <configuration>
                    <source>1.8source>
                    <target>1.8target>
                configuration>
            plugin>
        plugins>
    build>

resources下分模块:

    <build>
        <finalName>xxx项目名称xxxfinalName>
        
        <resources>
            <resource>
                <directory>src/main/javadirectory>
                <includes>
                    <include>**/*.xmlinclude>
                includes>
                
                <filtering>truefiltering>
            resource>
            <resource>
                <directory>src/main/resourcesdirectory>
                <includes>
                    <include>**/*.xmlinclude>
                includes>
                
                <filtering>truefiltering>
            resource>
        resources>
        <plugins>
            
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-jar-pluginartifactId>
                <configuration>
                    <excludes>
                        <exclude>**/*.propertiesexclude>
                        <exclude>**/*.ymlexclude>
                        <exclude>*.txtexclude>
                    excludes>
                    <archive>
                        <manifest>
                            
                            <mainClass>com.xxx.xxx.xxxmainClass>
                            <addClasspath>trueaddClasspath>
                            
                            <classpathPrefix>lib/classpathPrefix>
                        manifest>
                        <manifestEntries>
                            
                            <Class-Path>./resources/${project.build.finalName}/Class-Path>
                        manifestEntries>
                    archive>
                configuration>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <version>3.2version>
                <configuration>
                    <source>1.8source>
                    <target>1.8target>
                configuration>
            plugin>
            
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-dependency-pluginartifactId>
                <executions>
                    <execution>
                        <id>copy-dependenciesid>
                        <phase>packagephase>
                        <goals>
                            <goal>copy-dependenciesgoal>
                        goals>
                        <configuration>
                            <type>jartype>
                            <includeTypes>jarincludeTypes>
                            <useBaseVersion>falseuseBaseVersion>
                            <outputDirectory>
                                ${project.build.directory}/lib
                            outputDirectory>
                        configuration>
                    execution>
                executions>
            plugin>

            
            <plugin>
                <artifactId>maven-resources-pluginartifactId>
                <executions>
                    <execution>
                        <id>copy-resourcesid>
                        <phase>packagephase>
                        <goals>
                            <goal>copy-resourcesgoal>
                        goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/resources/${project.build.finalName}
                            outputDirectory>
                            <resources>
                                <resource>
                                    <directory>src/main/resourcesdirectory>
                                resource>
                            resources>
                        configuration>
                    execution>
                executions>
            plugin>
        plugins>
    build>

你可能感兴趣的:(<springboot项目>利用maven打包将资源文件分离)