How do I take a jar file that I have and add it to the dependency system in maven 2? I will be the maintainer of this dependency and my code needs this jar in the class path so that it will compile.
You'll have to do this in two steps:
1. Give your JAR a groupId, artifactId and version and add it to your repository.
If you don't have an internal repository, and you're just trying to add your JAR to your local repository, you can install it as follows, using any arbitrary groupId/artifactIds:
mvn install:install-file -DgroupId=com.stackoverflow... -DartifactId=yourartifactid... -Dversion=1.0 -Dpackaging=jar -Dfile=/path/to/jarfile
You can also deploy it to your internal repository if you have one, and want to make this available to other developers in your organization. I just use my repository's web based interface to add artifacts, but you should be able to accomplish the same thing using mvn deploy:deploy-file ....
2. Update dependent projects to reference this JAR.
Then update the dependency in the pom.xml of the projects that use the JAR by adding the following to the element:
<dependencies>
...
<dependency>
<groupId>com.stackoverflow...</groupId>
<artifactId>artifactId...</artifactId>
<version>1.0</version>
</dependency>
...
</dependencies>
You can also specify a dependency not in a maven repository. Could be usefull when no central maven repository for your team exist or if you have a CI server
<dependency>
<groupId>com.stackoverflow</groupId>
<artifactId>commons-utils</artifactId>
<version>1.3</version>
<scope>system</scope>
<systemPath>${basedir}/lib/commons-utils.jar</systemPath>
</dependency>
Actually, on investigating this, I think all these answers are incorrect. Your question is misleading because of our level of understanding of maven. And I say our because I'm just getting introduced to maven.
In Eclipse, when you want to add a jar file to your project, normally you download the jar manually and then drop it into the lib directory. With maven, you don't do it this way. Here's what you do:
Go to mvnrepository
Search for the library you want to add
Copy the dependency statement into your pom.xml
rebuild via mvn
Now, maven will connect and download the jar along with the list of dependencies, and automatically resolve any additional dependencies that jar may have had. So if the jar also needed commons-logging, that will be downloaded as well.
I'd do this:
add the dependency as you like in your pom:
<dependency>
<groupId>com.stackoverflow...</groupId>
<artifactId>artifactId...</artifactId>
<version>1.0</version>
</dependency>
run mvn install it will try to download the jar and fail. On the process, it
will give you the complete command of installing the jar with the error message. Copy that command and run it! easy huh?!
I'll assume that you're asking how to push a dependency out to a "well-known repository," and not simply asking how to update your POM.
If yes, then this is what you want to read.
And for anyone looking to set up an internal repository server, look here (half of the problem with using Maven 2 is finding the docs)
Related
This might be a naive question, so pardon me but I am new to maven.
I want to build a jar of project present on github. The project has a pom.xml file and additionally mentions in
Installation Note:
Releases are distributed on Maven central:
<dependency>
<groupId>some_grp_id</groupId>
<artifactId>all</artifactId>
<version>1.1.2</version>
<type>pom</type>
</dependency>
Am I suppose to add above in the existing pom.xml file ??
I an cloning the project on my desktop and then executing "mvn package".
If you want to simply use the library then you only have to add the above dependency to your pom.xml. Maven is doing all the rest for you (downloading the .jar-file).
If you really want to use the source code you have to do a
mvn install
on the checked out code. And also have to include the dependency in your own pom.xml.
I have built a non-maven project to display crystal reports with all necessary jars imported to the lib folder.
But, when I try to find the dependencies to add the same jars into my pom.xml for a maven project, I was not able to look it up online.
How can I find the crystal report dependencies for a maven project ? How to determine groupId and artifact for the crystal jar?
Below is the snapshot of jars I am looking for. Please suggest.
I tried looking into this link in the SCN but couldn't find much details.
You can specify your local jars as maven dependency using "system" scope
<dependency>
<groupId>group.id</groupId>
<artifactId>artifact</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${lib.path}/some_1.0.jar</systemPath>
</dependency>
define 'lib.path' in properties
Below are two solutions : One is pointing to local jars with system scope and the other is deploying the jars into m2. I felt the second solution more reliable.
First solution: I followed the link here ; but couldn't get it entirey working. So changed the scope to system and pointed to the local jars and was able to finally access the classes.
Please see below for the pom.xml
<dependency>
<groupId>com.sap.crystalReports</groupId>
<artifactId>CrystalCommon2</artifactId>
<version>1.0.0-RT</version>
<scope>system</scope>
<systemPath>${lib.path}/CrystalCommon2.jar</systemPath>
</dependency>
.....
Second solution is, from the comment in the question above, I came up with the idea of deploying all the crystal jars, one by one, which I downloaded on my local into my local m2 repository.
mvn deploy:deploy-file -DgroupId=com.crystalruntime.sdk -DartifactId=JDBInterface
-Dversion=1.5 -Dfile= point-to-the-jar-location-on your-local\JDBInterface.jar
-Dpackaging=jar -Durl=file://point-to-local-m2 repository
groupId, artifactId,version- can be modified, but it is good to keep consistency and readability, with the names chosen.
Run this command on any/all the jars that you want to install into m2. I then updated the project and did another clean build and was able to successfully run then application.
Also, I did remove the system scope from the pom.xml
<scope>system</scope>
<systemPath>${lib.path}/JDBInterface.jar</systemPath>
and modified it according to :
<dependency>
<groupId>com.crystalruntime.sdk</groupId>
<artifactId>JDBInterface</artifactId>
<version>1.5</version>
</dependency>
This made more sense to me, to use in the maven project.
This is the link I followed for reference.
There is a website with a jar I want to use in my project. I could download it, and make a local dependency on it, but that is lame.
Other people will build this project. I don't want to clutter source control by checking it in. I want to comunicate where the resource can be downloaded from and to easily change the versions. At some point I would like to attach the sources and javadocs.
Is there a plugin that will let me use the URL and download it to my .m2 with a spoofed pom?
The way I do it, is to search for the jars in Maven's central repo:
http://search.maven.org/
This gives you enough data to build the dependency in your pom.xml file
Example:
If I wanted to add jSoup to my project as dependency, then I go and search in the central repo
and add the dependency to the pom file with the info that's in there:
http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.jsoup%22%20AND%20a%3A%22jsoup%22
<dependencies>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.7.3</version>
<scope>test</scope>
</dependency>
</dependencies>
I think maven2 repo is included by default when creating the super pom, so you don't have to write it, it will look there by default.
I think the best solution is to set up a Nexus or Artifactory repo for the team available over the network. Download the jar from the third-party location, and then upload it with the proper pom GAV values to your new local repo. Then add the URL of this repo to the repositories section of the pom. Everyone will get it when they sync up with version control.
maven-install-plugin can install files in local repo and generate poms, usage example:
mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=10.2.0.3.0 -Dpackaging=jar -Dfile=ojdbc6_g.jar -DgeneratePom=true
There is a jar file lets say "abc.jar" which maven dependency does not exist(ie created a jar by using java command of own classes). I want to add this jar as maven dependency so that at build time it will automatically copy that jar in lib folder as like other maven dependency. how i will do. please help .
Add it as a dependency with a system scope. See the docs here.
However, rather than adding it as a system dependency it might be better to mavenize the jar itself, then you can build and install it into your dependency management system.
Also, see this question: Can I add jars to maven 2 build classpath without installing them?
You can use the systemPath attribute in the dependency tag in the POM file of your project.
In your pom.xml, use the following snippet corresponding to abc.jar:
<dependencies>
<!-- Other dependencies -->
<dependency>
<groupId>abc</groupId>
<artifactId>x</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>{path_to_abc.jar}</systemPath>
</dependency>
</dependencies>
The scope parameter corresponding to this artifact must be set to system, for the artifact to be picked up from the specified systemPath.
Hope this helps!
A normal maven dependency is always resolved by looking into a repository. So you must put your JAR file into a repository.
You could install your JAR into your local repository. Have a look at the install plugin. The install-file goal is your friend.
If other developers also need this JAR (because they are working with the same project), they either need to install it locally too, or - better - you deploy the JAR to a remote repository. Have a look at the deploy plugin. Here the deploy-file goal is your friend. For deploying artifacts, you need a repository manager like Nexus or Artifactory.
However, a dependency could also have the system scope (look at the other answers).
I have some local jar files from a non-maven project which I wish to include in my maven-based eclipse project.
These jar files are undergoing a lot of change as me and my project buddy attempt to 'fix' them, so I'd prefer not to upload them to a repository to avoid making a maven version of this non-maven project if this is possible.
Of course, the jar files need to be embedded in the resulting deployment jar. We did this before using Ant which let us specify that those jar files should be included.
How do you do the same thing in maven? Take into consideration that we do have maven dependencies too which all work fine and aren't required in the deployment. Some answers I've seen don't allow for this requirement.
Here's one of my attempts - the problem is that the jar does not get embedded:
<dependency>
<groupId>se.krka.kahlua</groupId>
<artifactId>kahlua-core</artifactId>
<version>5.1_2.1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/kahlua-5.1_2.1.0-core.jar</systemPath>
</dependency>
System paths are a very bad idea. When anybody else checks out your projects, he cannot build it anymore. (I always see such crap in many companies). The right solution would be to install the jar into the local repository:
$ mvn install:install-file -Dfile=[JAR NAME] -DgroupId=[GROUPID OF
JAR] -DartifactId=[ARTIFACT OF JAR] -Dversion=[VERSION OF JAR]
-Dpackaging=jar
In your project, you just add the dependency as usual after you installed the jar into the local repository.
<dependency>
<groupId>[GROUPID OF JAR]</groupId>
<artifactId>[ARTIFACT OF JAR]</artifactId>
<version>[VERSION OF JAR]</version>
</dependency>
You can use maven-install-plugin to install kahlua-5.1_2.1.0-core.jar into the local repository then this dependency will behave as any other, see http://maven.apache.org/plugins/maven-install-plugin/usage.html. Or make a remote repository in a location shared with your buddy and let him upload his jar there with maven-deploy-plugin:deploy-file (http://maven.apache.org/guides/mini/guide-3rd-party-jars-remote.html) each time he changes it and add this repository to your pom. You can use SNAPSHOT version if this jar changes often