springboot的jar和lib分开打包

laker 1年前 ⋅ 424 阅读

背景

项目更新频繁,只有在服务器上才能验证,而jar有几十M,且网络特别差,每次上传jar到服务器浪费了大量时间,所以这里把lib和jar分离,在不涉及依赖jar版本变更的情况下,每次上传的只有一个大小才几百k的jar,大大提高了工作效率。

实现

pom屏蔽自带的打包插件

<!-- 屏蔽自带的打包插件
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
-->

pom添加assembly插件

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <appendAssemblyId>false</appendAssemblyId>
        <descriptors>
            <descriptor>src/main/resources/assembly.xml</descriptor>
        </descriptors>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

resources下添加配置文件assembly.xml

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
    <id>package</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>${basedir}/src/main/resources</directory>
            <includes>
                <include>*.yml</include>
            </includes>
            <filtered>true</filtered>
            <outputDirectory>${file.separator}config</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>${project.build.directory}/lib</directory>
            <outputDirectory>${file.separator}lib</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>${file.separator}</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

pom添加插件来拷贝依赖,版本3.0.2

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>${maven.dependency.plugin.version}</version>
    <executions>
        <execution>
            <id>copy-lib</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>
                <includeScope>compile</includeScope>
            </configuration>
        </execution>
    </executions>
</plugin>

pom添加插件打包,指定main方法和依赖的位置,版本3.0.2

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>${maven.jar.plugin.version}</version>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
                <mainClass>com.memexue.shares.SharesApplication</mainClass>
            </manifest>
            <manifestEntries>
                <artifactId>${artifactId}</artifactId>
                <version>${project.version}</version>
                <project>${project.description}</project>
            </manifestEntries>
        </archive>
    </configuration>
</plugin>

pom添加snakeyaml,版本1.24

不添加的话项目会找不到yml文件。

<dependency>
    <groupId>org.yaml</groupId>
    <artifactId>snakeyaml</artifactId>
    <version>${snakeyaml.version}</version>
</dependency>

结果

运行mvn package,在target目录下生成项目对应的zip文件shares-0.0.1-SNAPSHOT.zip。解压后包含:

shares-0.0.1-SNAPSHOT.jar
config/
lib/

运行jar文件效果和自带的打整包效果一样。

注意

  • 如果依赖的文件有变更,版本变化或者新增、删除依赖,需要手动将lib中变化的文件同步到服务器上。

全部评论: 0

    我有话说: