springboot 打包 jar 含 lib

该文章详细介绍了如何配置Maven的pom.xml文件,包括设置SpringBoot父项目、引入HiveJDBC和ApachePOI等依赖,并排除特定的日志库。接着,文章说明了在命令行中使用mvncleaninstall和mvndependency:copy-dependencies命令将依赖打包到target/lib目录的过程。最后,提到将lib目录和jar包复制到服务器上进行执行的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

---------------- pom.xml 安装插件 ------------------

<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.cmhk</groupId>
  <artifactId>cmhkHiveHelper</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>cmhkHiveHelper</name>
  <url>http://maven.apache.org</url>;

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <poi.version>4.1.2</poi.version>
  </properties>

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

  <dependencies>
    <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <version>3.8.1</version>
       <scope>test</scope>
   </dependency>
   		<dependency>
			<groupId>org.apache.hive</groupId>
			<artifactId>hive-jdbc</artifactId>
			<version>2.1.1</version>
			<classifier>standalone</classifier>
			<exclusions>
				<exclusion>
					<groupId>org.slf4j</groupId>
					<artifactId>slf4j-log4j12</artifactId>
				</exclusion>
				<exclusion>
					<groupId>log4j</groupId>
					<artifactId>log4j</artifactId>
				</exclusion>
				 <exclusion>
                    <artifactId>log4j-over-slf4j</artifactId>
                    <groupId>org.slf4j</groupId>
                 </exclusion>
				 <exclusion>
                    <artifactId>log4j-over-slf4j</artifactId>
                    <groupId>org.slf4j</groupId>
                 </exclusion>
			</exclusions>
		</dependency>
		

		<dependency>
		    <groupId>org.apache.poi</groupId>
		    <artifactId>poi</artifactId>
		    <version>${poi.version}</version>
		</dependency>
		<dependency>
		    <groupId>org.apache.poi</groupId>
		    <artifactId>poi-ooxml</artifactId>
		    <version>${poi.version}</version>
		</dependency>
		<dependency>
		    <groupId>org.apache.poi</groupId>
		    <artifactId>poi-scratchpad</artifactId>
		    <version>${poi.version}</version>
		</dependency>
		<dependency>
		    <groupId>org.apache.poi</groupId>
		    <artifactId>poi-examples</artifactId>
		    <version>${poi.version}</version>
		</dependency>
		<dependency>
		    <groupId>org.apache.poi</groupId>
		    <artifactId>poi-excelant</artifactId>
		    <version>${poi.version}</version>
		</dependency>
		<dependency>
		    <groupId>org.apache.poi</groupId>
		    <artifactId>poi-ooxml-schemas</artifactId>
		    <version>${poi.version}</version>
		</dependency>
  </dependencies>
  
  
  <build>
	  <plugins>
	  	 <!-- lib 打包到target目录 -->
		  <plugin>
		  <groupId>org.apache.maven.plugins</groupId>
		  <artifactId>maven-jar-plugin</artifactId>
		  <configuration>
		    <archive>
		      <manifest>
		        <!-- 添加依赖jar路径 -->
		        <addClasspath>true</addClasspath>
		        <!-- 入口程序 -->
		        <mainClass>com.cmhk.cmhkHiveHelper.CmhkHiveHelperApplication</mainClass>
		        <classpathPrefix>lib/</classpathPrefix>
		      </manifest>
		    </archive>
		  </configuration>
		</plugin>
		
	  </plugins>
  </build>
  
</project>

------------- 用cmd 到项目目录中,通过命令把所需的依赖jar打包到 target 目录的 lib 目录中 ------------------
1、mvn clean install
2、mvn clean dependency:copy-dependencies -DoutputDirectory=target/lib package
3、刷新目录,看到生成的lib目录,把 lib目录和 jar包 两个 copy 到 服务器上,用命令运行即可,如:
/xxx/jdk1.8_LIN_X64/bin/java -jar /xxx/cmhkHiveHelper/cmhkHiveHelper-0.0.1-SNAPSHOT.jar

### Spring Boot 打包生成 JAR 文件教程 Spring Boot 提供了一种简单的方式来将项目打包为可执行的 JAR 文件。以下是关于如何实现这一目标的具体说明。 #### 配置 `pom.xml` 文件 为了支持生成可执行的 JAR 文件,需要在项目的 Maven 构建配置文件 (`pom.xml`) 中添加特定插件。具体来说,需引入 `spring-boot-maven-plugin` 插件来处理构建过程中的依赖项和资源文件[^3]: ```xml <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> ``` 此插件负责将所有的依赖项嵌入到最终的 JAR 文件中,并设置入口类以便于运行时加载应用上下文。 #### 使用 IDEA 进行打包 如果开发者正在使用 IntelliJ IDEA 开发工具,则可以通过内置功能完成打包操作。步骤如下: 1. 右键点击项目根目录; 2. 选择 **"Build Artifacts" -> "Build"** 或者直接通过菜单栏进入对应的选项卡进行编译并生成目标产物(即 `.jar` 文件)。完成后会在指定路径下找到所需的 JAR 文件[^2]。 #### 命令行方式打包 对于偏好命令行或者希望自动化部署流程的情况,也可以利用 Maven 来完成同样的任务。只需切换至终端窗口,在上述修改后的 POM 文件所在的目录内输入以下指令即可触发整个构建周期直至产出成品: ```bash mvn clean package ``` 这条语句会先清理旧版本再重新创建新的工件存储库(`target`)下的`.jar`形式的应用程序镜像副本[^4]。 #### 启动已打包好的 Jar 一旦成功制作出了独立式的 Java 归档文档之后, 就能够借助标准 JVM 参数轻松激活它了 : ```bash java -jar your-application-name.jar ``` 这一步骤将会初始化内部 Web 容器(如 Tomcat),从而使得服务能够在预设端口号上监听请求,默认情况下该数值设定为8080。 --- ### 注意事项 当涉及到频繁更新场景时,考虑到效率问题,可以采用部分重写而非全量重构策略——也就是所谓的“增量压缩”。例如针对某些子模块单独实施更新动作而不必每次都重复处理全部内容。实际做法可通过下面这个例子体现出来: ```bash jar -uvf myapp-0.0.1-SNAPSHOT.jar BOOT-INF/lib/some-library-version.jar ``` 这里展示了怎样仅替换掉某个第三方组件实例而保留其余不变的部分方法论[^5]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值