軟體清單: JDK 1.8 Maven 3.5 Eclipse Java EE IDE for Web Developers,Version: Luna Service Release 2 (4.4.2) Tomcat 8.0 1. Maven的下載、解壓及環境變數設定 首先確保本機中已經安裝好JDK,並配置好環境變數(JAVA_HOME,Path,classpath); 進入Maven官網下載最新版Maven:http://maven.apache.org/download.cgi。其中Binary zip/tar.gz archive是class檔案,可以直接使用,而Source zip/tar.gz archive是java檔案,即源碼,不能使用。Windows系統下載apache-maven-3.5.0-bin.zip。如下圖所示:
將maven壓縮包解壓到電腦某盤某路徑下,配置好環境變數(MAVEN_HOME,Path),具體操作步驟不再贅述。確保Maven版本與JDK版本匹配,比如,Maven 3.5需要JDK 1.7及JDK 1.7以上的版本; 2. 設定JDK版本
為了防止本地JDK與更新Maven項目時預設JDK版本不一致。
- 開啟Maven安裝目錄的conf/settings.xml,在<profiles></profiles> 標籤之間添加:
<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>
3. 配置Maven倉庫 3.1. 修改本地倉庫 開啟Maven安裝目錄的conf/settings.xml,在<settings></settings> 標籤之間添加一句話:
<localRepository>D:\Program Files\apache-maven-3.5.0\repository</localRepository>
D:\Program Files\apache-maven-3.5.0\repository即為本地倉庫路徑,可以自己定,repository檔案夾需提前建好; 將修改後的settings.xml複製一份到本地倉庫的repository下; 開啟eclipse→windows→Preferences→Maven→Installations,添加Maven安裝目錄;eclipse→windows→Preferences→Maven→User Settings,設定如下:
運行CMD,執行@mvn help:system; 3.2. 配置遠程倉庫
往後再整理,暫時參考:http://www.cnblogs.com/cnblog-long/p/6611383.html 4. 在Eclipse中使用Maven建立web項目
首先確保本機中已經安裝並配置好Tomcat。 4.1. 建立maven-archetype-webapp New→Other→Maven Project→Next→選擇maven-archetype-webapp,設定好Group Id(包結構,如com.ls)和Artifact Id(項目名) 配置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.ls</groupId> <artifactId>UserLoadMaven</artifactId> //項目名,因人而異 <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>UserLoadMaven 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> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies> <build> <defaultGoal>install</defaultGoal> <finalName>UserLoadMaven</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build></project>
pom.xml中,即使是注釋<!-- --> 中,也絕對不能出現中文及中文標點,否則,porm.xml檔案會報錯 4.2. 將Maven項目轉換成Eclipse工程 右鍵項目名Properties→Project Facets,並進行相應配置,如下圖所示:
如果在勾上Dynamic Web Module並修改版本號碼時報錯:Cannot change version of project facet Dynamic web module to 3.1,請參考:http://blog.csdn.net/steveguoshao/article/details/38414145
進行解決。 將Maven依賴包添加到本項目類路徑中:
最終完成形式:
而項目結構則是:
4.3. 使用Maven編譯和發布項目
在項目中的“pom.xml”檔案上點擊右鍵,在彈出的菜單中選擇“Run AS Maveninstall”來編譯和產生項目。
在編譯和產生過程中,可以在“Console”面板中可以看到Maven編譯過程的資訊,如果編譯和產生成功,可以在“Console”面板看到“BUILD SUCCESS”字樣。
另外,在修改pom.xml檔案中的相關jar包資訊時,可能由於倉庫中不存在,就會出現一些錯誤,這時,項目編譯就可能存在一些問題。此時可通過“Maven clean”選項來清除編譯,然後再修改pom.xml到正確情況。再通過“Maven install”來編譯和產生。