1. 程式人生 > >使用eclipse搭建maven多module專案(構建父子專案java子專案和web子專案)

使用eclipse搭建maven多module專案(構建父子專案java子專案和web子專案)

1、建立空的Maven專案

File–>new–>project–>Maven Project...

2、next


3、finish

4、配置pom

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.nancy</groupId>
  <artifactId>DemoParent</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
	<modules>
		<module>DemoParent-Son1</module>
		<module>DemoParentSon2</module>
	</modules>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<junit.version>4.11</junit.version>
		<spring.version>4.0.0.RELEASE</spring.version>
	</properties>
	<build>
		<finalName>DemoParent</finalName>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.3.2</version>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
					<compilerVersion>1.6</compilerVersion>
				</configuration>
			</plugin>
			<!-- jar外掛 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<version>2.5</version>
				<configuration>
					<archive>
						<manifest>
							<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
							<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
						</manifest>
					</archive>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>${junit.version}</version>
		</dependency>
		<!-- spring framework -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.2</version>
		</dependency>
	</dependencies>
</project>

5、建立maven子工程,在父工程上右鍵New..

6、next

7、next..我們這個子工程建立java專案

8、next


9、finish–>配置pom.xml (主要把多餘部分刪除,junit只要在父工程的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>
  <parent>
    <groupId>com.nancy</groupId>
    <artifactId>DemoParent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>DemoParent-Son1</artifactId>
  <version>0.0.1-SNAPSHOT</version>
</project>
10、建立web module
  1. 與建立java module的1、2點一樣;

  2. next


11、finish

12、此時可以看到,生成的web子工程有錯。

Q1:專案忽然出現 The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path 的錯誤


Solve:點選專案右鍵>>Build Path>>Configure Build Path>>AddLibrary

點選next,選擇一個自己配置好的tomcat服務

點選finish,然後apply and Close即可

Q2:在更改Dynamic Web Module的版本時(2.3->2.5),出現Cannot change version of project facet Dynamic web module

to 2.5錯誤

說明:我們用Eclipse建立Maven結構的web專案的時候選擇了Artifact Id為maven-artchetype-webapp,由於這個catalog比較老,用的servlet還是2.3的,而一般現在至少都是2.5,在Project Facets裡面修改Dynamic web module為2.5的時候就會出現Cannot change version of project facet Dynamic web module to 2.5

Solve:

1.更改專案的web.xml檔案,把servlet改為2.5

(1)改之前:

<!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>

(2)改之後:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
	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_2_5.xsd">
  <display-name>Archetype Created Web Application</display-name> 
 </web-app>

2.開啟Navigator檢視:window>>Show View,選擇Navigator;在Navigator檢視下,開啟專案下的.settings目錄下的org.eclipse.jdt.core.prefs,把1.5全部改為1.6


3. 開啟org.eclipse.wst.common.component,把project-version="1.5.0"改成project-version="1.6.0"

4. 開啟org.eclipse.wst.common.project.facet.core.xml,把<installed facet="java" version="1.5"/>改成<installed facet="java" version="1.6"/>,把<installed facet="jst.web" version="2.3"/>改成<installed facet="jst.web" version="2.5"/>

到此,已經Dynamic web module 2.3修改成2.5版本

Q3:設定部署程式集(Web Deployment Assembly),部署時的檔案釋出路徑


Solve:點選專案右鍵>>properties>> Deployment Assembly


13、此時,我們已經搭建好了整個專案,在父工程上右鍵RunAs>>Maven build...>>Goals:clean install

編譯一下整個工程,可以發現已經成功了。此時的專案架構如下:

14、我們的pom檔案如下:配置中有點說明下:把jar包自動拷貝到web-inf目錄地下,這個搞web開發都懂是為什麼了

<?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>
  <parent>
    <groupId>com.nancy</groupId>
    <artifactId>DemoParent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <groupId>com.nancy</groupId>
  <artifactId>DemoParent-Son2</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <finalName>DemoParent-Son2</finalName>
    <!-- 配置中有點說明下:把jar包自動拷貝到web-inf目錄地下,這個搞web開發都懂是為什麼了 -->
		<plugins>
			<plugin>
				<artifactId>maven-antrun-plugin</artifactId>
				<executions>
					<execution>
						<id>copy-lib-src-webapps</id>
						<phase>package</phase>
						<configuration>
							<tasks>
								<delete dir="WebContent/WEB-INF/lib" />
								<copy todir="WebContent/WEB-INF/lib">
									<fileset dir="target/${artifactId}/WEB-INF/lib">
										<include name="*" />
									</fileset>
								</copy>
							</tasks>
						</configuration>
						<goals>
							<goal>run</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
  </build>
</project>

15、接下來,我們在DemoParent-Son1這個java的子工程中呼叫DemoParent-Son2這個web子工程。直接在DemoParent-Son1的pom檔案中加上DemoParent-Son2的專案的引用(即座標)即可。

  <dependencies>
  	<dependency>
  		<groupId>com.nancy</groupId>
  		<artifactId>DemoParent-Son2</artifactId>
  		<version>0.0.1-SNAPSHOT</version>
  		<type>war</type>
  	</dependency>
  </dependencies>

注意:要標明引入的子工程的打包方式。即type屬性!