For Java libraries that are not available in the Maven Central or other Maven repositories, we need to install it into our Maven Local repository in order to use it as a project dependencies.

Note
You may interest at this How to add Oracle JDBC driver in your Maven local repository

In this tutorial, we will show you how to install the kaptcha jar into our Maven local repository.

1. Kaptcha

For example, kaptcha, a popular third party Java library, which is generating captcha image to stop spam, but it’s not available in the Maven center repository.

Update
Now, the kaptcha is available in the Maven Central Repository. It is fine, even it is in the Maven Central, we still can install the Kaptcha JAR file manually into our Maven local repository.

2. mvn install

Download the kaptcha, extract it and copy the kaptcha-${version}.jar to somewhere else, for example, C drive, and run this command:

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

For example:

> mvn install:install-file -Dfile=c:\kaptcha-2.3.jar -DgroupId=com.google.code 
-DartifactId=kaptcha -Dversion=2.3 -Dpackaging=jar

[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'install'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Default Project
[INFO]    task-segment: [install:install-file] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] [install:install-file]
[INFO] Installing c:\kaptcha-2.3.jar to 
D:\maven_repo\com\google\code\kaptcha\2.3\kaptcha-2.3.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: < 1 second
[INFO] Finished at: Tue May 12 13:41:42 SGT 2009
[INFO] Final Memory: 3M/6M
[INFO] ------------------------------------------------------------------------

Done, the kaptcha jar is installed (copied) into our Maven local repository.

Example install kaptcha on Ubuntu

$ mvn install:install-file -Dfile="/home/tvt/workspace/favtuts/maven/kaptcha-2.3.2/kaptcha-2.3.2.jar" -DgroupId=com.google.code -DartifactId=kaptcha -Dversion=2.3.2 -Dpackaging=jar

[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO] 
[INFO] --- maven-install-plugin:2.4:install-file (default-cli) @ standalone-pom ---
[INFO] Installing /home/tvt/workspace/favtuts/maven/kaptcha-2.3.2/kaptcha-2.3.2.jar to /home/tvt/.m2/repository/com/google/code/kaptcha/2.3.2/kaptcha-2.3.2.jar
[INFO] Installing /tmp/mvninstall14104026958708308836.pom to /home/tvt/.m2/repository/com/google/code/kaptcha/2.3.2/kaptcha-2.3.2.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.289 s
[INFO] Finished at: 2022-06-17T09:55:23+07:00
[INFO] ------------------------------------------------------------------------

3. pom.xml

After installed, just declares the kaptcha dependency as usual.

pom.xml

	<dependency>
		  <groupId>com.google.code</groupId>
		  <artifactId>kaptcha</artifactId>
		  <version>2.3</version>
	 </dependency>

4. Alternative Solution

Forget about the ‘mvn install`, we also can download the .jar and tell the project to find the .jar in the system path like this:

pom.xml

	<dependency>
		<groupId>com.google.code</groupId>
		<artifactId>kaptcha</artifactId>
		<version>2.3</version>
		<scope>system</scope>
		<systemPath>d:/projects/kaptcha.jar</systemPath>
	</dependency>

pom.xml

	<dependency>
		<groupId>com.google.code</groupId>
		<artifactId>kaptcha</artifactId>
		<version>2.3</version>
		<scope>system</scope>
		<systemPath>${project.basedir}/lib/kaptcha.jar</systemPath>
	</dependency>

References

Leave a Reply

Your email address will not be published. Required fields are marked *