What type of new project for the Hibernate tutorial?

I am trying to follow this Hibernate tutorial:

http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/tutorial.html

which seems pretty easy & straightforward, but I got stuck on the very 1st decision point: When selecting File > New Project to use the tutorial’s suggested pom.xml, which of the many type of projects should I select?

Intuitively, I thought of creating a new Maven project:

  1. Maven > Maven Project [Next]
  2. [v] Create a simple project (skip archtype selection) [Next]

But at step #3 it prompts me for the very same information provided by the tutorial’s pom.xml. So I am thinking the tutorial was aiming at creating a “blank project” and then manually adding the suggested pom.xml to it?

But what type of “blank project” would that be?

  • Java?
  • Java EE?
  • EJB?
  • JAXB?
  • JPA?
  • Maven?
  • Does it matter?

(sorry, don’t have much experience (yet) with this new jargon, will get there eventually)

UPDATE 1: Actually, the proposed method of selecting a Maven project (regardless whether starting with bogus or correct info) doesn’t work:

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[ERROR] FATAL ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Error building POM (may not be this project's POM).


Project ID: org.hibernate.tutorials:hibernate-tutorial
POM Location: C:/Users/Daniel/Workspace/First Hibernate Tutorial/pom.xml
Validation Messages:

    [0]  'dependencies.dependency.version' is missing for org.hibernate:hibernate-core:jar
    [1]  'dependencies.dependency.version' is missing for javax.servlet:servlet-api:jar
    [2]  'dependencies.dependency.version' is missing for org.slf4j:slf4j-simple:jar
    [3]  'dependencies.dependency.version' is missing for javassist:javassist:jar


Reason: Failed to validate POM for project org.hibernate.tutorials:hibernate-tutorial at C:/Users/Daniel/Workspace/First Hibernate Tutorial/pom.xml

I am going to check now whether creating a plain Java project, then converting it to a Maven (after copying that tutorial’s pom.xml) will do the trick.

UPDATE 2: Creating a plain Java project, then converting it to a Maven (after copying that tutorial’s pom.xml) produces the same errors.

What am I missing?

UPDATE 3: Thanks to @cowls tip below I found that “I” was missing were the version numbers for the Maven dependencies. “I” because it’s the tutorial that was missing them (what kind of a tutorial is this?)

Once I used some known version numbers, e.g.:

<dependencies>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
         <version>3.5.6-Final</version>
     </dependency>

    <!-- Because this is a web app, we also have a dependency on the servlet api. -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>1.2_02</version>
    </dependency>

    <!-- Hibernate uses slf4j for logging, for our purposes here use the simple backend -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.5.10</version>
    </dependency>

    <!-- Hibernate gives you a choice of bytecode providers between cglib and javassist -->
    <dependency>
        <groupId>javassist</groupId>
        <artifactId>javassist</artifactId>
        <version>3.8.0.GA</version>
    </dependency>
</dependencies>

I have been able to get rid of that scary FATAL ERROR message. That error message’s “tip” BTW was counter-productive as it was pointing to the wrong direction:

[INFO] Error building POM (may not be this project's POM).

Now… I am still getting a build error, albeit much less scary:

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building First Hibernate Tutorial
[INFO]    task-segment: [install]
[INFO] ------------------------------------------------------------------------
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:/Users/dben/ws-study/First Hibernate Tutorial/src/main/resources
Downloading: http://repo1.maven.org/maven2/javax/servlet/servlet-api/1.2_02/servlet-api-1.2_02.pom
[INFO] Unable to find resource 'javax.servlet:servlet-api:pom:1.2_02' in repository central (http://repo1.maven.org/maven2)
Downloading: http://repo1.maven.org/maven2/javax/servlet/servlet-api/1.2_02/servlet-api-1.2_02.jar
[INFO] Unable to find resource 'javax.servlet:servlet-api:jar:1.2_02' in repository central (http://repo1.maven.org/maven2)
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Failed to resolve artifact.

Missing:
----------
1) javax.servlet:servlet-api:jar:1.2_02

  Try downloading the file manually from the project website.

  Then, install it using the command: 
      mvn install:install-file -DgroupId=javax.servlet -DartifactId=servlet-api -Dversion=1.2_02 -Dpackaging=jar -Dfile=/path/to/file

  Alternatively, if you host your own repository you can deploy the file there: 
      mvn deploy:deploy-file -DgroupId=javax.servlet -DartifactId=servlet-api -Dversion=1.2_02 -Dpackaging=jar -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id]

  Path to dependency: 
    1) org.hibernate.tutorials:hibernate-tutorial:jar:1.0.0-SNAPSHOT
    2) javax.servlet:servlet-api:jar:1.2_02

----------
1 required artifact is missing.

for artifact: 
  org.hibernate.tutorials:hibernate-tutorial:jar:1.0.0-SNAPSHOT

from the specified remote repositories:
  central (http://repo1.maven.org/maven2)

Interesting tutorial: I was expecting to learn some Hibernate but instead I am finding myself learning Maven. 🙂

UPDATE 4: I corrected the javax.servlet version to 2.4 and all is well now.

LESSONS LEARNED:

  1. Maven’s pom.xml requires a <version> for each & every dependency.
  2. That <version>’s value must be valid.
  3. Creating a new Maven project, then replacing its pom.xml entirely would work, but it still creates it with the incorrect subdirectory under Workspace because there is no way to define the <name> in the wizard-based creation process.
  4. Creating a plain Java Project, then adding the pom.xml to its directory, then refreshing the Package Explorer view is the recommended way to go (no subdirectory renaming necessary).
  5. That tutorial recommends Maven’s “standard layout” but then breaks it by using the declared package “org.hibernate.tutorial.domain” which does not match the expected package “main.java.org.hibernate.tutorial.domain”… Do take Eclipse’s fix suggestion to rename the package to “main.java.org.hibernate.tutorial.domain”. (and move Event.java to the newly renamed package!)
  6. The Hibernate configuration command (mvn exec:java -Dexec.mainClass=”org.hsqldb.Server” etc.) must be run from the directory where pom.xml resides.

Answers

Easiest is to create a new maven project and use the maven-archetype-quickstart archetype.

This will create a basic maven project and you should be able to go from there.

Note that a maven project is just a Java project that uses Maven to build it, hence you will see a pom.xml file in your project.

Using this pom file you can build the project, include external dependencies and much more. I would suggest using the m2e plugin if you are developing with eclipse to help you manage the maven projects better: http://eclipse.org/m2e/

Each dependency you specify should have a version number associated with it.

e.g.

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.5.6-Final</version>
    </dependency>   
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>

To get the correct version number, just google artifactid maven dependency. E.g. hibernate-core maven dependency.

Look for the link for the maven central repository – i.e. http://mvnrepository.com. If you cant find it on Google you can search directly on the site, however I find its search feature quite poor.

Put your Java code in src/main/java and your resources in src/main/resources (e.g. xml files etc).

You can then build the project using mvn clean install