19

I am new to Maven. If I start new project with Maven, should I know any repository URLs for it to work?

For example, this Hibernate tutorial http://docs.jboss.org/hibernate/core/3.3/reference/en/html/tutorial.html says about how to create a sample project with pom.xml text. But this pom.xml does not contain any repositories.

So, my m2eclipse plugin says, for example Project build error: 'dependencies.dependency.version' for org.hibernate:hibernate-core:jar is missing., for all dependency tag in pom.xml

Is this because of repositories absence?

Where to know repositories URLs? Is there one big repository? Why doesn't it included by default?

UPDATE 1 It is said here, that Maven should use "central" repository by default: http://maven.apache.org/guides/introduction/introduction-to-repositories.html

I have searched there for hibernate-code artifact and found it. So, this artifact IS in central repository. By my maven says dependency not found. Hence it doesn't use it's central repository. Why?

Dims
  • 47,675
  • 117
  • 331
  • 600
  • I answered before your update, sorry. Your Hibernate dependency is [here](http://repo1.maven.org/maven2/org/hibernate/hibernate-core/). Do you have any other parent `pom.xml` defined and do you define some other repositories manually? – Tomasz Nurkiewicz Jan 19 '12 at 20:06
  • I don't know :) I just created very first Maven project in Eclipse and copied hibernate's pom.xml there. How to look for parent poms? – Dims Jan 19 '12 at 20:08
  • Can you strip the `pom.xml` into the smallest possible part that still works and exposes the problem? As little as possible, sources aren't important, just `pom.xml`. – Tomasz Nurkiewicz Jan 19 '12 at 20:11
  • If I remove all dependencies, Eclipse stops displaying errors. But how I can use Maven without a dependencies? – Dims Jan 19 '12 at 20:18
  • Something just stroked me: do you have `3.6.9.Final` (or whatever version you use) declared inside `` of Hibernate? – Tomasz Nurkiewicz Jan 19 '12 at 20:21
  • Ah, you are right, there is no version stamp there. Is this an error? – Dims Jan 19 '12 at 20:34
  • Yes, if you are not using `` then version is a must. If this was the problem let me know, I will edit my answer to contain the actual solution. – Tomasz Nurkiewicz Jan 19 '12 at 20:47
  • Looks like this was a problem. But I don't know which versions to put in the sample. So, the actual solution is probably related with your `` information. – Dims Jan 19 '12 at 20:50

2 Answers2

18

Apparently your Hibernate dependency is missing <version> tag:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>3.6.9.Final</version> <!-- this line is missing -->
</dependency>

Note that you don't have to specify version of dependencies previously declared in <dependencyManagement>.

Old answer:

Every build script (not only with Maven) should be reproducible and independent from environment. Standard pom.xml (called super pom), which every pom.xml inherits from, already defines main Maven central repository:

<repositories>
  <repository>
    <id>central</id>
    <name>Maven Repository Switchboard</name>
    <layout>default</layout>
    <url>https://repo1.maven.org/maven2</url>
    <snapshots>
      <enabled>false</enabled>
    </snapshots>
  </repository>
</repositories>

You don't have to define this repository, and you don't have to define any others if all your dependencies are there. On the other hand if you are using some external repositories, you must add them to pom.xml, so that every developer is always able to build.

The bottom line is: if you can build the project having a completely empty repository, your pom.xml is fine.

Hunsu
  • 3,281
  • 7
  • 29
  • 64
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
5

It's not advisable to define repositories in POM files as that causes a lot of issues (Maven will search those repositories for ANY artifact even the ones available at Central, poor portability, ...)

Best approach: Setup a repository manager (Artifactory, Nexus) and edit your settings.xml file to use the repo manager as a mirror.

Second best approach: Define the required repositories in your settings.xml file, not in your pom.xml files.

Repositories in poms is a bad idea.

Agustí Sánchez
  • 10,455
  • 2
  • 34
  • 25
  • 3
    These people disagree with @Augusti's answer about repository in POM: https://stackoverflow.com/questions/2225535/where-is-the-best-place-to-specify-maven-repositories-pom-xml-or-settings-xml – chrisinmtown Dec 26 '17 at 16:26
  • 4
    Putting non-standard repositories in the `settings.xml` file means every new developer must also edit that file. Putting non-standard repositories in the `pom.xml` file (which gets checked into source control) means every developer can build. But YES, your authentication for a repository server should be in your _private_ `settings.xml` file. – Jesse Chisholm Aug 29 '18 at 22:36