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


当前回答

在Apache Maven 3.5.4中,我必须添加双引号。如果没有双重报价,这对我来说是行不通的。

例子:mvn安装:安装文件“-Dfile=jar文件的位置”“-DgroupId=组id”“-DastifactId=工件id”“-D版本=版本”“-Dpackage=包类型”

其他回答

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

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

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

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

首先,我想把这个答案归功于一位匿名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的文件夹。当提供者没有公共存储库并且您需要手动添加大量库时,这非常有用。我决定构建一个.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文件中添加您自己的本地JAR,并在maven构建中使用它。

mvn install:install-file -Dfile=path-to-jar -DgroupId=owngroupid -DartifactId=ownartifactid -Dversion=ownversion -Dpackaging=jar

例如:

mvn install:install-file -Dfile=path-to-jar -DgroupId=com.decompiler -DartifactId=jd-core-java -Dversion=1.2 -Dpackaging=jar

然后将其添加到POM中,如下所示: