1687

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

4
  • 2
    Hi @Praneel PIDIKITI, Can you please change the accepted answer to the one with the most votes? Commented May 6, 2017 at 5:51
  • 2
    @nslntmnx That won't be better solution, as all solution have drawbacks stackoverflow.com/questions/364114/… Commented Jul 25, 2017 at 14:20
  • 2
    If your libs are updated or extended on occasion see this answer to I want to load all JARs from my libs project folder with maven for a "pomified" way and such avoiding an additional repo folder and cumbersome cmd lines or install-file scripts. Commented Feb 7, 2018 at 19:45
  • 1
    I think adding local jars is at least a "bad smell". Either you must push them in SCM, or others (or the build systems) might not have them locally. This just means possible build failures (and similar troubles). Take the time, push them to the artifact repository used by your team (e.g., as "legacy" libraries), and reference them from that repo in the project. Commented Oct 7, 2022 at 16:53

35 Answers 35

1857

You can add local dependencies directly (as mentioned in Build Maven project with proprietary 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>

In new releases, this feature is marked as deprecated, but it still working and is not removed yet (you just see warning in the log during Maven start).

An issue was raised at the Maven group about this, System Dependencies Deprecation (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 the repository. If this feature is removed, well, there are lots of good answers here which I can chose from later!

Sign up to request clarification or add additional context in comments.

20 Comments

There are times when you want to specifically test an old jar for example, and I think this answer is a good fit for that. It's the one I needed. Upvoted
As nice and easy as it looks, this solution has the problem that yourJar.jar will not be included in a WAR file for your app.
Above solution doesn't work anymore, it returns: " 'dependencies.dependency.systemPath' for xx.jar should not point at files within the project directory" This has been already dealt in stackoverflow.com/questions/10935135/…
In this answer, aren't the artifactId and groupId the wrong way around?
According to Maven docu (maven.apache.org/guides/introduction/…): Important note: This is marked deprecated.
|
1136

Install the JAR file into your local Maven repository (typically .m2 in your home folder) 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

10 Comments

Instructions to install on my build had everything except the generatePom part. That appears to be crucial.
The answer doesn't mention a README or that the jars are brought along. However, if the project brings the jars along then you might as well put the repo in the project as mentioned here stackoverflow.com/a/36602256/1000011 then you have no need for a README as the project will just work as if the jars were in maven central without any extra manual steps.
@opticyclic your comment needs more upvotes, or this answer needs to be edited. It is a recipe for disaster for novices who don't realize that installing to the local Maven repo would not include for everyone else.
Is there a way to install the local java doc and sources?
If there is the pom.xml file available, the command is simpler: mvn install:install-file -Dfile=<path-to-file> -DpomFile=<path-to-pomfile> (maven.apache.org/guides/mini/guide-3rd-party-jars-local.html)
|
224

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 file is available at Maven Central, you just need to delete it from your local repository, and it will be pulled from the default repository.

<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 repository can be committed to a code repository, and the whole set-up is system independent. (Working example in GitHub).

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

14 Comments

Local relative directory as a maven repo... Helped a lot
If you want to do this in pom.xml, check out baeldung.com/install-local-jar-with-maven
Since ${project.parent.basedir} doesn't seem to resolve to anything nowadays I used ${project.basedir}/.. and worked perfectly.
The stackoverflow answer that could not be found by OP is probably here: stackoverflow.com/a/19226672/2364405
A tip: please check $HOME/.m2/settings.xml , avoid "local-maven-repo" is mirrored by settings/mirrors/mirror <mirrorOf>*</mirrorOf> .
|
206

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

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

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

Then for each external JAR file 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]

10 Comments

This is the only correct answer on here as it will create your repo correctly when using deploy.
@user2748659 yes if in your CI build servers the folder local-maven-repo is included (as a child in this example) in your source folder
This answer is what worked for me. For a shared project, having the repo in the project directory and added to version control makes sure that anyone who checks out the project will not have missing dependencies. If you have a lot of dependencies, then a shared, remote repo is probably a better solution, otherwise keeping the repo in the project's directory is perfectly fine.
do we still need to add it as a dependency?
Note that to do this you may need to add -Dpackaging=jar. Otherwise you get "artifact info is incomplete or invalid: packaging is missing".
|
81

I'd like such a solution. Use maven-install-plugin in a 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 a JAR file will be installed in the local Maven repository. Now this JAR file is available during any Maven step on this machine (do not forget to include this dependency as any other Maven dependency in the POM file with <dependency></dependency> tag). It is also possible to bind a JAR file install not to the initialize step, but any other step you like.

6 Comments

This works well for me, but only if I run mvn initialize before mvn package: I cannot mvn initialize package or else it tries to download the JAR from the central repo. Why is this? I thought it would run this goals/phases in order.
Actually, they should be run in order. Take a look at the list of default lifecycle: maven.apache.org/guides/introduction/… You can use another step to bind.
I tried every method, but in the end I had to resort to this solution. The main reason is, I wanted to be able to locally build the packages in offline mode. If I declared it as a dependency with a locally defined repository, this was always considered as just another online repo and the maven build complained about not fining the artifact. This solution works fine for every case.
I think is better to use clean phase because initialize will be executed each time when we use mvn package when Its unnecessary. Finally if we only need generate the jar/war we can use directly mvn clean package.
"It is also possible to bind jar install not to initialize step, but any other step you like." is not necessarily true. If the dependency isn't in the repo yet and a phase is used that comes after a phase that resolves dependencies (e.g. compile) the build will fail.
|
55

The really quick-and-dirty way is to point to a local file. Please note "system" is deprecated by now:

<dependency>
    <groupId>com.sample</groupId>
    <artifactId>samplifact</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 file:

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 solution, a relative path is also possible using the basedir variable:

<url>file:${basedir}</url>

5 Comments

For local repository URLs, can they be relative or must they be absolute?
@Dragas I haven't tried, please let us know if you did.
Yeah, apparently you need to use <url>file:${basedir}</url> as base url instead.
<scope>system is deprecated.
@GeroldBroser Thank you for the input, i added a note. Cheers!
32
<dependency>
    <groupId>group id name</groupId>
    <artifactId>artifact name</artifactId>
    <version>version number</version>
    <scope>system</scope>
    <systemPath>jar location</systemPath>
</dependency>

3 Comments

<scope>system is deprecated now.
@GeroldBroser - ok. what can we use instead of that ?
@MasterJoe2 As also mentioned in the accepted answer install:install-file the artifact to the local repo and use it as a "normal" dependency (with default scope compile) or use an in-project repository solution.
26

An important part in a 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>

1 Comment

<scope>system is deprecated.
21

Add your own local JAR file in a POM file and use that in the 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 file like this:

Enter image description here

2 Comments

I get an error Failed to install artifact (access denied). How can I solve this? @Aurasphere
@RamzahRehman try opening the command prompt with admi privileges by right clicking it and then choosing "Run as administrator"
15

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

<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 JAR files inside some folder, let's say lib, and place this lib folder inside your project so as to use a project-specific relative path and avoid adding a system-specific absolute path.

If you have multiple external JAR files, just repeat the <execution> for other JAR files 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 JAR files 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 JAR files to your local .m2 Maven repository. That's it. It doesn't automatically include these JAR files as Maven dependencies to your project.

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

1 Comment

There is no <phase>clean if you run e.g. mvn compile and if there wasn't a mvn clean before the build will fail.
12

One way is to upload it to your own Maven repository manager (such as Nexus). It's good practice to have an own repository manager anyway.

Another nice way I've recently seen is to include the Maven Install Plugin in your build lifecycle: You declare in the POM to install the files to the local repository. It's a little, but small, overhead and no manual step involved.

See install:install-file

1 Comment

End up with switching to gradle. It doesn't work If the local jar is defined as dependencies, maven will not execute plugins before resolving the dependencies, a manual installation is unavoidable. found a disscussion about this situation: stackoverflow.com/questions/5951999/…
11

Of course you can add JAR files to that folder. But maybe it does not what you want to achieve...

If you need these JAR files for compilation, check this related question: Can I add JAR files to Maven 2 build classpath without installing them?

Also, before anyone suggests it, do not use the system scope.

Comments

10

Another interesting case is when you want to have in your project private Maven JAR files. 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.

Comments

10

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

If you have precompiled 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 these 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>

1 Comment

If you are using Windows's powershell, you will need to escape the . character not part of a file by enclosing the string between quotation marks. For instance : -DgroupId="org.primefaces" -Dversion="12.0.3"
9

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 JAR files:

<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 aren't any conflicts.

Now when you mvn install or mvn test, the local JAR files will be added automatically.

Comments

8

Command line:

mvn install:install-file -Dfile=c:\kaptcha-{version}.jar -DgroupId=com.google.code
    -DartifactId=kaptcha -Dversion={version} -Dpackaging=jar

1 Comment

That ought to be explained. How is it different from the other answers?
6

This is a short syntax for newer versions:

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

It works when the JAR file was built by Apache Maven, the most common case. Then it'll contain a pom.xml file in a subfolder of the META-INF directory, which will be read by default.

Source: Guide to installing third-party JAR files

Comments

5

It is not an answer to the original question. However, it might be useful for someone.

There isn't any 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 previous answers.

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' // Dependencies from Maven Central

    implementation name: 'akka-actor_2.12-2.6.1' // Dependencies from lib folder
    implementation name: 'akka-protobuf-v3_2.12-2.6.1'
    implementation name: 'akka-stream_2.12-2.6.1'
}

Comments

4

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.

Comments

4

I want to share a code where you can upload a folder full of JAR files. It's useful when a provider doesn't have a public repository, and you need to add lots of libraries manually. I've decided to build a .bat file instead of call directly to Maven because it could be Out of Memory errors. It was prepared for a Windows environment, but it is easy to adapt it to a Linux OS:

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;

public class CreateMavenRepoApp {

    private static final String OCB_PLUGIN_FOLDER = "C://your_folder_with_jars";

    public static void main(String[] args) throws IOException {

        File directory = new File();
        // Get all the files from a directory
        PrintWriter writer = new PrintWriter("update_repo_maven.bat", "UTF-8");
        writer.println("rem " + new Date());
        File[] fList = directory.listFiles();
        for (File file : fList) {
            if (file.isFile()) {
                String absolutePath = file.getAbsolutePath();
                Manifest  m = new JarFile(absolutePath).getManifest();
                Attributes attributes = m.getMainAttributes();
                String symbolicName = attributes.getValue("Bundle-SymbolicName");

                if(symbolicName!=null &&symbolicName.contains("com.yourCompany.yourProject")) {
                    String[] parts = symbolicName.split("\\.");
                    String artifactId = parts[parts.length-1];
                    String groupId = symbolicName.substring(0, symbolicName.length()-artifactId.length()-1);
                    String version = attributes.getValue("Bundle-Version");
                    String mavenLine = "call mvn org.apache.maven.plugins:maven-install-plugin:2.5.1:install-file -Dfile=" + absolutePath + " -DgroupId=" + groupId + " -DartifactId=" + artifactId + " -Dversion=" + version + " -Dpackaging=jar ";
                    writer.println(mavenLine);
                }

            }
        }
        writer.close();
    }
}

After running this main from any IDE, run the update_repo_maven.bat file.

2 Comments

Your code String symbolicName = attributes.getValue("Bundle-SymbolicName"); if(symbolicName!=null &&symbolicName.contains("com.yourCompany.yourProject")) seems to indicate that only custom jars will be supported. That's not what we need: instead a bunch of third party jars. Do you have suggestions how to install any jar this way?
I have fixed your code: i put an answer at the bottom.
4
  1. Create a local Maven repository directory, Your project root should look something like this to start with:

    yourproject
    +- pom.xml
    +- src
    
  2. Add a standard Maven repository directory called "repo" for the group com.example and version 1.0:

    yourproject
    +- pom.xml
    +- src
    +- repo
    
  3. 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
    
  4. Install POM file corresponding to your JAR file, so that your project can find the JAR file during Maven build from the local repository:

    mvn install:install-file -Dfile=/path-to-your-jar-1.0.jar -DpomFile=/path-to-your-pom-1.0.pom
    
  5. Add the repository 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>
    
  6. Add the dependency in your POM file:

    <dependency>
        <groupId>com.groupid</groupId>
        <artifactId>myid</artifactId>
        <version>1.0</version>
    </dependency>
    

Comments

3

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.

Comments

3
  1. Download the JAR file

  2. copy the JAR file to the project folder

  3. get the IntelliJ IDEA Maven command area

  4. type the below command

    mvn install:install-file -Dfile=YOUR_JAR_FILE_LOCATION\JARNAME.jar -DgroupId=org.primefaces.themes -DartifactId=iMetro -Dversion=1.0.1 -Dpackaging=jar
    

Example:

mvn install:install-file -Dfile=C:\Users\ranushka.l\Desktop\test\spring-web-1.0.2.jar -DgroupId=org.primefaces.themes -DartifactId=iMetro -Dversion=1.0.1 -Dpackaging=jar

Comments

2

To install a third-party JAR file, please call the command like below:

mvn install:install-file -DgroupId= -DartifactId= -Dversion= -Dpackaging=jar -Dfile=path

Comments

2

For some reason, in the web application I was maintaining, neither Alireza Fattahi's solution nor JJ Roman's solution worked correctly. In both cases, the compilation goes okay (it sees the JAR file), but the packaging fails to include the JAR file inside the WAR file.

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

Comments

1

Note that it is not necessarily a good idea to use a local repository.

If this project is shared with others then everyone else will have problems and questions when it doesn't work, and the JAR file won't be available even in your source control system!

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

3 Comments

if you add the jars in the source control system the libraries will always be available together with the source. No source, No libraries. With maven instead the source might be ok but the repository unavailable.
@Frank... any idea about how to create executable jar without including external dependencies(library files)?
For someone new to maven, and looking for an answer to the original question, what does "embedding the jar" mean?
1

In your local repository, you can install your JAR file 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 the Maven guide for the same.

Comments

1
  1. mvn install

You can write the code below on the command line, or if you're using the Eclipse built-in Maven, right-click on the project → Run AsRun configurations...in the left panel, right-click on Maven BuildNew 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

  1. After installed, just declares the JAR file in the pom.xml file.
<dependency>
    <groupId>com.google.code</groupId>
    <artifactId>kaptcha</artifactId>
    <version>2.3</version>
</dependency>

Comments

1

In Apache Maven 3.5.4, I had to add double quotation. Without double quotation it wasn't worked for me.

Example:

mvn install:install-file "-Dfile=location to the jar file" "-DgroupId=group id" "-DartifactId=artifact id" "-Dversion=version" "-Dpackaging=package type"

Comments

1

Perhaps someone will be interested in maven-artifact-generator.

It is a console program to generate Apache Maven artifacts in the local repository, and configure dependencies for file pom.xml, based on the path to the JAR files. You can do this for one file, but it's most useful if you have multiple JAR files.

Path JAR files:

java -jar maven-artifact-generator-X.X.X.jar -p path_to_jars -g com.test -V 1.2.3 -P jar

JAR file:

java -jar maven-artifact-generator-X.X.X.jar -f file_jar -g com.test -V 1.2.3 -P jar

This will generate an artifact in the local Maven repository, and generate dependencies for file pom.xml in gen.log. ArtifactId is the name of the JAR file.

It requires an installed Maven. It was tested on Windows 7 and Mac OS X (Unix/Linux).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.