Eclipse 能否将 Maven Web 项目作为“动态 Web 应用程序”导入?
高级目标:创建一个可以在 Netbeans 和 Eclipse 中正确使用的单个 Maven Web 应用程序项目
将 Maven 项目转换为 Eclipse 时,项目类型似乎未设置为 Dynamic Web App使用命令时: mvn eclipse:eclipse -Dwtpversion=2.0
在 Eclipse 中,如果创建动态 Web 应用程序,项目资源管理器中会显示更多选项,例如: JAX-WS Web服务,部署描述符等是完全不同的框架结构。
另外,使用上述命令转换为 Eclipse 后,我无法“运行”它,即使转换的输出显示:[INFO]用作 WTP 服务器:Apache Tomcat v7.0。它不想作为 Web 服务运行,而是询问我这是 Java Servlet、Applet 还是单元测试。
使用动态 Web 应用程序(本机 Eclipse),运行项目时会显示以下内容:
也许我对 Eclipse 的要求太多了,要获得内置的 Web 应用程序功能,它不能来自马文?
还有另一种我可以尝试。我可以在 Eclipse 中创建一个动态 Web 应用程序,然后点击“转换为 Maven”。获取该 POM,进行修改,然后查看 netbeans 是否也会加载它。
我的“pom.xml”:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.voxel</groupId>
<artifactId>servermap</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>userprofile</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>com.sun.grizzly</groupId>
<artifactId>grizzly-servlet-webserver</artifactId>
<version>1.9.18-m</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2</url>
</repository>
</repositories>
High Level Goal: Create a single Maven Web Application project that can be used correctly in both Netbeans and Eclipse
When converting a Maven project to Eclipse, the project type doesn't seem to be set to Dynamic Web App when using the command: mvn eclipse:eclipse -Dwtpversion=2.0
In Eclipse, if creating a Dynamic Web Application, many more options are shown in the Project Explorer, such as: JAX-WS Web Services, Deployment Descriptor, etc. It's a completely different framework structure.
Also, after converting to Eclipse with the above command, I can't "Run" it even though the output from the conversion says: [INFO] Using as WTP server : Apache Tomcat v7.0. Instead of wanting to run as a web service, it asks me if this is Java Servlet, Applet, or Unit test.
With a Dynamic Web App (native Eclipse), this is what shows up when Running the project:
Maybe I am asking too much of Eclipse, and to get the built-in web app functionality, it can't come from Maven?
There is one other I could try. I could create a Dynamic Web App in Eclipse, then hit "Convert to Maven" . Take that POM, modify, then see if netbeans will also load it.
My 'pom.xml':
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.voxel</groupId>
<artifactId>servermap</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>userprofile</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>com.sun.grizzly</groupId>
<artifactId>grizzly-servlet-webserver</artifactId>
<version>1.9.18-m</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2</url>
</repository>
</repositories>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如 jhericks 所建议的,如果您使用
m2eclipse
,则不需要使用eclipse:eclipse goal
。eclipse:eclipse goal
所做的事情也可以由 m2eclipse 完成。要将项目转换为动态 Web 应用程序 ,您可以在项目属性中配置
Project Facets
选项As suggested by jhericks ,if you use
m2eclipse
, you don't need to useeclipse:eclipse goal
. What theeclipse:eclipse goal
does can also be done by the m2eclipse.To convert a project to the Dynamic Web Application , you can configure the
Project Facets
options in your project properties我使用 Sonatype 的 m2eclipse 插件 运气更好。当我开始时,我犯了一个错误,尝试同时使用 m2eclipse 和 eclipse:eclipse 目标。它们所做的事情并不完全相同,因此如果您安装了该插件,请停止执行 mvn eclipse:eclipse。不管怎样,它对我来说对于单个 Web 项目非常有效,对于多项目 Reactor 风格的项目也非常有效。
我遇到的唯一问题是尝试将一个 war 文件部署到在 eclipse 中配置的服务器(如果该 war 文件是使用覆盖层构建的),但这对您来说似乎不是问题。
I have had better luck with the m2eclipse plugin from Sonatype. When I started, I made the mistake of trying to use both m2eclipse and the eclipse:eclipse goal. They don't do exactly the same thing, so if you install the plugin, stop doing mvn eclipse:eclipse. Anyway, it has worked very well for me with single web projects and very well with multi-project reactor style projects as well.
The only problem I've had is with trying to deploy a war file to a server configured in eclipse if that war file is built with an overlay, but that doesn't seem to be an issue for you.
m2e-wtp 插件会自动执行此操作,如下所述:在 Eclipse 中导入现有 Maven 作为 WTP
导入后,所有方面、源文件夹等都将正确设置。
https://www.eclipse.org/m2e-wtp/
m2e-wtp plugin does this automatically, as stated here: Import Existing Maven in Eclipse as WTP
All facets, source folders etc will be correctly set after import.
https://www.eclipse.org/m2e-wtp/