ChatGPT解决这个技术问题 Extra ChatGPT

Add a dependency in Maven

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.


C
Community

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>

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.
This answer should get all the likes! I've used this to successfully add dependencies for my maven projects on both Google App Engine and Heroku.
F
Frederic Morin

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>

Can I depend on another pom file instead of a jar?
J
Jhonny D. Cano -Leftware-

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 am using Spring Tool Suite (built on Eclipse). After steps 1 & 2 in above solution: 1. Open pom.xml in STS editor, click "Dependencies" tab at bottom 2. Click "Add" in Dependencies group 3. Copy/paste "Maven" tab dependency info from browser window (groupId, artifactId, version) into corresponding fields in "Select Dependency" popup in STS 4. Click OK 5. Save pom.xml Once saved, my maven directory on hard drive and maven dependency setting in STS is updated within seconds. You can edit the .xml directly, but using the tool may help avoid xml format problems.
This is indeed the preferred way to do things, but there are situations where you can't do that (e.g. proprietary jar not on mvnrepository). In that case you'll need a way to add it "manually", in which case another answer would have the solution.
It's also possible that the question asked was less of a beginner question. I put this in for people looking to do something without the proper phrasing that comes from time spent working with maven. (As I was at the time)
K.I.S.S. Thank you, I read that this is the C# Nuget equivalent in Java but I was trying to figure out what button to click or cmd to run. Not exactly a 1:1 but does the job.
How to rebuild project in maven in intelleJ?
F
FabianCook

I'd do this:

add the dependency as you like in your pom: com.stackoverflow... artifactId... 1.0 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?!


k
kdgregory

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)


I would like to maintain a dependancy myself. This is an in-house library.