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

2020-12-02 09:47

阅读:431

标签:log   framework   source   bat   nap   lex   man   parse   end   

对于不包含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.pluginsgroupId>  
                artifactId>maven-jar-pluginartifactId>  
                configuration>  
                    archive>  
                        manifest>  
                            addClasspath>trueaddClasspath>  
                            classpathPrefix>lib/classpathPrefix>  
                            mainClass>com.xxx.uploadFilemainClass>  
                        manifest>  
                    archive>  
                configuration>  
            plugin>  

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

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

plugin>
                groupId>org.apache.maven.pluginsgroupId>
                artifactId>maven-dependency-pluginartifactId>
                version>3.0.1version>
                executions>
                    execution>
                        id>copyid>
                        phase>packagephase>
                        goals>
                            goal>copygoal>
                        goals>
                        configuration>
                            artifactItems>
                                artifactItem>
                                    groupId>junitgroupId>
                                    artifactId>junitartifactId>
                                    version>4.11version>
                                    outputDirectory>${project.build.directory}/lib/lib1outputDirectory>
                                artifactItem>
                                artifactItem>
                                    groupId>org.slf4jgroupId>
                                    artifactId>slf4j-log4j12artifactId>
                                    version>1.7.7version>
                                    outputDirectory>${project.build.directory}/lib/lib1outputDirectory>
                                artifactItem>
                            artifactItems>
                        configuration>
                    execution>
                executions>
            plugin>

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

 plugin>
            groupId>org.apache.maven.pluginsgroupId>
            artifactId>maven-dependency-pluginartifactId>
            executions>
                execution>
                    id>copy-dependenciesid>
                    phase>prepare-packagephase>
                    goals>
                        goal>copy-dependenciesgoal>
                    goals>
                    configuration>
                        outputDirectory>${project.build.directory}/liboutputDirectory>
                        overWriteReleases>falseoverWriteReleases>
                        overWriteSnapshots>falseoverWriteSnapshots>
                        overWriteIfNewer>trueoverWriteIfNewer>
                    configuration>
                execution>
            executions>
        plugin>

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

            plugin>
                groupId>org.springframework.bootgroupId>
                artifactId>spring-boot-maven-pluginartifactId>
                configuration>
                    layout>ZIPlayout>
                    mainClass>org.abc.ConsumerStartermainClass>
                    excludes>
                        exclude>
                            groupId>abc.orggroupId>
                            artifactId>biz-serviceartifactId>
                        exclude>
                    excludes>
                    includeSystemScope>trueincludeSystemScope>
                configuration>
                executions>
                    execution>
                        goals>
                            goal>repackagegoal>
                        goals>
                    execution>
                executions>
            plugin>
            plugin>
                groupId>org.apache.maven.pluginsgroupId>
                artifactId>maven-dependency-pluginartifactId>
                version>3.1.1version>
                executions>
                    execution>
                        id>copyid>
                        phase>packagephase>
                        goals>
                            goal>copygoal>
                        goals>
                        configuration>
                            artifactItems>
                                artifactItem>
                                    groupId>abc.orggroupId>
                                    artifactId>biz-serviceartifactId>
                                    version>1.1.0version>
                                    outputDirectory>${project.build.directory}/liboutputDirectory>
                                artifactItem>
                            artifactItems>
                        configuration>
                    execution>
                executions>
            plugin>

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

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

标签:log   framework   source   bat   nap   lex   man   parse   end   

原文地址:https://www.cnblogs.com/zhjh256/p/10987038.html


评论


亲,登录后才可以留言!