How to add local jar files to a Maven project?

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

    Add your own local JAR in POM file and use that in maven build.

    mvn install:install-file -Dfile=path-to-jar -DgroupId=owngroupid -DartifactId=ownartifactid -Dversion=ownversion -Dpackaging=jar
    

    For example:

    mvn install:install-file -Dfile=path-to-jar -DgroupId=com.decompiler -DartifactId=jd-core-java -Dversion=1.2 -Dpackaging=jar
    

    Then add it to the POM like this:

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

    You can add local dependencies directly (as mentioned in build maven project with propriatery libraries included) like this:

    <dependency>
        <groupId>com.sample</groupId>
        <artifactId>sample</artifactId>
        <version>1.0</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/src/main/resources/Name_Your_JAR.jar</systemPath>
    </dependency>
    

    Update

    In new releases this feature is marked as deprecated but still working and not removed yet ( You just see warning in the log during maven start). An issue is raised at maven group about this https://issues.apache.org/jira/browse/MNG-6523 ( You can participate and describe why this feature is helpful in some cases). I hope this feature remains there!

    If you are asking me, as long as the feature is not removed, I use this to make dependency to only one naughty jar file in my project which is not fit in repository. If this feature is removed, well, there are lots of good answers here which I can chose from later!

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

    Also take a look at...

    <scope>compile</scope>
    

    Maven Dependencies. This is the default but I've found in some cases explicitly setting that scope also Maven to find local libraries in the local repository.

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

    I'd like such solution - use maven-install-plugin in pom file:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-install-plugin</artifactId>
        <version>2.5.2</version>
        <executions>
            <execution>
                <phase>initialize</phase>
                <goals>
                    <goal>install-file</goal>
                </goals>
                <configuration>
                    <file>lib/yourJar.jar</file>
                    <groupId>com.somegroup.id</groupId>
                    <artifactId>artefact-id</artifactId>
                    <version>x.y.z</version>
                    <packaging>jar</packaging>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    In this case you can perform mvn initialize and jar will be installed in local maven repo. Now this jar is available during any maven step on this machine (do not forget to include this dependency as any other maven dependency in pom with <dependency></dependency> tag). It is also possible to bind jar install not to initialize step, but any other step you like.

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

    command line :

    mvn install:install-file -Dfile=c:\kaptcha-{version}.jar -DgroupId=com.google.code
    -DartifactId=kaptcha -Dversion={version} -Dpackaging=jar
    
    0 讨论(0)
  • 2020-11-21 05:12

    Add local jar libraries, their sources and javadoc to a Maven project

    If you have pre-compiled jar files with libraries, their sources and javadoc, then you can install them to your local Maven repository like this:

    mvn install:install-file
        -Dfile=awesomeapp-1.0.1.jar \
        -DpomFile=awesomeapp-1.0.1.pom \
        -Dsources=awesomeapp-1.0.1-sources.jar \
        -Djavadoc=awesomeapp-1.0.1-javadoc.jar \
        -DgroupId=com.example \
        -DartifactId=awesomeapp \
        -Dversion=1.0.1 \
        -Dpackaging=jar
    

    Then in your project you can use this libraries:

    <!-- com.example -->
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>awesomeapp</artifactId>
        <version>1.0.1</version>
    </dependency>
    

    See: maven-install-plugin usage.


    Or you can build these libraries yourself with their sources and javadoc using maven-source-plugin and maven-javadoc-plugin, and then install them.

    Example project: library

    <?xml version="1.0" encoding="UTF-8"?>
    <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>
    
        <url>https://example.com/awesomeapp</url>
    
        <groupId>com.example</groupId>
        <artifactId>awesomeapp</artifactId>
        <name>awesomeapp</name>
        <version>1.0.1</version>
        <packaging>jar</packaging>
    
        <properties>
            <java.version>12</java.version>
        </properties>
    
        <build>
            <finalName>awesomeapp</finalName>
            <defaultGoal>install</defaultGoal>
    
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                    <configuration>
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
                <plugin>
                    <inherited>true</inherited>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-source-plugin</artifactId>
                    <version>3.2.1</version>
                    <executions>
                        <execution>
                            <id>attach-sources</id>
                            <goals><goal>jar</goal></goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <inherited>true</inherited>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-javadoc-plugin</artifactId>
                    <version>3.2.0</version>
                    <executions>
                        <execution>
                            <id>attach-javadocs</id>
                            <goals><goal>jar</goal></goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </project>
    

    Execute maven install goal:

    mvn install
    

    Check your local Maven repository:

    ~/.m2/repository/com/example/awesomeapp/1.0.1/
     ├─ _remote.repositories
     ├─ awesomeapp-1.0.1.jar
     ├─ awesomeapp-1.0.1.pom
     ├─ awesomeapp-1.0.1-javadoc.jar
     └─ awesomeapp-1.0.1-sources.jar
    

    Then you can use this library:

    <!-- com.example -->
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>awesomeapp</artifactId>
        <version>1.0.1</version>
    </dependency>
    
    0 讨论(0)
提交回复
热议问题