如何将本地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。

其他回答

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

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

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

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

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

依赖关系的重要部分是:${pom.basedir}(而不仅仅是${basedir})

<dependency>
    <groupId>org.example</groupId>
    <artifactId>example</artifactId>
    <version>1.0</version>
    <scope>system</scope>
    <systemPath>${pom.basedir}/src/lib/example.jar</systemPath>
</dependency>

步骤1:使用pom.xml中的目标安装文件配置maven安装插件

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-install-plugin</artifactId>
    <executions>
        <execution>
            <id>install-external-non-maven-jar-MWS-Client-into-local-maven-repo</id>
            <phase>clean</phase>
            <configuration>
                <repositoryLayout>default</repositoryLayout>
                <groupId>com.amazonservices.mws</groupId>
                <artifactId>mws-client</artifactId>
                <version>1.0</version>
                <file>${project.basedir}/lib/MWSClientJavaRuntime-1.0.jar</file>
                <packaging>jar</packaging>
                <generatePom>true</generatePom>
            </configuration>
            <goals>
                <goal>install-file</goal>
            </goals>
        </execution>
    </executions>
</plugin>

确保根据您的实际文件路径编辑文件路径(建议将这些外部非maven jar放在某个文件夹中,比如说lib,并将该lib文件夹放在项目中,以便使用项目特定的相对路径,避免添加系统特定的绝对路径)。

如果您有多个外部jar,只需对同一maven安装插件中的其他jar重复<execution>即可。

步骤2:在pom.xml文件中如上所示配置maven安装插件后,您必须像往常一样使用pom.xml中的这些jar:

    <dependency>
        <groupId>com.amazonservices.mws</groupId>
        <artifactId>mws-client</artifactId>
        <version>1.0</version>
    </dependency>

注意,maven安装插件只将外部jar复制到本地的.m2 maven存储库中。就是这样。它不会自动将这些jar作为maven依赖项包含到项目中。

这是一个小问题,但有时很容易忽略。

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

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