help for use maven artifacts

吃可爱长大的小学妹 提交于 2020-01-05 05:09:33

问题


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?


回答1:


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</url>
    </repository>
  </repositories>
  ...
</project>



回答2:


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




回答3:


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>


来源:https://stackoverflow.com/questions/3700817/help-for-use-maven-artifacts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!