使用eclipse新建一个简单的maven web工程

因为每次新建工程都会忘(新建工程比较少),也没去研究下怎么自定义archetypes,写一份手顺备忘吧

jdk:1.7

maven:3.3.1

sts 3.6.4(eclipse集成spring版

首先新建工程 new-->Maven Project
使用eclipse新建一个简单的maven web工程
使用eclipse新建一个简单的maven web工程
在New Maven Project窗口中,选择框 :
□ Create a simple project(skip archetype selection) (创建一个简单项目(跳过原型选择))。如果选择这个将直接跳过maven的项目原型(模版)选择,建议不要勾选,使用内置的模版。
□ Use default workspace location (使用默认的工作区间),勾选后,建立的项目将放在默认工作区间,如不选则点击Browse(浏览),选择一个工作区间存放项目。
□ Add project(s) to working set (添加项目到工作集),选择则将新建的项目放入工作集中,这里的工作集概念就是项目归类,类似文件夹集合一样,方便区分,可选可不选。
新建好工程结构如下:
使用eclipse新建一个简单的maven web工程
新建好工程结构如下:
使用eclipse新建一个简单的maven web工程

因为项目选择的maven archetypes(原型、典型)过老,定义的jdk版本过低,所以需要更改jdk版本和project facets中Dynamic Web Module 的版本过老。如下图:
使用eclipse新建一个简单的maven web工程
我们可以看到
Dynamic Web Module 2.3 Requires the following facet:Java 1.3 or newer(动态网页模板2.3,要求jdk1.3或者更新的版本)
Dynamic Web Module 3.1 Requires the following facet:Java 1.7 or newer(动态网页模板3.1,要求jdk1.7或者更新的版本)
我们选择3.0版本已经够用了

先更改jdk

使用eclipse新建一个简单的maven web工程
使用eclipse新建一个简单的maven web工程
使用eclipse新建一个简单的maven web工程

使用eclipse新建一个简单的maven web工程
更改后结构如图:
使用eclipse新建一个简单的maven web工程

接下来改Dynamic Web Module
使用eclipse新建一个简单的maven web工程

进来发现project facets的jdk版本都还没变
使用eclipse新建一个简单的maven web工程

更改下:
使用eclipse新建一个简单的maven web工程

应用后更改dynamic web module 的版本
使用eclipse新建一个简单的maven web工程
会发现无法更改,个人认为是因为我们的web.xml文件配置已经生成需要更改,接下来按图示操作(因为之前dynamic web module已经在使用中无法配置更改web.xml)
1.去除dynamic web module的选中点击Apply
使用eclipse新建一个简单的maven web工程
2.选中更改版本后会出现 further configuration available 点击进去修改
使用eclipse新建一个简单的maven web工程

3.如图操作
使用eclipse新建一个简单的maven web工程
现在的文件结构图为
使用eclipse新建一个简单的maven web工程

web.xml的文件对比:
使用eclipse新建一个简单的maven web工程

删除后的文档目录结构
使用eclipse新建一个简单的maven web工程

另外需再次删除Deployment Assembly选项下的webContent目录的deploy设置,添加webapp目录deploy
使用eclipse新建一个简单的maven web工程

使用eclipse新建一个简单的maven web工程

使用eclipse新建一个简单的maven web工程

使用eclipse新建一个简单的maven web工程

最终:
使用eclipse新建一个简单的maven web工程

是不是感觉已经完成啦?在你不强制更新更新maven update project的情况下没什么问题,一旦你maven update projec(alt+F5)后,你会发现一切回到*。。。。
使用eclipse新建一个简单的maven web工程

一切工作似乎都白做了

使用eclipse新建一个简单的maven web工程

什么原因导致的呢?个人认为是万恶的模板(是不是卸磨杀驴了?),因为maven编译的时候还是依据着pom文件的配置,只是个人猜想,打开了pom文件头部的http://maven.apache.org/xsd/maven-4.0.0.xsd看到了一段说明:
<xs:element minOccurs="0" name="jdk" type="xs:string">
<xs:annotation>
<xs:documentation source="version">4.0.0+</xs:documentation>
<xs:documentation source="description">
Specifies that this profile will be activated when a matching JDK is detected. For example, <code>1.4</code> only activates on JDKs versioned 1.4, while <code>!1.4</code> matches any JDK that is not version 1.4. Ranges are supported too: <code>[1.5,)</code> activates when the JDK is 1.5 minimum.
</xs:documentation>
</xs:annotation>
</xs:element>
看太懂放弃了,个人认为是它捣的鬼,把我的jdk版本又弄回1.5了。
网上找到了一个方法,使用插件maven-compiler-plugin指定jdk版本
在pom的build标签里添加插件
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
使用eclipse新建一个简单的maven web工程

重新maven update projec(alt+F5)后,一切都是那么美好:
使用eclipse新建一个简单的maven web工程

现在开始愉快的开发吧
(ps:图片上传的我都快吐了,咱们能不能优化一下。。。。。。)