views:

2727

answers:

5

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.

+1  A: 

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)

kdgregory
I would like to maintain a dependancy myself. This is an in-house library.
Milhous
+10  A: 

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>
Jack Leow
I'll give that a try.
Milhous
Can you add more detail regarding the deploy? We have an internal repository, and I want to deploy my local jar installed file to that repository so other developers and our build server will get the jar as well.
Kieveli
+3  A: 

I'd do this:

  1. add the dependency as you like in your pom:

    <dependency>
            <groupId>com.stackoverflow...</groupId>
            <artifactId>artifactId...</artifactId>
            <version>1.0</version>
    </dependency>
    

  2. run maven -- 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?!

A: 

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.

When you want to add a jar file to your project, normally you download the jar, then drop it into the lib directory. With maven, you don't do it this way. Here's what you do:

  • Goto mvnrespoitory
  • Search for the library you want to add
  • Copy in the 'dependancy' statement into your pom.xml
  • rebuild via mvn

Now, maven will connect and download the jar along with the list of dependancies, and automatically resolve any additional dependancies that jar may have had. So if the jar also needed commons-logging, that will be downloaded as well.

Kieveli
+5  A: 

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>
Frederic Morin