对于不包含MANIFEST.MF,或jar包中的MANIFEST.MF未指定MainClass的jar,可以通过java命令行选项-classpath指定classpath。但是如果是包含MainClass的jar,例如:

Manifest-Version: 1.0
Bundle-License: http://www.apache.org/licenses/LICENSE-2.0.txt
Bundle-SymbolicName: org.mybatis.generator.mybatis-generator-core
Archiver-Version: Plexus Archiver
Built-By: zjhua
Bnd-LastModified: 1559632976998
Specification-Title: MyBatis Generator Core
Implementation-Vendor-Id: org.mybatis.generator
Bundle-DocURL: http://www.mybatis.org/mybatis-generator/mybatis-genera
tor-core/
Include-Resource: org/mybatis/generator/config/xml/ibator-config_1_0.d
td=src/main/resources/org/mybatis/generator/config/xml/ibator-config_
1_0.dtd,org/mybatis/generator/config/xml/mybatis-generator-config_1_0
.dtd=src/main/resources/org/mybatis/generator/config/xml/mybatis-gene
rator-config_1_0.dtd,org/mybatis/generator/internal/util/messages/mes
sages.properties=src/main/resources/org/mybatis/generator/internal/ut
il/messages/messages.properties
Import-Package: com.mysql.jdbc,javax.xml.parsers,org.apache.commons.la
ng3,org.apache.log4j,org.apache.tools.ant,org.apache.tools.ant.types,
org.w3c.dom,org.xml.sax
Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.6))"
Main-Class: org.mybatis.generator.api.ShellRunner
Implementation-Build-Date: 2019-06-04 07:22:46+0000

其classpath可以通过maven打包插件指定,如下:

            <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.xxx.uploadFile</mainClass>
</manifest>
</archive>
</configuration>
</plugin>

这样和jar文件在同一目录的lib/下的所有jar就都是能够访问到了。

然后我们通过maven-dependency-plugin插件将特定的包拷贝到lib目录,如下:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<outputDirectory>${project.build.directory}/lib/lib1</outputDirectory>
</artifactItem>
<artifactItem>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
<outputDirectory>${project.build.directory}/lib/lib1</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>

也可以拷贝所有依赖,如下:

 <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</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>
</configuration>
</execution>
</executions>
</plugin>

实际情况是我们通常希望基础三方库打到包中,应用jar拷贝到lib目录,同时兼顾易升级和管理复杂性,所以最终是这样的。

            <plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<layout>ZIP</layout>
<mainClass>org.abc.ConsumerStarter</mainClass>
<excludes>
<exclude>
<groupId>abc.org</groupId>
<artifactId>biz-service</artifactId>
</exclude>
</excludes>
<includeSystemScope>true</includeSystemScope>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>abc.org</groupId>
<artifactId>biz-service</artifactId>
<version>1.1.0</version>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>

这样打出来的jar就不包含biz-service,在单独的lib目录中。结合layout ZIP特性,就可以完美实现增量升级。

包含MANIFEST.MF的jar可执行应用指定classpath及spring boot应用增量升级打包实现的更多相关文章

  1. 采用MANIFEST.MF之jar报错ClassNotFoundException解法

    检查n多遍也试了n多次,证明下面是MANIFEST.MF文件正确写法: Manifest-Version: 1.0 Premain-Class: cn.yandz.monitor.SizeOfObje ...

  2. 将 Spring boot 项目打成可执行Jar包,及相关注意事项(main-class、缺少 xsd、重复打包依赖)

    最近在看 spring boot 的东西,觉得很方便,很好用.对于一个简单的REST服务,都不要自己部署Tomcat了,直接在 IDE 里 run 一个包含 main 函数的主类就可以了. 但是,转念 ...

  3. [转] - JAR文件包及jar命令详解 ( MANIFEST.MF的用法 )

    常常在网上看到有人询问:如何把 java 程序编译成 .exe 文件.通常回答只有两种,一种是制作一个可执行的 JAR 文件包,然后就可以像. chm 文档一样双击运行了:而另一种是使用 JET 来进 ...

  4. 打包文件 MANIFEST.MF 功能详解

    最近研究了如何在java工程打包,期间遇到的一些问题进行总结,如打包成test.jar 文件 Manifest-Version: 1.0 Main-Class: windows.VideoWindow ...

  5. Spring Boot 打包成的可执行 jar ,为什么不能被其他项目依赖?

    前两天被人问到这样一个问题: "松哥,为什么我的 Spring Boot 项目打包成的 jar ,被其他项目依赖之后,总是报找不到类的错误?" 大伙有这样的疑问,就是因为还没搞清楚 ...

  6. 通过ANT生成MANIFEST.MF中的Class-Path属性

    原文地址:http://reason2003.iteye.com/blog/1627353 之前做一个项目,主程序打包成一个jar文件,因为用到了很多第三方的lib包,所以直接通过java命令运行ja ...

  7. 精尽Spring Boot源码分析 - Jar 包的启动实现

    该系列文章是笔者在学习 Spring Boot 过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring Boot 源码分析 GitHub 地址 进行阅读 Sprin ...

  8. IDEA spring boot项目插件打包方式jar

    一.打包 1.pom.xml中添加插件依赖 <build> <plugins> <plugin> <!--打包成可执行jar--> <groupI ...

  9. jar MANIFEST.MF 汇总

    : Manifest-Version: 1.0Created-By: Apache Ant 1.5.1Extension-Name: Struts FrameworkSpecification-Tit ...

随机推荐

  1. tomcat 安装记录 centos7 开放对外端口

    //端口查询 [root@CentOS7 bin]# firewall-cmd --query-port=9090/tcp no //添加端口 [root@CentOS7 bin]# firewall ...

  2. day 03 预科

    目录 什么是变量: 变量的组成 变量的风格 1.驼峰体 2.下划线 变量名的组成规范 注释的作用 turtle库的简单使用 什么是变量: 1.是变化的量. 2.变:现实世界中的状态是会发生改变的. 3 ...

  3. python爬虫系列:三、URLError异常处理

    1.URLError 首先解释下URLError可能产生的原因: 网络无连接,即本机无法上网 连接不到特定的服务器 服务器不存在 在代码中,我们需要用try-except语句来包围并捕获相应的异常. ...

  4. 复盘一篇讲sklearn库学习文章(上)

    认识 sklearn 官网地址: https://scikit-learn.gor/stable/ 从2007年发布以来, scikit-learn已成为重要的Python机器学习库, 简称sklea ...

  5. django中使用AJAX时如何获取表单参数(按钮携带参数)

    前提是函数和相应的视图路由都已经配置好了,然后就是表单了: <form id="SmsForm" method="post" class="a& ...

  6. go语言笔记2

    上接Go语言学习笔记(一) 11    Go错误处理11.1    nil函数通常在最后的返回值中返回错误信息.使用errors.New 可返回一个错误信息: package main         ...

  7. UDS诊断学习笔记

    定义介绍: UDS(Unified Diagnostic Service)诊断的诊断服务.诊断协议是面向整车所有ECU(电控单元)的一种诊断通信协议,是诊断服务的规范化标准. UDS本质上是一系列服务 ...

  8. ETL工程师笔试题

    1.参考答案 1)建表 CREATE TABLE `ta` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `cx` varchar(20) DEFAULT NUL ...

  9. 使用Arduino开发板实现与MPU6050陀螺仪传感器连接的方法

    MPU6050陀螺仪传感器具有许多强大的功能,采用单芯片封装.它是由一个MEMS加速度计.一个MEMS陀螺仪和温度传感器组成.该模块在将模拟量转换为数字量时非常准确,因为每个通道都有一个16位的模数转 ...

  10. 初学FWT(快速沃尔什变换) 一点心得

    FWT能解决什么 有的时候我们会遇到要求一类卷积,如下: Ci=∑j⊕k=iAj∗Bk\large C_i=\sum_{j⊕k=i}A_j*B_kCi​=j⊕k=i∑​Aj​∗Bk​此处乘号为普通乘法 ...