How to add local jar files to a Maven project?

后端 未结 30 3253
悲&欢浪女
悲&欢浪女 2020-11-21 04:58

How do I add local jar files (not yet part of the Maven repository) directly in my project\'s library sources?

相关标签:
30条回答
  • 2020-11-21 05:04

    Important part in dependency is: ${pom.basedir} (instead of just ${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>
    
    0 讨论(0)
  • 2020-11-21 05:05

    Yes , you can have but its not good idea.

    Instead install all these jars to maven repos

    Also See

    • How to build maven project with propriatery libraries included
    0 讨论(0)
  • 2020-11-21 05:06

    Install the JAR into your local Maven repository as follows:

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

    Where each refers to:

    <path-to-file>: the path to the file to load e.g → c:\kaptcha-2.3.jar

    <group-id>: the group that the file should be registered under e.g → com.google.code

    <artifact-id>: the artifact name for the file e.g → kaptcha

    <version>: the version of the file e.g → 2.3

    <packaging>: the packaging of the file e.g. → jar

    Reference

    • Maven FAQ: I have a jar that I want to put into my local repository. How can I copy it in?
    • Maven Install Plugin Usage: The install:install-file goal
    0 讨论(0)
  • 2020-11-21 05:06

    I think a better solution for this problem is to use maven-install-plugin to automatically install the files at install time. This is how I set it up for my project.

    First, add the path (where you store the local .jars) as a property.

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

    Then, under plugins add a plugin to install the jars when compiling.

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

    Finally, in dependencies, you can add the jars

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

    By Setting up your project like this, the project will continue to build even when you take it to another computer (given that it has all the jar files in the path specified by the property local.sdk).

    For groupId use a unique name just to make sure that there are no conflicts.

    Now when you mvn install or mvn test local jars will be added automatically.

    0 讨论(0)
  • 2020-11-21 05:06
    1. Create a local Maven repository directory, Your project root should look something like this to start with:
    yourproject
    +- pom.xml
    +- src
    
    1. Add a standard Maven repository directory called repo for the group com.example and version 1.0:
    yourproject
    +- pom.xml
    +- src
    +- repo
    
    1. Deploy the Artifact Into the Repo, Maven can deploy the artifact for you using the mvn deploy:deploy-file goal:
    mvn deploy:deploy-file -Durl=file:///pathtoyour/repo -Dfile=your.jar -DgroupId=your.group.id -DartifactId=yourid -Dpackaging=jar -Dversion=1.0
    
    1. install pom file corresponding to your jar so that your project can find jar during maven build from local repo:
    mvn install:install-file -Dfile=/path-to-your-jar-1.0.jar -DpomFile=/path-to-your-pom-1.0.pom
    
    1. add repo in your pom file:
    <repositories>
        <!--other repositories if any-->
        <repository>
            <id>project.local</id>
            <name>project</name>
            <url>file:${project.basedir}/repo</url>
        </repository>
    </repositories>
    
    1. add the dependency in your pom:
    <dependency>
        <groupId>com.groupid</groupId>
        <artifactId>myid</artifactId>
        <version>1.0</version>
    </dependency>
    
    0 讨论(0)
  • 2020-11-21 05:07

    To install third party jar, Please call the command like below

    mvn install:install-file -DgroupId= -DartifactId= -Dversion= -Dpackaging=jar -Dfile=path
    
    0 讨论(0)
提交回复
热议问题