开发者

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

开发者 https://www.devze.com 2023-01-04 14:44 出处:网络
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

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>
  • Develop a Spring MVC application step by step.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消