Peter-TI Peter-TI - 1 year ago 98
Java Question

Maven add dependecies to project

In eclipse you can easily add dependecies (i.e. a JAR file) to you're project.
Right click on the project and click -> Build Path-> add libraries.

Now a hidden file is createn inside the project, ".classpath".
Inside this file is a classpathEntry added so now I can use the libraries by adding it in a java file:


This "application" can now be exported to a single jar.

How can I achieve this with Maven and without eclipse?
I switched to emacs... :)

With the command:

mvn archetype:generate -DartifactId=my-cli -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

I get myself a sample start application.
inside a src folder is a file writing "hello World" on a commandline.

with the following in the pom.xml I obtain myself a jar inside my project(maven indedependent deployment) succesfully:

<!--${project.basedir} ${} -->

Now how can I use this jar inside my
Do I have to create a .classpath file manually?
How can I arrange my classpathentries automatically?
I have been trying to create a manifest.MF with the classpassentries but with no succes. I have tried several tutorials.

I had no succes with:

/resources/repo/optional- new-name.jar</include>









after creating the manifest.MF manually.

after the command

mvn clean install

the manifest.MF remains empty.

Help would be appreciated.


Answer Source

+1 for not using eclipse ;^)

If you are looking to reference code from another JAR then try adding the following to your POM:


Where xxx, yyy and zzz are the maven coordinates for the JAR you want to import. You can get these from your Nexus

If you are looking to combine the contents of all your dependencies with your code and release them as a single JAR then you might want to take a look at the maven-shade-plugin. See How to package a jar and all dependencies within a new jar with maven

And one other thing, you should never play with the .classpath file. This is something Eclipse specific. Changing it won't effect Maven and will probably confuse Eclipse

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download