How to add local jar files to a Maven project?
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
How to include local jar files in Maven project
Have you considered adding those two JARs as system
dependencies? e.g.,
<project>
...
<dependencies>
<dependency>
<groupId>sun.jdk</groupId>
<artifactId>tools</artifactId>
<version>1.5.0</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>
...
</project>
Just a word of note, this is NOT recommended and should be used very sparingly, if ever.
how to add a local jar as a dependency to a maven project?
You'll need to add the jar to your maven local repository.
mvn install:install-file -Dfile=/path/to/ssj.jar -DgroupId=ca.umontreal.iro -DartifactId=ssj -Dversion=2.5 -Dpackaging=jar
(Change /path/to/ssj.jar to the path of the the file in your computer)
This will make it possible for Maven to resolve this JAR from the local repository using the dependency defined above, when you are building your application.
Cannot add local jar files in maven project
Even if I still don't understand why, I found out that executing:
mvn install:install-file -Dfile=/home/luca/openbeans-1.0.jar -DgroupId=com.googlecode.openbeans -DartifactId=openbeans -Dversion=1.0 -Dpackaging=jar -DgeneratePom=true
And using in pom:
<dependency>
<groupId>com.googlecode.openbeans</groupId>
<artifactId>openbeans</artifactId>
<version>1.0</version>
</dependency>
The code is compiled without error. But as I said, still wondering why.
Is there a way to create maven dependency in pom file from a local jar
This code
public static void main(String[] args) throws IOException {
Set<String> missingMavenData = new HashSet<String>();
String FOLDER = "/path/to/your/folder/with/jars";
Files
.walk(Paths.get(FOLDER), FileVisitOption.FOLLOW_LINKS)
.map(Path::toFile)
.filter(f -> f.isFile())
.filter(f -> f.getName().endsWith(".jar"))
.map(f -> {
try {
return new JarFile(f);
} catch (IOException e) {
e.printStackTrace();
return null;
}
})
.filter(Objects::nonNull)
.map(jar -> {
Properties properties = null;
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
JarEntry jarEntry = entries.nextElement();
if (jarEntry.getName().matches("^META-INF/maven/.*/pom\\.properties$")) {
try {
properties = new Properties();
properties.load(jar.getInputStream(jarEntry));
break;
} catch (IOException e) {
e.printStackTrace();
}
};
}
if (properties == null) {
missingMavenData.add(jar.getName());
}
return properties;
})
.filter(Objects::nonNull)
.forEach(properties -> {
System.out.println("< dependency>");
System.out.println(" <groupId>" + properties.getProperty("groupId")+ "</groupId>");
System.out.println(" <artifactId>" + properties.getProperty("artifactId")+ "</artifactId>");
System.out.println(" <version>" + properties.getProperty("version")+ "</version>");
System.out.println("</dependency>");
});
System.out.println("Those JAR files do not contain Maven metadata:");
missingMavenData.forEach(System.out::println);
}
will iterate over your jar files and try to find the Maven metadata in them. It will generate the POM entries for those who have it and will list those that don't have it so you can add it manually. I hope this helps.
UPDATE:
I added bom-helper:fromJars
goal to BOM Helper Maven Plugin which does more or less the same thing as the above code. One can now simply add the plugin
<plugin>
<groupId>com.commsen.maven</groupId>
<artifactId>bom-helper-maven-plugin</artifactId>
<version>0.2.0</version>
</plugin>
and configure it to call fromJars
goal. It can also be called form command line. For example:
mvn bom-helper:fromJars \
-Dbom-helper.jarsFolder=/path/to/folder/with/jars \
-Dbom-helper.inplace=true \
-Dbom-helper.recursive
will update current pom with entries for all jars that have Maven metadata in them.
Related Topics
Using Jfreechart to Display Recent Changes in a Time Series
What Is a Classpath and How to Set It
Why Can't I Use Switch Statement on a String
Do Java Arrays Have a Maximum Size
Javafx 8 Compatibility Issues - Fxml Static Fields
Why Does Gson Fromjson Throw a Jsonsyntaxexception: Expected Begin_Object But Was Begin_Array
Difference Between Int[] Array and Int Array[]
Method Overloading For Null Argument
Why Is Using a Wild Card With a Java Import Statement Bad
How to Properly Stop the Thread in Java
Creating a Custom Button in Java With Jbutton
Why Is It Bad Practice to Call System.Gc()
When Would You Use the Builder Pattern
How Do the Post Increment (I++) and Pre Increment (++I) Operators Work in Java