简体   繁体   中英

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

You can follow this: Importing-jars , but I think you should work with a repository manager.

First, you should setup a local repository manager like Nexus or Artifactory to proxy external repositories and host your local artifacts.

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

From your pom, you can get the jar with declaring a new dependency:

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

I hope this helps.

Here is a convenient method to pomify an external JAR (and only one)

The corresponding POM.XML file template is as follows. You have to change the project's groupId , artifactId , version and the property wrapped-lib-file and that's it. In the example, the external lib is supposed to be 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!)

If you are going to use obfuscated libs as Maven modules, this method will save you from great pain.

To install a JAR in the local repository use the following command:

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:

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. 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. In that case, all you need to do is:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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