我的目标是使用 maven 为 Eclipse 创建一个 Web 应用程序项目,然后可以将其导入 Eclipse。我很确定有一种超级简单的方法可以做到这一点,我想知道它是什么。
使用 maven 原型插件生成您的项目。maven-archetype-webapp
这是从命令行调用它时如何告诉它使用的方法:
mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp
但是当我将项目导入 eclipse 时,我看不到 Run As > Run on server 选项。
这实际上完全取决于您用于 Eclipse/Maven 集成的内容。基本上有两种选择(它们都提供 WTP 集成):
- 这
maven-eclipse-plugin
是一个Maven 插件,可以生成 Eclipse 文件(.project
等等.classpath
),允许将项目作为现有项目导入 Workspace。
- m2eclipse插件,它是一个Eclipse 插件,在Eclipse中提供 Maven 集成,并允许将 Maven 项目作为现有 Maven 项目导入。
maven-eclipse-plugin 方法
If you use the maven-eclipse-plugin
, you have to configure it for WTP support and here is a typical configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.8</version>
<configuration>
<projectNameTemplate>[artifactId]-[version]</projectNameTemplate>
<wtpmanifest>true</wtpmanifest>
<wtpapplicationxml>true</wtpapplicationxml>
<wtpversion>2.0</wtpversion>
<manifest>${basedir}/src/main/resources/META-INF/MANIFEST.MF</manifest>
</configuration>
</plugin>
With this configuration, running mvn eclipse:eclipse
on your maven project will generate the WTP files so that the project can be recognized as a Dynamic project (i.e. runnable on a Server). Then import it via Import... > Existing projects into Workspace.
The m2eclipse approach
If you use the m2eclipse plugin (and that would be my recommendation), make sure to install the Maven Integration for WTP from the extras. From the install instructions:
Installing m2eclipse Extras
To install optional m2eclipse
components, you will need to use the
m2eclipse Extras update site. This
update site contains the following
m2eclipse components:
- Maven SCM Integration
- Maven SCM handler for Team/CVS
- Maven SCM handler for Subclipse
- Maven issue tracking configurator for Mylyn 3.x
- Maven Integration for WTP
- M2Eclipse Extensions Development Support
m2eclipse Extras Update Site: http://m2eclipse.sonatype.org/sites/m2e-extras
And then just import your project via Import... > Existing Maven projects and if it's a webapp, it should get recognized as a Dynamic project.
Indigo: m2eclipse approach for Indigo is different. See Maven/Tomcat Projects In Eclipse Indigo/3.7
Important: Note that both approaches are exclusive, use one or the other. But in both cases, there is no need to add a facet manually if you use them correctly.