Add a dependency in Maven
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?!
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 yourpom.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.
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>