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


当前回答

当然,您可以将jar添加到该文件夹中。但也许这并不是你想要实现的。。。

如果您需要这些jar进行编译,请检查这个相关问题:我可以在不安装jar的情况下将它们添加到maven 2构建类路径吗?

此外,在任何人提出建议之前,不要使用系统范围。

其他回答

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

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

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

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

 }

我认为这个问题的更好解决方案是使用maven安装插件在安装时自动安装文件。这是我为项目设置的方式。

首先,将路径(存储local.jar的位置)添加为属性。

<properties>
    <local.sdk>/path/to/jar</local.sdk>
</properties>

然后,在插件下添加一个插件,以便在编译时安装jar。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-install-plugin</artifactId>
    <version>2.5.2</version>
    <executions>
        <execution>
            <id>1</id>
            <phase>initialize</phase>
            <goals>
                <goal>install-file</goal>
            </goals>
            <configuration>
                <groupId>com.local.jar</groupId> 
                <artifactId>appengine-api</artifactId>
                <version>1.0</version>
                <packaging>jar</packaging>
                <file>${local.sdk}/lib/impl/appengine-api.jar</file>
            </configuration>
        </execution>
        <execution>
            <id>appengine-api-stubs</id>
            <phase>initialize</phase>
            <goals>
                <goal>install-file</goal>
            </goals>
            <configuration>
                <groupId>com.local.jar</groupId>
                <artifactId>appengine-api-stubs</artifactId>
                <version>1.0</version>
                <packaging>jar</packaging>
                <file>${local.sdk}/lib/impl/appengine-api-stubs.jar</file>
            </configuration>
        </execution>
    </executions>
</plugin>

最后,在依赖项中,可以添加jar

<dependency>
    <groupId>com.local.jar</groupId>
    <artifactId>appengine-api</artifactId>
    <version>1.0</version>
</dependency>

<dependency>
    <groupId>com.local.jar</groupId>
    <artifactId>appengine-api-stubs</artifactId>
    <version>1.0</version>
    <scope>test</scope>
</dependency>

通过这样设置项目,即使您将其带到另一台计算机,项目也将继续构建(假定它在local.sdk属性指定的路径中包含所有jar文件)。

对于groupId,请使用唯一的名称,以确保没有冲突。

现在,当您进行mvn安装或mvn测试时,将自动添加本地jar。

这是较新版本的简短语法:

mvn install:install-file -Dfile=<path-to-file>

当JAR由ApacheMaven构建时,这是最常见的情况。然后它将在META-INF目录的子文件夹中包含一个pom.xml,默认情况下将读取该文件。

资料来源:http://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html

首选方法是创建自己的远程存储库。

有关如何执行此操作的详细信息,请参阅此处。查看“上载到远程存储库”部分。