`

创建支持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;
设定父工程的编译等级:
  <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工程的依赖
		<dependency>
			<groupId>com.pandy.p</groupId>
			<artifactId>PandyC1</artifactId>
			<version>1.0-SNAPSHOT</version>
		</dependency>


Web模块设定插件,eclipse插件,jetty插件,注意<finalName>PandyC2</finalName>和
<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 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 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 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节点,已经定义了。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics