Download local repository jar dependency files through maven - java

I was not able to download one jar through maven so i have created one local repository and downloaded jar manually and then added to pom by using system scope
<scope>system</scope>
<systemPath>c:\\bdh\gdhs</systemPath>
but i requires the dependencies of the local repository jar. what should i do then? manually adding each and every dependency jars is not a good solution it seems
someone help please..

Do not use system scope.
Install the downloaded file to local repository.
If you have both pom & artifact
mvn install:install-file -Dfile=<path-to-file> -DpomFile=<path-to-pomfile>
To have its dependencies also resolved correctly - so not to add every dependency by hand - you need the pom.
If you do not have the pom
mvn install:install-file -Dfile=<path-to-file> -DgroupId=**group-id** \
-DartifactId=**artifact-id** -Dversion=**version** -Dpackaging=<packaging>
Without pom the dependencies of installed artifact can not be resolved automatically and you need to manually install them (preferably with pom).
Then just add it as dependency:
<dependency>
<groupId>***group-id**</groupId>
<artifactId>**artifact-id**</artifactId>
<version>**version**</version>
</dependency>
Refer Guide to installing 3rd party JARs for more details

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 doesn't see dependencies which are JAR files in a subfolder

I have a project where I use Maven as the build tool.
In my pom.xml file I have these dependencies:
<dependency>
<groupId>com.my.group1</groupId>
<artifactId>MyArtifact1</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.my.group2</groupId>
<artifactId>MyArtifact2</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>com.my.group3</groupId>
<artifactId>MyArtifact3</artifactId>
<version>1.3</version>
</dependency>
Within the folder where I have my pom.xml, I also have a subfolder, \external. In this subfolder, I have the above listed artifacts as jars: MyArtifact1-1.0.jar, MyArtifact2-1.1.jar, MyArtifact3-1.3.jar.
The problem is that when I run mvn install, I get this:
[ERROR] Failed to execute goal on project my-project: Could not resolve dependencies for project com.my.group4:sample-project:jar:20161207.3: The following artifacts could not be resolved: com.my.group1:MyArtifact1:jar:1.0, com.my.group2:MyArtifact2:jar:1.1, com.my.group3:MyArtifact3:jar:1.3: Failure to find com.my.group1:MyArtifact1:jar:1.0 in https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced
So far, I tied running this:
mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=./external/MyArtifact1-1.0.jar -DgroupId=com.my.group1 -DartifactId=MyArtifact1 -Dversion=1.0 -Dpackaging=jar
I tried the above for all 3 packages, but still it looks like it didn't have any effect.
You are trying to include in the maven compilation .jar files you downloaded in your computer ?
Remember that Maven will try to get your dependencies from the local repository first (a folder in your computer) and from the global repositories later. If your dependencies (.jar) are not in those repositories, you will get an error message.
Here I give you some ideas/solutions:
If the .jar files resulted from other maven projects
If the .jar files are the product of other maven projects you created in your computer. You can install the outputs of that project in your local repository. Once they are installed, maven will find the dependencies locally.
// in the other projects
mvn clean install
If you have many local projects, you may setup your own maven repository to manage all your reusable components. Many companies setup their own repository in their local network. You can use software such as Artifactory
or Nexus Repository Manager. You can even built a repository using your filesystem. Note that, in order to use these repositories, you have to configure them in the pom.xml file of your project.
<project>
...
<repositories>
<repository>
<id>my-internal-site</id>
<url>http://myserver/repo</url>
</repository>
</repositories>
...
</project>
If the .jar files were downloaded from other websites
Sometimes you need .jar files that exist in global maven repositories. You may try first to search in these repositories. You can go to sites such as MVN repository to search or browse the existing packages in the repositories. If you find the dependencies you need, you can obtain too the required dependency to include in the pom.xml.
If the .jar files does not exist in any repository, you can install the file in your local repository. There is a tutorial in the Heroku's devcenter and another one in Mkyong. You must pick a groupId-artifactId-version for the file, install the file and add the dependency to the pom.xml of the project.
Note that, if the .jar was not created using maven, you must provide all the parameters
mvn install:install-file -Dfile=path-to-your-artifact-jar \
-DgroupId=your.groupId \
-DartifactId=your-artifactId \
-Dversion=version \
-Dpackaging=jar \
-DlocalRepositoryPath=path-to-specific-local-repo
If the .jar was generated with Maven, that file will include their own pom.xml with metadata. If you use Maven 2.5+, you do not need to provide the groupId-artifactId and version.
mvn install:install-file -Dfile=path-to-your-artifact-jar \
-DlocalRepositoryPath=path-to-specific-local-repo
There is another option. You can use the system scope with a local systemPath. However, I think this solution requires that people keep the library in the same location for all the developers, or requires to include the .jar file in the project/code repository. Using a repository is a better idea.
<dependency>
<artifactId>..</artifactId>
<groupId>..</groupId>
<scope>system</scope>
<systemPath>${basedir}/lib/dependency.jar</systemPath>
</dependency>
Please check whether these jars present in your local repository.
You can find it's location from your maven setting.xml file , if not mentioned there then it will take the default path i.e. .m2 folder inside c drive.
You need to check first whether these jars are present there or not. If not, then either you need to copy those in proper folder structure or you can install those jars on your local firing below command :
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>
The recommended way is:
use a Maven Repository manager such as Nexus
configure Maven to use a Single Repository Group
configure proxy repos to other Maven Repositories as needed and add them to the group you specified in your settings file (see previous step)
if not already available, configure a repo for 3rd party libs in your repository manager and add it to the group you specified in your settings file (see previous step)
if your dependencies are not available from any repo, then add them to your 3rd party repo
remove all JARs from the source code

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.

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

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

Categories

Resources