springboot项目jar包瘦身,打包时不打依赖包

springboot项目jar包瘦身,打包时不打依赖包 在pom文件中添加:

     <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!--启动类位置-->
                    <mainClass>com.kaying.luck.IcbcLuckApplication</mainClass>
                    <layout>ZIP</layout>
                    <includes>
                        <!-- 没有自定义的jar包写 nothing-->
                        <include>
                            <groupId>nothing</groupId>
                            <artifactId>nothing</artifactId>
                        </include>
                    </includes>
                    <layers>
                        <enabled>false</enabled>
                    </layers>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
        </plugins>
    </build>

打出来的包只有333kb

将maven里的jar包打出来

mvn dependency:copy-dependencies -DoutputDirectory=F:\public-jar

将public-jar文件夹上传至服务器,然后jar包指定文件夹运行即可

nohup java -Xms500m -Xmx500m -Xmn250m -Xss256k -server -XX:+HeapDumpOnOutOfMemoryError -Dloader.path=$PUBLIC_JAR -jar $APP_NAME --server.port=$PORT > ./main.log 2>&1 &
tail -200f main.log

脚本修改

在上方添加jar包文件夹

#jar包文件夹
PUBLIC_JAR=/home/public-jar 

更改启动脚本为指定文件夹启动

#指定文件夹启动
nohup java -Xms500m -Xmx500m -Xmn250m -Xss256k -server -XX:+HeapDumpOnOutOfMemoryError -Dloader.path=$PUBLIC_JAR -jar $APP_NAME --server.port=$PORT > ./main.log 2>&1 &
tail -200f main.log

完整脚本示例:

#jar名称
APP_NAME=icbc-hz-sbkh-0.0.1-SNAPSHOT.jar
#jar包文件夹
PUBLIC_JAR=/home/public-jar 
ACTICE=test
#启动端口
PORT=7766

#使用说明,用来提示输入参数
usage() {
    echo "Usage: sh 执行脚本.sh [start|stop|restart|status]"
exit 1
}

#检查程序是否在运行
is_exist(){
pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' `
#如果不存在返回1,存在返回0
if [ -z "${pid}" ]; then
return 1
else
return 0
fi
}

#启动方法
start(){
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is already running. pid=${pid} ."
else
#指定文件夹启动
nohup java -Xms500m -Xmx500m -Xmn250m -Xss256k -server -XX:+HeapDumpOnOutOfMemoryError -Dloader.path=$PUBLIC_JAR -jar $APP_NAME --server.port=$PORT > ./main.log 2>&1 &
tail -200f main.log
fi
}

#停止方法
stop(){
is_exist
if [ $? -eq "0" ]; then
kill -9 $pid
else
echo "${APP_NAME} is not running"
fi
}

#输出运行状态
status(){
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is running. Pid is ${pid}"
else
echo "${APP_NAME} is NOT running."
fi
}

#重启
restart(){
stop
start
}

#根据输入参数,选择执行对应方法,不输入则执行使用说明
case "$1" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
*)
usage
;;
esac

猜你喜欢

转载自blog.csdn.net/m0_58709145/article/details/129621780