SpringBoot打包 lib分离 config分离

最终打包后的目录结构

│   start.sh
├───config
│       application-dev.yml
│       application-prod.yml
│       application.yml
│       ehcache.xml
│       logback.xml
├───logs
├───lib
│       spring-context-support-5.1.2.RELEASE.jar
│       spring-core-5.1.2.RELEASE.jar
│       spring-oxm-5.1.2.RELEASE.jar
│       spring-retry-1.2.2.RELEASE.jar
│       spring-tx-5.1.2.RELEASE.jar
│       stax2-api-3.1.4.jar
│       woodstox-core-5.0.3.jar
│       zkclient-0.10.jar
│       zookeeper-3.4.11.jar
│ 		 .........
└───logs

pom.xml

<build>
        <plugins>
            <!-- Compiler 插件, 设定JDK版本 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>

            <!-- resource插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>**/*.properties</exclude>
                        <exclude>**/*.xml</exclude>
                        <exclude>**/*.yml</exclude>
                        <exclude>static/**</exclude>
                        <exclude>templates/**</exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <layout>ZIP</layout>
                    <includes>
                        <include>
                            <groupId>cn.wisec.meerkat</groupId>
                            <artifactId>meerkat-slice</artifactId>
                        </include>
                    </includes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>target/lib</outputDirectory>
                            <excludeTransitive>false</excludeTransitive>
                            <stripVersion>false</stripVersion>
                            <includeScope>runtime</includeScope>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.5.3</version>
                <executions>  <!--执行器 mvn assembly:assembly -->
                    <execution>
                        <id>make-zip</id><!--名字任意 -->
                        <phase>package</phase><!-- 绑定到package生命周期阶段上 -->
                        <goals>
                            <goal>single</goal><!-- 只运行一次 -->
                        </goals>
                        <configuration>
                            <descriptors> <!--描述文件路径 -->
                                <descriptor>src/main/bin/assembly.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

src/main/bin/assembly.xml

<assembly
        xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>bin</id>
    <formats>
        <format>zip</format>
    </formats>
    <fileSets>
        <fileSet>
            <directory>src/main/resources</directory>
            <outputDirectory>/config/</outputDirectory>
            <includes>
                <include>**/*</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>lib/*.jar</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>/lib</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>src/main/bin</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>start.sh</include>
                <include>logs</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

src/main/bin/start.sh

#!/bin/sh
dir=`cd $(dirname $0); pwd -P`
jar=$(find ./lib -type f -name  meerkat-slice-*.jar)
echo $jar
nohup java -server -Duser.dir=$dir -XX:-UseGCOverheadLimit -Xms1024m -Xmx30720m -XX:PermSize=256M -XX:MaxPermSize=512M -Dloader.path=./lib,./config -jar $jar > /dev/null &

注意事项

  • 不同的项目需要在pom.xml/build/plugins中
 		<plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <layout>ZIP</layout>
                    <includes>
                        <include>
                            <groupId>cn.wisec.meerkat</groupId> //需要换成你打包工程的groupId
                            <artifactId>meerkat-slice</artifactId>//需要换成你打包工程的artifactId
                        </include>
                    </includes>
                </configuration>
		 </plugin>
  • 在start.sh中将jar=$(find ./lib -type f -name meerkat-slice-*.jar)中的meerkat-slice改成你打包工程的artifactId
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Maven 是一个构建工具,用于管理和构建 Java 项目的依赖关系。在 Maven 中,lib 是指项目所需的第三方库文件,而 jar 是指项目本身的可执行文件。 在 Maven 中,可以通过配置来实现 lib 和 jar 分离打包。主要的步骤如下: 1. 在项目的 pom.xml 中,配置 Maven 的插件和属性。 2. 在 pom.xml 中,通过配置 dependencies 标签来管理项目所需的依赖库。将需要的库文件的 groupId、artifactId 和版本号等信息填写在 dependencies 中。 3. 设置打包方式。通过配置 Maven 的插件,将项目编译为一个可执行的 jar 文件,并将所有依赖的库文件打包为一个单独的 lib 目录。 4. 配置 Maven 的插件实现打包。可以使用 Maven 自带的插件如 maven-jar-plugin 和 maven-dependency-plugin 来实现分离打包。 5. 运行 Maven 命令进行项目构建和打包。 在执行以上步骤后,Maven 会根据 pom.xml 中的配置,将项目本身的可执行 jar 文件和所有的依赖库文件分别打包。项目本身的 jar 文件将通过 maven-jar-plugin 插件生,并存放在 target 目录下。而所有的依赖库文件将通过 maven-dependency-plugin 插件生,并存放在 target 目录下的 lib 目录中。 通过以上的操作,我们可以实现 lib 和 jar 分离打包。这样,在部署项目时,只需要将项目本身的 jar 文件和 lib 目录一起拷贝到目标环境中即可。这样可以减小发布包的大小,并且便于管理和维护。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值