1. 程式人生 > 其它 >2021-11-11 Maven引入本地jar包並生成jar包執行修改MANIFEST.MF檔案

2021-11-11 Maven引入本地jar包並生成jar包執行修改MANIFEST.MF檔案

從第三方下載的jar複製到src/main/resources/lib目錄下(新建lib目錄),

引用方式:

<dependency>
  <groupId>SMS_SDK_JAVA</groupId>
  <artifactId>SMS_SDK_JAVA</artifactId>
  <version>v2.6.3r</version>
  <scope>system</scope>
  <systemPath>${project.basedir}/src/main/resources/lib/SMS_SDK_JAVA_v2.6
.3r.jar</systemPath> </dependency>

這裡的scope只能是system範圍,systemPath屬性指定jar的路徑。

看下原本的打包方式:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <version>2.3.2</version>
  <configuration>
    <archive>
      <manifest>
        <-- 在jar包的MF檔案中生成Class-Path屬性 -->
        <addClasspath>true
</addClasspath> <-- Class-Path 字首 --> <classpathPrefix>lib/</classpathPrefix> <-- main全限定包名 --> <mainClass>xxx.MainTask</mainClass> </manifest> </archive> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.3
</version> <configuration> <descriptors> <descriptor>src/main/resources/assembly.xml</descriptor> </descriptors> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin>

assembly.xml就不貼出來了,主要工作就是打tar.gz包。 打出來的jar包中,並不包含system範圍的jar包,就是第三方的包。並且在jar包的MF檔案的classpath也未找到第三方的lib路徑。

最後修改如下:

<configuration>
  <archive>
    <manifest>
      <addClasspath>true</addClasspath>
      <classpathPrefix>lib/</classpathPrefix>
      <mainClass>com.madhouse.platform.maddsp.task.MainTask</mainClass>
    </manifest>
    <-- 新增classpath缺少的內容-->
    <manifestEntries>
      <Class-Path>lib/SMS_SDK_JAVA_v2.6.3r.jar</Class-Path>
    </manifestEntries>
  </archive>
</configuration>

最主要是這個地方:

這樣第三方的jar包就包含在classpath中,執行的java -jar xxx.jar的時候,也就不會報NoClassFound錯誤了。

完整的MF內容如下:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: zhujiajun
Build-Jdk: 1.7.0_72
Main-Class: xxx.MainTask
Class-Path: lib/SMS_SDK_JAVA_v2.6.3r.jar lib/org.apache.oltu.oauth2.resourceserver-1.0.0.jar l
 ib/org.apache.oltu.oauth2.common-1.0.0.jar lib/json-20131018.jar lib/
 org.apache.oltu.oauth2.resourceserver-filter-1.0.0.jar lib/org.apache
 .oltu.oauth2.client-1.0.0.jar lib/druid-1.0.14.jar lib/ehcache-core-2
 .6.10.jar lib/spring-websocket-4.1.2.RELEASE.jar lib/spring-core-4.1.
 2.RELEASE.jar lib/spring-messaging-4.1.2.RELEASE.jar lib/spring-beans
 -4.1.2.RELEASE.jar lib/mail-1.4.4.jar lib/activation-1.1.jar lib/slf4
 j-api-1.7.5.jar lib/logback-classic-1.0.11.jar lib/logback-core-1.0.1
 1.jar lib/poi-3.11.jar lib/commons-codec-1.9.jar lib/poi-ooxml-3.11.j
 ar lib/poi-ooxml-schemas-3.11.jar lib/xmlbeans-2.6.0.jar lib/stax-api
 -1.0.1.jar lib/junit-4.10.jar lib/hamcrest-core-1.1.jar lib/spring-te
 st-4.1.2.RELEASE.jar lib/fastjson-1.1.37.jar lib/commons-fileupload-1
 .3.1.jar lib/commons-io-2.2.jar lib/httpmime-4.3.5.jar lib/httpclient
 -4.3.5.jar lib/httpcore-4.3.2.jar lib/commons-logging-1.1.3.jar lib/j
 stl-1.2.jar lib/jedis-2.6.0.jar lib/commons-pool2-2.0.jar lib/mybatis
 -3.2.8.jar lib/mybatis-spring-1.2.2.jar lib/mysql-connector-java-5.1.
 31.jar lib/spring-web-4.1.2.RELEASE.jar lib/spring-aop-4.1.2.RELEASE.
 jar lib/aopalliance-1.0.jar lib/spring-webmvc-4.1.2.RELEASE.jar lib/s
 pring-expression-4.1.2.RELEASE.jar lib/spring-context-4.1.2.RELEASE.j
 ar lib/spring-context-support-4.1.2.RELEASE.jar lib/spring-jdbc-4.1.2
 .RELEASE.jar lib/spring-aspects-4.1.2.RELEASE.jar lib/aspectjweaver-1
 .8.3.jar lib/spring-tx-4.1.2.RELEASE.jar lib/spring-data-redis-1.4.2.
 RELEASE.jar

另外如果有需要可以定製MF檔案,參考Apache Maven manifestEntries官網說明IBM Apache Maven您不知道的5件事

檔案的詳解:

JAR包中的MANIFEST.MF檔案詳解以及編寫規範

專案引入第三方包,及把依賴的jar包抽離出來。pom.xml檔案的配置。

<build>
        <plugins>
        <!-- 第一步:打包業務jar包,排除配置檔案 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                 <configuration>
                    <archive>
                        <manifest>
                            <mainClass>cn.paohe.Application</mainClass>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib</classpathPrefix>
                        </manifest>
                        <!-- 新增classpath缺少的內容,要拼在一起,要不然會只讀最後一個-->
                        <manifestEntries>
                            <Class-Path>resources/ lib/ssosdk-2.0.0.jar lib/ssomodel-2.0.0.jar</Class-Path>
                        </manifestEntries>
                    </archive>
                    <outputDirectory>${project.build.directory}</outputDirectory>
                    <excludes>
                        <exclude>**/*.properties</exclude>
                        <exclude>**/*.xml</exclude>
                        <exclude>**/*.yml</exclude>
                        <exclude>static/**</exclude>
                        <exclude>templates/**</exclude>
                    </excludes> 
                </configuration>
            </plugin>
            <!-- 第二步:將三方依賴jar包輸出到步驟一指定目錄lib -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>

                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!-- 第三步:將靜態資源配置檔案輸出到步驟一指定目錄resources -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                    <id>copy-resources</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <encoding>UTF-8</encoding> 
                        <outputDirectory>${project.build.directory}/resources</outputDirectory>
                        <resources>
                            <resource>
                                <directory>src/main/resources</directory>
                                <filtering>true</filtering>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
                </executions>
            </plugin>
            <!-- 最後調整一下spring-boot-maven-plugin -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>cn.paohe.Application</mainClass>
                    <includes>
                        <include>
                             <groupId>nothing</groupId>
                             <artifactId>nothing</artifactId>
                        </include>
                    </includes>
                    <addResources>true</addResources>
                    <!--值為true是指打包時包含scope為system的第三方Jar包-->
                    <includeSystemScope>true</includeSystemScope>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

打包後項目的目錄。