How to add local jar files to a Maven project?

后端 未结 30 3254
悲&欢浪女
悲&欢浪女 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:24

    I had the same error for a set of dependencies in my pom.xml turns out the versions of the dependencies was not specified in the pom.xml and was mentioned in the parent repository. For some reason the version details was not syncing with this repo. Hence i manually entered the versions using the tag and it worked like a charm. Little bit of time needed to look up the versions in the parent and specify here. But this can be done just for the jars that are showing the artifactid error and it works. Hope this helps someone.

    0 讨论(0)
  • 2020-11-21 05:25

    The preferred way would be to create your own remote repository.

    See here for details on how to do it. Have a look at the 'Uploading to a Remote Repository' section.

    0 讨论(0)
  • 2020-11-21 05:28

    On your local repository you can install your jar by issuing the commands

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

    Follow this useful link to do the same from mkyoung's website. You can also check maven guide for the same

    0 讨论(0)
  • 2020-11-21 05:31

    Firstly, I would like to give credit for this answer to an anonymous Stack Overflow user - I am pretty sure I've seen a similar answer here before - but now I cannot find it.

    The best option for having local JAR files as a dependency is to create a local Maven repository. Such a repository is nothing more than a proper directory structure with pom files in it.

    For my example: I have my master project on ${master_project} location and subproject1 is on ${master_project}/${subproject1}.

    Then I create a Maven repository in: ${master_project}/local-maven-repo.

    In the pom file in subproject1 located at ${master_project}/${subproject1}/pom.xml, the repository needs to be specified which would take file path as a URL parameter:

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

    The dependency can be specified as for any other repository. This makes your pom repository independent. For instance, once the desired JAR is available in Maven central, you just need to delete it from your local repo and it will be pulled from the default repo.

        <dependency>
            <groupId>org.apache.felix</groupId>
            <artifactId>org.apache.felix.servicebinder</artifactId>
            <version>0.9.0-SNAPSHOT</version>
        </dependency>
    

    The last but not least thing to do is to add the JAR file to local repository using -DlocalRepositoryPath switch like so:

    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
    

    Once the JAR file is installed, your Maven repo can be committed to a code repository, and the whole set-up is system independent. (Working example in GitHub).

    I agree that having JARs committed to source code repo is not a good practice, but in real life, quick and dirty solutions are sometimes better than a full blown Nexus repo to host one JAR that you cannot publish.

    0 讨论(0)
  • 2020-11-21 05:31

    Create a new folder, let's say local-maven-repo at the root of your Maven project.

    Just add a local repo inside your <project> of your pom.xml:

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

    Then for each external jar you want to install, go at the root of your project and execute:

    mvn deploy:deploy-file -DgroupId=[GROUP] -DartifactId=[ARTIFACT] -Dversion=[VERS] -Durl=file:./local-maven-repo/ -DrepositoryId=local-maven-repo -DupdateReleaseInfo=true -Dfile=[FILE_PATH]
    
    0 讨论(0)
  • 2020-11-21 05:31

    For some reason, in the web application I'm giving maintenance to, neither Alireza Fattahi's solution nor JJ Roman's solution worked correctly. In both cases, the compilation goes okay (it sees the jar), but the packaging fails to include the jar inside the war.

    The only way I managed to make it work was by putting the jar on /src/main/webapp/WEB-INF/lib/ and then combining it with either Fattahis's or Roman's solution.

    0 讨论(0)
提交回复
热议问题