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

其他回答

另一个有趣的例子是,当您希望在项目中拥有私有maven jar时。您可能希望保留Maven的功能来解决可传递的依赖关系。解决方案相当简单。

在项目中创建文件夹库在pom.xml文件中添加以下行<properties(财产)><local.repository.folder>${pom.basedir}/libs/</local.reppository.folder></财产><存储库><存储库><id>本地maven存储库</id><url>文件://${local.resource.folder}</url><发布><enabled>真</enabled></releases><快照><enabled>真</enabled></snapshot></repository></存储库>打开.m2/repository文件夹,将要导入的项目的目录结构复制到libs文件夹中。

例如,假设您要导入依赖项

<dependency>
    <groupId>com.mycompany.myproject</groupId>
    <artifactId>myproject</artifactId>
    <version>1.2.3</version>
</dependency>

只需转到.m2/repository,您将看到以下文件夹

com/mycompany/myproject/1.2.3

复制libs文件夹中的所有内容(同样,包括.m2/repository下的文件夹),就完成了。

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

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

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

下载jar文件将jar文件复制到项目文件夹get inteliJ idea Maven命令区键入以下命令

mvn安装:安装文件-Dfile=YOUR_JAR_file_LOCATION*JARNAME.JAR-DgroupId=org.primefaces.themes-DastifactId=iMetro-Dversion=1.0.1-Dpackage=JAR*


例子:

mvn安装:安装文件-Dfile=C:\Users\ranushka.l\Desktop\test\spring-web-1.0.2.jar-DgroupId=org.primefaces.themes-DartifactId=iMetro-Dversion=1.0.1-Dpackage=jar

这是较新版本的简短语法:

mvn install:install-file -Dfile=<path-to-file>

当JAR由ApacheMaven构建时,这是最常见的情况。然后它将在META-INF目录的子文件夹中包含一个pom.xml,默认情况下将读取该文件。

资料来源:http://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html