0

Suppose I have created an empty Maven project in Eclipse. What is the shortest way to observe Maven's ability of automatic jar downloading and dependency checking? For example, I need to use Swing or log4j library. How can I add them without thinking where to download?

Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385
  • Add them as dependencies in your pom....and run a build ...Maven will handle that for you... – khmarbaise Jan 28 '12 at 16:52
  • No need to run a build if you have m2e installed: it will fetch things for you automatically. – Martin Ellis Jan 28 '12 at 16:55
  • You don't have to think about where to download, just add the dependency to pom.xml. Most of the time, this is all you need, unless you have some oddball package that's not in the default repository. (There might be some additional steps to make Eclipse work with Maven - if so, I don't know what they are.) – Mike Baranczak Jan 28 '12 at 17:22
  • This does not work. Eclipse says many artefacts missing, like `Missing artifact javax.jms:jms:jar:1.1`. Resolving these errors is more complex than if I would download jars myself, without maven. – Suzan Cioc Jan 28 '12 at 19:37

2 Answers2

2

Add a dependency, for example, Log4J 1.2.16; it will be downloaded automatically, along with its dependencies. Then check in your home directory's .m2/repository directory.

(You'll find mvnrepository.com helpful; bookmark it!)

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • 1
    @SuzanCioc That POM file isn't going to write itself ;) Yes, top-level dependencies need to be added manually (in general). – Dave Newton Jan 28 '12 at 17:26
  • @SuzanCioc If you have m2e installed, there's an 'Add…'button on the dependencies tab of the pom.xml, which will give you a dialog for adding/searching artifacts. I've never known anyone use it though. Copying the XML for the dependency from search.maven.org or mvnrepository.com into the pom would work too. – Martin Ellis Jan 28 '12 at 17:35
  • I believe you are right, but this does not work for me. After adding dependency, many errors occur. – Suzan Cioc Jan 28 '12 at 19:35
  • @SuzanCioc "Many errors" isn't enough to diagnose the problem, but start by doing a clean/rebuild. – Dave Newton Jan 28 '12 at 19:38
  • @Dave Newton Nothing to rebuild, the project is empty, only log4j added as I was told to. The detailed problem is here: http://stackoverflow.com/questions/9047949/missing-artifact-com-sun-jdmkjmxtoolsjar1-2-1 – Suzan Cioc Jan 28 '12 at 19:42
-1

Add this at the end of your pom :

    <repository>
        <id>jboss-nexus</id>
        <url>https://repository.jboss.org/nexus/</url>
    </repository>

Then go to this location and look for the artifacts you need. Once you've found them add them to your project dependencies (the site even shows the complete dependency). Run mvn install and then mvn eclipse:eclipse and voila, all done...

  • I'm not sure why you'd suggest the jboss nexus instance. The question explicitly says "without thinking where to download". The JBoss Nexus instance doesn't have nearly the same replication as the default (central) repository, and 'central' is load balanced to pick a repository near the user. Also `mvn eclipse:eclipse` is not the way to get the best Maven integration - the 'supremacy' :) - in Eclipse. – Martin Ellis Jan 28 '12 at 17:07
  • By adding a repository to your pom you are not really "thinking" where you are downloading. Try to add "gnu-getopt" from the "SUPREME" repository. – Alex Calugarescu Jan 28 '12 at 17:17
  • @AlexCalugarescu You're thinking enough to know to add a repository, which isn't really what I'd tell a first-time user to do--IMO keep it simple until they run in to a dependency they can't resolve. – Dave Newton Jan 28 '12 at 17:26