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

Posted zhjh256

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了包含MANIFEST.MF的jar可执行应用指定classpath及spring boot应用增量升级打包实现相关的知识,希望对你有一定的参考价值。

对于不包含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应用增量升级打包实现的主要内容,如果未能解决你的问题,请参考以下文章

jar包的MANIFEST.MF文件

关于Java打依赖于第三方Jar的可执行Jar包

JAR包结构,META-INF/MANIFEST.MF文件详细说明[全部属性][打包][JDK]

Java Jar Manifest

java命令执行jar包的方式

java命令执行jar包的方式