用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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM