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


当前回答

我想分享一个代码,你可以上传一个装满jar的文件夹。当提供者没有公共存储库并且您需要手动添加大量库时,这非常有用。我决定构建一个.bat而不是直接调用maven,因为这可能是内存不足错误。它是为windows环境准备的,但很容易适应linux操作系统:

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;

public class CreateMavenRepoApp {

    private static final String OCB_PLUGIN_FOLDER = "C://your_folder_with_jars";

    public static void main(String[] args) throws IOException {

    File directory = new File();
    //get all the files from a directory
    PrintWriter writer = new PrintWriter("update_repo_maven.bat", "UTF-8");
    writer.println("rem "+ new Date());  
    File[] fList = directory.listFiles();
    for (File file : fList){
        if (file.isFile()){               
        String absolutePath = file.getAbsolutePath() ;
        Manifest  m = new JarFile(absolutePath).getManifest();
        Attributes attributes = m.getMainAttributes();
        String symbolicName = attributes.getValue("Bundle-SymbolicName");

        if(symbolicName!=null &&symbolicName.contains("com.yourCompany.yourProject")) {
            String[] parts =symbolicName.split("\\.");
            String artifactId = parts[parts.length-1];
            String groupId = symbolicName.substring(0,symbolicName.length()-artifactId.length()-1);
            String version = attributes.getValue("Bundle-Version");
            String mavenLine= "call mvn org.apache.maven.plugins:maven-install-plugin:2.5.1:install-file -Dfile="+ absolutePath+" -DgroupId="+ groupId+" -DartifactId="+ artifactId+" -Dversion="+ version+" -Dpackaging=jar ";
            writer.println(mavenLine);          
        }

        }
    }
    writer.close();
    }

}

从任何IDE运行此main之后,运行update_repo_maven.bat。

其他回答

我想要这样的解决方案-在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安装绑定到初始化步骤,而是绑定到您喜欢的任何其他步骤。

您可以直接添加本地依赖项(如build maven项目中所述,包含专有库),如下所示:

<dependency>
    <groupId>com.sample</groupId>
    <artifactId>sample</artifactId>
    <version>1.0</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/src/main/resources/Name_Your_JAR.jar</systemPath>
</dependency>

使现代化

在新版本中,此功能被标记为已弃用,但仍在工作,尚未删除(您只需在maven启动期间的日志中看到警告)。maven集团对此提出了一个问题https://issues.apache.org/jira/browse/MNG-6523(您可以参与并描述此功能在某些情况下有帮助的原因)。我希望这一功能仍然存在!

若你们问我,只要这个特性没有被删除,我就用它来依赖我项目中的一个不适合存储库的jar文件。如果删除此功能,那么,这里有很多好的答案,我可以稍后选择!

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

 }

根项目上进行构建

您可以在命令行中编写以下代码,或者如果您使用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>