Java 如何在 Eclipse 中使用 Maven 创建 Spring Web 项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3078438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I create Spring web project with Maven in Eclipse?
提问by newbie
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?
我尝试通过从 Eclipse 创建新的 maven 项目中选择 Archtype Web Application 来创建项目。但是当我创建一个新的 java 类来投影源文件夹时,它在包中不包含包名称,源文件夹包含在类路径中。那么我的设置是否有问题,或者为什么 Eclipse 不会自动将包添加到新创建的类?
采纳答案by sourcerebels
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:
- 使用 m2eclipse 插件进行 Maven 集成和 m2eclipse WTP 集成(来自 m2eclipse extras 更新站点)。
- 使用原型创建新的 maven 项目:maven-archetype-webapp。
- 创建 src/main/java 文件夹并手动添加到构建路径。
- 添加 spring 和 spring-webmvc 依赖项。对于 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>
<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>
回答by VonC
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
”包?
回答by Stephen C
The SpringSource guys have developed an Eclipse-based Spring Tool Suitefor 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.
SpringSource 开发人员开发了一个基于 Eclipse 的Spring 工具套件来开发 Spring 项目。我从未使用过 STS ……但如果您正在开始一个新项目,这可能是尝试一下的好时机。
回答by Pascal Thivent
The easiest way would to use Spring Tool Suiteas 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 templatesfor Spring projects (via New > Project...). This gives you a fully configured project.
最简单的方法是使用@Stephen 建议的Spring Tool Suite,它是用于 Spring 开发的 Eclipse 的捆绑版本。它打包了 Spring IDE、M2Eclipse 等插件,并为 Spring 项目提供了自定义项目模板(通过New > 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 依赖项(所以不,您的设置没有什么特别的错误)。
回答by Rob
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:
在 Spring Tool Suite (STS) 中你可以试试这个。创建一个 Maven 项目。您可以将 pom.xml 依赖项添加到您可能需要的任何 Spring 库中。例如:
<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.
项目创建完成后,在Package Explorer中右击,选择Spring Tools -> Add Spring Project Nature。瞧,你应该有一个 Maven/Spring 项目。