简体   繁体   English

如何在Eclipse中使用Maven创建Spring Web项目?

[英]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. 我试图通过从Eclipse创建新的maven项目中选择Archtype Web Application来制作项目。 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. 但是当我创建一个新的java类到项目源文件夹时,它在包中没有包含packacge名称,并且源文件夹包含在类路径中。 So is there something wrong with my setup or why doesn't Eclipse add automatically package to newly created Class? 那么我的设置有什么问题,或者为什么Eclipse不会自动将包添加到新创建的类中?

This is what I do: 这就是我做的:

  • Using m2eclipse plugin for Maven integration and m2eclipse WTP integration (from m2eclipse extras update site). 使用m2eclipse插件进行Maven集成和m2eclipse WTP集成(来自m2eclipse extras更新站点)。
  • Create new maven project using archetype: maven-archetype-webapp. 使用原型创建新的maven项目:maven-archetype-webapp。
  • Create src/main/java folder and add to build path manually. 创建src / main / java文件夹并手动添加到构建路径。
  • Add spring and spring-webmvc dependencies. 添加spring和spring-webmvc依赖项。 For 2.5.6.SEC01 version use: 对于2.5.6.SEC01版本使用:
 <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. 最简单的方法是使用@Stephen建议的Spring Tool Suite ,它是Eclipse开发的Eclipse捆绑版本。 It packages plugins such as Spring IDE, M2Eclipse, etc and offers custom Project templates for Spring projects (via New > Project... ). 它打包了诸如Spring IDE,M2Eclipse等插件,并为Spring项目提供了自定义项目模板 (通过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). Maven webapp原型允许获得类似的结果但涉及更多的手动步骤:你必须添加src/main/java ,创建基础包并手动添加Spring依赖项(所以不,你的设置没有什么特别的错误) 。

In Spring Tool Suite (STS) you can try this. 在Spring Tool Suite(STS)中,您可以尝试这一点。 Create a Maven project. 创建一个Maven项目。 You can add pom.xml dependencies to whatever Spring libs you may need. 您可以将pom.xml依赖项添加到您可能需要的任何Spring库中。 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. 创建项目后,在Package Explorer中右键单击它,然后选择Spring Tools - > Add Spring Project Nature。 Et voila, you should have a Maven/Spring project. 瞧,你应该有一个Maven / Spring项目。

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 . 然后它会要求您输入groupId的值,artifactId以及要创建的项目的版本以及基本包

[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? 源文件夹是否包含空的“ com.company.project ”包?

The SpringSource guys have developed an Eclipse-based Spring Tool Suite for developing Spring projects. SpringSource人员开发了一个基于Eclipse的Spring Tool Suite来开发Spring项目。 I've never used STS ... but if you are starting a new project, this might be a good time to give it a spin. 我从来没有使用STS ......但如果你要开始一个新项目,这可能是一个让它旋转的好时机。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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