簡體   English   中英

如何將本地jar文件添加到Maven項目中?

[英]How to add local jar files to a Maven project?

如何直接在項目的庫源中添加本地 jar 文件(還不是 Maven 存儲庫的一部分)?

您可以直接添加本地依賴項(如build maven project with propriatery libraries include中所述),如下所示:

<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 安裝到本地 Maven 存儲庫(通常是主文件夾中的.m2 ),如下所示:

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

其中每個指的是:

<path-to-file> :要加載的文件的路徑,例如 → c:\kaptcha-2.3.jar

<group-id> : 文件應該注冊的組,例如 → com.google.code

<artifact-id> :文件的工件名稱,例如 → kaptcha

<version> :文件的版本,例如 → 2.3

<packaging> : 文件的包裝 eg → jar

參考

首先,我想將這個答案歸功於一個匿名的 Stack Overflow 用戶——我很確定我以前在這里看到過類似的答案——但現在我找不到了。

將本地 JAR 文件作為依賴項的最佳選擇是創建本地 Maven 存儲庫。 這樣的存儲庫只不過是一個適當的目錄結構,其中包含 pom 文件。

對於我的示例:我的主項目位於${master_project}位置,而 subproject1 位於${master_project}/${subproject1}

然后我在${master_project}/local-maven-repo中創建一個 Maven 存儲庫。

在位於${master_project}/${subproject1}/pom.xml的 subproject1 的 pom 文件中,需要指定將文件路徑作為 URL 參數的存儲庫:

<repositories>
    <repository>
        <id>local-maven-repo</id>
        <url>file:///${project.parent.basedir}/local-maven-repo</url>
    </repository>
</repositories>

可以為任何其他存儲庫指定依賴項。 這使您的 pom 存儲庫獨立。 例如,一旦所需的 JAR 在 Maven 中心可用,您只需從本地存儲庫中刪除它,它將從默認存儲庫中提取。

    <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 存儲庫可以提交到代碼存儲庫,並且整個設置與系統無關。 GitHub 中的工作示例)。

我同意將 JAR 提交到源代碼 repo 不是一個好習慣,但在現實生活中,快速而骯臟的解決方案有時比一個完整的 Nexus repo 更好地托管一個您無法發布的 JAR。

創建一個新文件夾,比如在 Maven 項目的根目錄下的local-maven-repo

只需在pom.xml<project>中添加一個本地倉庫:

<repositories>
    <repository>
        <id>local-maven-repo</id>
        <url>file:///${project.basedir}/local-maven-repo</url>
    </repository>
</repositories>

然后對於您要安裝的每個外部 jar,進入項目的根目錄並執行:

mvn deploy:deploy-file -DgroupId=[GROUP] -DartifactId=[ARTIFACT] -Dversion=[VERS] -Durl=file:./local-maven-repo/ -DrepositoryId=local-maven-repo -DupdateReleaseInfo=true -Dfile=[FILE_PATH]

我想要這樣的解決方案 - 在 pom 文件中使用maven-install-plugin

<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 initialize ,並且 jar 將安裝在本地 maven repo 中。 現在這個 jar 在這台機器上的任何 maven 步驟中都是可用的(不要忘記在 pom 中包含這個依賴項作為帶有<dependency></dependency>標記的任何其他 maven 依賴項)。 也可以綁定 jar install 而不是initialize步驟,而是您喜歡的任何其他步驟。

真正快速而骯臟的方法是指向本地文件,請注意“系統”現在已棄用:

<dependency>
    <groupId>com.sample</groupId>  
    <artifactId>samplifact</artifactId>  
    <version>1.0</version> 
    <scope>system</scope>
    <systemPath>C:\DEV\myfunnylib\yourJar.jar</systemPath>
</dependency>

但是,這只會存在於您的機器上(顯然),對於共享,使用適當的 m2 存檔(nexus/artifactory)通常是有意義的,或者如果您沒有這些存檔或不想設置一個本地 maven結構化存檔並在您的 pom 中配置“存儲庫”:

當地的:

<repositories>
    <repository>
        <id>my-local-repo</id>
        <url>file://C:/DEV//mymvnrepo</url>
    </repository>
</repositories>

偏僻的:

<repositories>
    <repository>
        <id>my-remote-repo</id>
        <url>http://192.168.0.1/whatever/mavenserver/youwant/repo</url>
    </repository>
</repositories>

對於這個解決方案,相對路徑也可以使用 basedir 變量:

<url>file:${basedir}</url>
<dependency>
    <groupId>group id name</groupId>
    <artifactId>artifact name</artifactId>
    <version>version number</version>
    <scope>system</scope>
    <systemPath>jar location</systemPath>
</dependency>

依賴的重要部分是: ${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>

在 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 中:

在此處輸入圖像描述

如何在項目的庫源代碼中直接添加本地jar文件(尚未成為Maven存儲庫的一部分)?

第 1 步:使用pom.xml中的目標install-file配置maven-install-plugin

<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>

確保根據您的實際文件路徑編輯file路徑(建議將這些外部非 maven jar 放在某個文件夾中,比如說lib ,並將這個lib文件夾放在您的項目中,以便使用項目特定的相對路徑和避免添加系統特定的絕對路徑。

如果您有多個外部 jar,只需對同一maven-install-plugin中的其他 jar 重復<execution>即可。

第 2 步:一旦您在pom.xml文件中配置了如上所示的maven-install-plugin ,您必須像往常一樣在pom.xml中使用這些 jar:

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

請注意, maven-install-plugin僅將您的外部 jar 復制到本地.m2 maven 存儲庫。 而已。 它不會自動將這些 jars 作為 maven 依賴項包含到您的項目中。

這是一個小問題,但有時很容易錯過。

一種方法是將其上傳到您自己的 Maven 存儲庫管理器(例如 Nexus)。 無論如何,擁有自己的存儲庫管理器是一種很好的做法。

我最近看到的另一種好方法是在構建生命周期中包含 Maven 安裝插件:您在 POM 中聲明將文件安裝到本地存儲庫。 這是一個很小但很小的開銷,並且不涉及手動步驟。

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

當然,您可以將 jars 添加到該文件夾​​中。 但也許它不是你想要實現的......

如果您需要這些 jars 進行編譯,請檢查以下相關問題: 我可以將 jars 添加到 maven 2 build classpath 而不安裝它們嗎?

此外,在任何人建議之前,不要使用系統范圍。

另一個有趣的案例是當你想在你的項目中擁有私有的 maven jars。 您可能希望保留 Maven 的功能來解決傳遞依賴關系。 解決方案相當簡單。

  1. 在你的項目中創建一個文件夾libs
  2. 在 pom.xml 文件中添加以下行

    <properties><local.repository.folder>${pom.basedir}/libs/</local.repository.folder> </properties> <repositories> <repository> <id>local-maven-repository</id> <url>file://${local.repository.folder}</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories>
  3. 打開.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下的文件夾),您就完成了。

命令行 :

mvn install:install-file -Dfile=c:\kaptcha-{version}.jar -DgroupId=com.google.code
-DartifactId=kaptcha -Dversion={version} -Dpackaging=jar

將本地jar庫、它們的sourcesjavadoc添加到 Maven 項目

如果你已經預編譯了帶有庫、它們的sourcesjavadocjar文件,那么你可以像這樣將它們install到本地 Maven 存儲庫中:

mvn install:install-file
    -Dfile=awesomeapp-1.0.1.jar \
    -DpomFile=awesomeapp-1.0.1.pom \
    -Dsources=awesomeapp-1.0.1-sources.jar \
    -Djavadoc=awesomeapp-1.0.1-javadoc.jar \
    -DgroupId=com.example \
    -DartifactId=awesomeapp \
    -Dversion=1.0.1 \
    -Dpackaging=jar

然后在您的項目中,您可以使用以下庫:

<!-- com.example -->
<dependency>
    <groupId>com.example</groupId>
    <artifactId>awesomeapp</artifactId>
    <version>1.0.1</version>
</dependency>

請參閱: maven-install-plugin用法。


或者您可以使用maven-source-pluginmaven-javadoc-plugin使用它們的sourcesjavadoc自己build這些庫,然后install它們。

示例項目:圖書館

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
         http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <url>https://example.com/awesomeapp</url>

    <groupId>com.example</groupId>
    <artifactId>awesomeapp</artifactId>
    <name>awesomeapp</name>
    <version>1.0.1</version>
    <packaging>jar</packaging>

    <properties>
        <java.version>12</java.version>
    </properties>

    <build>
        <finalName>awesomeapp</finalName>
        <defaultGoal>install</defaultGoal>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <inherited>true</inherited>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals><goal>jar</goal></goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <inherited>true</inherited>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>3.2.0</version>
                <executions>
                    <execution>
                        <id>attach-javadocs</id>
                        <goals><goal>jar</goal></goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

執行 maven install目標:

mvn install

檢查您的本地 Maven 存儲庫:

~/.m2/repository/com/example/awesomeapp/1.0.1/
 ├─ _remote.repositories
 ├─ awesomeapp-1.0.1.jar
 ├─ awesomeapp-1.0.1.pom
 ├─ awesomeapp-1.0.1-javadoc.jar
 └─ awesomeapp-1.0.1-sources.jar

然后你可以使用這個庫:

<!-- com.example -->
<dependency>
    <groupId>com.example</groupId>
    <artifactId>awesomeapp</artifactId>
    <version>1.0.1</version>
</dependency>

我認為這個問題更好的解決方案是使用maven-install-plugin在安裝時自動安裝文件。 這就是我為我的項目設置它的方式。

首先,將路徑(存儲本地 .jars 的位置)添加為屬性。

<properties>
    <local.sdk>/path/to/jar</local.sdk>
</properties>

然后,在plugins下添加一個插件來在編譯時安裝 jars。

<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>

最后,在依賴項中,您可以添加罐子

<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 installmvn test時,將自動添加本地 jar。

這是較新版本的簡短語法:

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

當 JAR 由 Apache Maven 構建時它可以工作——最常見的情況。 然后它將在 META-INF 目錄的子文件夾中包含一個 pom.xml,默認情況下會讀取該文件。

來源: http ://maven.apache.org/guides/mini/guide-3rd-party-jars-local.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。

首選方法是創建您自己的遠程存儲庫。

有關如何執行此操作的詳細信息,請參見此處 查看“上傳到遠程存儲庫”部分。

  1. 創建一個本地 Maven 存儲庫目錄,您的項目根目錄應如下所示:
yourproject
+- pom.xml
+- src
  1. 為組 com.example 和版本 1.0 添加一個名為 repo 的標准 Maven 存儲庫目錄:
yourproject
+- pom.xml
+- src
+- repo
  1. 將工件部署到存儲庫中,Maven 可以使用 mvn deploy:deploy-file 目標為您部署工件:
mvn deploy:deploy-file -Durl=file:///pathtoyour/repo -Dfile=your.jar -DgroupId=your.group.id -DartifactId=yourid -Dpackaging=jar -Dversion=1.0
  1. 安裝與您的 jar 對應的 pom 文件,以便您的項目可以在本地 repo 的 maven 構建期間找到 jar:
mvn install:install-file -Dfile=/path-to-your-jar-1.0.jar -DpomFile=/path-to-your-pom-1.0.pom
  1. 在你的 pom 文件中添加 repo:
<repositories>
    <!--other repositories if any-->
    <repository>
        <id>project.local</id>
        <name>project</name>
        <url>file:${project.basedir}/repo</url>
    </repository>
</repositories>
  1. 在你的 pom 中添加依賴項:
<dependency>
    <groupId>com.groupid</groupId>
    <artifactId>myid</artifactId>
    <version>1.0</version>
</dependency>

也來看看...

<scope>compile</scope>

Maven 依賴項 這是默認設置,但我發現在某些情況下明確設置該范圍還 Maven 以在本地存儲庫中查找本地庫。

要安裝第三方 jar,請調用如下命令

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

出於某種原因,在我正在維護的 Web 應用程序中, Alireza Fattahi 的解決方案JJ Roman 的解決方案都不能正常工作。 在這兩種情況下,編譯都正常(它看到了 jar),但打包未能將 jar 包含在 war 中。

我設法使其工作的唯一方法是將 jar 放在/src/main/webapp/WEB-INF/lib/上,然后將其與 Fattahis 或 Roman 的解決方案結合使用。

不是原始問題的答案,但它可能對某人有用

沒有使用 Maven 從文件夾中添加多個 jar 庫的正確方法。 如果只有很少的依賴項,那么配置maven-install-plugin可能更容易,如上面的答案中所述。

但是對於我的特殊情況,我有一個包含 100 多個專有 jar 文件的lib文件夾,我必須以某種方式添加這些文件。 對我來說,將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'

 }

請注意,使用本地存儲庫不一定是個好主意。 如果這個項目與其他人共享,那么其他人在它不起作用時都會遇到問題和疑問,並且即使在您的源代碼控制系統中也無法使用該 jar!

盡管共享 repo 是最好的答案,但如果由於某種原因你不能這樣做,那么嵌入 jar 比本地 repo 更好。 僅限本地的 repo 內容可能會導致很多問題,尤其是隨着時間的推移。

在本地存儲庫中,您可以通過發出命令來安裝 jar

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

按照這個有用的鏈接從 mkyoung 的網站上做同樣的事情。 您也可以查看Maven 指南以獲取相同的信息

  1. mvn 安裝

您可以在命令行中編寫下面的代碼,或者如果您使用 eclipse 內置 maven 右鍵單擊​​項目 -> 運行方式 -> 運行配置... -> 在左側面板中右鍵單擊 Maven 構建 -> 新配置 -> 編寫目標和基目錄中的代碼:${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>:要加載的文件的路徑 eg -> c:\kaptcha-2.3.jar

<group-id>: 文件應該注冊的組 eg -> com.google.code

< artifact-id >:文件的工件名稱 eg -> kaptcha

<version>: 文件的版本 eg -> 2.3

<packaging>:文件的打包 eg -> jar

2.安裝后,只需在pom.xml中聲明jar即可。

 <dependency>
      <groupId>com.google.code</groupId>
      <artifactId>kaptcha</artifactId>
      <version>2.3</version>
 </dependency>

也許有人會感興趣: https ://github.com/Limraj/maven-artifact-generator

控制台程序,用於在本地存儲庫中生成 maven 工件,並根據 jar 的路徑為 pom.xml 配置依賴項。 您可以對一個文件執行此操作,但如果您有多個 jar 文件,它最有用。

路徑罐子:java -jar maven-artifact-generator-XXXjar -p path_to_jars -g com.test -V 1.2.3 -P jar

jar: java -jar maven-artifact-generator-XXXjar -f file_jar -g com.test -V 1.2.3 -P jar

這將在本地 maven 存儲庫中生成一個工件,並在 gen.log 中為 pom.xml 生成依賴項。 ArtifactId 是 jar 文件的名稱。

需要已安裝的 Maven。 在 widnows 7 和 macOS X (unix/linux) 上進行測試。

  1. 下載jar文件
  2. 復制jar文件到項目文件夾
  3. 獲取inteliJ idea Maven命令區
  4. 輸入以下命令

mvn install:install-file -Dfile= YOUR_JAR_FILE_LOCATION *JARNAME .jar -DgroupId=org.primefaces.themes -DartifactId=iMetro -Dversion=1.0.1 -Dpackaging=jar *


例子:

mvn install:install-file -Dfile=C:\Users\ranushka.l\Desktop\test\spring-web-1.0.2.jar -DgroupId=org.primefaces.themes -DartifactId=iMetro -Dversion=1.0.1 -Dpackaging=罐

此答案僅適用於日食用戶:

如果使用的是Eclipse,請將jar放在lib /中,右鍵單擊jar名稱,然后單擊“添加到構建路徑”。 Eclipse將創建一個“引用庫”並為您放置jar

它立即為我解決了程序中jar的導入

我的 pom.xml 中的一組依賴項出現了相同的錯誤,結果是 pom.xml 中沒有指定依賴項的版本,並且在父存儲庫中提到了這些版本。 由於某種原因,版本詳細信息未與此 repo 同步。 因此,我使用標簽手動輸入了版本,它就像一個魅力。 在父項中查找版本並在此處指定需要一點時間。 但這僅適用於顯示 artifactid 錯誤的 jar 並且有效。 希望這可以幫助某人。

在 Apache Maven 3.5.4 中,我不得不添加雙引號。 沒有雙引號,它對我不起作用。

例如: mvn install:install-file "-Dfile=位置到 jar 文件" "-DgroupId=group id" "-DartifactId=artifact id" "-Dversion=version" "-Dpackaging=package type"

我的陰影 jar 文件不包含使用 AlirezaFattahi 解決方案的第三方庫。 但是,我記得上次我為同一個項目嘗試過它后它就可以工作了。 所以,我嘗試了自己的解決方案:

  1. mkdir .m2/repositories 目錄下的項目路徑(類似於該目錄下的其他 maven 依賴項目錄)
  2. 將第三方jar文件放入其中。
  3. 添加依賴項,就像 Maven 存儲庫上的庫一樣。

最后,它對我有用。 :)

就我而言,事實證明我現有的 maven 構建已經將有問題的 jar 文件安裝到我的本地存儲庫中,但我沒有看到預期的結果,因為本地構建會生成帶有后綴“-SNAPSHOT”的工件。 對我來說非常簡單的解決方案是更新使用本地構建 jar 文件的項目的 pom.xml 文件,以匹配名稱。

<myproject.foo.version>1.88-SNAPSHOT</myproject.foo.version>

代替

<myproject.foo.version>1.88</myproject.foo.version>

要點是,如果您的 jar 文件似乎應該已經在您的本地 .m2/maven 存儲庫中,那么很可能是,您只需要相應地更新您的依賴項。

如果您是 Spring 開發人員,您會發現您從 Spring Boot 構建生成的 JAR 不會在另一個構建中作為依賴項 JAR 工作。 檢查你的 POM 是否有這個:

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

並且還要檢查,如果您運行mvn package ,您是否在target/目錄中看到 .jar.original 文件以及 .jar 文件。 如果是這樣,您需要將該 .jar.original 文件用於任何本地開發(將其放入 lib/ 或其他東西中。)

你當然可以解決這個問題; 請參閱在 Spring 中引用插件手冊的原始帖子 我認為,你需要<configuration> <attach>false</attach> <configuration>然后你必須部署並且 Maven 應該使用 .jar.original 文件。 我自己沒有嘗試過,所以請告訴我!

指向${project.directory}/repo/或 file:// uri 的 pom 存儲庫可以與源駐留的 jarfile 一起使用,以便及時凍結簡單項目的 deps。 這些不會泄漏到系統 ~/.m2 存儲庫中,但會在未指定時影響對(通常是較新的)頂級的 Maven 版本選擇的選擇。

對於在分發時捆綁的現有 deps,類似於 shade 但更直接,如果您可以將“target”作為您最喜歡的結構或 maven 的默認值運行:

-cp "$PWD/target/classes:$PWD/target/lib/*" pkg.random.ClassWithMain

然后

<build>
        <plugins>
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>install</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>

我對ojdbc6有同樣的問題。 我看到了此鏈接,但是沒有用。 該命令是正確的,但我還需要一個參數,

這是鏈接: http : //roufid.com/3-ways-to-add-local-jar-to-maven-project/

這是示例:

install:install-file -Dfile=C:\driversDB\ojdbc6.jar -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0.3 -Dpackaging=jar

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM