Managing dependencies in a Maven Project - java

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).

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.

Maven dependency gives NoSuchMethodError

I am working in a Maven project (Java, using pom.xml files), where I need to use the FastMath library (part of the Apache commons math3 library).
The top level library on which I am working finds FastMath without problems. However, one of the dependencies (a local .jar that I installed so that it appears in the .m2/repository) also needs FastMath, but it gives the NoSuchMethodError message.
Installed the .jar as follows:
mvn install:install-file -Dfile=/home/me/Libraries/A.jar -DgroupId=x.y -DartifactId=y -Dversion=1.0 -Dpackaging=jar
Included it in the pom.xml file as follows:
<dependency>
<groupId>x.y</groupId>
<artifactId>y</artifactId>
<version>1.0</version>
</dependency>
It compiles without problems, the error only occurs when I attempt to run/debug.
Thanks, Michiel
This type of maven repository is not available in central repository also
.to resolve that one try to add manulaly dependencies to project http://mvnrepository.com/search?q=x.y

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.

How to install Google API's json-simple jar to a maven project?

I've just made a java project with maven:
mvn archetype:create -DgroupId=com.company -DartifactId=\
myproject -DarchetypeArtifactId=maven- archetype-quickstart
Then need to import a 3rd party jar, path is ~/Downloads/json-simple-1.1.1.jar, I tried the following commands:
mvn -e install:install-file -Dfile=~/Downloads/json_simple-1.1.1.jar\
-DgroupiId=org.json.simple - DartifactId=json_simple -Dversion=1.1.1 -Dpackaging=jar
but an error occoured:
[ERROR] BUILD ERROR
[INFO] Missing group, artifact, version, or packaging information
I'm using maven 2.2.1, java 1.6.0_35 and Ubuntu 11.10.
The dependency is already in maven central repository. Just the following dependency to the pom of your project and you are done:
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
You don't need to install json_simple locally. It is in Maven Central. Go to http://search.maven.org and search.
groupid: com.googlecode.json-simple
artifactid: json-simple
version 1.1.1
It is better to satisfy a dependency from Central rather than relying on one you installed locally. For a start, it will make it easier for other people to build your code.
As for the build problem, I suspect that you have made a mistake in the dependencies section of your project's "POM.xml" file.
Don't remember the exact syntax but you can di the following:
Add thus as a dependency in the pom file of the project you want to build
The build will fail as this jar is not part of the repo yet
But maven will also generate the command to install the jar in repo copy paste that, change the path the jar and execute.
This should solve the purpose.
But looks like this jar is already part of some repo.gGoogle it out might just need to add a new repo

Maven: How to include jars, which are not available in reps into a J2EE project?

in my J2EE project I've a couple of dependencies, which are not available in any Maven repository, because they're proprietary libraries. These libraries need to be available at runtime, so that have to be copied to target/.../WEB-INF/lib ...
Right now, I'm listing them as system dependency in my POM, but with this method the problem is, that aren't being copied to the target build during compilation. Also this method is not very elegant.
So which is the best way to integrate them in Maven?
Note: I don't want to create my own Maven repository.
For people wanting a quick solution to this problem:
<dependency>
<groupId>LIB_NAME</groupId>
<artifactId>LIB_NAME</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${basedir}/WebContent/WEB-INF/lib/YOUR_LIB.jar</systemPath>
</dependency>
just give your library a unique groupID and artifact name and point to where it is in the file system. You are good to go.
Of course this is a dirty quick fix that will ONLY work on your machine and if you don't change the path to the libs. But some times, that's all you want, to run and do a few tests.
EDIT: just re-red the question and realised the user was already using my solution as a temporary fix. I'll leave my answer as a quick help for others that come to this question. If anyone disagrees with this please leave me a comment. :)
As you've said you don't want to set up your own repository, perhaps this will help.
You can use the install-file goal of the maven-install-plugin to install a file to the local repository. If you create a script with a Maven invocation for each file and keep it alongside the jars, you (and anyone else with access) can easily install the jars (and associated pom files) to their local repository.
For example:
mvn install:install-file -Dfile=/usr/jars/foo.jar -DpomFile=/usr/jars/foo.pom
mvn install:install-file -Dfile=/usr/jars/bar.jar -DpomFile=/usr/jars/bar.pom
or just
mvn install:install-file -Dfile=ojdbc14.jar -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=10.2.0 -Dpackaging=jar
You can then reference the dependencies as normal in your project.
However your best bet is still to set up an internal remote repository and I'd recommend using Nexus myself. It can run on your development box if needed, and the overhead is minimal.
Create a repository folder under your project. Let's take
${project.basedir}/src/main/resources/repo
Then, install your custom jar to this repo:
mvn install:install-file -Dfile=[FILE_PATH] \
-DgroupId=[GROUP] -DartifactId=[ARTIFACT] -Dversion=[VERS] \
-Dpackaging=jar -DlocalRepositoryPath=[REPO_DIR]
Lastly, add the following repo and dependency definitions to the projects pom.xml:
<repositories>
<repository>
<id>project-repo</id>
<url>file://${project.basedir}/src/main/resources/repo</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>[GROUP]</groupId>
<artifactId>[ARTIFACT]</artifactId>
<version>[VERS]</version>
</dependency>
</dependencies>
You need to set up a local repository that will host such libraries. There are a number of projects that do exactly that. For example Artifactory.
None of the solutions work if you are using Jenkins build!! When pom is run inside Jenkins build server.. these solutions will fail, as Jenkins run pom will try to download these files from enterprise repository.
Copy jars under src/main/resources/lib (create lib folder). These will be part of your project and go all the way to deployment server. In deployment server, make sure your startup scripts contain src/main/resources/lib/* in classpath. Viola.
you can install them in a private, local repository (e.g. .m2/repository under your home directory): more details here
If I am understanding well, if what you want to do is to export dependencies during the compilation phase so there will be no need to retrieve manually each needed libraries, you can use the mojo copy-dependencies.
Hope it can be useful in your case (examples)
#Ric Jafe's solution is what worked for me.
This is exactly what I was looking for. A way to push it through for research test code. Nothing fancy. Yeah I know that that's what they all say :) The various maven plugin solutions seem to be overkill for my purposes. I have some jars that were given to me as 3rd party libs with a pom file. I want it to compile/run quickly. This solution which I trivially adapted to python worked wonders for me. Cut and pasted into my pom. Python/Perl code for this task is in this Q&A: Can I add jars to maven 2 build classpath without installing them?
def AddJars(jarList):
s1 = ''
for elem in jarList:
s1+= """
<dependency>
<groupId>local.dummy</groupId>
<artifactId>%s</artifactId>
<version>0.0.1</version>
<scope>system</scope>
<systemPath>${project.basedir}/manual_jars/%s</systemPath>
</dependency>\n"""%(elem, elem)
return s1
Continue to use them as a system dependency and copy them over to target/.../WEB-INF/lib ... using the Maven dependency plugin:
http://maven.apache.org/plugins/maven-dependency-plugin/examples/copying-artifacts.html
Install alone didn't work for me.
mvn deploy:deploy-file -Durl=file:///home/me/project/lib/ \
-Dfile=target/jzmq-2.1.3-SNAPSHOT.jar -DgroupId=org.zeromq \
-DartifactId=zeromq -Dpackaging=jar -Dversion=2.1.3

Categories

Resources