添加本地JAR文件到Maven项目中

第一种方案:将jar安装到本地Maven仓库

第一步

首先确定你的电脑已经安装了Maven。在命令行中键入mvn -v命令,如果出现类似如下图所示,说明你的电脑已经安装了Maven,可进行第二步,如果没有请安装Maven

检查是否安装maven

第二步

安装jar到本地仓库

命令:mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>

<path-to-file>: 要安装的JAR的本地路径 
<group-id>:要安装的JAR的Group Id 
<artifact-id>: 要安装的JAR的 Artificial Id 
<version>: JAR 版本 

<packaging>: 打包类型,例如JAR

NOTE:最好在pom.xml文件所在的目录运行上述命令,个人经验不在根目录运行有时会安装不成功

第三步

使用,例如我安装了百度推送JAR到本地,我就可以在pom.xml文件中这样引用它了

<dependency>
    <groupId>com.baidu.app</groupId>
    <artifactId>bdpush</artifactId>
    <version>3.0.1</version>
</dependency>
  • 总结:这种方法弊端较大,程序的可维护性以及移植性较低。例如当你改变本地Maven仓库时需要重新安装。如果引用此JAR的项目是多人协调工作的项目,则每个人都要将其安装在自己的本地仓库。

解决办法

可以将此JAR文件放在工程的根目录下,让其随着项目走,然后在pom.xml文件中使用maven-install-pluginMaven初始化阶段完成安装。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-install-plugin</artifactId>
    <version>2.5</version>
    <executions>
        <execution>
            <phase>initialize</phase>
            <goals>
                <goal>install-file</goal>
            </goals>
            <configuration>
                <groupId>com.baidu.app</groupId>
                <artifactId>bdpush</artifactId>
                <version>3.0.1</version>
                <packaging>jar</packaging>
                <file>${basedir}/lib/bdpush-3.0.1.ja</file>
            </configuration>
        </execution>
    </executions>
</plugin>
  • 我使用IDEA上面的代码是可以工作的,如果你们使用Eclipse报错的话,加入如下代码
<pluginManagement>
    <plugins>
        <!--This plugin's configuration is used to store Eclipse m2e settings only. 
            It has no influence on the Maven build itself. -->
        <plugin>
            <groupId>org.eclipse.m2e</groupId>
            <artifactId>lifecycle-mapping</artifactId>
            <version>1.0.0</version>
            <configuration>
                <lifecycleMappingMetadata>
                    <pluginExecutions>
                        <pluginExecution>
                            <pluginExecutionFilter>
                                <groupId>org.codehaus.mojo</groupId>
                                <artifactId>aspectj-maven-plugin</artifactId>
                                <versionRange>[1.0,)</versionRange>
                                <goals>
                                    <goal>test-compile</goal>
                                    <goal>compile</goal>
                                </goals>
                            </pluginExecutionFilter>
                            <action>
                                <execute />
                            </action>
                        </pluginExecution>
                        <pluginExecution>
                            <pluginExecutionFilter>
                                <groupId>
                                    org.apache.maven.plugins
                                </groupId>
                                <artifactId>
                                    maven-install-plugin
                                </artifactId>
                                <versionRange>
                                    [2.5,)
                                </versionRange>
                                <goals>
                                    <goal>install-file</goal>
                                </goals>
                            </pluginExecutionFilter>
                            <action>
                                <execute>
                                    <runOnIncremental>false</runOnIncremental>
                                </execute>
                            </action>
                        </pluginExecution>
                    </pluginExecutions>
                </lifecycleMappingMetadata>
            </configuration>
        </plugin>
    </plugins>
</pluginManagement>
  • 下面是我在IDEA中使用SpringBoot时候的配置
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
            <phase>initialize</phase>
            <goals>
                <goal>install-file</goal>
            </goals>
            <configuration>
                <groupId>com.baidu.app</groupId>
                <artifactId>bdpush</artifactId>
                <version>3.0.1</version>
                <packaging>jar</packaging>
                <file>${basedir}/lib/bdpush-3.0.1.jar</file>
            </configuration>
        </execution>
    </executions>
</plugin>

${basedir}表示pom.xml文件所在的目录

第二种方案

第二种方法比较粗暴简单,具体为将依赖设置为系统域,通过完全路径引用。例如要引用的JAR文件在 <PROJECT_ROOT_FOLDER>/lib下,那么使用如下方法添加依赖

<dependency>
    <groupId>com.baidu.app</groupId>
    <artifactId>bdpush</artifactId>
    <version>3.0.1</version>
    <scope>system</scope>
    <systemPath>${basedir}/lib/bdpush-3.0.1.ja</systemPath>
</dependency>

${basedir}表示pom.xml文件所在的目录,例如你的JAR文件在D盘下的jarLibs里面,就将${basedir}替换为“D:/jarLibs”即可。

note: 这种方法我自己在SpringBoot项目中打包成war文件时,没有成功打包到里面

第三种方案

第三种方案与第一种差不多,不同的是JAR文件被安装在一个单独的仓库里。这个本地仓库建在你项目的根目录下,随着项目走。

例如 
1:我们在${basedir}pom.xml文件所在路径)目录下建立一个叫“maven-repository”的本地仓库。

2:使用如下命令安装我们要引用的JAR到此仓库中

mvn deploy:deploy-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=jar -Durl=file:./maven-repository/ -DrepositoryId=maven-repository -DupdateReleaseInfo=true    

3:在pom.xml中如下使用

申明仓库

<repositories>
    <repository>
        <id>maven-repository</id>
        <url>file:///${project.basedir}/maven-repository</url>
    </repository>
</repositories>

然后添加引用

<dependency>
    <groupId>com.baidu.app</groupId>
    <artifactId>bdpush</artifactId>
    <version>3.0.1</version>
</dependency>

转载自: https://blog.csdn.net/ShuSheng0007/article/details/78547264

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Maven项目引入本地的jar包有两种方式。 第一种方式是将本地的jar包手动安装到本地的Maven仓库,然后在项目pom.xml文件添加相关的依赖。首先,将本地的jar包安装到Maven仓库,可以通过以下命令实现: ``` mvn install:install-file -Dfile=path/to/jarfile.jar -DgroupId=groupId -DartifactId=artifactId -Dversion=version -Dpackaging=jar ``` 其,`path/to/jarfile.jar`为本地jar包的路径,`groupId`为指定的组织ID,`artifactId`为项目ID,`version`为版本号。安装成功后,可以在项目pom.xml文件添加以下依赖: ``` <dependency> <groupId>groupId</groupId> <artifactId>artifactId</artifactId> <version>version</version> </dependency> ``` 替换其的`groupId`、`artifactId`和`version`为实际的值。 第二种方式是通过使用Maven的system scope将本地的jar包引入到项目。在项目pom.xml文件添加以下依赖: ``` <dependency> <groupId>groupId</groupId> <artifactId>artifactId</artifactId> <version>version</version> <scope>system</scope> <systemPath>path/to/jarfile.jar</systemPath> </dependency> ``` 同样,需要替换其的`groupId`、`artifactId`、`version`和`path/to/jarfile.jar`为实际的值。 需要注意的是,第一种方式可以保证在其他机器上构建项目时也能正确引入该jar包,而第二种方式则需要保证在构建项目时本地的jar包路径是正确的。因此,推荐使用第一种方式将本地的jar包安装到Maven仓库

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值