如何将本地jar文件(还不是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'

 }

其他回答

请注意,使用本地回购并不一定是个好主意。如果这个项目与其他人共享,那么当它不起作用时,其他人都会遇到问题和疑问,甚至在您的源代码管理系统中也无法使用jar!

尽管共享回购是最好的解决方案,但如果由于某种原因无法做到这一点,那么嵌入jar比本地回购要好。仅限本地的回购内容可能会导致很多问题,特别是随着时间的推移。

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

没有正确的方法可以使用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'

 }

首先,我想把这个答案归功于一位匿名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文件不包含使用AlirezaFattahi解决方案的第三方库。然而,我记得,我上次在同一个项目中尝试过它之后,它就开始工作了。因此,我尝试了自己的解决方案:

mkdir.m2/repositories目录下的项目路径(类似于该目录下的其他maven依赖项目录)将第三方jar文件放入其中。像maven存储库上的库一样添加依赖项。

最后,它对我有用。:)

在maven项目的根目录创建一个新文件夹,比如本地maven repo。

只需在pom.xml的<project>中添加一个本地repo:

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

然后,对于要安装的每个外部jar,转到项目的根目录并执行:

mvn deploy:deploy-file -DgroupId=[GROUP] -DartifactId=[ARTIFACT] -Dversion=[VERS] -Durl=file:./local-maven-repo/ -DrepositoryId=local-maven-repo -DupdateReleaseInfo=true -Dfile=[FILE_PATH]