mvn build jar依赖和源码本身分开。减轻编译后的jar大小

问题场景:平时在springboot项目中,打jar包都是默认把开发的代码和依赖jar包都打到一个jar包里,导致每次打包费时而且jar包还很大,升级一下都要把一个很大的包拷来拷去。如果是修改问题需要频繁替换jar包,那简直要疯

解决方案:把代码包打成我们需要的jar包,把依赖的jar包打在依赖jar包文件夹中,其实绝大部分都是依赖包,自己开发的代码打包其实是很小的,比如我这次遇到的问题就是整个jar包一百多M,但是我自己开发的核心代码还不到1M

具体实现方法

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.jettech.jettomanager</groupId>
	<artifactId>jettomanager-basic</artifactId>
	<version>v7.5.0</version>
	<name>jettomanager-basic</name>
	<packaging>jar</packaging>
	<description>Jettech jettomanager basic</description>
	<url>http://www.jettech.jettomanager.com</url>
	<organization>
		<name> Jettech Software, Inc.</name>
		<url>http://www.jettech.com</url>
	</organization>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.5.RELEASE</version>
	</parent>

	<properties>

	</properties>

	<dependencies>

		
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.RELEASE</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
			<dependency>
				<groupId>com.alibaba</groupId>
				<artifactId>druid-spring-boot-starter</artifactId>
				<version>${druid-spring-boot-starter.version}</version>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<finalName>${project.artifactId}-${project.version}</finalName>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<!-- 启动类的路径 -->
					<mainClass>xxxxxxxxxxxxx</mainClass>
					<!-- 包含引入本地的Jar -->
					<layout>ZIP</layout>
					<includes>
						<include>
							<groupId>nothing</groupId>
							<artifactId>nothing</artifactId>
						</include>
					</includes>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-dependency-plugin</artifactId>
				<executions>
					<execution>
						<id>copy</id>
						<phase>package</phase>
						<goals>
							<goal>copy-dependencies</goal>
						</goals>
						<configuration>                <!--指定的依赖路径-->
							<outputDirectory>
								${project.build.directory}/lib
							</outputDirectory>
						</configuration>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<configuration>
					<skipTests>true</skipTests>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
       <!--打包的时候去除依赖包-->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.3.7.RELEASE</version>
            <configuration>
                <mainClass>com.sunwei.syslog.SyslogApplication</mainClass>
                <layout>ZIP</layout>
                <includes>
                    <include>
                        <groupId>nothing</groupId>
                        <artifactId>nothing</artifactId>
                    </include>
                </includes>
            </configuration>
            <executions>
                <execution>
                    <id>repackage</id>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    <!--target/lib是依赖jar包的输出目录,根据自己喜好配置-->
        <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>

    </plugins>
</build>

2.在以上配置完成后,执行打包命令 mvn clean package ,会看到代码被打成了比较小jar包,依赖包被打在target文件夹下面的lib文件夹中

3.启动项目方式。将jar包和依赖的lib文件夹拷贝到服务器的同一文件目录下执行

java -Dloader.path="lib" -jar xx.jar

即可启动项目,xx.jar就是你打的那些个jar包

相关推荐
Light606 小时前
领码方案|微服务与SOA的世纪对话(5):未来已来——AI 驱动下的智能架构哲学
微服务·智能双生体·ai 增强 ddd·自驱动 mesh·预测型 ci/cd·自演进闭环
赴前尘9 小时前
Go 微服务框架排行榜(按 GitHub Star 排序)
微服务·golang·github
青衫客361 天前
基于 Python 构建的安全 gRPC 服务——TLS、mTLS 与 Casbin 授权实战
python·安全·微服务
虫师c1 天前
分布式系统设计模式:从理论到实践
微服务·设计模式·系统架构·高可用·分布式系统
K_i1342 天前
Docker、容器、虚拟机到底是什么
docker·微服务·云原生·容器·kubernetes
new_daimond2 天前
微服务网关技术详细介绍
微服务·云原生·架构
Light602 天前
领码方案|微服务与SOA的世纪对话(4):迁移与避坑——从 SOA 到微服务的演进路线图
微服务·云原生·架构·自动化运维·容器化·服务治理·渐进式迁移
虫师c2 天前
分布式系统实战:电商平台架构演进
微服务·架构·高并发·架构演进·分布式系统·电商架构
岁岁岁平安2 天前
分布式系统相关概念(单体、集群、分布式、分布式集群、微服务)
分布式·微服务
眠りたいです2 天前
基于脚手架微服务的视频点播系统-脚手架开发部分-brpc中间件介绍与使用及二次封装
c++·微服务·中间件·rpc·架构·brpc