创建支持eclipse的多模块maven项目

Maven命令行来创建项目 http://everlook.iteye.com/blog/1446672
Maven最佳实践:划分模块 http://blog.csdn.net/yydcj/article/details/8698222

如果碰到问题,或者觉得不详细,可以参考
http://blog.csdn.net/yydcj/article/details/8699633

一些命令说明:
直接创建maven web项目
mvn archetype:create -DgroupId=cn.everlook.myweb -DartifactId=myweb -DarchetypeArtifactId=maven-archetype-webapp
说明:archetypeArtifactId(项目骨架的类型)
* maven-archetype-archetype
* maven-archetype-j2ee-simple
* maven-archetype-mojo
* maven-archetype-portlet
* maven-archetype-profiles (currently under development)
* maven-archetype-quickstart
* maven-archetype-simple (currently under development)
* maven-archetype-site
* maven-archetype-site-simple
* maven-archetype-webapp

然后进入myweb目录,执行mvn eclipse:eclipse -Dwtpversion=2.0,这就可以用wtp插件发布了。

根据原文,我得到自己的操作:
环境: Maven 3.0.x, OS: Fedora17

1.创建父工程,并配置
mvn archetype:create -DgroupId=com.pandy.p -DartifactId=PandyTest  -DarchetypeArtifactId=maven-archetype-quickstart
cd PandyTest
rm -rf src

修改pom.xml里面的packaging为pom;
设定父工程的编译等级:
Xml代码 复制代码  收藏代码
  1. <build>  
  2.     <plugins>  
  3.         <plugin>  
  4.             <artifactId>maven-compiler-plugin</artifactId>  
  5.             <configuration>  
  6.                 <source>1.7</source>  
  7.                 <target>1.7</target>  
  8.                 <encoding>UTF-8</encoding>  
  9.             </configuration>  
  10.         </plugin>  
  11.     </plugins>  
  12. </build>  
  <build>
      <plugins>
          <plugin>
              <artifactId>maven-compiler-plugin</artifactId>
              <configuration>
                  <source>1.7</source>
                  <target>1.7</target>
                  <encoding>UTF-8</encoding>
              </configuration>
          </plugin>
      </plugins>
  </build>

2.创建子工程,并做相应配置
mvn archetype:create -DgroupId=com.pandy.p -DartifactId=PandyC1  -DarchetypeArtifactId=maven-archetype-quickstart
mvn archetype:create -DgroupId=com.pandy.p -DartifactId=PandyC2  -DarchetypeArtifactId=maven-archetype-webapp

Web模块设定对C1工程的依赖
Xml代码 复制代码  收藏代码
  1. <dependency>  
  2.     <groupId>com.pandy.p</groupId>  
  3.     <artifactId>PandyC1</artifactId>  
  4.     <version>1.0-SNAPSHOT</version>  
  5. </dependency>  
		<dependency>
			<groupId>com.pandy.p</groupId>
			<artifactId>PandyC1</artifactId>
			<version>1.0-SNAPSHOT</version>
		</dependency>


Web模块设定插件,eclipse插件,jetty插件,注意<finalName>PandyC2</finalName>和
Xml代码 复制代码  收藏代码
  1. <contextPath>/PandyC2</contextPath>节点:   
  2.   <build>  
  3.       <finalName>PandyC2</finalName>  
  4.       <plugins>  
  5.           <plugin>  
  6.               <groupId>org.apache.maven.plugins</groupId>  
  7.               <artifactId>maven-eclipse-plugin</artifactId>  
  8.               <configuration>  
  9.                   <wtpmanifest>true</wtpmanifest>  
  10.                   <wtpapplicationxml>true</wtpapplicationxml>  
  11.                   <wtpversion>2.0</wtpversion>  
  12.               </configuration>  
  13.           </plugin>  
  14.          
  15.             <plugin>  
  16.                 <groupId>org.apache.maven.plugins</groupId>  
  17.                 <artifactId>maven-compiler-plugin</artifactId>  
  18.                 <version>2.3.2</version>  
  19.                 <configuration>  
  20.                     <source>1.6</source>  
  21.                     <target>1.6</target>  
  22.                 </configuration>  
  23.             </plugin>  
  24.             <plugin>  
  25.                 <groupId>org.apache.maven.plugins</groupId>  
  26.                 <artifactId>maven-surefire-plugin</artifactId>  
  27.                 <version>2.5</version>  
  28.             </plugin>  
  29.             <plugin>  
  30.                 <groupId>org.apache.maven.plugins</groupId>  
  31.                 <artifactId>maven-war-plugin</artifactId>  
  32.                 <version>2.1.1</version>  
  33.                 <configuration>  
  34.                     <archive>  
  35.                         <manifest>  
  36.                             <!--<addClasspath>true</addClasspath> -->  
  37.                         </manifest>  
  38.                         <manifestEntries>  
  39.                             <Built-By>org-builder</Built-By>  
  40.                             <Build-Jdk>${java.version}</Build-Jdk>  
  41.                         </manifestEntries>  
  42.                     </archive>  
  43.                 </configuration>  
  44.             </plugin>  
  45.             <plugin>  
  46.                 <groupId>org.mortbay.jetty</groupId>  
  47.                 <artifactId>maven-jetty-plugin</artifactId>  
  48.                 <version>6.1.20</version>  
  49.                 <configuration>  
  50.                     <contextPath>/PandyC2</contextPath>  
  51.                     <!--<webDefaultXml>webdefault.xml</webDefaultXml> -->  
  52.                     <scanIntervalSeconds>0</scanIntervalSeconds>  
  53.                     <scanTargetPatterns>  
  54.                         <scanTargetPattern>  
  55.                             <directory>src/main/webapp/WEB-INF</directory>  
  56.                             <excludes>  
  57.                                 <exclude>**/*.jsp</exclude>  
  58.                             </excludes>  
  59.                             <includes>  
  60.                                 <include>**/*.properties</include>  
  61.                                 <include>**/*.xml</include>  
  62.                             </includes>  
  63.                         </scanTargetPattern>  
  64.                     </scanTargetPatterns>  
  65.                 </configuration>  
  66.             </plugin>  
  67.             <plugin>  
  68.                 <artifactId>maven-war-plugin</artifactId>  
  69.                 <version>2.0.2</version>  
  70.                 <configuration>  
  71.                     <archive>  
  72.                         <manifest>  
  73.                             <addDefaultImplementationEntries>true</addDefaultImplementationEntries>  
  74.                         </manifest>  
  75.                         <manifestEntries>  
  76.                             <Implementation-Build>${buildNumber}</Implementation-Build>  
  77.                         </manifestEntries>  
  78.                     </archive>  
  79.                     <dependentWarExcludes>  
  80.                         **/jdbc.properties,**/web.xml,WEB-INF/classes/META-INF/**   
  81.                     </dependentWarExcludes>  
  82.                 </configuration>  
  83.             </plugin>  
  84.         </plugins>  
  85.   </build>  
<contextPath>/PandyC2</contextPath>节点:
  <build>
      <finalName>PandyC2</finalName>
      <plugins>
          <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-eclipse-plugin</artifactId>
              <configuration>
                  <wtpmanifest>true</wtpmanifest>
                  <wtpapplicationxml>true</wtpapplicationxml>
                  <wtpversion>2.0</wtpversion>
              </configuration>
          </plugin>
      
            <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>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.5</version>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.1.1</version>
				<configuration>
					<archive>
						<manifest>
							<!--<addClasspath>true</addClasspath> -->
						</manifest>
						<manifestEntries>
							<Built-By>org-builder</Built-By>
							<Build-Jdk>${java.version}</Build-Jdk>
						</manifestEntries>
					</archive>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.mortbay.jetty</groupId>
				<artifactId>maven-jetty-plugin</artifactId>
				<version>6.1.20</version>
				<configuration>
					<contextPath>/PandyC2</contextPath>
					<!--<webDefaultXml>webdefault.xml</webDefaultXml> -->
					<scanIntervalSeconds>0</scanIntervalSeconds>
					<scanTargetPatterns>
						<scanTargetPattern>
							<directory>src/main/webapp/WEB-INF</directory>
							<excludes>
								<exclude>**/*.jsp</exclude>
							</excludes>
							<includes>
								<include>**/*.properties</include>
								<include>**/*.xml</include>
							</includes>
						</scanTargetPattern>
					</scanTargetPatterns>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.0.2</version>
				<configuration>
					<archive>
						<manifest>
							<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
						</manifest>
						<manifestEntries>
							<Implementation-Build>${buildNumber}</Implementation-Build>
						</manifestEntries>
					</archive>
					<dependentWarExcludes>
						**/jdbc.properties,**/web.xml,WEB-INF/classes/META-INF/**
					</dependentWarExcludes>
				</configuration>
			</plugin>
        </plugins>
  </build>

3.针对eclipse处理
退回到父目录: mvn eclipse:eclipse , 或者: mvn eclipse:eclipse -Dwtpversion=2.0
别人的命令:mvn eclipse:eclipse -Dwtpversion=1.0
记得不要缺少 -Dwtpversion=1.0,这个说明了eclipse认出它就是web项目,而不是简单的java项目,大家应该知道web项目在eclipse可以做一些其他的事情吧。

修改Webapp工程的.classpath文件,增加: 这个很重要
<classpathentry kind="src" path="src/main/java" including="**/*.java"/>
否则,导入之后,创建java的代码,总是在eclipse上位置现实不对。

倒入到eclipse, 导入的时候选择Maven已存在项目,选择父工程的根目录。
会看见导入三个工程,其中 子工程有错误,点击错误右键,快速修复(Quick Fix),即可解决问题
如果要在eclipse中使用m2eclipse,需要使用:
mvn eclipse:m2eclipse
4. 编译过程:
到父工程右键--运行方式--Maven clean , Maven install。
这个过程
A: 自动会编译子模块C1,并打包成jar,
B: 自动会编译子模块C2,并打包成war,  并把C1生成的jar放到C2.war下面去。
5. eclipse运行webapp工程:
因为C2是Webapp的工程,所以必须跑到C2下面执行: jetty:run
问题:如何实时调试C1的java代码
Maven多模块项目 eclipse热部署 Maven项目实现 tomcat热部署 http://blog.csdn.net/laoshuisheng/article/details/6420003
多模块Maven环境下,如何在eclipse构建J2EE调试环境(热替换) http://www.cnblogs.com/hsiayong/archive/2012/06/25/2561185.html
关于部署,可以参考http://panyongzheng.iteye.com/blog/1866757


其他:
按照上面的带的三个pom.xml大概如下:
父pom.xml
Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  4.     <modelVersion>4.0.0</modelVersion>  
  5.   
  6.     <groupId>com.pandy.p</groupId>  
  7.     <artifactId>PandyTest</artifactId>  
  8.     <version>1.0-SNAPSHOT</version>  
  9.     <packaging>pom</packaging>  
  10.   
  11.     <name>PandyTest</name>  
  12.     <url>http://maven.apache.org</url>  
  13.         ..........   
  14.     <modules>  
  15.         <module>PandyC1</module>  
  16.         <module>PandyC2</module>  
  17.     </modules>  
  18. </project>  
<?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.pandy.p</groupId>
	<artifactId>PandyTest</artifactId>
	<version>1.0-SNAPSHOT</version>
	<packaging>pom</packaging>

	<name>PandyTest</name>
	<url>http://maven.apache.org</url>
        ..........
	<modules>
		<module>PandyC1</module>
		<module>PandyC2</module>
	</modules>
</project>


非Webapp的子Module pom.xml
Xml代码 复制代码  收藏代码
  1. <?xml version="1.0"?>  
  2. <project  
  3.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"  
  4.     xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  
  5.     <modelVersion>4.0.0</modelVersion>  
  6.     <parent>  
  7.         <groupId>com.pandy.p</groupId>  
  8.         <artifactId>PandyTest</artifactId>  
  9.         <version>1.0-SNAPSHOT</version>  
  10.     </parent>  
  11.     <groupId>com.pandy.p</groupId>  
  12.     <artifactId>PandyC1</artifactId>  
  13.     <version>1.0-SNAPSHOT</version>  
  14.     <name>PandyC1</name>  
  15.     <url>http://maven.apache.org</url>  
  16.     .....   
  17.     .....   
  18. </project>  
<?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.pandy.p</groupId>
		<artifactId>PandyTest</artifactId>
		<version>1.0-SNAPSHOT</version>
	</parent>
	<groupId>com.pandy.p</groupId>
	<artifactId>PandyC1</artifactId>
	<version>1.0-SNAPSHOT</version>
	<name>PandyC1</name>
	<url>http://maven.apache.org</url>
	.....
	.....
</project>


Webapp的子Module pom.xml
Xml代码 复制代码  收藏代码
  1. <?xml version="1.0"?>  
  2. <project  
  3.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"  
  4.     xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  
  5.     <modelVersion>4.0.0</modelVersion>  
  6.     <parent>  
  7.         <groupId>com.pandy.p</groupId>  
  8.         <artifactId>PandyTest</artifactId>  
  9.         <version>1.0-SNAPSHOT</version>  
  10.     </parent>  
  11.     <groupId>com.pandy.p</groupId>  
  12.     <artifactId>PandyC2</artifactId>  
  13.     <version>1.0-SNAPSHOT</version>  
  14.     <packaging>war</packaging>  
  15.     <name>PandyC2 Maven Webapp</name>  
  16.     <url>http://maven.apache.org</url>  
  17.     <dependencies>  
  18.         ......   
  19.         <dependency>  
  20.             <groupId>com.pandy.p</groupId>  
  21.             <artifactId>PandyC1</artifactId>  
  22.             <version>1.0-SNAPSHOT</version>  
  23.         </dependency>  
  24.     </dependencies>  
  25.     <build>  
  26.         <finalName>PandyC2</finalName>  
  27.         ......   
  28.     </build>  
  29. </project>  
<?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.pandy.p</groupId>
		<artifactId>PandyTest</artifactId>
		<version>1.0-SNAPSHOT</version>
	</parent>
	<groupId>com.pandy.p</groupId>
	<artifactId>PandyC2</artifactId>
	<version>1.0-SNAPSHOT</version>
	<packaging>war</packaging>
	<name>PandyC2 Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		......
		<dependency>
			<groupId>com.pandy.p</groupId>
			<artifactId>PandyC1</artifactId>
			<version>1.0-SNAPSHOT</version>
		</dependency>
	</dependencies>
	<build>
		<finalName>PandyC2</finalName>
		......
	</build>
</project>

其实,两个子Module的pom.xml可以去掉
<groupId>com.pandy.p</groupId>,<version>1.0-SNAPSHOT</version>
因为他们自己的pom.xml里面的parent节点,已经定义了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值