简体   繁体   中英

How can I create Spring web project with Maven in Eclipse?

I tried to make project by selecting Archtype Web Application from Eclipse create new maven project. But when I created a new java Class to project sources folder, it didn't contain packacge name in package and source folder was included in class path. So is there something wrong with my setup or why doesn't Eclipse add automatically package to newly created Class?

This is what I do:

  • Using m2eclipse plugin for Maven integration and m2eclipse WTP integration (from m2eclipse extras update site).
  • Create new maven project using archetype: maven-archetype-webapp.
  • Create src/main/java folder and add to build path manually.
  • Add spring and spring-webmvc dependencies. For 2.5.6.SEC01 version use:
 <dependency> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> <version>2.5.6.SEC01</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>2.5.6.SEC01</version> </dependency> 

The easiest way would to use Spring Tool Suite as suggested by @Stephen which is a bundled version of Eclipse for Spring development. It packages plugins such as Spring IDE, M2Eclipse, etc and offers custom Project templates for Spring projects (via New > Project... ). This gives you a fully configured project.

The Maven webapp archetype allows to obtain a similar result but involves more manual steps: you'll have to add src/main/java , create the base package and add Spring dependencies manually (so no, there is nothing particularly wrong with you setup).

In Spring Tool Suite (STS) you can try this. Create a Maven project. You can add pom.xml dependencies to whatever Spring libs you may need. For example:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>3.0.5.RELEASE</version>
  <type>pom</type>
  <scope>compile</scope>
</dependency>

After the project is created, right click it in the Package Explorer and choose Spring Tools -> Add Spring Project Nature. Et voila, you should have a Maven/Spring project.

From the new project creation man page :

It then asks you to enter the values for the groupId, the artifactId and the version of the project to create and the base package for the sources .

[INFO] artifact org.apache.maven.archetypes:maven-archetype-quickstart: checking for updates from maven-archetype-quickstart-repo
Downloading: http://repo1.maven.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.jar
4K downloaded
Define value for groupId: : com.company 
Define value for artifactId: : project
Define value for version: : 1.0
Define value for package: : com.company.project
Confirm properties configuration:
groupId: com.company
artifactId: project
version: 1.0
package: com.company.project
 Y: : 

If the base package is set, a newly created class should be under that package.
Does the source folder contains an empty ' com.company.project ' package?

The SpringSource guys have developed an Eclipse-based Spring Tool Suite for developing Spring projects. I've never used STS ... but if you are starting a new project, this might be a good time to give it a spin.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM