包含MANIFEST.MF的jar可執行應用指定classpath及spring boot應用增量升級打包實現

對於不包含MANIFEST.MF,或jar包中的MANIFEST.MF未指定MainClass的jar,能夠經過java命令行選項-classpath指定classpath。可是若是是包含MainClass的jar,例如:java

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+0000mysql

其classpath能夠經過maven打包插件指定,以下:spring

            <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就都是可以訪問到了。sql

而後咱們經過maven-dependency-plugin插件將特定的包拷貝到lib目錄,以下:apache

<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>

也能夠拷貝全部依賴,以下:api

 <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目錄,同時兼顧易升級和管理複雜性,因此最終是這樣的。mybatis

            <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特性,就能夠完美實現增量升級。dom

相關文章
相關標籤/搜索