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

    Note that it is NOT necessarily a good idea to use a local repo. If this project is shared with others then everyone else will have problems and questions when it doesn't work, and the jar won't be available even in your source control system!

    Although the shared repo is the best answer, if you cannot do this for some reason then embedding the jar is better than a local repo. Local-only repo contents can cause lots of problems, especially over time.

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

    The really quick and dirty way is to point to a local file:

    <dependency>
          <groupId>sample</groupId>  
           <artifactId>com.sample</artifactId>  
           <version>1.0</version> 
          <scope>system</scope>
          <systemPath>C:\DEV\myfunnylib\yourJar.jar</systemPath>
    </dependency>
    

    However this will only live on your machine (obviously), for sharing it usually makes sense to use a proper m2 archive (nexus/artifactory) or if you do not have any of these or don't want to set one up a local maven structured archive and configure a "repository" in your pom: local:

    <repositories>
        <repository>
            <id>my-local-repo</id>
            <url>file://C:/DEV//mymvnrepo</url>
        </repository>
    </repositories>
    

    remote:

    <repositories>
        <repository>
            <id>my-remote-repo</id>
            <url>http://192.168.0.1/whatever/mavenserver/youwant/repo</url>
        </repository>
    </repositories>
    

    for this a relative path is also possible using the basedir variable:

    <url>file:${basedir}</url>
    
    0 讨论(0)
  • 2020-11-21 05:21

    Not an answer to the original question, however it might be useful for someone

    There is no proper way to add multiple jar libraries from the folder using Maven. If there are only few dependencies, it is probably easier to configure maven-install-plugin as mentioned in the answers above.

    However for my particular case, I had a lib folder with more than 100 proprietary jar files which I had to add somehow. And for me it was much easier for me to convert my Maven project to Gradle.

    plugins {
        id 'org.springframework.boot' version '2.2.2.RELEASE'
        id 'io.spring.dependency-management' version '1.0.8.RELEASE'
        id 'java'
    }
    
    group = 'com.example'
    version = '0.0.1-SNAPSHOT'
    sourceCompatibility = '1.8'
    
    repositories {
        mavenCentral()
        flatDir {
           dirs 'libs' // local libs folder
       }
    }
    
    dependencies {
        implementation 'org.springframework.boot:spring-boot-starter-web'
        testImplementation('org.springframework.boot:spring-boot-starter-test') {
            exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
        }
        
        implementation 'io.grpc:grpc-netty-shaded:1.29.0'
        implementation 'io.grpc:grpc-protobuf:1.29.0'
        implementation 'io.grpc:grpc-stub:1.29.0' // dependecies from maven central
    
        implementation name: 'akka-actor_2.12-2.6.1' // dependecies from lib folder
        implementation name: 'akka-protobuf-v3_2.12-2.6.1'
        implementation name: 'akka-stream_2.12-2.6.1'
    
     }
    
    0 讨论(0)
  • 2020-11-21 05:22

    Another interesting case is when you want to have in your project private maven jars. You may want to keep the capabilities of Maven to resolve transitive dependencies. The solution is fairly easy.

    1. Create a folder libs in your project
    2. Add the following lines in your pom.xml file

      <properties><local.repository.folder>${pom.basedir}/libs/</local.repository.folder>
      </properties>
      
      <repositories>
         <repository>
              <id>local-maven-repository</id>
              <url>file://${local.repository.folder}</url>
              <releases>
                  <enabled>true</enabled>
              </releases>
              <snapshots>
                  <enabled>true</enabled>
              </snapshots>
         </repository>
      </repositories>
      
    3. Open the .m2/repository folder and copy the directory structure of the project you want to import into the libs folder.

    E.g. suppose you want to import the dependency

    <dependency>
        <groupId>com.mycompany.myproject</groupId>
        <artifactId>myproject</artifactId>
        <version>1.2.3</version>
    </dependency>
    

    Just go on .m2/repository and you will see the following folder

    com/mycompany/myproject/1.2.3

    Copy everything in your libs folder (again, including the folders under .m2/repository) and you are done.

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

    Step 1: Configure the maven-install-plugin with the goal install-file in your pom.xml

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-install-plugin</artifactId>
        <executions>
            <execution>
                <id>install-external-non-maven-jar-MWS-Client-into-local-maven-repo</id>
                <phase>clean</phase>
                <configuration>
                    <repositoryLayout>default</repositoryLayout>
                    <groupId>com.amazonservices.mws</groupId>
                    <artifactId>mws-client</artifactId>
                    <version>1.0</version>
                    <file>${project.basedir}/lib/MWSClientJavaRuntime-1.0.jar</file>
                    <packaging>jar</packaging>
                    <generatePom>true</generatePom>
                </configuration>
                <goals>
                    <goal>install-file</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    

    Make sure to edit the file path based on your actual file path (recommended is to place these external non-maven jars inside some folder, let's say lib, and place this lib folder inside your project so as to use project-specific relative path and avoid adding system specific absolute path.

    If you have multiple external jars, just repeat the <execution> for other jars within the same maven-install-plugin.

    Step 2: Once you have configured the maven-install-plugin as shown above in your pom.xml file, you have to use these jars in your pom.xml as usual:

        <dependency>
            <groupId>com.amazonservices.mws</groupId>
            <artifactId>mws-client</artifactId>
            <version>1.0</version>
        </dependency>
    

    Note that the maven-install-plugin only copies your external jars to your local .m2 maven repository. That's it. It doesn't automatically include these jars as maven dependencies to your project.

    It's a minor point, but sometimes easy to miss.

    0 讨论(0)
  • 2020-11-21 05:24
    1. mvn install

    You can write code below in command line or if you're using eclipse builtin maven right click on project -> Run As -> run configurations... -> in left panel right click on Maven Build -> new configuration -> write the code in Goals & in base directory :${project_loc:NameOfYourProject} -> Run

    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

    2.After installed, just declares jar in pom.xml.

     <dependency>
          <groupId>com.google.code</groupId>
          <artifactId>kaptcha</artifactId>
          <version>2.3</version>
     </dependency>
    
    0 讨论(0)
提交回复
热议问题