简体   繁体   English

我们如何将外部jar放入我们的本地Maven存储库

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

Here I am going to build a new java project and here the project need some external jar's which are need to be used repeatedly by different projects. 在这里,我将构建一个新的Java项目,在这里,该项目需要一些外部jar,这些jar需要由不同的项目重复使用。 How can we store the the external jar's into my local repository and access when when ever we need 'How can we set the dependency of particular jars in the pom.xml`. How can we store the the external jar's into my local repository and access when when ever we need “如何在pom.xml中设置特定jar的依赖关系”。

You can follow this: Importing-jars , but I think you should work with a repository manager. 您可以遵循以下步骤: Importing-jars ,但是我认为您应该使用存储库管理器。

First, you should setup a local repository manager like Nexus or Artifactory to proxy external repositories and host your local artifacts. 首先,您应该设置一个本地存储库管理器(例如NexusArtifactory)来代理外部存储库并托管您的本地工件。

Then with a simple command line execution like this one: 然后通过一个简单的命令行执行像这样:

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

You upload the artifact to your repository manager (in my case it's Nexus). 您将工件上传到存储库管理器(在我的情况下是Nexus)。

From your pom, you can get the jar with declaring a new dependency: 从pom中,您可以通过声明一个新的依赖项来获得jar:

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

The initial call will add the jar from Nexus to your local .m2 (local repository) and be available to any other maven project needing it later on. 初始调用会将Nexus中的jar添加到您的本地.m2(本地存储库)中,以后可用于需要它的任何其他maven项目。

I hope this helps. 我希望这有帮助。

Here is a convenient method to pomify an external JAR (and only one) 这是一种对外部JAR(仅一个)进行pom处理的便捷方法

The corresponding POM.XML file template is as follows. 相应的POM.XML文件模板如下。 You have to change the project's groupId , artifactId , version and the property wrapped-lib-file and that's it. 您必须更改项目的groupIdartifactIdversion和属性wrapped-lib-file ,仅此而已。 In the example, the external lib is supposed to be lib/external.jar . 在示例中,外部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>

You can then use this pomified JAR in dependencies of other projects (using the target's groupId, artifactId and version of course!) 然后,您可以在其他项目的依赖项中使用此Pom化的JAR(当然使用目标的groupId,artifactId和版本!)

If you are going to use obfuscated libs as Maven modules, this method will save you from great pain. 如果要使用混淆的库作为Maven模块,则此方法将使您免于痛苦。

To install a JAR in the local repository use the following command: 要在本地存储库中安装JAR,请使用以下命令:

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

If there's a pom-file as well, you can install it with the following command: 如果还有pom文件,则可以使用以下命令进行安装:

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

With version 2.5 of the maven-install-plugin it gets even better. 使用2.5版的maven-install-plugin会变得更好。 If the JAR was built by Apache Maven, it'll contain a pom.xml in a subfolder of the META-INF directory, which will be read by default. 如果JAR是由Apache Maven构建的,则它将在META-INF目录的子文件夹中包含pom.xml,默认情况下将读取该子文件夹。 In that case, all you need to do is: 在这种情况下,您需要做的只是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM