如何将本地jar文件(还不是Maven存储库的一部分)直接添加到项目的库源中?


当前回答

根项目上进行构建

您可以在命令行中编写以下代码,或者如果您使用eclipse内置maven,右键单击项目->运行方式->运行配置…->在左侧面板中,右键单击Maven Build->new configuration->在基本目录中的Goals&中编写代码:${project_loc:NameOfYourProject}->运行

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

其中每一个都指:

<path to file>:要加载的文件的路径,例如->c:\kapcha-2.3jar

<group id>:文件应在例如com.google.code下注册的组

<artifact-id>:文件的工件名称,例如->kappcha

<version>:文件的版本,例如->2.3

<package>:文件的打包,例如->jar

2.安装后,只需在pom.xml中声明jar。

 <dependency>
      <groupId>com.google.code</groupId>
      <artifactId>kaptcha</artifactId>
      <version>2.3</version>
 </dependency>

其他回答

首先,我想把这个答案归功于一位匿名StackOverflow用户——我很肯定我以前在这里也看到过类似的答案——但现在我找不到了。

将本地JAR文件作为依赖项的最佳选择是创建本地Maven存储库。这样的存储库只不过是一个适当的目录结构,其中包含pom文件。

例如:我的主项目位于${master_project}位置,子项目1位于${master _project}/${subject1}位置。

然后,我在以下位置创建Maven存储库:${master_project}/本地maven repo。

在子项目1中位于${master_project}/${subject1}/pom.xml的pom文件中,需要指定存储库,该存储库将文件路径作为URL参数:

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

可以为任何其他存储库指定依赖项。这使pom存储库独立。例如,一旦所需的JAR在Maven central中可用,您只需从本地回购中删除它,它将从默认回购中取出。

    <dependency>
        <groupId>org.apache.felix</groupId>
        <artifactId>org.apache.felix.servicebinder</artifactId>
        <version>0.9.0-SNAPSHOT</version>
    </dependency>

最后但并非最不重要的一件事是使用-DlocalRepositoryPath开关将JAR文件添加到本地存储库,如下所示:

mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file  \
    -Dfile=/some/path/on/my/local/filesystem/felix/servicebinder/target/org.apache.felix.servicebinder-0.9.0-SNAPSHOT.jar \
    -DgroupId=org.apache.felix -DartifactId=org.apache.felix.servicebinder \
    -Dversion=0.9.0-SNAPSHOT -Dpackaging=jar \
    -DlocalRepositoryPath=${master_project}/local-maven-repo

一旦安装了JAR文件,就可以将Maven repo提交到代码存储库,并且整个设置与系统无关。(GitHub中的工作示例)。

我同意将JAR提交给源代码回购并不是一个好的做法,但在现实生活中,快速而肮脏的解决方案有时比一个完整的Nexus回购托管一个无法发布的JAR要好。

在本地存储库中,您可以通过发出以下命令来安装jar

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

点击这个有用的链接,从mkyoung的网站进行同样的操作。您也可以查看maven指南

不是最初问题的答案,但它可能对某人有用

没有正确的方法可以使用Maven从文件夹中添加多个jar库。如果只有几个依赖项,那么配置maven安装插件可能更容易,如上面的答案所述。

然而,对于我的特殊情况,我有一个lib文件夹,其中包含100多个专有jar文件,我必须以某种方式添加这些文件。对我来说,将Maven项目转换为Gradle要容易得多。

plugins {
    id 'org.springframework.boot' version '2.2.2.RELEASE'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
    flatDir {
       dirs 'libs' // local libs folder
   }
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
    
    implementation 'io.grpc:grpc-netty-shaded:1.29.0'
    implementation 'io.grpc:grpc-protobuf:1.29.0'
    implementation 'io.grpc:grpc-stub:1.29.0' // dependecies from maven central

    implementation name: 'akka-actor_2.12-2.6.1' // dependecies from lib folder
    implementation name: 'akka-protobuf-v3_2.12-2.6.1'
    implementation name: 'akka-stream_2.12-2.6.1'

 }

步骤1:使用pom.xml中的目标安装文件配置maven安装插件

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-install-plugin</artifactId>
    <executions>
        <execution>
            <id>install-external-non-maven-jar-MWS-Client-into-local-maven-repo</id>
            <phase>clean</phase>
            <configuration>
                <repositoryLayout>default</repositoryLayout>
                <groupId>com.amazonservices.mws</groupId>
                <artifactId>mws-client</artifactId>
                <version>1.0</version>
                <file>${project.basedir}/lib/MWSClientJavaRuntime-1.0.jar</file>
                <packaging>jar</packaging>
                <generatePom>true</generatePom>
            </configuration>
            <goals>
                <goal>install-file</goal>
            </goals>
        </execution>
    </executions>
</plugin>

确保根据您的实际文件路径编辑文件路径(建议将这些外部非maven jar放在某个文件夹中,比如说lib,并将该lib文件夹放在项目中,以便使用项目特定的相对路径,避免添加系统特定的绝对路径)。

如果您有多个外部jar,只需对同一maven安装插件中的其他jar重复<execution>即可。

步骤2:在pom.xml文件中如上所示配置maven安装插件后,您必须像往常一样使用pom.xml中的这些jar:

    <dependency>
        <groupId>com.amazonservices.mws</groupId>
        <artifactId>mws-client</artifactId>
        <version>1.0</version>
    </dependency>

注意,maven安装插件只将外部jar复制到本地的.m2 maven存储库中。就是这样。它不会自动将这些jar作为maven依赖项包含到项目中。

这是一个小问题,但有时很容易忽略。

要安装第三方jar,请调用如下命令

mvn install:install-file -DgroupId= -DartifactId= -Dversion= -Dpackaging=jar -Dfile=path