Problems of popular approaches

Most of the answers you'll find around the internet will suggest you to either install the dependency to your local repository or specify a "system" scope in the pom and distribute the dependency with the source of your project. But both of these solutions are actually flawed.

Why you shouldn't apply the "Install to Local Repository" approach

When you install a dependency to your local repository it remains there. Your distribution artifact will do fine as long as it has access to this repository. The problem is in most cases this repository will reside on your local machine, so there'll be no way to resolve this dependency on any other machine. Clearly making your artifact depend on a specific machine is not a way to handle things. Otherwise this dependency will have to be locally installed on every machine working with that project which is not any better.

Why you shouldn't apply the "System Scope" approach

The JAR files you depend on with the "System Scope" approach neither get installed to any repository or attached to your target packages. That's why your distribution package won't have a way to resolve that dependency when used. That I believe was the reason why the use of system scope even got deprecated. Anyway you don't want to rely on a deprecated feature.

The static in-project repository solution

After putting this in your POM file:

<repository>
    <id>repo</id>
    <releases>
        <enabled>true</enabled>
        <checksumPolicy>ignore</checksumPolicy>
    </releases>
    <snapshots>
        <enabled>false</enabled>
    </snapshots>
    <url>file://${project.basedir}/repo</url>
</repository>

for each artifact with a group id of form x.y.z Maven will include the following location inside your project directory in its search for artifacts:

repo/
| - x/
|   | - y/
|   |   | - z/
|   |   |   | - ${artifactId}/
|   |   |   |   | - ${version}/
|   |   |   |   |   | - ${artifactId}-${version}.jar

To elaborate more on this, you can read this blog post.

Use Maven to install to project repository

Instead of creating this structure by hand, I recommend to use a Maven plugin to install your JAR files as artifacts. So, to install an artifact to an in-project repository under repo folder execute:

mvn install:install-file -DlocalRepositoryPath=repo -DcreateChecksum=true -Dpackaging=jar -Dfile=[your-jar] -DgroupId=[...] -DartifactId=[...] -Dversion=[...]

If you'll choose this approach, you'll be able to simplify the repository declaration in the POM file to:

<repository>
    <id>repo</id>
    <url>file://${project.basedir}/repo</url>
</repository>

A helper script

Since executing installation command for each library is kind of annoying and definitely error-prone, I've created a utility script which automatically installs all the JAR files from a lib folder to a project repository, while automatically resolving all metadata (groupId, artifactId, etc.) from names of files. The script also prints out the dependencies XML file for you to copy-paste in your POM file.

Include the dependencies in your target package

When you'll have your in-project repository created, you'll have solved a problem of distributing the dependencies of the project with its source, but since then your project's target artifact will depend on non-published JAR files, so when you'll install it to a repository, it will have unresolvable dependencies.

To beat this problem, I suggest to include these dependencies in your target package. This you can do with either the Assembly Plugin or better with the OneJar Plugin. The official documentation on OneJar is easy to grasp.

Answer from Nikita Volkov on Stack Overflow
Top answer
1 of 16
641

Problems of popular approaches

Most of the answers you'll find around the internet will suggest you to either install the dependency to your local repository or specify a "system" scope in the pom and distribute the dependency with the source of your project. But both of these solutions are actually flawed.

Why you shouldn't apply the "Install to Local Repository" approach

When you install a dependency to your local repository it remains there. Your distribution artifact will do fine as long as it has access to this repository. The problem is in most cases this repository will reside on your local machine, so there'll be no way to resolve this dependency on any other machine. Clearly making your artifact depend on a specific machine is not a way to handle things. Otherwise this dependency will have to be locally installed on every machine working with that project which is not any better.

Why you shouldn't apply the "System Scope" approach

The JAR files you depend on with the "System Scope" approach neither get installed to any repository or attached to your target packages. That's why your distribution package won't have a way to resolve that dependency when used. That I believe was the reason why the use of system scope even got deprecated. Anyway you don't want to rely on a deprecated feature.

The static in-project repository solution

After putting this in your POM file:

<repository>
    <id>repo</id>
    <releases>
        <enabled>true</enabled>
        <checksumPolicy>ignore</checksumPolicy>
    </releases>
    <snapshots>
        <enabled>false</enabled>
    </snapshots>
    <url>file://${project.basedir}/repo</url>
</repository>

for each artifact with a group id of form x.y.z Maven will include the following location inside your project directory in its search for artifacts:

repo/
| - x/
|   | - y/
|   |   | - z/
|   |   |   | - ${artifactId}/
|   |   |   |   | - ${version}/
|   |   |   |   |   | - ${artifactId}-${version}.jar

To elaborate more on this, you can read this blog post.

Use Maven to install to project repository

Instead of creating this structure by hand, I recommend to use a Maven plugin to install your JAR files as artifacts. So, to install an artifact to an in-project repository under repo folder execute:

mvn install:install-file -DlocalRepositoryPath=repo -DcreateChecksum=true -Dpackaging=jar -Dfile=[your-jar] -DgroupId=[...] -DartifactId=[...] -Dversion=[...]

If you'll choose this approach, you'll be able to simplify the repository declaration in the POM file to:

<repository>
    <id>repo</id>
    <url>file://${project.basedir}/repo</url>
</repository>

A helper script

Since executing installation command for each library is kind of annoying and definitely error-prone, I've created a utility script which automatically installs all the JAR files from a lib folder to a project repository, while automatically resolving all metadata (groupId, artifactId, etc.) from names of files. The script also prints out the dependencies XML file for you to copy-paste in your POM file.

Include the dependencies in your target package

When you'll have your in-project repository created, you'll have solved a problem of distributing the dependencies of the project with its source, but since then your project's target artifact will depend on non-published JAR files, so when you'll install it to a repository, it will have unresolvable dependencies.

To beat this problem, I suggest to include these dependencies in your target package. This you can do with either the Assembly Plugin or better with the OneJar Plugin. The official documentation on OneJar is easy to grasp.

2 of 16
508

For throwaway code only

Set scope == system and just make up a groupId, artifactId, and version:

<dependency>
    <groupId>org.swinglabs</groupId>
    <artifactId>swingx</artifactId>
    <version>0.9.2</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/swingx-0.9.3.jar</systemPath>
</dependency>

Note: System dependencies are not copied into the resulting JAR/WAR file (see How to include system dependencies in a WAR file built using Maven)

๐ŸŒ
Apache Maven
maven.apache.org โ€บ shared โ€บ maven-archiver โ€บ examples โ€บ classpath.html
Set Up The Classpath โ€“ Apache Maven Archiver
December 20, 2025 - Maven Archiver can add the classpath of your project to the manifest. This is done with the <addClasspath> configuration element. <project> ... <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> ...
Discussions

Maven: add a folder or jar file into current classpath - Stack Overflow
I am using maven-compile plugin to compile classes. Now I would like to add one jar file into the current classpath. That file stays in another location (let's say c:/jars/abc.jar . I prefer to leave More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - Maven: add external jar folder to classpath - Stack Overflow
I've simple Eclipse Web Project based on Adobe framework. Now I have to upgrade my project to a new version of framework and I wish to use Maven to manage dependencies, packaging, ecc. The problem... More on stackoverflow.com
๐ŸŒ stackoverflow.com
May 22, 2017
maven - how to add my external jar file to class path - Stack Overflow
This JAR file only contains (main) artifacts of the project. If you take just that and run it, clearly the dependencies are missing -- by design. Typically Maven artifacts are reused in combination with their POM so that at the point of use it's know what the dependencies are. More on stackoverflow.com
๐ŸŒ stackoverflow.com
build - Maven - how can I add an arbitrary classpath entry to a jar? - Stack Overflow
I have an unusual situation where I need to add an arbitrary classpath entry (that points to a jar file) into the manifest of an executable jar. (This is for a Swing desktop application.) The mave... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
GitHub
github.com โ€บ markkolich โ€บ blog โ€บ blob โ€บ master โ€บ content โ€บ entries โ€บ maven-add-local-jar-dependency-to-classpath.md
blog/content/entries/maven-add-local-jar-dependency-to-classpath.md at master ยท markkolich/blog
Yes, you read that correctly, publish the .jar to a ~/.m2 like repo within your project that is checked into SCM! Here's how... On disk, your project probably looks something like this: project/ src/main/java src/main/resources src/test/java pom.xml ยท Create a lib directory in your project root โ€” this lib directory will act as a local Maven repository within the project.
Author ย  markkolich
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ jvm โ€บ ways to add jars to classpath in java
Ways to Add JARs to Classpath in Java | Baeldung
July 19, 2025 - We can easily add JAR files to our projectโ€™s classpath using popular IDEs like Eclipse or IntelliJ. Both IDEs provide user-friendly interfaces to include external libraries in our project. For detailed instructions, we can refer to the IDE-specific documentation for the most accurate and up-to-date guidance. While the above methods work well for small projects, projects both large and small can take advantage of a build tool. Maven and Gradle are the popular build tools used for this purpose.
Top answer
1 of 5
12

This might have been asked before. See Can I add jars to maven 2 build classpath without installing them?

In a nutshell: include your jar as dependency with system scope. This requires specifying the absolute path to the jar.

See also http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

2 of 5
4

The classpath setting of the compiler plugin are two args. Changed it like this and it worked for me:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.6.1</version>
    <configuration>
      <compilerArgs>
         <arg>-cp</arg>
         <arg>{basedir}/lib/bad.jar</arg>
      </compilerArgs>
    </configuration>
   </plugin>

I used the gmavenplus-plugin to read the path and create the property 'cp':

<plugin>
        <!--
          Use Groovy to read classpath and store into
          file named value of property <cpfile>

          In second step use Groovy to read the contents of
          the file into a new property named <cp>

          In the compiler plugin this is used to create a
          valid classpath
        -->
        <groupId>org.codehaus.gmavenplus</groupId>
        <artifactId>gmavenplus-plugin</artifactId>
        <version>1.12.0</version>
        <dependencies>
          <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <!-- any version of Groovy \>= 1.5.0 should work here -->
            <version>3.0.6</version>
            <type>pom</type>
            <scope>runtime</scope>
          </dependency>
        </dependencies>
        <executions>
          <execution>
            <id>read-classpath</id>
            <phase>validate</phase>
            <goals>
              <goal>execute</goal>
            </goals>
          </execution>

        </executions>
        <configuration>
          <scripts>
            <script><![CDATA[
                    def file = new File(project.properties.cpfile)
                    /* create a new property named 'cp'*/
                    project.properties.cp = file.getText()
                    println '<<< Retrieving classpath into new property named <cp> >>>'
                    println 'cp = ' + project.properties.cp
                  ]]></script>
          </scripts>
        </configuration>
      </plugin>
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 456391 โ€บ build-tools โ€บ Maven-jars-classpath-defining-dependencies
Maven: Using jars in classpath without defining dependencies (Other Build Tools forum at Coderanch)
You'd still have a lot of work to do, since you can't add all the JARs in a directory to the classpath in a generic way, you have to add each one individually. You will have to define them as dependencies to get Maven to pull them into its test classpath. You can avoid adding them to the repository, if you prefer and just make them external references, but I forget the scope value that does that.
๐ŸŒ
Apache Maven
maven.apache.org โ€บ plugins-archives โ€บ maven-surefire-plugin-2.12.4 โ€บ examples โ€บ configuring-classpath.html
Configuring the Classpath - Apache Maven
But, if you must, you can use the additionalClasspathElements element to add custom resources/jars to your classpath. This will be treated as an absolute file system path, so you may want use ${basedir} or another property combined with a relative path. Note that additional classpath elements ...
Find elsewhere
Top answer
1 of 2
5

Maven out of the box will come up with a JAR file (default packaging). This JAR file only contains (main) artifacts of the project. If you take just that and run it, clearly the dependencies are missing -- by design.

Typically Maven artifacts are reused in combination with their POM so that at the point of use it's know what the dependencies are. Edit: if you're using APKs and installing them on a phone, there may be mechanisms to deal with dependencies, I'm answering this merely from a Maven standpoint.

If you want to create a JAR with dependencies you have to tell Maven to do so, that's not the default. Ways of having Maven do that are (probably not exhaustive):

  • Maven Assembly plugin, jar-with-dependencies predefined descriptor:

    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4</version>
        <configuration>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
        </configuration>
    ...
    
  • Maven Shade plugin

2 of 2
3

That way it'll create a single-jar of large size and build time will be large everytime you try to build.

I instead prefer adding all jars to a lib folder and including in the classpath (jar's manifest), because of which when we have to make some change or redeploy to the client or some place, we can simply give the small jar (not all the dependencies merged within jar)

          <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>com.kalindiinfotech.webcrawler.MainGUI</mainClass>
                            <!--                            <mainClass>com.KalindiInfotech.busbookingmaven.form.LoginForm</mainClass>-->
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>install</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
๐ŸŒ
W3Docs
w3docs.com โ€บ java
Can I add jars to Maven 2 build classpath without installing them?
Yes, you can add jars to the Maven build classpath without installing them.
Top answer
1 of 4
85

I found that there is an easy solution for this problem. You can add a <Class-Path> element to <manifestEntries> element, and set <addClassPath>true</addClassPath> to <manifest> element. So value of <Class-Path> element is added to class-path automatically. Example:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <configuration>
    <archive>
      <manifest>
        <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
        <addClasspath>true</addClasspath>
        <mainClass>your.main.Class</mainClass>
      </manifest>
      <manifestEntries>
        <Class-Path>../conf/</Class-Path>
      </manifestEntries>
    </archive>
  </configuration>
</plugin>
2 of 4
18

Update: Here's how to filter a classpath into a custom manifest.

The maven-dependency-plugin's build-classpath goal can be configured to output the classpath to a file in the properties format (i.e. classpath=[classpath]). You then configure the filters element to use the generated classpath file, and configure the resources directory to be filtered.

For example:

<build>
  <plugins>
    <plugin>
      <artifactId>maven-dependency-plugin</artifactId>
      <version>2.1</version>
      <executions>
        <execution>
          <phase>generate-resources</phase>
          <goals>
            <goal>build-classpath</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <outputFilterFile>true</outputFilterFile>
        <outputFile>${project.build.directory}/classpath.properties</outputFile>
      </configuration>
    </plugin>
    <plugin>
      <artifactId>maven-jar-plugin</artifactId>
      <configuration>
        <archive>
          <manifestFile>
            ${project.build.outputDirectory}/META-INF/MANIFEST.MF
          </manifestFile>
        </archive>
      </configuration>
    </plugin>
  </plugins>
  <filters>
    <filter>${project.build.directory}/classpath.properties</filter>
  </filters>
  <resources>
    <resource>
      <directory>src/main/resources</directory>
      <filtering>true</filtering>
    </resource>
  </resources>
</build>

Then specify the following in src/main/resources/META-INF/Manifest.MF:

Bundle-Version: 4.0.0
...
Classpath: ${classpath};[specify additional entries here]

Note: there is a bug with this processing using the standard window path separator (\), the generate path is stripped of separators (note it works fine on Linux). You can get the classpath to be generated correctly for Windows by specifying <fileSeparator>\\\\</fileSeparator> in the build-classpath goal's configuration.


You can customise the manifest in the jar-plugin's configuration. To do so you'd add something like this to your pom.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  ...
  <configuration>
    <archive>
      <index>true</index>
      <manifest>
        <addClasspath>true</addClasspath>
      </manifest>
      <manifestEntries>
        <mode>development</mode>
        <url>${pom.url}</url>
        <key>value</key>
      </manifestEntries>
    </archive>
  </configuration>
  ...
</plugin>

The full archiver specification provides quite a few options. See the examples page for options on configuring the classpath.

If none of these work for you, you can define your own Manifest, set up properties containing the required entries and use a filter to populate the manifest with those properties

๐ŸŒ
Narkive
users.maven.apache.narkive.com โ€บ 0euYSvQf โ€บ add-directory-to-jar-manifest-classpath
Add Directory to Jar Manifest Classpath
Permalink I am building an executable JAR that depends on a handful of other JARs and a few config files being on the classpath. I want the config files to be editable by the end user, so did not add them as internal JAR resources. I've configured the maven-jar-plugin to generate a manifest ...
๐ŸŒ
Quora
quora.com โ€บ How-do-I-add-local-JAR-files-to-a-Maven-project
How to add local JAR files to a Maven project - Quora
Quora is a place to gain and share knowledge. It's a platform to ask questions and connect with people who contribute unique insights and quality answers.
๐ŸŒ
Java Code Geeks
javacodegeeks.com โ€บ home โ€บ core java
Include Jars In Java Classpath Example - Java Code Geeks
February 13, 2025 - Run the Java program as usual without specifying the JAR in the classpath ยท However, this mechanism was removed in Java 9 due to security and modularization improvements introduced by Project Jigsaw. For modern Java versions, consider using explicit classpath settings or dependency management tools like Maven or Gradle. Modern IDEs like Eclipse and IntelliJ IDEA provide built-in mechanisms to easily add JAR files to your projectโ€™s classpath.
๐ŸŒ
Apache Maven
maven.apache.org โ€บ shared-archives โ€บ maven-archiver-2.5 โ€บ examples โ€บ classpath.html
Set Up The Classpath
Maven Archiver can add the classpath of your project to the manifest. This is done with the <addClasspath> configuration element. <project> ... <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> ...
๐ŸŒ
Medium
medium.com โ€บ @aryanvania03 โ€บ can-i-add-jars-to-maven-2-build-classpath-without-installing-them-e78c3235b8f7
Can I add jars to Maven 2 build classpath without installing them? | by Daniel Martin | Medium
November 10, 2024 - Yes, you can add JAR files to the Maven 2 build classpath without installing them into your local Maven repository. This can be useful for temporary dependencies or when you want to test something quickly without modifying the local repository.
๐ŸŒ
Apache Maven
maven.apache.org โ€บ surefire โ€บ maven-surefire-plugin โ€บ examples โ€บ configuring-classpath.html
Configuring the Classpath โ€“ Maven Surefire Plugin
Since version 3.2.0 the additionalClasspathDependencies parameter can be used to add arbitrary dependencies to your test execution classpath via their regular Maven coordinates. Those are resolved from the repository like regular Maven project dependencies and afterwards added as additional classpath elements to the end of the classpath, so you cannot use these to override project dependencies or resources (except those which are filtered with classpathDependencyExclude).