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


当前回答

在我的案例中,我的现有maven构建已经在将有问题的jar文件安装到我的本地存储库中,但我没有看到预期的结果,因为本地构建会生成后缀为“-SNAPSHOT”的工件。对我来说,非常简单的解决方案是更新使用本地构建jar文件的项目的pom.xml文件,以匹配名称。

<myproject.foo.version>1.88-SNAPSHOT</myproject.foo.version>

而不是

<myproject.foo.version>1.88</myproject.foo.version>

结果是,如果您的jar文件似乎已经在local.m2/maven repo中,那么很可能是这样,您只需要相应地更新依赖关系。

其他回答

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

没有正确的方法可以使用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进行编译,请检查这个相关问题:我可以在不安装jar的情况下将它们添加到maven 2构建类路径吗?

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

指向${project.directory}/rebo/或file://uri的pom存储库可以与源驻留jarfiles一起工作,用于一个小项目的dep。这些信息不会泄漏到系统~/.m2 repo中,但如果未指定,则会影响maven版本选择与(通常是最近的)顶级版本的选择。

对于现有的dep,如果您可以使用“target”作为您最喜欢的结构或maven的默认值运行,则在分发时绑定的dep与shade类似,但更直接:

-cp“$PWD/target/classs:$PWD/target/lib/*”pkg.random.ClassWithMain

then

<build>
        <plugins>
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>install</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>

我想要这样的解决方案-在pom文件中使用maven安装插件:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-install-plugin</artifactId>
    <version>2.5.2</version>
    <executions>
        <execution>
            <phase>initialize</phase>
            <goals>
                <goal>install-file</goal>
            </goals>
            <configuration>
                <file>lib/yourJar.jar</file>
                <groupId>com.somegroup.id</groupId>
                <artifactId>artefact-id</artifactId>
                <version>x.y.z</version>
                <packaging>jar</packaging>
            </configuration>
        </execution>
    </executions>
</plugin>

在这种情况下,您可以执行mvn初始化,jar将安装在本地maven repo中。现在,这个jar可以在这台机器上的任何maven步骤中使用(不要忘记在pom中使用<dependency></dependency>标记将这个依赖项作为其他maven依赖项)。也可以不将jar安装绑定到初始化步骤,而是绑定到您喜欢的任何其他步骤。

命令行:

mvn install:install-file -Dfile=c:\kaptcha-{version}.jar -DgroupId=com.google.code
-DartifactId=kaptcha -Dversion={version} -Dpackaging=jar