用STS和Maven的方式创建一个JavaWeb项目

一.创建项目

1.Eclipse中用Maven创建项目,选maven-archetype-webapp,如下图:

 

创建好项目后,目录如下:

至此,项目已经创建完毕,下边是配置。关键所在!!!

 

二.项目配置

1.添加Source Folder

Maven规定,必须创建以下几个Source Folder

src/main/resources

src/main/java(如果直接不能建,可以先建src/main/java1,然后在把java1改成java)

src/test/resources

src/test/java

添加以上的Source Folder

 

 创建好后的目录如下:

2.配置Build Path

 3.设定4个文件夹的输出Output folder,双击修改

 

分别修改输出路径为

src/main/resources   对应  target/classes
src/main/java      对应  target/classes
src/test/resources   对应  target/test-classes
src/test/java      对应  target/test-classes

5.设定Libraries

 或者设置maven的pom.xml文件

 1 <properties>
 2         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 3     </properties>
 4     <dependencies>
 5         <!-- Servlet -->
 6         <dependency>
 7             <groupId>javax</groupId>
 8             <artifactId>javaee-web-api</artifactId>
 9             <version>7.0</version>
10             <scope>compile</scope>
11             <optional>true</optional>
12         </dependency>
13         <dependency>
14             <groupId>commons-logging</groupId>
15             <artifactId>commons-logging</artifactId>
16             <version>1.2</version>
17         </dependency>
18         <dependency>
19             <groupId>log4j</groupId>
20             <artifactId>log4j</artifactId>
21             <version>1.2.17</version>
22             <exclusions>
23                 <exclusion>
24                     <groupId>javax.jms</groupId>
25                     <artifactId>jms</artifactId>
26                 </exclusion>
27                 <exclusion>
28                     <groupId>com.sun.jdmk</groupId>
29                     <artifactId>jmxtools</artifactId>
30                 </exclusion>
31                 <exclusion>
32                     <groupId>com.sun.jmx</groupId>
33                     <artifactId>jmxri</artifactId>
34                 </exclusion>
35             </exclusions>
36         </dependency>
37     </dependencies>
38     <build>
39         <!-- http://blog.csdn.net/xxd851116/article/details/25197373 -->
40         <pluginManagement>
41             <plugins>
42                 <plugin>
43                     <groupId>org.apache.maven.plugins</groupId>
44                     <artifactId>maven-compiler-plugin</artifactId>
45                     <version>3.1</version>
46                     <configuration>
47                         <source>1.7</source>
48                         <target>1.7</target>
49                         <compilerArgument>-Xlint:all</compilerArgument>
50                         <showWarnings>true</showWarnings>
51                         <showDeprecation>true</showDeprecation>
52                     </configuration>
53                     <dependencies>
54                         <dependency>
55                             <groupId>org.codehaus.plexus</groupId>
56                             <artifactId>plexus-compiler-eclipse</artifactId>
57                             <version>2.2</version>
58                         </dependency>
59                     </dependencies>
60                 </plugin>
61                 <plugin>
62                     <groupId>org.apache.maven.plugins</groupId>
63                     <artifactId>maven-war-plugin</artifactId>
64                     <version>2.1.1</version>
65                     <configuration>
66                         <!-- http://maven.apache.org/plugins/maven-war-plugin/ -->
67                         <packagingExcludes>WEB-INF/web.xml</packagingExcludes>
68                     </configuration>
69                 </plugin>
70                 <plugin>
71                     <groupId>org.codehaus.mojo</groupId>
72                     <artifactId>exec-maven-plugin</artifactId>
73                     <version>1.2.1</version>
74                     <configuration>
75                         <mainClass>org.test.int1.Main</mainClass>
76                     </configuration>
77                 </plugin>
78                 <plugin>
79                     <groupId>org.apache.tomcat.maven</groupId>
80                     <artifactId>tomcat7-maven-plugin</artifactId>
81                     <version>2.2</version>
82                     <configuration>
83                         <port>8888</port>
84                         <path>/</path>
85                         <uriEncoding>UTF-8</uriEncoding>
86                         <finalName>com.sem.view.market</finalName>
87                         <server>tomcat7</server>
88                         <username>admin</username>
89                         <password>admin</password>
90                     </configuration>
91                 </plugin>
92             </plugins>
93         </pluginManagement>
94     </build>
pom.xml

6.配置完Build Path后目录如下:

7.将项目转换成Dynamic Web Project在项目上右键Properties,在左侧选择 Project Facets,单击右侧的 "Convert faceted from"

8.修改Java为你当前项目的JDK,并添加Dynamic Web Module ,最后单击”Further Configuration available“ 链接:

9.修改Content directory 为 src/main/webapp ,单击OK:

10.设置完Content directory,ok后再次点击前一界面ok,完成转换成Dynamic Web Project项目

11.设置部署程序集(Web Deployment Assembly)

在项目上右键单击,选择Properties,在左侧选择Deployment Assembly

12.设置部署时的文件发布路径

  1,我们删除test的两项,因为test是测试使用,并不需要部署。
  2,设置将Maven的jar包发布到lib下。
    Add -> Java Build Path Entries -> Maven Dependencies -> Finish

设置完成后如图

ok后,web项目就创建完毕了,目录机构如图

Maven3路程(三)用Maven创建第一个web项目(1)

posted @ 2016-04-13 18:57  —八戒—  阅读(9026)  评论(0编辑  收藏  举报