maven引用本地jar包

背景:

公司前置的项目包对接客户公司的sdk包,由于此前置项目完全定制化,且改sdk包非通用包,不好上传至公司的maven私服使用,故引入本地jar包,此处总结改方案

1. 代码引入第三方jar包

在项目中新建成lib文件夹,结构与srv平级

img

2. pom文件修改

<dependency>
    <groupId>cn.xxx</groupId>
    <artifactId>xxx</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <scope>system</scope>
    <systemPath>${basedir}/lib/cn.xxx.xxx-1.0.0-SNAPSHOT.jar</systemPath>
</dependency>

打包插件配置

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <useUniqueVersions>false</useUniqueVersions>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib</classpathPrefix>
                    </manifest>
                    <manifestEntries>
                        <Class-Path>lib/cn.xxx.xxx-1.0.0-SNAPSHOT.jar</Class-Path>
                    </manifestEntries>
                </archive>
            </configuration>
        </plugin>
         <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>target/lib</outputDirectory>
                            <excludeArtifactIds>
                                cn.xxx.xxx-1.0.0-SNAPSHOT.jar
                            </excludeArtifactIds>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
    
        <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.3.0</version>
                <executions>
                    <execution>
                        <id>copy-resources-ext</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>target/lib</outputDirectory>
                            <overwrite>true</overwrite>
                            <resources>
                                <resource>
                                    <directory>${basedir}/lib</directory>
                                    <includes>
                                        <include>*.jar</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
    </plugins>
</build>

该插件三个部分

  1. 将本地包打包至指定位置lib下
  2. 所有的jar包拷贝至target/lib下,出了改本地包
  3. 将所有的target/lib包拷贝至jar包外的lib包下(这样实现第三方jar包与业务包分离,而不是打包成一个整体,方便后续更新只更新指定的包)

至此,引入本地包可运行项目,打包也是正常

posted @   God-slayer  阅读(122)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
点击右上角即可分享
微信分享提示