eclipse使用maven创建web3.0项目

挺别扭的,eclipse和maven发展了这么久,二者都没有很好的解决这个问题。

默认情况下,使用maven骨架撞见的webapp只支持servlet2.3,eclipse又不允许随便修改为3.0。

网上流传最多的版本可能是修改项目目录中.settings/org.eclipse.wst.common.project.facet.core.xml文件,然后还要注意一堆东西。

我梳理了一种办法,感觉处理起来相对合理,如下:

1.创建普通的webapp项目

点击菜单“File - New - Other - Maven - Maven Project”;

Next;

Next;

Catalog 选择"Internal",选中最后一个骨架"maven-archetype-webapp","Next";

输入你自己的groupId,artifactId,"Finished"。

2.添加maven插件

右键点击项目名,选择“Maven - Add Plugin”;

在搜索框输入"maven-compiler-plugin",选中"org.apache.maven.plugins"开头的插件,"OK";

在搜索框输入"maven-eclipse-plugin",选中"org.apache.maven.plugins"开头的插件,"OK";

此时插件只是插入了基本模板:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.6.0</version>
</plugin>
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-eclipse-plugin</artifactId>
  <version>2.10</version>
</plugin>

3.调整依赖

非必须,只是建议。

双击打开pom.xml,并切换到"Dependencies"选项卡;

建议:选中junit 3.8.1,点击"Properties",版本改为"4.12";

点击"Add",在搜索框输入javax.servlet-api,选择"javax.servlet"开头的依赖,并调整版本为"3.1.0",scope改为"provided","OK";

以上基本上依靠eclipse就可以实现,不需要直接修改pom。这里只是列出xml片段:

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>3.1.0</version>
  <scope>provided</scope>
</dependency>

4.调整第二步中的插件配置

调整compiler插件

在`</plugin>`上面插入一个空行;

按"Alt + /",选择"configuration"插入节点;

在"configuration"节点内重复上一步,插入encoding=utf8,source=1.8,target=1.8;

提示:如果你能记住这几个单词,而且又安装了emmet的话,不放输入"configuration>encoding{UTF-8}+source{1.8}+target{1.8}"按下Tab看看。

调整eclipse插件

同上,增加"configuration"及下面的节点wtpversion=2.0,jeeversion=6.0;

你还想知道emmet表达式?好吧,"configuration>wtpversion{2.0}+jeeversion{6.0}"

最终插件配置

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.6.0</version>
    <configuration>
      <encoding>UTF-8</encoding>
      <source>1.8</source>
      <target>1.8</target>
    </configuration>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-eclipse-plugin</artifactId>
    <version>2.10</version>
    <configuration>
      <wtpversion>2.0</wtpversion>
      <jeeversion>6.0</jeeversion>
    </configuration>
  </plugin>

5.重新生成eclipse项目构建

右键点击项目目录,选择"Run As - Maven build..."(注意选带...的);

在Goals中输入"eclipse:clean eclipse:eclipse",点"Run";

6.重建web.xml

删除src/main/webapp/WEB-INF/web.xml;

右键点击项目目录,选择"Jave EE Tools - Generate Deployment Descriptor Stub";

7.恢复现场

第6步执行完后,你可能会发现,右键点击项目名,"Maven"选项没有了。别急,恢复一下现场就可以了。

右键点击项目名,依次选择"Configure - Convert to Maven Project"。

右键点击项目名,选择"Maven - Update Project..."。

8.不太好的消息

maven3将maven-eclipse-plugin标记为了'Deprecated',说是交给eclipse来处理,但是eclipse根本没有处理好这个问题,蛇精病。

好在只是标记为'Deprecated',说不准多少年内还能用的。

转载于:https://my.oschina.net/polly/blog/881943

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值