views:

78

answers:

3

I'm learning Maven and I'd need a little help to get started. I use the m2eclipse plugin (Maven) and I would like to generate a project like Struts 2, Hibernate 3, MySQL. For now I just create a simple project with the archetype: maven-archetype-webapp

What are the dependencies I need to add?

A: 

You just need to find struts or any dependencies and put them in your pom. Here is a bit about dependencies :

http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

c0mrade
just i put dependencies ..? For example when i put dependencies for Hibernate why it don't generate file in my project like hibernateconfig.xml ..?
Scandolous
A: 

The archetype in Maven are used to create a simple structure for a specific type of projects. Basically, it will create the pom.xml file, the structure of directories using the Maven conventions, and some simple files. Some archetype will create more complex structures, depending on which type of project they are related.

In your case, MySQL and Hibernate have no specific information in Maven, except the dependencies. So the best thing to do is to generate a web structure, eventually using the Struts Maven archetype (I never used it, so I can't tell if the quality of this archetype is good or not), and then add the adequate Hibernate / MySQL driver dependencies.

You can use the MvnRepository site to find the groupId, artifactId or version of a specific dependency.

For your concern, I suggest org.hibernate:hibernate:3.xx and the mysql:mysql-connector-java libraries (use the scope runtime for a JDBC driver):

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate</artifactId>
    <version>3.1.3</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.13</version>
    <scope>runtime</scope>
</dependency>
romaintaz
ok, i put this dependecy in my pom.xml, i don't understand about the JDBC Driver, I must add a JDBC driver dependencies.?
Scandolous
You add it in the list of dependencies, but using the `runtime` scope. This way, this dependency will *not* be used during compilation or unit test, but will be bundled in the final package (your WAR file).
romaintaz
+2  A: 

For now I just create a simple project with the archetype: maven-archetype-webapp

My suggestion would be to use the struts2-archetype-blank archetype instead to bootstrap your Struts 2 application. You can invoke it either from m2eclipse (via the wizards) or from the command line. For example from the command line:

mvn archetype:generate -B \
                       -DgroupId=tutorial \
                       -DartifactId=tutorial \
                       -DarchetypeGroupId=org.apache.struts \
                       -DarchetypeArtifactId=struts2-archetype-blank \
                       -DarchetypeVersion=2.2.1

The, add the required dependencies for Hibernate 3 and the MySQL JDBC driver. As often, there are several ways to do that:

  • manually (by adding <dependency> elements in the pom.xml)
  • using the m2eclipse wizards
    • via the dependencies tab of the pom editor
    • via a right-click on your project and then Maven > Add Dependencies
  • via the Eclipse quick-fix options

The Adding Dependencies Using m2eclipse blog post has a screen cast demonstrating some of them.

Whatever solution you'll choose, at the end, your pom.xml should at least declare the following deps:

<project>
  <dependencies>
    ...
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-core</artifactId>
      <version>2.2.1</version>
    </dependency>
    ...
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>3.3.2.GA</version>
    </dependency>
    <dependency>
      <groupId>javassist</groupId>
      <artifactId>javassist</artifactId>
      <version>3.9.0.GA</version>
    </dependency>
    ...
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.13</version>
    </dependency>
  </dependencies>

</project>

And If you want to use the latest version of Hibernate artifacts, you'll have to add the JBoss repository under the repositories element since they are not available in the maven central repository (sorry for making things more complicated but, well, that's how things are):

<project>
  <dependencies>
    ...
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>3.5.5-Final</version>
    </dependency>
    ...
  <dependencies>
  ...
  <repositories>
    <repository>
      <id>repository.jboss.org-public</id>
      <name>JBoss repository</name>
      <url>https://repository.jboss.org/nexus/content/groups/public&lt;/url&gt;
    </repository>
  </repositories>
  ...
</project>
Pascal Thivent
thx for your help, but when i want to chose struts2-archetype-blank in the wizzard it's not present
Scandolous
@Scandolous Then the artifact index is probably out of date. You can force a rebuild of the index from the repository view if I'm not wrong. Or run the command on the command line.
Pascal Thivent
why i have this error when i compile my project: Missing artifact javassist:javassist:jar:3.9.0.GA:compile
Scandolous
@Scandolous Ah, indeed, Maven central only has version 3.8.0.GA. Add the JBoss repository as suggested.
Pascal Thivent