一、Maven的安裝配置
參考此博客:http://qincao./blog/614022
PS:很簡潔易懂,引用一下,呵呵
二、使用maven構建多工程
1、通過cmd,進入Dos下執(zhí)行mvn創(chuàng)建指令
2、創(chuàng)建主工程,假設在E盤創(chuàng)建
- mvn archetype:create -DgroupId=com.cy -DartifactId=mallshop -Dversion=1.0
備注:
- <groupId>:一個項目群的ID,一個項目群可以包含多個項目
- <artifactId>:一個項目的ID,是每個項目的唯一ID.
- <version>:描述了項目當前的版本.
需要注意,必須修改pom.xml,參考錯誤一的解決方法
3、進入主工程下(Dos下執(zhí)行cd E:\mallshop進入),創(chuàng)建多個子工程,如下:
- mvn archetype:create -DgroupId=com.cy.biz.dal -DartifactId=mallshop-biz-dal -Dversion=1.0
- mvn archetype:create -DgroupId=com.cy.biz.core -DartifactId=mallshop-biz-core -Dversion=1.0
- mvn archetype:create -DgroupId=com.cy.biz.manager -DartifactId=mallshop-biz-manager -Dversion=1.0
- mvn archetype:create -DgroupId=com.cy.web -DartifactId=mallshop-web -Dversion=1.0 -DarchetypeArtifactId=maven-archetype-webapp
引用 其中修改的重點為打包方式(war/jar)改為pom形式,這也就意味這這是一個父工程,另外版本號默認是SNAPSHOT意思是快照的意思,就是項目開發(fā)中的意思,你要是看著不爽可以把它刪掉,另外需要說明一下dependencyManagement標簽,這個標簽表示子類可以隱式的繼承父pom文件的依賴庫,在子pom中不需要指定版本號,推薦這樣,這樣可以方便開發(fā),你要修改什么依賴的版本只需要更改父pom就可以了,dependencies是顯示繼承,你要是在子pom中聲明,就必須寫明版本號,不寫默認就繼承了。
4、修改各個pom,添加相關依賴
-
- <dependency>
- <groupId>com.cy.biz.dal</groupId>
- <artifactId>cy-biz-dal</artifactId>
- <version>${project.version}</version>
- </dependency>
5、將Maven項目轉為Eclipse項目
具體操作為將dos命令窗口切換到Maven項目的目錄下,輸入命令:
mvn eclipse:eclipse
6、導入eclipse
- 在該項目上點右鍵Maven->Enable
- 在該項目上點右鍵Build Path->Configure Build Path->Java Build Path->Libraries->去掉Maven添加的變量路徑
- 在該項目上點右鍵MyEclipse->Add Web Capabilities->修改Web root地址(點【瀏覽】按鈕指定為當前工作空間下的src/main/webapp文件夾)
- 配置完畢,可以用MyEclipse插件配置服務器等開始正常開發(fā),自動部署,調試等操作了。
三、整合ssi
1、在主工程下的pom.xml中添加相應的配置,完整如下
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven./POM/4.0.0" xmlns:xsi="http://www./2001/XMLSchema-instance" xsi:schemaLocation="http://maven./POM/4.0.0 http://maven./maven-v4_0_0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.cy</groupId>
- <artifactId>mallshop</artifactId>
- <packaging>pom</packaging>
- <version>1.0</version>
- <name>mallshop</name>
- <url>http://maven.</url>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.7</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>1.2.14</version>
- </dependency>
- <dependency>
- <groupId>commons-dbcp</groupId>
- <artifactId>commons-dbcp</artifactId>
- <version>1.2.2</version>
- </dependency>
- <dependency>
- <groupId>commons-beanutils</groupId>
- <artifactId>commons-beanutils</artifactId>
- <version>1.7.0</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-mock</artifactId>
- <version>2.0.8</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>2.5.6</version>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.9</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring</artifactId>
- <version>2.5.6</version>
- </dependency>
- <dependency>
- <groupId>org.apache.ibatis</groupId>
- <artifactId>ibatis-sqlmap</artifactId>
- <version>2.3.4.726</version>
- </dependency>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>servlet-api</artifactId>
- <version>2.4</version>
- </dependency>
- <dependency>
- <groupId>javax.mail</groupId>
- <artifactId>mail</artifactId>
- <version>1.4.1</version>
- </dependency>
- <dependency>
- <groupId>org.apache.struts</groupId>
- <artifactId>struts2-jfreechart-plugin</artifactId>
- <version>2.1.8</version>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <configuration>
- <source>1.6</source>
- <target>1.6</target>
- <encoding>GBK</encoding>
- </configuration>
- </plugin>
- </plugins>
- </build>
- <modules>
- <module>mallshop-biz-dal</module>
- <module>mallshop-biz-core</module>
- <module>mallshop-biz-manager</module>
- <module>mallshop-web</module>
- </modules>
- </project>
PS:具體根據(jù)自己的需要
2、執(zhí)行如下mvn指令,完成
mvn install
mvn clean
mvn eclipse:eclipse
mvn clean package -Dmaven.test.skip=true;
執(zhí)行下載
mvn eclipse:clean eclipse:eclipse -DdownloadSources=true
3、Bingo!整合完畢??!
在此過程中遇到錯誤整理:
1、未執(zhí)行如下操作,就創(chuàng)建子工程,會報這樣的錯誤:
引用 Embedded error: Unable to add module to the current project as it is not of packaging type 'pom'
解決方案:
修改主工程目錄下的pom文件,設置如下:
<packaging>pom</packaging>
2、source 1.3 中不支持泛型
maven打包時始終出現(xiàn)以下提示:
引用 1、-source 1.3 中不支持泛型(請使用 -source 5 或更高版本以啟用泛型)List<User> userList= new ArrayList<User>();
2、-source 1.3 中不支持注釋(請使用 -source 5 或更高版本以啟用注釋)@WebService(endpointInterface = "com.webservice.service.LoadService")
解決辦法:
用命令mvn -v查看結果:
引用 C:\Users\Administrator>mvn -v
Apache Maven 2.2.1 (r801777; 2009-08-07 03:16:01+0800)
Java version: 1.6.0
Java home: D:\Program Files\Java\jdk1.6.0\jre
Default locale: zh_CN, platform encoding: GBK
OS name: "windows vista" version: "6.1" arch: "x86" Family: "windows"
PS:顯然與所配置的JDK無關。
最終解決辦法:
在項目的pom.xml(放在主工程下的)中,添加
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <configuration>
- <source>1.5</source>
- <target>1.5</target>
- </configuration>
- </plugin>
- </plugins>
- </build>
再執(zhí)行mvn install,OK。
3、新建的類中,右擊,執(zhí)行相應操作,會出現(xiàn)如下提示框:
原因是找不到路徑,具體的操作方式如下:
在web工程中新建Source Folder,取名為src/main/resources或src/main/java,然后新建package,在此包下創(chuàng)建class;在src->main中新建Folder,取名為webconfig,存放spring配置文件
|