How can add the proprietary library in maven project with dependency? - java

I have a proprietary library. I am using this library in my java desktop project but now, I have a maven project. I want to add this library in my maven project with dependency. How can I do this?

Firstly you should search if this library is available as maven dependency. Try maven search.
For example if you want to include a library commons-io-2.4.jar, you serach for it in the link above, if it is in public repository (it is), then you get maven dependency:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
You should paste this dependency to your pom to dependencies tag.
If above library is not available in public repository, you have to place your jar manually in your local repository by refering to: http://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
Example:
mvn install:install-file -DgroupId=commons-io -DartifactId=commons-io -Dpackaging=jar -Dversion=2.4 -Dfile=home_folder_path/commons-io-2.4.jar -DgeneratePom=true

If it is not already on maven repository, simply add it to your pom.xml, and add it to your local repository.
<dependencies>
<dependency>
<groupId>com.proppath</groupId>
<artifactId>mylib.jar</artifactId>
<version>myjarversion</version>
</dependency>
And also put this prop library into
.m2\repository\com\proppath\myjarversion
Give any groupId you like but make sure that you have corresponding path on repository to find it.
Normally, in large projets these kind of propriatery libraries are stored in Nexus of your enterprise.

Related

How to create a custom pom dependecy which contains multiple jars? Is it possible?

My project contains around 30 jars which does not have a maven dependency settings.
So I have to manually add the jars in th local maven repo and then use the custom maven dependencies to project.
However I have to add 30 dependencies in the POM file which I feel might not be a good way to do it.
Is there any way so that those all 30 jar files can be added with a single pom dependency?
For adding the jar in the local maven repo and then use the dependency, I am using below method:
Ex:
$ mvn install:install-file -Dfile=c:\kaptcha-{version}.jar -DgroupId=com.google.code
-DartifactId=kaptcha -Dversion={version} -Dpackaging=jar
and then use the dpendency as below:
<dependency>
<groupId>com.google.code</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3</version>
</dependency>
Kapcha jar is just for reference and I have dfferent 30 jars to add in repo and use in the project.
Please help.
First of all, I would check whether you can draw a fair amount of these dependencies form MavenCentral (avoiding the manual hassle).
Secondly, if you are in a company with more than two Java developers, you should set up a Nexus or Artifactory server to handle the JARs. So no manual installation any more.
After saying this: You can create a POM that contains a list of dependencies. If you add this POM as dependency in your project, then all the dependencies of the POM will become (transitive) dependencies of your project.

NoClassDefFoundError on Maven dependency present locally

I am new to Maven Project. I am making changes to one of the open source maven project. I am facing a problem in adding a library to the project. So far i have done this :-
I added a library named jni4net.j-0.8.8.0.jar to the resources folder of the project.
I right clicked the jar(in Intellij) and clicked 'Add as library'.
Then in the pom.xml i added:-
<dependency>
<groupId>jar.0.8.8.0.jni4net</groupId>
<artifactId>jar.0.8.8.0.jni4net</artifactId>
<version>0.8.8.0</version>
<scope>system</scope>
<systemPath>${basedir}/src/main/resources/jni4net.j-
0.8.8.0.jar</systemPath>
</dependency>
But when i build this project(build is successful, test cases are running) and use this it throws following error:-
java.lang.NoClassDefFoundError: net/sf/jni4net/Bridge
Please help me resolve it. I am new to maven and pom. I have looked at various answers, but not getting it right.
PS - I named groupId and artifactID as just reverse of jar file
This is not the right way to add that dependency.
All you need is:
<dependency>
<groupId>net.sf.jni4net</groupId>
<artifactId>jni4net.j</artifactId>
<version>0.8.8.0</version>
</dependency>
The dependency will be retrieved from Maven Central when you build.
Using <systemPath>...</systemPath> is highly discouraged as it usually ties your project to a local environment.
Since jni4net.j dependency is available in maven central, You don't have to download and put the dependency manually. Maven will download and store the dependency locally in `'.m2' folder. Just add dependency as bellow.
<dependency>
<groupId>net.sf.jni4net</groupId>
<artifactId>jni4net.j</artifactId>
<version>0.8.8.0</version>
</dependency>

how to add jar dependency in maven?

When I run
mvn compile
I get package com.ibm.icu.util does not exist. So I downloaded the ICU4J jar and installed it into the local repository. I confirmed it's in .m2/repository/com/ibm/icu/icu4j/3.4.4/icu4j-3.4.4.jar. Inside that jar file is the missing class file com/ibm/icu/util/Calendar.class. Then I added the following into the dependencies section of pom.xml:
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>3.4.4</version>
</dependency>
But when I run mvn compile again, I get the same error. What am I doing wrong?
You should avoid adding dependencies manually.
If you don't know a groupId and artifactId of the dependency you need, search for it at http://mvnrepository.com/. Usually, groupId matches the package names in the jar file.
For your case, the dependency is already there: http://mvnrepository.com/search?q=com.ibm.icu
So, go to http://mvnrepository.com/artifact/com.ibm.icu/icu4j and get the version of the dependency you need, e.g. 55.1: http://mvnrepository.com/artifact/com.ibm.icu/icu4j/55.1
Grab maven dependency xml and put it to your pom.xml file:
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>55.1</version>
</dependency>
If you didn't find your dependency try to find it in google. Sometimes the dependency may be found in some corporate public repositories, not in a central. In this case you need to add the third-party repository to repositories section of your pom.xml.
If you're unable to find your dependency in the public repository then you have three options:
A. Install jar to internal repository server (e.g. nexus)
B. Put the JAR file in your project sources and declare project maven repository :
<repositories>
<repository>
<id>my-local-repo</id>
<url>file://${basedir}/my-repo</url>
</repository>
</repositories>
Important: You should keep the maven repository layout in your local repository.
C. [Bad Practice] Use maven install plugin to install your custom jar to local repository on your machine. But it's a badIt's not recommended.
mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=path-to-your-artifact-jar -DpomFile=path-to-pom
D. [Bad Practice] Use system dependency with absolute path to the JAR file, although it's a bad practice and should be avoided.
<dependency>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>X.Y.Z</version>
<scope>system</scope>
<systemPath>${user.home}/jars/my.jar</systemPath>
</dependency>
You should not be manually installing things into the maven repository directory. That directory is maintained by maven itself.
The way dependencies work is that when you run mvn compile or some other goal, it will connect to the maven central repository and download the needed dependencies and their dependencies.
If you manually install a jar file, it may not have it's dependencies. That icu artifact will likely have other things it depends on. Maven will automatically resolve these dependencies.
I would recommend using mvn clean install this will clean the target directory and rebuild everything.
If it fails to download, then you likely need to change the maven configuration. For example, if you are behind a proxy server you need to configure maven with the proxy credentials.
Also, if you manually copied anything into the .m2/repository/ directory you should delete it and let maven put it in there correctly instead.
The beauty of maven is that you don't need to worry about things like downloading jars. It just handles that for you. Let it do it's job.
If you have an internal artifactory like JFrog maybe you should check that the jar is there. Do not download manually to .m2 because it's at least strange. At most you can upload the jar in that artifactory manually.

Maven swizzle dependency doesn't work

I try to compile this pom file but it doesn't download
import org.codehaus.swizzle.confluence.*;
dependency. I get pom file from maven's site. Search Maven Swizzle
That's the POM of the swizzle-confluence library, i.e. the POM file that the library's project uses for its own build.
Instead, you need to add the dependency declaration for this library to your project's POM. In Maven Central search, this is available on the left-hand-side of the version information page, under Dependency Information.
So, open up the page for swizzle-conflunce:1.6.2, and you can see that this is:
<dependency>
<groupId>org.codehaus.swizzle</groupId>
<artifactId>swizzle-confluence</artifactId>
<version>1.6.2</version>
</dependency>
Also, to help yourself in the future, consider reading about the POM and how dependencies work.

Managing dependencies in a Maven Project

I have 2 JARs that are supposed to be imported into a maven project. I followed this tutorial (click here) and imported those JARs into my maven project. Basically, I executed this code in the terminal:
mvn install:install-file -Dfile=myfile.jar -DgroupId=mygroup -DartifactId=com.mygroup.project -Dversion=1.0 -Dpackaging=jar -DlocalRepositoryPath=lib -DcreateChecksum=true
and then I imported the library into my Maven project.
All this works fine. However, the JARs I am importing are supposed to have few dependencies themselves. As I understand, Maven handles the internal dependencies automatically. Now I have a list of dependencies (with group ID, artefact ID and version) but I don't understand where do I write those. In the folder 1.0 of the library, there is a file called myjar-1.0.pom. I tried writing the dependencies there but it was of no use.
Could you tell me a way of manually telling Maven to load up a few dependencies?
I also tried specifying these dependencies in the main pom.xml but it results in errors - saying the repo-url/dependency/file.pom was not found. So I guess it needs to be mentioned in internal dependency only - but I can't figure out a way of manually defining them. Will I need to create a pom.xml within those libraries, or is there something that I am missing out?
You can do this:
write the pom for you jar, and declare the dependencies in the pom.
user mvn install:install-file -Dfile=path-to-your-artifact-jar -DpomFile=path-to-pom to install you jar.
link: http://maven.apache.org/plugins/maven-install-plugin/examples/custom-pom-installation.html
Let's say you have the following JARs and dependencies:
myMavenProject
--> library1.jar
--> library2.jar
--> library3.jar
(I'm assuming that your library1, library2 and library3 are not Maven projects)
For each library1, library2 and library3, perform mvn install:install-file like what you've did before:
mvn install:install-file -Dfile=library1.jar -DgroupId=com.mygroup.project -DartifactId=library1 -Dversion=1.0 -Dpackaging=jar
(Note that I swap your groupId and artifactId here)
Then in your Maven project, update your pom.xml to have following:
<dependency>
<groupId>com.mygroup.project</groupId>
<artifactId>library1</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.mygroup.project</groupId>
<artifactId>library2</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.mygroup.project</groupId>
<artifactId>library3</artifactId>
<version>1.0</version>
</dependency>
Unless you have a very specific requirement, I'm not sure why you want to have -DlocalRepositoryPath=lib extra options for Maven and not using your local M2 Repository.
The tutorial you are following might be working, but it is not The Maven Way! If you will follow this path, you will have troubles all along the way!
You'd better read some tutorial from the maven user center or the maven books and resources page. Maven: the definitve guide for example is available online for free.
Why the approach you are currently following is not good and how you'd do this better is mentioned by Stephen Connolly on a very good blog post of him (Stephen is an active maven developer).

Categories

Resources