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


当前回答

我认为这个问题的更好解决方案是使用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。

其他回答

一种方法是将其上传到您自己的Maven存储库管理器(如Nexus)。无论如何,拥有一个自己的存储库管理器是很好的做法。

我最近看到的另一个好方法是在构建生命周期中包含Maven安装插件:在POM中声明将文件安装到本地存储库。这是一个有点但很小的开销,并且不涉及手动步骤。

http://maven.apache.org/plugins/maven-install-plugin/install-file-mojo.html

我想分享一个代码,你可以上传一个装满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。

在我的案例中,我的现有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中,那么很可能是这样,您只需要相应地更新依赖关系。

还可以看看。。。

<scope>compile</scope>

Maven依赖项。这是默认设置,但我发现在某些情况下,也会显式设置Maven的作用域,以便在本地存储库中查找本地库。

我认为这个问题的更好解决方案是使用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。