How do I add local jar files (not yet part of the Maven repository) directly in my project's library sources?
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!
Install the JAR 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
Maven FAQ: I have a jar that I want to put into my local repository. How can I copy it in?
Maven Install Plugin Usage: The install:install-file goal
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.
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]
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.
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:
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>
<dependency>
<groupId>group id name</groupId>
<artifactId>artifact name</artifactId>
<version>version number</version>
<scope>system</scope>
<systemPath>jar location</systemPath>
</dependency>
Important part in 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>
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:
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.
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.
http://maven.apache.org/plugins/maven-install-plugin/install-file-mojo.html
Of course you can add jars to that folder. But maybe it does not what you want to achieve...
If you need these jars for compilation, check this related question: Can I add jars to maven 2 build classpath without installing them?
Also, before anyone suggests it, do NOT use the system scope.
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.
Create a folder libs in your project
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>
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.
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>
command line :
mvn install:install-file -Dfile=c:\kaptcha-{version}.jar -DgroupId=com.google.code
-DartifactId=kaptcha -Dversion={version} -Dpackaging=jar
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 jars
<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 are no conflicts.
Now when you mvn install or mvn test local jars will be added automatically.
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'
}
This is a short syntax for newer versions:
mvn install:install-file -Dfile=<path-to-file>
It works when the JAR was built by Apache Maven - the most common case. Then it'll contain a pom.xml in a subfolder of the META-INF directory, which will be read by default.
Source: http://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
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.
I want to share a code where you can upload a folder full of jars. 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 instead of call directly to maven because It could be Out of Memory errors. It was prepared for a windows environment but is easy to adapt it to 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 run this main from any IDE, run the update_repo_maven.bat.
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.
Create a local Maven repository directory, Your project root should look something like this to start with:
yourproject
+- pom.xml
+- src
Add a standard Maven repository directory called repo for the group com.example and version 1.0:
yourproject
+- pom.xml
+- src
+- repo
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
install pom file corresponding to your jar so that your project can find jar during maven build from local repo:
mvn install:install-file -Dfile=/path-to-your-jar-1.0.jar -DpomFile=/path-to-your-pom-1.0.pom
add repo 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>
add the dependency in your pom:
<dependency>
<groupId>com.groupid</groupId>
<artifactId>myid</artifactId>
<version>1.0</version>
</dependency>
To install third party jar, Please call the command like below
mvn install:install-file -DgroupId= -DartifactId= -Dversion= -Dpackaging=jar -Dfile=path
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.
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.
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
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>
Perhaps someone will be interested in:
https://github.com/Limraj/maven-artifact-generator
Console program to generate maven artifacts in the local repository, and configure dependencies for pom.xml, based on the path to the jars.
You can do this for one file, but it's most useful if you have multiple jar files.
path jars:
java -jar maven-artifact-generator-X.X.X.jar -p path_to_jars -g com.test -V 1.2.3 -P jar
jar:
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 dependecies for pom.xml in gen.log. ArtifactId is the name of the jar file.
Requires an installed maven.
Testing on widnows 7 and macOS X (unix/linux).
Download jar file
copy jar file to the project folder
get inteliJ idea Maven command area
type 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
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.
Related
How do I add local jar files (not yet part of the Maven repository) directly in my project's library sources?
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!
Install the JAR 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
Maven FAQ: I have a jar that I want to put into my local repository. How can I copy it in?
Maven Install Plugin Usage: The install:install-file goal
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.
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]
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.
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:
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>
<dependency>
<groupId>group id name</groupId>
<artifactId>artifact name</artifactId>
<version>version number</version>
<scope>system</scope>
<systemPath>jar location</systemPath>
</dependency>
Important part in 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>
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:
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.
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.
http://maven.apache.org/plugins/maven-install-plugin/install-file-mojo.html
Of course you can add jars to that folder. But maybe it does not what you want to achieve...
If you need these jars for compilation, check this related question: Can I add jars to maven 2 build classpath without installing them?
Also, before anyone suggests it, do NOT use the system scope.
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.
Create a folder libs in your project
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>
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.
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>
command line :
mvn install:install-file -Dfile=c:\kaptcha-{version}.jar -DgroupId=com.google.code
-DartifactId=kaptcha -Dversion={version} -Dpackaging=jar
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 jars
<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 are no conflicts.
Now when you mvn install or mvn test local jars will be added automatically.
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'
}
This is a short syntax for newer versions:
mvn install:install-file -Dfile=<path-to-file>
It works when the JAR was built by Apache Maven - the most common case. Then it'll contain a pom.xml in a subfolder of the META-INF directory, which will be read by default.
Source: http://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
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.
I want to share a code where you can upload a folder full of jars. 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 instead of call directly to maven because It could be Out of Memory errors. It was prepared for a windows environment but is easy to adapt it to 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 run this main from any IDE, run the update_repo_maven.bat.
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.
Create a local Maven repository directory, Your project root should look something like this to start with:
yourproject
+- pom.xml
+- src
Add a standard Maven repository directory called repo for the group com.example and version 1.0:
yourproject
+- pom.xml
+- src
+- repo
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
install pom file corresponding to your jar so that your project can find jar during maven build from local repo:
mvn install:install-file -Dfile=/path-to-your-jar-1.0.jar -DpomFile=/path-to-your-pom-1.0.pom
add repo 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>
add the dependency in your pom:
<dependency>
<groupId>com.groupid</groupId>
<artifactId>myid</artifactId>
<version>1.0</version>
</dependency>
To install third party jar, Please call the command like below
mvn install:install-file -DgroupId= -DartifactId= -Dversion= -Dpackaging=jar -Dfile=path
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.
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.
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
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>
Perhaps someone will be interested in:
https://github.com/Limraj/maven-artifact-generator
Console program to generate maven artifacts in the local repository, and configure dependencies for pom.xml, based on the path to the jars.
You can do this for one file, but it's most useful if you have multiple jar files.
path jars:
java -jar maven-artifact-generator-X.X.X.jar -p path_to_jars -g com.test -V 1.2.3 -P jar
jar:
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 dependecies for pom.xml in gen.log. ArtifactId is the name of the jar file.
Requires an installed maven.
Testing on widnows 7 and macOS X (unix/linux).
Download jar file
copy jar file to the project folder
get inteliJ idea Maven command area
type 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
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.
I have a proprietary jar that I want to add to my pom as a dependency.
But I don't want to add it to a repository. The reason is that I want my usual maven commands such as mvn compile, etc, to work out of the box. (Without demanding from the developers a to add it to some repository by themselves).
I want the jar to be in a 3rdparty lib in source control, and link to it by relative path from the pom.xml file.
Can this be done? How?
I want the jar to be in a 3rdparty lib in source control, and link to it by relative path from the pom.xml file.
If you really want this (understand, if you can't use a corporate repository), then my advice would be to use a "file repository" local to the project and to not use a system scoped dependency. The system scoped should be avoided, such dependencies don't work well in many situation (e.g. in assembly), they cause more troubles than benefits.
So, instead, declare a repository local to the project:
<repositories>
<repository>
<id>my-local-repo</id>
<url>file://${project.basedir}/my-repo</url>
</repository>
</repositories>
Install your third party lib in there using install:install-file with the localRepositoryPath parameter:
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<myGroup> \
-DartifactId=<myArtifactId> -Dversion=<myVersion> \
-Dpackaging=<myPackaging> -DlocalRepositoryPath=<path>
Update: It appears that install:install-file ignores the localRepositoryPath when using the version 2.2 of the plugin. However, it works with version 2.3 and later of the plugin. So use the fully qualified name of the plugin to specify the version:
mvn org.apache.maven.plugins:maven-install-plugin:2.3.1:install-file \
-Dfile=<path-to-file> -DgroupId=<myGroup> \
-DartifactId=<myArtifactId> -Dversion=<myVersion> \
-Dpackaging=<myPackaging> -DlocalRepositoryPath=<path>
maven-install-plugin documentation
Finally, declare it like any other dependency (but without the system scope):
<dependency>
<groupId>your.group.id</groupId>
<artifactId>3rdparty</artifactId>
<version>X.Y.Z</version>
</dependency>
This is IMHO a better solution than using a system scope as your dependency will be treated like a good citizen (e.g. it will be included in an assembly and so on).
Now, I have to mention that the "right way" to deal with this situation in a corporate environment (maybe not the case here) would be to use a corporate repository.
Using the system scope. ${basedir} is the directory of your pom.
<dependency>
<artifactId>..</artifactId>
<groupId>..</groupId>
<scope>system</scope>
<systemPath>${basedir}/lib/dependency.jar</systemPath>
</dependency>
However it is advisable that you install your jar in the repository, and not commit it to the SCM - after all that's what maven tries to eliminate.
This is another method in addition to my previous answer at Can I add jars to maven 2 build classpath without installing them?
This will get around the limit when using multi-module builds especially if the downloaded JAR is referenced in child projects outside of the parent. This also reduces the setup work by creating the POM and the SHA1 files as part of the build. It also allows the file to reside anywhere in the project without fixing the names or following the maven repository structure.
This uses the maven-install-plugin. For this to work, you need to set up a multi-module project and have a new project representing the build to install files into the local repository and ensure that one is first.
You multi-module project pom.xml would look like this:
<packaging>pom</packaging>
<modules>
<!-- The repository module must be first in order to ensure
that the local repository is populated -->
<module>repository</module>
<module>... other modules ...</module>
</modules>
The repository/pom.xml file will then contain the definitions to load up the JARs that are part of your project. The following are some snippets of the pom.xml file.
<artifactId>repository</artifactId>
<packaging>pom</packaging>
The pom packaging prevents this from doing any tests or compile or generating any jar file. The meat of the pom.xml is in the build section where the maven-install-plugin is used.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<executions>
<execution>
<id>com.ibm.db2:db2jcc</id>
<phase>verify</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<groupId>com.ibm.db2</groupId>
<artifactId>db2jcc</artifactId>
<version>9.0.0</version>
<packaging>jar</packaging>
<file>${basedir}/src/jars/db2jcc.jar</file>
<createChecksum>true</createChecksum>
<generatePom>true</generatePom>
</configuration>
</execution>
<execution>...</execution>
</executions>
</plugin>
</plugins>
</build>
To install more than one file, just add more executions.
This is working for me:
Let's say I have this dependency
<dependency>
<groupId>com.company.app</groupId>
<artifactId>my-library</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/my-library.jar</systemPath>
</dependency>
Then, add the class-path for your system dependency manually like this
<Class-Path>libs/my-library-1.0.jar</Class-Path>
Full config:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifestEntries>
<Build-Jdk>${jdk.version}</Build-Jdk>
<Implementation-Title>${project.name}</Implementation-Title>
<Implementation-Version>${project.version}</Implementation-Version>
<Specification-Title>${project.name} Library</Specification-Title>
<Specification-Version>${project.version}</Specification-Version>
<Class-Path>libs/my-library-1.0.jar</Class-Path>
</manifestEntries>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.company.app.MainClass</mainClass>
<classpathPrefix>libs/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/libs/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
Basically, add this to the pom.xml:
...
<repositories>
<repository>
<id>lib_id</id>
<url>file://${project.basedir}/lib</url>
</repository>
</repositories>
...
<dependencies>
...
<dependency>
<groupId>com.mylibrary</groupId>
<artifactId>mylibraryname</artifactId>
<version>1.0.0</version>
</dependency>
...
</dependencies>
I've previously written about a pattern for doing this.
It is very similar to the solution proposed by Pascal, though it moves all such dependencies into a dedicated repository module so that you don't have to repeat it everywhere the dependency is used if it is a multi-module build.
we switched to gradle and this works much better in gradle ;). we just specify a folder we can drop jars into for temporary situations like that. We still have most of our jars defined i the typicaly dependency management section(ie. the same as maven). This is just one more dependency we define.
so basically now we can just drop any jar we want into our lib dir for temporary testing if it is not a in maven repository somewhere.
One small addition to the solution posted by Pascal
When I followed this route, I got an error in maven while installing ojdbc jar.
[INFO] --- maven-install-plugin:2.5.1:install-file (default-cli) # validator ---
[INFO] pom.xml not found in ojdbc14.jar
After adding -DpomFile, the problem was resolved.
$ mvn install:install-file -Dfile=./lib/ojdbc14.jar -DgroupId=ojdbc \
-DartifactId=ojdbc -Dversion=14 -Dpackaging=jar -DlocalRepositoryPath=./repo \
-DpomFile=~/.m2/repository/ojdbc/ojdbc/14/ojdbc-14.pom
I was facing with the same issue, and it works just removing the DlocalRepositoryPath parameter and defining the correct path from current location in the Dfile parameter:
mvn install:install-file -Dfile=./repo/com/tridion/cd_core/1.0/cd_core-1.0.jar -DgroupId=com.tridion -DartifactId=cd_core -Dversion=1.0 -Dpackaging=jar
Note: Apache Maven 3.8.6
You can use eclipse to generate a runnable Jar :
Export/Runable Jar file
It's my first couple of days learning Maven and I'm still struggling with the basics. I have an external .jar file (not available in the public repos) that I need to reference in my project and I'm trying to figure out what my best option is.
It's a small scale project without a central repository for libraries, so it has to be either a local repository (somehow added to source control, don't know if it's supposed to work that way?) or the .jar needs to be stored on disk outside of any formal repository.
1) What's my best option for adding the .jar file to my project's references with maven given that I want both the project and the library to be in source control?
2) I still can't seem to have Eclipse see the dependency. I manually added it to the section of the pom, and it shows up fine in the Dependencies list in m2eclipse. mvn compile and mvn package both succeed, but running the program results in:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
LibraryStuff cannot be resolved to a type
This is after editing the POM as:
<dependency>
<groupId>stuff</groupId>
<artifactId>library</artifactId>
<version>1.0</version>
<systemPath>${lib.location}/MyLibrary.jar</systemPath>
<scope>system</scope>
</dependency>
Should I be executing mvn install:install-file even thought I already have the pom.xml edited as above?
Thanks!
You can create an In Project Repository, so you don't have to run mvn install:install-file every time you work on a new computer
<repository>
<id>in-project</id>
<name>In Project Repo</name>
<url>file://${project.basedir}/libs</url>
</repository>
<dependency>
<groupId>dropbox</groupId>
<artifactId>dropbox-sdk</artifactId>
<version>1.3.1</version>
</dependency>
/groupId/artifactId/version/artifactId-verion.jar
detail read this blog post
https://web.archive.org/web/20121026021311/charlie.cu.cc/2012/06/how-add-external-libraries-maven
I think you should use mvn install:install-file to populate your local repository with the library jars then you should change the scope from system to compile.
If you are starting with maven I suggest to use maven directly not IDE plugins as it adds an extra layer of complexity.
As for the error, do you put the required jars on your classpath? If you are using types from the library, you need to have access to it in the runtime as well. This has nothing to do with maven itself.
I don't understand why you want to put the library to source control - it is for sources code not binary jars.
This can be easily achieved by using the <scope> element nested inside <dependency> element.
For example:
<dependencies>
<dependency>
<groupId>ldapjdk</groupId>
<artifactId>ldapjdk</artifactId>
<scope>system</scope>
<version>1.0</version>
<systemPath>${basedir}\src\lib\ldapjdk.jar</systemPath>
</dependency>
</dependencies>
Reference: http://www.tutorialspoint.com/maven/maven_external_dependencies.htm
The Maven manual says to do this:
mvn install:install-file -Dfile=non-maven-proj.jar -DgroupId=some.group -DartifactId=non-maven-proj -Dversion=1 -Dpackaging=jar
update We have since just installed our own Nexus server, much easier and cleaner.
At our company we had some jars that we some jars that were common but were not hosted in any maven repositories, nor did we want to have them in local storage.
We created a very simple mvn (public) repo on Github (but you can host it on any server or locally):
note that this is only ideal for managing a few rarely chaning jar files
Create repo on GitHub:
https://github.com/<user_name>/mvn-repo/
Add Repository in pom.xml
(Make note that the full path raw file will be a bit different than the repo name)
<repository>
<id>project-common</id>
<name>Project Common</name>
<url>https://github.com/<user_name>/mvn-repo/raw/master/</url>
</repository>
Add dependency to host (Github or private server)
a. All you need to know is that files are stored in the pattern mentioned by #glitch
/groupId/artifactId/version/artifactId-version.jar
b. On your host create the folders to match this pattern.
i.e if you have a jar file named service-sdk-0.0.1.jar, create the folder service-sdk/service-sdk/0.0.1/ and place the jar file service-sdk-0.0.1.jar into it.
c. Test it by trying to download the jar from a browser (in our case: https://github.com/<user_name>/mvn-repo/raw/master/service-sdk/service-sdk/0.0.1/service-sdk-0.0.1.jar
Add dependency to your pom.xml file:
<dependency>
<groupId>service-sdk</groupId>
<artifactId>service-sdk</artifactId>
<version>0.0.1</version>
</dependency>
Enjoy
Don't use systemPath. Contrary to what people have said here, you can put an external jar in a folder under your checked-out project directory and haven Maven find it like other dependencies. Here are two crucial steps:
Use "mvn install:install-file" with -DlocalRepositoryPath.
Configure a repository to point to that path in your POM.
It is fairly straightforward and you can find a step-by-step example here:
http://randomizedsort.blogspot.com/2011/10/configuring-maven-to-use-local-library.html
If you meet the same problem and you are using spring-boot v1.4+, you can do it in this way.
There is an includeSystemScope that you can use to add system-scope dependencies to the jar.
e.g.
I'm using oracle driver into my project.
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.3.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/extra-jars/ojdbc14-10.2.0.3.0.jar</systemPath>
</dependency>
then make includeSystemScope=true to include the jar into path /BOOT-INF/lib/**
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
and exclude from resource to avoid duplicated include, the jar is fat enought~
<build>
<testSourceDirectory>src/test/java</testSourceDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>**/*.jar</exclude>
</excludes>
</resource>
</resources>
</build>
Good luck!
Maven way to add non maven jars to maven project
Maven Project and non maven jars
Add the maven install plugins in your build section
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>${version.maven-install-plugin}</version>
<executions>
<execution>
<id>install-external-non-maven1-jar</id>
<phase>clean</phase>
<configuration>
<repositoryLayout>default</repositoryLayout>
<groupId>jar1.group</groupId>
<artifactId>non-maven1</artifactId>
<version>${version.non-maven1}</version>
<file>${project.basedir}/libs/non-maven1.jar</file>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
<goals>
<goal>install-file</goal>
</goals>
</execution>
<execution>
<id>install-external-non-maven2-jar</id>
<phase>clean</phase>
<configuration>
<repositoryLayout>default</repositoryLayout>
<groupId>jar2.group</groupId>
<artifactId>non-maven2</artifactId>
<version>${version.non-maven2}</version>
<file>${project.basedir}/libs/non-maven2.jar</file>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
<goals>
<goal>install-file</goal>
</goals>
</execution>
<execution>
<id>install-external-non-maven3-jar</id>
<phase>clean</phase>
<configuration>
<repositoryLayout>default</repositoryLayout>
<groupId>jar3.group</groupId>
<artifactId>non-maven3</artifactId>
<version>${version.non-maven3}</version>
<file>${project.basedir}/libs/non-maven3.jar</file>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
<goals>
<goal>install-file</goal>
</goals>
</execution>
</executions>
</plugin>
Add the dependency
<dependencies>
<dependency>
<groupId>jar1.group</groupId>
<artifactId>non-maven1</artifactId>
<version>${version.non-maven1}</version>
</dependency>
<dependency>
<groupId>jar2.group</groupId>
<artifactId>non-maven2</artifactId>
<version>${version.non-maven2}</version>
</dependency>
<dependency>
<groupId>jar3.group</groupId>
<artifactId>non-maven3</artifactId>
<version>${version.non-maven3}</version>
</dependency>
</dependencies>
References Note I am the owner of the blog
Change your systemPath.
<dependency>
<groupId>stuff</groupId>
<artifactId>library</artifactId>
<version>1.0</version>
<systemPath>${project.basedir}/MyLibrary.jar</systemPath>
<scope>system</scope>
</dependency>
The pom.xml is going to look at your local repository to try and find the dependency that matches your artifact.
Also you shouldn't really be using the system scope or systemPath attributes, these are normally reserved for things that are in the JDK and not the JRE
See this question for how to install maven artifacts.
Note that all of the example that use
<repository>...</respository>
require outer
<repositories>...</repositories>
enclosing tags. It's not clear from some of the examples.
The best solution here is to install a repository: Nexus or Artifactory. If gives you a place to put things like this, and further it speeds things up by caching your stuff from the outside.
If the thing you are dealing with is open source, you might also consider putting in into central.
See the guide.
With Eclipse Oxygen you can do the below things:
Place your libraries in WEB-INF/lib
Project -> Configure Build Path -> Add Library -> Web App Library
Maven will take them when installing the project.
If the external jar is created by a Maven project only then you can copy the entire project on your system and run a
mvn install
in the project directory. This will add the jar into .m2 directory which is local maven repository.
Now you can add the
<dependency>
<groupId>copy-from-the=maven-pom-of-existing-project</groupId>
<artifactId>copy-from-the=maven-pom-of-existing-project</artifactId>
<version>copy-from-the=maven-pom-of-existing-project</version>
</dependency>
This will ensure that you
mvn exec:java
works. If you use suggested here
<scope>system</scope>
Then you will have to add classes individually while using executing through command line.
You can add the external jars by the following command described here
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> \
-DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>
The most efficient and cleanest way I have found to deal with this problem is by using Github Packages
Create a simple empty public/private repository on GitHub as per your requirement whether you want your external jar to be publicly hosted or not.
Run below maven command to deploy you external jar in above created github repository
mvn deploy:deploy-file \
-DgroupId= your-group-id \
-DartifactId= your-artifact-id \
-Dversion= 1.0.0 -Dpackaging= jar -Dfile= path-to-file \
-DrepositoryId= id-to-map-on-server-section-of-settings.xml \
-Durl=https://maven.pkg.github.com/github-username/github-reponame-created-in-above-step
Above command will deploy you external jar in GitHub repository mentioned in -Durl=.
You can refer this link on How to deploy dependencies as GitHub Packages GitHub
Package Deployment Tutorial
After that you can add the dependency using groupId,artifactId and version mentioned in above step in maven pom.xml and run mvn install
Maven will fetch the dependency of external jar from GitHub Packages registry and provide in your maven project.
For this to work you will also need to configure you maven's settings.xml to fetch from GitHub Package registry.
I need to use a third-party JAR library in my project (actually it's Dresden OCL for Eclipse) that is not provided as a Maven artifact. Instead, it's just a downloadable JAR file. Can I instruct Maven to use this JAR file the same way I'm using <dependencies>? I suppose that there should be some plugin for this purpose?
ps. I just don't want to add 35Mb of third-party binaries into my SVN repository.
Would be nice to have it configured this way:
<build>
<plugins>
<plugin>
<groupId>com.example</groupId>
<artifactId>non-maven-dependencies-injector</artifactId>
<configuration>
<libraries>
<library>http://www.example.com/something*.jar</library>
<library>http://www.example.com/something-else*.jar</library>
</libraries>
</configuration>
</plugin>
<plugins>
</build>
And this plugin would 1) download these JAR files, and 2) add them as dependencies into pom.xml. Maybe this plugin could store them somewhere in ~/.m2/temp/...
yes you can install it into your local repository with maven-install plugin
mvn install:install-file -Dfile=your-artifact-1.0.jar \
-DgroupId=org.some.group \
-DartifactId=your-artifact \
-Dversion=1.0 \
-Dpackaging=jar \
-DgeneratePom=true
If you want your other team members to be able to download this dependency without having to install it them thelves, you need to setup your own artifact repo, and deploy the artifact there with maven-deploy-plugin in same way as you installed it localy.
Yes. This (using non-Mavenized dependencies) is supported by the maven-external-dependency-plugin.
Example:
<artifactItem>
<groupId>jwbroek.cuelib</groupId>
<artifactId>cuelib</artifactId>
<version>${cuelib-version}</version>
<packaging>jar</packaging>
<downloadUrl>http://cuelib.googlecode.com/files/cuelib-${cuelib-version}.jar</downloadUrl>
<checksum>d03b6b960b3b83a2a419e8b5f07b6ba4bd18387b</checksum>
</artifactItem>
It can also extract artifacts from zip files:
<artifactItem>
<groupId>mediautil</groupId>
<artifactId>mediautil</artifactId>
<version>${mediautil-version}</version>
<packaging>jar</packaging>
<install>true</install>
<force>false</force>
<downloadUrl>http://downloads.sourceforge.net/project/mediachest/MediaUtil/Version%201.0/mediautil-1.zip</downloadUrl>
<checksum>aa7ae51bb24a9268a8e57c6afe478c4293f84fda</checksum>
<extractFile>mediautil-${mediautil-version}/mediautil-${mediautil-version}.jar</extractFile>
<extractFileChecksum>e843cd55def75dce57123c79b7f36caca4841466</extractFileChecksum>
</artifactItem>
You can use "system"-scope in your pom.xml for local library dependencies:
system This scope is similar to
provided except that you have to
provide the JAR which contains it
explicitly. The artifact is always
available and is not looked up in a
repository.
systemPath
is used only if the the dependency scope is system. Otherwise, the build will fail if this element is set. The path must be absolute, so it is recommended to use a property to specify the machine-specific path (more on properties below), such as ${java.home}/lib. Since it is assumed that system scope dependencies are installed a priori, Maven will not check the repositories for the project, but instead checks to ensure that the file exists. If not, Maven will fail the build and suggest that you download and install it manually.
<dependency>
<groupId>some.id</groupId>
<artifactId>artifact</artifactId>
<version>1.2.3</version>
<scope>system</scope>
<systemPath>${basedir}/path/to/jarFile.jar</systemPath>
</dependency>
AFAIK, you can use pretty much what you want for groupId, artifactId and version. See Maven System Depencies and this question.
Taking that your example Dresden OCL has over 20 JAR files, and you need to distribute it to many developers, the best solution would be to install a repository manager somewhere (like nexus or artifactory), take your time to upload these 20 jars to that repository, and use them. Best bet would be to put them in a private groupId, so if they'll get published to a m2 repo sometime, you won't end up with name conflicts.
Another point, would be to ask the Dresden OCL Maintainers, if they can offer a m2 repository. Now that m2eclipse is an eclipse incubator project, this might interest more people.
You can install it local or deploy it to your site-local (e.g. company-wide) repository.
If you use a dependency maven cannot find in the configured repositories, it even gives you the required commands:
Then, install it using the command:
mvn install:install-file -DgroupId=mygid -DartifactId=myaid -Dversion=1.0 -Dpackaging=jar -Dfile=/path/to/file
Alternatively, if you host your own repository you can deploy the file there:
mvn deploy:deploy-file -DgroupId=mygid -DartifactId=myaid -Dversion=1.0 -Dpackaging=jar -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id]
Please note: This way you only add the .jar to your repository, there will be no pom along with it to specify transient dependencies. So if your third-party library has dependencies of it's own you'll have to add them manually to your pom.xml as there will be no automatic resolution.
"mvn install:install-file" mentioned here will help you to install the jars into your local repository. However, if you want to make these jars subsequently available to the developers on the project automaticall, copy the relevant pieces to a repository available to everybody on the project and make this repository available using the tag, for example by checking all the files into the SVN. See http://code.google.com/p/codebistro/wiki/BuildNumber#Maven_Plugin_Example which refers to a SVN-hosted repository for example.
P.S. Have a safe trip to the SV!
Here's a different approach that'll let add a Maven repository inside the project directory, use that as a repository in you pom.xml, and share the repository on SVN with anyone checking out the project. Crosspost from here.
In your project directory, create a folder called repo, which we'll use as a folder based Maven repository.
Add the following file repository to your project pom.xml:
<repositories>
<repository>
<id>file.repo</id>
<url>file://${project.basedir}/repo</url>
</repository>
</repositories>
Deploy the external jar(s) to the file repository with the following command:
mvn deploy:deploy-file
-Durl=file:///absolute/path/to/your-project/repo \
-DrepositoryId=file.repo \
-Dfile=path-to-your.jar \
-DgroupId=some.external.project.group \
-DartifactId=the-artifact-name \
-Dversion=1.0 \
-Dpackaging=jar;
Following this you can just add a normal dependency on the jar in your project pom.xml, using the values for groupId, artifactId and version you passed above. You can then add the repo folder to SVN, and commit the changes to your pom.xml. Any developer checking out your project will now be able to use the same dependency without any effort.
I think the below answer will help you...just place jar files on your SVN and tell them all to e synch with SVN and here the ${lib.directory} will be t he local path
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>db2jcc9</id>
<phase>compile</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<groupId>com.ibm</groupId>
<artifactId>db2jcc</artifactId>
<version>9</version>
<packaging>jar</packaging>
<file>${lib.directory}\db2jcc-9.jar</file>
</configuration>
</execution>
</executions>
<plugin>
I have a proprietary jar that I want to add to my pom as a dependency.
But I don't want to add it to a repository. The reason is that I want my usual maven commands such as mvn compile, etc, to work out of the box. (Without demanding from the developers a to add it to some repository by themselves).
I want the jar to be in a 3rdparty lib in source control, and link to it by relative path from the pom.xml file.
Can this be done? How?
I want the jar to be in a 3rdparty lib in source control, and link to it by relative path from the pom.xml file.
If you really want this (understand, if you can't use a corporate repository), then my advice would be to use a "file repository" local to the project and to not use a system scoped dependency. The system scoped should be avoided, such dependencies don't work well in many situation (e.g. in assembly), they cause more troubles than benefits.
So, instead, declare a repository local to the project:
<repositories>
<repository>
<id>my-local-repo</id>
<url>file://${project.basedir}/my-repo</url>
</repository>
</repositories>
Install your third party lib in there using install:install-file with the localRepositoryPath parameter:
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<myGroup> \
-DartifactId=<myArtifactId> -Dversion=<myVersion> \
-Dpackaging=<myPackaging> -DlocalRepositoryPath=<path>
Update: It appears that install:install-file ignores the localRepositoryPath when using the version 2.2 of the plugin. However, it works with version 2.3 and later of the plugin. So use the fully qualified name of the plugin to specify the version:
mvn org.apache.maven.plugins:maven-install-plugin:2.3.1:install-file \
-Dfile=<path-to-file> -DgroupId=<myGroup> \
-DartifactId=<myArtifactId> -Dversion=<myVersion> \
-Dpackaging=<myPackaging> -DlocalRepositoryPath=<path>
maven-install-plugin documentation
Finally, declare it like any other dependency (but without the system scope):
<dependency>
<groupId>your.group.id</groupId>
<artifactId>3rdparty</artifactId>
<version>X.Y.Z</version>
</dependency>
This is IMHO a better solution than using a system scope as your dependency will be treated like a good citizen (e.g. it will be included in an assembly and so on).
Now, I have to mention that the "right way" to deal with this situation in a corporate environment (maybe not the case here) would be to use a corporate repository.
Using the system scope. ${basedir} is the directory of your pom.
<dependency>
<artifactId>..</artifactId>
<groupId>..</groupId>
<scope>system</scope>
<systemPath>${basedir}/lib/dependency.jar</systemPath>
</dependency>
However it is advisable that you install your jar in the repository, and not commit it to the SCM - after all that's what maven tries to eliminate.
This is another method in addition to my previous answer at Can I add jars to maven 2 build classpath without installing them?
This will get around the limit when using multi-module builds especially if the downloaded JAR is referenced in child projects outside of the parent. This also reduces the setup work by creating the POM and the SHA1 files as part of the build. It also allows the file to reside anywhere in the project without fixing the names or following the maven repository structure.
This uses the maven-install-plugin. For this to work, you need to set up a multi-module project and have a new project representing the build to install files into the local repository and ensure that one is first.
You multi-module project pom.xml would look like this:
<packaging>pom</packaging>
<modules>
<!-- The repository module must be first in order to ensure
that the local repository is populated -->
<module>repository</module>
<module>... other modules ...</module>
</modules>
The repository/pom.xml file will then contain the definitions to load up the JARs that are part of your project. The following are some snippets of the pom.xml file.
<artifactId>repository</artifactId>
<packaging>pom</packaging>
The pom packaging prevents this from doing any tests or compile or generating any jar file. The meat of the pom.xml is in the build section where the maven-install-plugin is used.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<executions>
<execution>
<id>com.ibm.db2:db2jcc</id>
<phase>verify</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<groupId>com.ibm.db2</groupId>
<artifactId>db2jcc</artifactId>
<version>9.0.0</version>
<packaging>jar</packaging>
<file>${basedir}/src/jars/db2jcc.jar</file>
<createChecksum>true</createChecksum>
<generatePom>true</generatePom>
</configuration>
</execution>
<execution>...</execution>
</executions>
</plugin>
</plugins>
</build>
To install more than one file, just add more executions.
This is working for me:
Let's say I have this dependency
<dependency>
<groupId>com.company.app</groupId>
<artifactId>my-library</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/my-library.jar</systemPath>
</dependency>
Then, add the class-path for your system dependency manually like this
<Class-Path>libs/my-library-1.0.jar</Class-Path>
Full config:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifestEntries>
<Build-Jdk>${jdk.version}</Build-Jdk>
<Implementation-Title>${project.name}</Implementation-Title>
<Implementation-Version>${project.version}</Implementation-Version>
<Specification-Title>${project.name} Library</Specification-Title>
<Specification-Version>${project.version}</Specification-Version>
<Class-Path>libs/my-library-1.0.jar</Class-Path>
</manifestEntries>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.company.app.MainClass</mainClass>
<classpathPrefix>libs/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/libs/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
Basically, add this to the pom.xml:
...
<repositories>
<repository>
<id>lib_id</id>
<url>file://${project.basedir}/lib</url>
</repository>
</repositories>
...
<dependencies>
...
<dependency>
<groupId>com.mylibrary</groupId>
<artifactId>mylibraryname</artifactId>
<version>1.0.0</version>
</dependency>
...
</dependencies>
I've previously written about a pattern for doing this.
It is very similar to the solution proposed by Pascal, though it moves all such dependencies into a dedicated repository module so that you don't have to repeat it everywhere the dependency is used if it is a multi-module build.
we switched to gradle and this works much better in gradle ;). we just specify a folder we can drop jars into for temporary situations like that. We still have most of our jars defined i the typicaly dependency management section(ie. the same as maven). This is just one more dependency we define.
so basically now we can just drop any jar we want into our lib dir for temporary testing if it is not a in maven repository somewhere.
One small addition to the solution posted by Pascal
When I followed this route, I got an error in maven while installing ojdbc jar.
[INFO] --- maven-install-plugin:2.5.1:install-file (default-cli) # validator ---
[INFO] pom.xml not found in ojdbc14.jar
After adding -DpomFile, the problem was resolved.
$ mvn install:install-file -Dfile=./lib/ojdbc14.jar -DgroupId=ojdbc \
-DartifactId=ojdbc -Dversion=14 -Dpackaging=jar -DlocalRepositoryPath=./repo \
-DpomFile=~/.m2/repository/ojdbc/ojdbc/14/ojdbc-14.pom
I was facing with the same issue, and it works just removing the DlocalRepositoryPath parameter and defining the correct path from current location in the Dfile parameter:
mvn install:install-file -Dfile=./repo/com/tridion/cd_core/1.0/cd_core-1.0.jar -DgroupId=com.tridion -DartifactId=cd_core -Dversion=1.0 -Dpackaging=jar
Note: Apache Maven 3.8.6
You can use eclipse to generate a runnable Jar :
Export/Runable Jar file