簡體   English   中英

我們如何將外部jar放入我們的本地Maven存儲庫

[英]how we put the external jar's into our local maven repository

在這里,我將構建一個新的Java項目,在這里,該項目需要一些外部jar,這些jar需要由不同的項目重復使用。 How can we store the the external jar's into my local repository and access when when ever we need “如何在pom.xml中設置特定jar的依賴關系”。

您可以遵循以下步驟: Importing-jars ,但是我認為您應該使用存儲庫管理器。

首先,您應該設置一個本地存儲庫管理器(例如NexusArtifactory)來代理外部存儲庫並托管您的本地工件。

然后通過一個簡單的命令行執行像這樣:

mvn deploy:deploy-file -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0.1 -Dpackaging=jar -Dfile=c:\oracle_jdbc\jdk16_11.2.0.1\ojdbc6.jar -Durl=http://my-nexus/nexus/content/repositories/thirdparty -DrepositoryId=thirdparty

您將工件上傳到存儲庫管理器(在我的情況下是Nexus)。

從pom中,您可以通過聲明一個新的依賴項來獲得jar:

<dependency>
  <groupId>com.oracle</groupId>
  <DartifactId>ojdbc6</DartifactId>
  <version>11.2.0.1</version>
</dependency>

初始調用會將Nexus中的jar添加到您的本地.m2(本地存儲庫)中,以后可用於需要它的任何其他maven項目。

我希望這有幫助。

這是一種對外部JAR(僅一個)進行pom處理的便捷方法

相應的POM.XML文件模板如下。 您必須更改項目的groupIdartifactIdversion和屬性wrapped-lib-file ,僅此而已。 在示例中,外部lib應該是lib/external.jar

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

    <groupId>target-groupId</groupId>
    <artifactId>target-artifactId</artifactId>
    <version>target-version</version>
    <packaging>jar</packaging>

    <properties>
        <wrapped-lib-file>lib/external.jar</wrapped-lib-file>
    </properties>

    <build>
        <plugins>
            <plugin>
                <!-- Pomify the external JAR specified by ${wrapped-lib-file} -->
                <inherited>false</inherited>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-install-plugin</artifactId>
                <version>2.3.1</version>
                <executions>
                    <execution>
                        <id>Installation of wrapped library ${wrapped-lib-file}</id>
                        <phase>install</phase>
                        <goals>
                            <goal>install-file</goal>
                        </goals>
                        <configuration>
                            <file>${wrapped-lib-file}</file>
                            <groupId>${project.groupId}</groupId>
                            <artifactId>${project.artifactId}</artifactId>
                            <version>${project.version}</version>
                            <packaging>jar</packaging>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

然后,您可以在其他項目的依賴項中使用此Pom化的JAR(當然使用目標的groupId,artifactId和版本!)

如果要使用混淆的庫作為Maven模塊,則此方法將使您免於痛苦。

要在本地存儲庫中安裝JAR,請使用以下命令:

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

如果還有pom文件,則可以使用以下命令進行安裝:

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

使用2.5版的maven-install-plugin會變得更好。 如果JAR是由Apache Maven構建的,則它將在META-INF目錄的子文件夾中包含pom.xml,默認情況下將讀取該子文件夾。 在這種情況下,您需要做的只是:

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

暫無
暫無

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

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