Maven入門項目創建
阿新 • • 發佈:2018-10-13
eba mage src inf upd info 報錯 圖片 com
項目構建
1.新建maven項目
2.跳過骨架選擇,如果不跳過骨架選擇創建出的項目目錄是不全的(骨架其實就是項目的模板)
3.Group Id,Artifact Id,Version稱為項目的坐標,當別人使用項目時,通過坐標來查找。
Packaging有三個選項:jar,pom,war
jar:創建的工程為java工程
pom:復工程
war:創建的工程為web工程
最後,點擊finish,完成項目的創建。
4.處理pom.xml紅色叉號
手動在src/main/webapp文件夾下創建一個WEB-INF文件夾,在裏面放一個web.xml文件
5.處理編譯版本
在pom.xml中添加如下代碼
<build> <!-- 配置了很多插件 --> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build>
刷新項目:右擊工程→Maven→Update Project
更新後,不再報錯
6.在src/main/java下創建servlet
點擊finish
接下來,修改web.xml
刪除重復的代碼
xmlns="http://java.sun.com/xml/ns/javaee"
保存,web.xml不再報錯。
添加jar包,在pom.xml中添加如下代碼:
<!-- 添加servlet-api,jsp-api --> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency> </dependencies>
保存。項目中多了新的內容,會發現jar包已添加到項目中
7.啟動項目
右擊項目→Run As→5 Maven build...
在Goals中輸入tomcat:run
點擊Run,項目開始運行。
Maven入門項目創建