springboot服务引入外部jar包,maven打包部署到linux服务器报找不到jar包中的类

1、问题描述

最近开发了一个springboot程序,需要依赖第三方jar包,这个jar包无法直接通过pom远程仓库下载,需要从自己本地引入,于是配置pom文件如下:将本地jar包引入工程,systemPath为jar所在的本地路径

        <dependency>
            <groupId>com.hikvision.js</groupId>
            <artifactId>masmgc.sdk.mms</artifactId>
            <version>1.0.3-SNAPSHOT</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/masmgc.sdk.mms-1.0.3-SNAPSHOT.jar</systemPath>
        </dependency>
<assembly>  
    <id>bin</id>
    <!-- 最终打包成一个用于发布的zip文件 -->  
    <formats>  
        <format>zip</format>
    </formats>  
  
    <!-- Adds dependencies to zip package under lib directory -->  
    <dependencySets>  
        <dependencySet>  
            <!-- 
               不使用项目的artifact,第三方jar不要解压,打包进zip文件的lib目录 
           -->  
            <useProjectArtifact>false</useProjectArtifact>  
            <outputDirectory>lib</outputDirectory>  
            <unpack>false</unpack>  
        </dependencySet>  
    </dependencySets>  
  
    <fileSets>  
        <!-- 把项目相关的说明文件,打包进zip文件的根目录 -->  
        <fileSet>  
            <directory>${project.basedir}</directory>
            <outputDirectory>/</outputDirectory>
            <includes>  
                <include>README*</include>  
                <include>LICENSE*</include>  
                <include>NOTICE*</include>  
            </includes>  
        </fileSet>  
          
        <!-- 把项目的配置文件,打包进zip文件的config目录 -->  
        <fileSet>
            <directory>${project.basedir}/src/main/resources/</directory>
            <outputDirectory>config</outputDirectory>
            <excludes>
                <exclude>**/mybatis/**</exclude>
                <exclude>**/temp/**</exclude>
            </excludes>
        </fileSet>
        <fileSet>
            <directory>${project.basedir}/src/main/resources/temp</directory>
            <outputDirectory>temp</outputDirectory>
        </fileSet>
        <!-- 把项目脚本,打包进zip文件的script目录 -->
		<fileSet>  
            <directory>${project.basedir}/src/main/bin/</directory>
            <outputDirectory>bin</outputDirectory>
            <includes>  
                <include>*.bat</include>  
                <include>*.sh</include>  
            </includes>  
        </fileSet>

        <!-- 把依赖的本地第三方jar包打包到lib目录下 -->
        <fileSet>
            <directory>${project.basedir}/lib/</directory>
            <outputDirectory>lib</outputDirectory>
        </fileSet>

        <!-- 把项目自己编译出来的jar文件,打包进zip文件的根目录 -->  
        <fileSet>  
            <directory>${project.build.directory}</directory>  
            <outputDirectory></outputDirectory>  
            <includes>  
                <include>deployalarmsm-1.0.0004.jar</include>
            </includes>  
        </fileSet>
    </fileSets>  
</assembly>  
maven-assembly-plugin的打包配置文件package.xml中添加配置,把本地依赖的jar打包到了lib目录下,但服务器上运行还是找不到jar包中的类。

2.问题排查

波折

网上搜索答案,说原因是:

如果项目依赖了pom中定义的dependency之外的外部jar包,maven-jar-plugin默认是不会把这

些额外jar包的依赖信息放在jar文件的MANIFEST.MF文件中的,这会导致jar包运行时出现找不到指定类的错误。

要包jar包添加到配置<Class-Path>中,添加后,打包后查看MANIFEST.MF文件,类路径下已经有了本地依赖的这个jar包,但运行还是报找不到类。。。。。。

 <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <!-- The configuration of the plugin -->
                <configuration>
                    <!-- Configuration of the archiver -->
                    <archive>
                        <!-- 生成的jar中,不要包含pom.xml和pom.properties这两个文件 -->
                        <addMavenDescriptor>false</addMavenDescriptor>
                        <!-- Manifest specific configuration -->
                        <manifest>
                            <!-- 是否要把第三方jar放到manifest的classpath中 -->
                            <addClasspath>true</addClasspath>
                            <!-- 生成的manifest中classpath的前缀,因为要把第三方jar放到lib目录下,所以classpath的前 缀是lib/ -->
                            <classpathPrefix>lib/</classpathPrefix>
                            <!-- 应用的main class -->
                            <mainClass>com.hikvision.js.deployalarmsm.DeployalarmsmApplication</mainClass>
                        </manifest>
                        <manifestEntries>
                            <Class-Path>lib/masmgc.sdk.mms-1.0.3-SNAPSHOT.jar config/</Class-Path>
                        </manifestEntries>
                    </archive>
                    <!-- 过滤掉不希望包含在jar中的文件 -->
                    <excludes>
                        <classesDirectory>${project.basedir}/src/main/resources/</classesDirectory>
                        <exclude>*.properties</exclude>
                        <exclude>*.xml</exclude>
                    </excludes>
                </configuration>
            </plugin>

重新maven打包,发现有个warning:

[WARNING] 
[WARNING] Some problems were encountered while building the effective model for com.hikvision.js.deployalarmsm:deployalarmsm:jar:1.0.0004
[WARNING] 'dependencies.dependency.systemPath' for com.hikvision.js:masmgc.sdk.mms:jar should not point at files within the project directory, ${project.basedir}/lib/masmgc.sdk.mms-1.0.3-SNAPSHOT.jar will be unresolvable by dependent projects @ line 229, column 25
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.

显然提示的是通过system Scope方式引入jar包不建议使用了,甚至高版本的maven已经不支持了,于是需要把第三方jar包发布到maven私服,在通过正常的maven依赖进来;

        <!--第三方jar包引入-->
        <dependency>
            <groupId>com.hikvision.js</groupId>
            <artifactId>masmgc.sdk.mms</artifactId>
            <version>1.0.3-SNAPSHOT</version>
        </dependency>

通过插件将jar包发布到私服,执行clean 即可;

 <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-install-plugin</artifactId>
                <version>2.5.2</version>
                <executions>
                    <execution>
                        <id>install-masmgc.sdk.mms</id>
                        <phase>clean</phase>
                        <configuration>
                            <file>${project.basedir}/lib/masmgc.sdk.mms-1.0.3-SNAPSHOT.jar</file>
                            <repositoryLayout>default</repositoryLayout>
                            <groupId>com.hikvision.js</groupId>
                            <artifactId>masmgc.sdk.mms</artifactId>
                            <version>1.0.3-SNAPSHOT</version>
                            <packaging>jar</packaging>
                            <generatePom>true</generatePom>
                        </configuration>
                        <goals>
                            <goal>install-file</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将外部Jar包引入Spring Boot项目打包Jar包,可以按照以下步骤进行操作: 1. 在pom.xml文件添加依赖项。例如,要引入一个名为example.jar的外部Jar包,可以通过以下方式添加依赖项: ``` <dependency> <groupId>com.example</groupId> <artifactId>example</artifactId> <version>1.0.0</version> <scope>system</scope> <systemPath>${project.basedir}/lib/example.jar</systemPath> </dependency> ``` 其,systemPath指定了外部Jar包的路径,scope设置为system,表示使用系统路径下的Jar包。 2. 将外部Jar包复制到项目的lib目录下,例如,将example.jar复制到项目目录下的lib文件夹。 3. 在pom.xml文件添加Maven插件,以将外部Jar包打包到生成的Jar包。例如,可以添加以下插件: ``` <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.2.0</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>com.example.Application</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> ``` 其,classpathPrefix指定了Jar包lib文件夹下的依赖项路径前缀,mainClass指定了Spring Boot应用程序的主。 4. 使用Maven命令进行打包,生成的Jar包将包含外部Jar包。例如,使用以下命令进行打包: ``` mvn clean package ``` 这样,生成的Jar包就包含了外部Jar包,并可以在运行时使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值