springBoot代码和依赖分开打包

目的:

  1、分离代码和依赖(包括第三方的引入包)

  2、分离配置文件到外部

  3、修改/META-INF/MANIFEST.MF中加载依赖包的位置和启动类,包含第三方依赖jar包

先查看项目的结构

 

 

 由于我的项目目前依赖的第三方的jar包,在src/lib/xxx.jar所有在打载入外部lib的时候也要把第三方依赖的包载入

<build>
    <finalName>bim-wm</finalName>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <!--MANIFEST.MF 中 Class-Path 加入资源文件目录 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>false</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                    </configuration>
                </execution>
            </executions>
        </plugin>


        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <archive>
                    <manifest>
                        <!--指定入口类 -->
                        <mainClass>com.jarvis.BimWmApplication</mainClass>
                        <addClasspath>true</addClasspath>
                        <!--MANIFEST.MF 中 Class-Path 加入前缀 -->
                        <classpathPrefix>lib/</classpathPrefix>
                        <!--jar包不包含唯一版本标识 -->
                        <useUniqueVersions>false</useUniqueVersions>
                    </manifest>
                    <manifestEntries>
                        <!--MANIFEST.MF 中 Class-Path 加入资源文件目录 -->
                        <Class-Path>./</Class-Path>
                        <!-- 要是打入第三方依赖的包的时候,这个重点!!!否则可以忽视,在META-INF/MANIFEST.MF的Class-Path添加加载这3个jar包 -->
                        <Class-Path>lib/olami-java-client-1.5.1-javadoc.jar lib/olami-java-client-1.5.1-full.jar lib/olami-java-client-1.5.1.jar</Class-Path>
                    </manifestEntries>
                </archive>
                <excludes>
                    <exclude>config/**</exclude>
                    <!-- 需要过滤2个地方,防止重复将第三方的jar也打进来 -->
                    <exclude>BOOT-INF/lib/olami-*.jar</exclude>
                    <exclude>lib/*.jar</exclude>
                </excludes>
            </configuration>
        </plugin>

        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}</outputDirectory>
                        <resources>
                            <resource>
                                <directory>src/main/resources</directory>
                                <filtering>true</filtering>
                            </resource>
                        </resources>
                    </configuration>

                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

最后是这样的(重点是jar包只有200kb扔到服务器也比较方便)

 

 

 

 将lib、xxx.jar、applicaion.xml扔到服务器中 ,用java -jar bim-wn.jar启动即可!

posted @ 2019-10-24 17:01  石智文的博客  阅读(2295)  评论(0编辑  收藏  举报