Eclipse 創(chuàng)建 maven 項(xiàng)目動(dòng)態(tài) web 工程
一、操作環(huán)境
- Eclipse Oxygen 4.7.0
eclipse版本.png
- maven 3.5.0
maven版本
- JDK 1.8.0_20
jdk1.8
- Apache Tomcat 7.0
Tomcat7
二虐骑、創(chuàng)建 maven 的 web 項(xiàng)目
- 新建項(xiàng)目 File(或者選擇 Eclipse 左側(cè)的 “Package Explorer” 中的某個(gè)工作集,右鍵 ) -> New -> Other,在彈出的對(duì)話框中找到 “Maven” -> 選擇”Maven Project” -> “Next”
新建項(xiàng)目
-
Next
New Maven Project
注:
此處我是在我的工作集(working set) testWorkSet 上點(diǎn)擊右鍵來創(chuàng)建的項(xiàng)目。因此,會(huì)自動(dòng)將創(chuàng)建項(xiàng)目添加到 Test 工作集辟癌。此處不用任何設(shè)置,直接 Next 即可荐捻。
- 選擇 maven 項(xiàng)目模板 在彈出的對(duì)話框中找到 Artifact Id 為 “maven-archetype-webapp” 的條目黍少,點(diǎn)擊 “Next”
選中webapp
注:
此處可在 Filter 后的文本框中輸入 “webapp” 來進(jìn)行快速過濾與定位
如果同一個(gè) Artifact 有多個(gè)版本,請(qǐng)選擇最新版本(”Show the last version of Archetype only” 勾選了則不會(huì)出現(xiàn)多個(gè)版本)
- 設(shè)置 maven 項(xiàng)目信息
在 “Group Id”处面,”Artifact Id”厂置,”Package” 后的文本框中設(shè)置該項(xiàng)目的信息,然后點(diǎn)擊 “Finish”
注:
- Group Id 的設(shè)置一般使用公司或者組織的倒置域名魂角。例如:spring-mvc 項(xiàng)目的 groupId 為 org.springframework昵济,artifactId 為 spring-webmvc
- Artifact Id 設(shè)置為該項(xiàng)目的名稱。例如:apache 組織的 tomcat 項(xiàng)目,groupId 為 org.apache访忿,artifactId 為 tomcat
- Package 是為 maven 項(xiàng)目設(shè)置一個(gè)包路徑瞧栗。如此處:groupId 為 com.jzl,artifactId 為 mvnweb醉顽,則 Package 自動(dòng)生成為 com.jzl.mvnweb沼溜。Package 路徑設(shè)置也可以不使用默認(rèn)的規(guī)則,完全可以自由設(shè)定游添,甚至為空系草,不過約定俗成的方式一般是使用公司或者組織的倒置域名加上項(xiàng)目名組成。
- version 在首次創(chuàng)建的時(shí)候保持默認(rèn)即可唆涝,它用來標(biāo)識(shí)項(xiàng)目的版本
- 此處設(shè)置的 groupId找都、artifactId、version 等信息會(huì)在項(xiàng)目創(chuàng)建完成后生成到 pom.xml 配置文件中廊酣,并且如果你的項(xiàng)目是需要作為第三方依賴提供給其他人使用能耻,此處設(shè)置的信息也對(duì)應(yīng)到對(duì)方配置的 pom.xml 中的 標(biāo)簽中的配置。當(dāng)然亡驰,這些信息也可以在項(xiàng)目創(chuàng)建完成以后在 pom.xml 中來更改晓猛。
- 完成項(xiàng)目創(chuàng)建
在上一步中點(diǎn)擊 “Finish” 后,等待一段時(shí)間凡辱,項(xiàng)目創(chuàng)建完成戒职,會(huì)生成一個(gè)如下圖所示的項(xiàng)目結(jié)構(gòu)。至此透乾,項(xiàng)目創(chuàng)建完成
注:
如果是第一次使用 eclipse 的 maven 創(chuàng)建項(xiàng)目洪燥,會(huì)從 maven 倉庫下載一些依賴包,所以可能需要一點(diǎn)時(shí)間(取決于網(wǎng)速與電腦配置)乳乌,請(qǐng)耐心等待(右下角狀態(tài)欄會(huì)有下載的進(jìn)度條信息)
結(jié)構(gòu)捧韵。至此,項(xiàng)目創(chuàng)建完成
注:
如果是第一次使用 eclipse 的 maven 創(chuàng)建項(xiàng)目汉操,會(huì)從 maven 倉庫下載一些依賴包再来,所以可能需要一點(diǎn)時(shí)間(取決于網(wǎng)速與電腦配置),請(qǐng)耐心等待(右下角狀態(tài)欄會(huì)有下載的進(jìn)度條信息)
三磷瘤、項(xiàng)目配置
配置項(xiàng)目 JRE
在項(xiàng)目創(chuàng)建完成后芒篷,在 workspace 中的項(xiàng)目根目錄下生成了 .classpath 文件:mvnweb/.classpath
,此文件內(nèi)容如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
上面的的文件內(nèi)容即對(duì)應(yīng)了 mvnweb -> Properties -> Java Build Path -> Source 中的 Source folders膀斋,如下所示:
我們發(fā)現(xiàn) Source 下的 mvnweb/src/main/java
以及 mvnweb/src/test/java
兩個(gè) Source 目錄前面有紅叉梭伐,后面有括號(hào)顯示為 missing,并且 Package Explorer 中也沒有顯示 src/main/java
以及 src/test/java
這兩個(gè) source folder仰担。但當(dāng)我們右鍵 mvnweb -> New -> Source Folder -> 填寫 src/main/java
時(shí)顯示為已經(jīng)存在此 Source Folder糊识。
原因是 maven 項(xiàng)目生成的 .classpath 中使用的 JRE_CONTAINER 是 J2SE-1.5绩社,而在 Eclipse 中配置的 Installed JREs 中,設(shè)置的 JRE 版本與 1.5 不匹配赂苗。
解決方法就是配置正確的 JRE 環(huán)境:
右鍵 mvnweb -> Build Path -> Configure Build Path -> Libraries -> “JRE System Library [J2SE-1.5]” -> Edit
在彈出的對(duì)話框中愉耙,選擇 “Workspace default JRE(jdk-1.8.0_20)” -> Finish
注:
- 也可以使用 Execution environment -> 選擇 J2SE-1.8 (jdk-1.8.0_20) 或者 Alternate JRE -> 選擇 jdk-1.8.0_20
- 此處的 “jdk-1.8.0_20” 與你系統(tǒng)中安裝的 JDK 以及在 Eclipse 中配置的 JDK 有關(guān),如果使用的其他版本的 JDK拌滋,則以安裝的 JDK 版本為準(zhǔn)
如下: Window -> Preferences -> Java -> Installed JREs
在設(shè)置了正確的 JRE 后朴沿,可以看到 Source folders 下面的顯示已經(jīng)正常,而且 Package Explorer 中這兩個(gè)資源目錄也顯示出來了
可以雙擊 Source 下的各項(xiàng)屬性來配置 Source 屬性(當(dāng)然败砂,你也可以在 mvnweb/.classpath
文件中修改)赌渣,如 output folder,不過此處保持默認(rèn)即可:
配置 Java 編譯器版本 (Java compiler level)
當(dāng)把 JRE 配置到當(dāng)前 Eclipse 使用的 JDK 之后昌犹,解決了資源目錄不顯示的問題坚芜,但又出現(xiàn)了新問題,可能看到 mvnweb 項(xiàng)目名稱前面出現(xiàn)了紅叉斜姥,而 Problems 視圖中則顯示了問題的原因(Java 編譯器版本不匹配)如下:
解決方法如下:
(1) 確保 Java Compiler 的版本與 Java Build Path 中的 JRE 版本一致:
mvnweb -> 右鍵 -> Properties -> Java Compiler -> JDK Compliance -> 確保 Use compliance from execution evironment 已經(jīng)勾選鸿竖,并且版本一致。
此處配置對(duì)應(yīng) mvnweb/.settings/org.eclipse.jdt.core.prefs 文件铸敏,內(nèi)容如下:
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.8
如果不勾選 “Use compliance from execution evironment”缚忧,也可以自行在下方設(shè)置 “Java Compiler level”
(2) 配置 Project Factets 中的 Java Version
mvnweb -> 右鍵 -> Properties -> Project Facets -> Java -> Version -> 1.8 -> “Apply and close”
可以看到,mvnweb 項(xiàng)目的錯(cuò)誤已經(jīng)沒有了杈笔。
(3) 配置 maven compiler 版本(重要)
當(dāng)你以為配置了上面兩部分配置闪水,似乎已經(jīng)完美解決了問題時(shí),其實(shí)這一步才是非常重要桩撮,也是最大的一個(gè)坑敦第。
雙擊 mvnweb/pom.xml -> 左下角標(biāo)簽欄 pom.xml峰弹,可以看到新創(chuàng)建的 maven 項(xiàng)目除了項(xiàng)目基本信息和一個(gè) junit 的依賴包店量,并沒有其他的設(shè)置。那么問題來了鞠呈,maven 如何實(shí)現(xiàn)對(duì)項(xiàng)目的打包融师,更新,編譯等等一系列操作的呢蚁吝?
pox.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jzl</groupId>
<artifactId>mvnweb</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>mvnweb Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>mvnweb</finalName>
</build>
</project>
答案就在 Effective POM 標(biāo)簽頁中旱爆,點(diǎn)擊 pom.xml 左側(cè)的 Effective POM 標(biāo)簽頁,可以看到這個(gè)標(biāo)簽頁中有很多的 maven 插件窘茁。因?yàn)?Effective POM 是包含了從 maven 的超級(jí) POM 中繼承的元素以及項(xiàng)目配置的 pom 屬性怀伦。
Effective POM :
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jzl</groupId>
<artifactId>mvnweb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>mvnweb Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
</pluginRepository>
</pluginRepositories>
<build>
<sourceDirectory>D:\eclipseWorkspace\mvnweb\src\main\java</sourceDirectory>
<scriptSourceDirectory>D:\eclipseWorkspace\mvnweb\src\main\scripts</scriptSourceDirectory>
<testSourceDirectory>D:\eclipseWorkspace\mvnweb\src\test\java</testSourceDirectory>
<outputDirectory>D:\eclipseWorkspace\mvnweb\target\classes</outputDirectory>
<testOutputDirectory>D:\eclipseWorkspace\mvnweb\target\test-classes</testOutputDirectory>
<resources>
<resource>
<directory>D:\eclipseWorkspace\mvnweb\src\main\resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>D:\eclipseWorkspace\mvnweb\src\test\resources</directory>
</testResource>
</testResources>
<directory>D:\eclipseWorkspace\mvnweb\target</directory>
<finalName>mvnweb</finalName>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
</plugin>
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.3.2</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>default-clean</id>
<phase>clean</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>default-testResources</id>
<phase>process-test-resources</phase>
<goals>
<goal>testResources</goal>
</goals>
</execution>
<execution>
<id>default-resources</id>
<phase>process-resources</phase>
<goals>
<goal>resources</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>default-war</id>
<phase>package</phase>
<goals>
<goal>war</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<executions>
<execution>
<id>default-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>default-testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<executions>
<execution>
<id>default-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>default-install</id>
<phase>install</phase>
<goals>
<goal>install</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>default-deploy</id>
<phase>deploy</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.3</version>
<executions>
<execution>
<id>default-site</id>
<phase>site</phase>
<goals>
<goal>site</goal>
</goals>
<configuration>
<outputDirectory>D:\eclipseWorkspace\mvnweb\target\site</outputDirectory>
<reportPlugins>
<reportPlugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
</reportPlugin>
</reportPlugins>
</configuration>
</execution>
<execution>
<id>default-deploy</id>
<phase>site-deploy</phase>
<goals>
<goal>deploy</goal>
</goals>
<configuration>
<outputDirectory>D:\eclipseWorkspace\mvnweb\target\site</outputDirectory>
<reportPlugins>
<reportPlugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
</reportPlugin>
</reportPlugins>
</configuration>
</execution>
</executions>
<configuration>
<outputDirectory>D:\eclipseWorkspace\mvnweb\target\site</outputDirectory>
<reportPlugins>
<reportPlugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
</reportPlugin>
</reportPlugins>
</configuration>
</plugin>
</plugins>
</build>
<reporting>
<outputDirectory>D:\eclipseWorkspace\mvnweb\target\site</outputDirectory>
</reporting>
</project>
注:
- 所有的 POM 都繼承自一個(gè) maven 的基類,這個(gè)基礎(chǔ)的 POM 被稱為超級(jí) POM山林,并包含默認(rèn)值房待。
- Maven 使用 Effective POM (超級(jí) POM 加項(xiàng)目配置的屬性) 執(zhí)行操作。它可以幫助開發(fā)人員指定最低配置的詳細(xì)信息寫在 pom.xml 中。
- 如果在項(xiàng)目的 pom.xml 中沒有配置某一屬性桑孩,那么 maven 在執(zhí)行操作時(shí)就會(huì)使用 Effective POM 中從超級(jí) POM 繼承的默認(rèn)配置拜鹤。而如果項(xiàng)目中配置了某屬性,則會(huì)直接覆蓋超級(jí) POM 中的配置流椒。
說了這么多敏簿,問題的關(guān)鍵就在于如果沒有在項(xiàng)目的 pom.xml 中配置 maven-compiler-plugin,而 maven 又需要使用此插件宣虾,那么 maven 在進(jìn)行編譯時(shí)就會(huì)使用 Effective POM 中默認(rèn)的 maven-compiler-plugin 配置惯裕,而從上面的截圖也可以看到,默認(rèn)的 compiler-plugin 沒有配置編譯版本(默認(rèn)使用的是 JDK 1.5)绣硝。因此轻猖,當(dāng)我們進(jìn)行任何的 maven 操作時(shí),就會(huì)將項(xiàng)目的 JDK 版本又設(shè)置到 JDK-1.5 的版本域那,導(dǎo)致我們前面所配置的 JRE咙边,Java Compiler 都失效。
說明:
Maven官方文檔有如下描述:
編譯器插件用來編譯項(xiàng)目的源文件.從3.0版本開始,
用來編譯Java源文件的默認(rèn)編譯器是javax.tools.JavaCompiler (如果你是用的是java 1.6) 次员。
如果你想強(qiáng)制性的讓插件使用javac,你必須配置插件選項(xiàng) forceJavacCompilerUse败许。
同時(shí)需要注意的是目前source選項(xiàng)和target 選項(xiàng)的默認(rèn)設(shè)置都是1.5, 與運(yùn)行Maven時(shí)的JDK版本無> > 關(guān)。如果你想要改變這些默認(rèn)設(shè)置, 可以參考 Setting the -source and -target of the Java Compiler中的描述來設(shè)置 source 和target 選項(xiàng)淑蔚。
這是Maven已知的一個(gè)特性市殷。除非在你的POM文件中顯示的指定一個(gè)版本,否則會(huì)使用編譯器默認(rèn)的source/target版本1.5刹衫。主要還是在于Eclipse中Maven的集成方式起到了關(guān)鍵作用, 它會(huì)從POM文件中生成項(xiàng)目的.project,.classpath以及.settings, 因此除非POM文件指定了正確的JDK版本, 否則你每次更新項(xiàng)目配置的時(shí)候它都會(huì)重置到1.5版本醋寝。
解決方法是
方法一: 在 pom.xml -> project -> build 節(jié)點(diǎn)下,添加如下內(nèi)容:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<compilerVersion>1.8</compilerVersion>
<fork>true</fork>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jzl</groupId>
<artifactId>mvnweb</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>mvnweb Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>mvnweb</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<compilerVersion>1.8</compilerVersion>
<fork>true</fork>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
注:
- 如果你的 build 下已經(jīng)配置過 節(jié)點(diǎn)了带迟,那么就在 節(jié)點(diǎn)下面添加 部分的內(nèi)容即可
- 與 節(jié)點(diǎn)配置的版本與上面配置的 JRE 與 Java Compiler 版本一致 (即當(dāng)前使用的 JDK 版本)
方法二: 修改maven的配置文件 setting.xml
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
最后, 右鍵 mvnweb -> Maven -> Update Proejct音羞,編譯版本問題解決
配置 web app version
由于 maven 使用 maven-artchetype-webapp-1.0 創(chuàng)建的 web-app 模板默認(rèn)的 Dynamic Web Module 版本是 2.3,對(duì)應(yīng)的 servlet 版本比較低,仓犬,如下所示:
mvnweb/src/main/webapp/WEB-INFO/web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd"; >
<web-app>
<display-name>Archetype Created Web Application</display-name>
</web-app>
改為:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>Archetype Created Web Application</display-name>
</web-app>
Proejct Facets
注:
此處對(duì)應(yīng)的配置文件是 mvnweb/.settings/org.eclipse.wst.common.project.facet.core.xml
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<fixed facet="wst.jsdt.web"/>
<installed facet="jst.web" version="2.3"/>
<installed facet="wst.jsdt.web" version="1.0"/>
<installed facet="java" version="1.8"/>
</faceted-project>
由于在操作時(shí)發(fā)現(xiàn)在 Dynamic Web Module 后面的 Version 處選擇 版本 3.0 顯示不能從 2.3 轉(zhuǎn)換到 3.0 (此處原因不詳嗅绰,但我把 Dynamic Web Module 前的勾去掉再重新勾選可以轉(zhuǎn)換)。采取直接修改配置文件的方式搀继,將上方的 org.eclipse.wst.common.project.facet.core.xml 改為:
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<fixed facet="wst.jsdt.web"/>
<installed facet="jst.web" version="3.0"/>
<installed facet="wst.jsdt.web" version="1.0"/>
<installed facet="java" version="1.8"/>
</faceted-project>
然后 右鍵 mvnweb -> Refresh窘面,可以看到 Project Facets 中的 Dynamic Web Module 已經(jīng)變成了 3.0 版本
四、eclipse 中運(yùn)行 maven web 項(xiàng)目
右鍵 mvnweb -> Run As -> Run on Server -> 選擇一個(gè)本地配置好的運(yùn)行時(shí) Server (配置方法見附錄二) -> Finish
至此叽躯,一個(gè)完整的财边,規(guī)范的 maven web 項(xiàng)目搭建完成。
附錄一:配置 Project Facets Runtimes
在 Project Facets -> Runtimes 中可以配置一個(gè)運(yùn)行時(shí)環(huán)境点骑,但前提是你必須已經(jīng)安裝有 java web 的運(yùn)行時(shí)環(huán)境酣难,比如 tomcat 7们童,在勾選了運(yùn)行時(shí)環(huán)境后,會(huì)將運(yùn)行時(shí)環(huán)境中的 jar 包加載到項(xiàng)目中鲸鹦。此步驟一般不用設(shè)置慧库。
附錄二:配置 Runtimes 環(huán)境
運(yùn)行時(shí)環(huán)境配置: Window -> Preferences -> Server -> Runtime Environments -> add -> 選擇運(yùn)行時(shí)環(huán)境及版本(如 apache tomcat v7.0)
填寫一個(gè)運(yùn)行時(shí)環(huán)境的名字(任意取,只用來做標(biāo)識(shí)用) -> 選擇運(yùn)行時(shí)安裝的路徑 -> 選擇 JRE -> Finish
附錄三:修改項(xiàng)目部署組件
右鍵 mvnweb -> Properties -> Deployment Assembly
附錄四:項(xiàng)目 Eclipse 配置文件
- 可以在 eclipse 的 workspace 中的項(xiàng)目目錄下找到
在 eclipse 中的 navigator 中查看
Window -> Show View -> Other -> General -> Navigator -> Open
轉(zhuǎn)自 : Eclipse 創(chuàng)建 maven 項(xiàng)目動(dòng)態(tài) web 工程
對(duì)原文進(jìn)行了排版和少量的修改