Maven命令行来创建项目 [url]http://everlook.iteye.com/blog/1446672[/url]
一些命令说明:
直接创建maven web项目
[color=blue]mvn archetype:create -DgroupId=cn.everlook.myweb -DartifactId=myweb -DarchetypeArtifactId=[b]maven-archetype-webapp[/b][/color]
说明:archetypeArtifactId(项目骨架的类型)
[i]* 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[/i]
然后进入myweb目录,执行mvn eclipse:eclipse -Dwtpversion=2.0,这就可以用wtp插件发布了。
根据原文,我得到自己的操作:
环境: Maven 3.0.x, OS: Fedora17
[color=red][b]1.创建父工程,并配置[/b][/color]
[color=blue]mvn archetype:create -DgroupId=com.pandy.p -DartifactId=PandyTest -DarchetypeArtifactId=maven-archetype-quickstart
cd PandyTest
rm -rf src[/color]
修改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>
[color=red][b]2.创建子工程,并做相应配置 [/b][/color]
[color=blue]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[/color]
[b]Web模块设定对C1工程的依赖[/b]:
<dependency>
<groupId>com.pandy.p</groupId>
<artifactId>PandyC1</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
[b]Web模块设定插件[/b],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>
[color=red][b]3.针对eclipse处理 [/b][/color]
退回到父目录:[color=blue]mvn eclipse:eclipse[/color] , 或者: [color=blue]mvn eclipse:eclipse -Dwtpversion=2.0[/color]
别人的命令:mvn eclipse:eclipse -Dwtpversion=1.0
记得不要缺少 -Dwtpversion=1.0,这个说明了eclipse认出它就是web项目,而不是简单的java项目,大家应该知道web项目在eclipse可以做一些其他的事情吧。
修改Webapp工程的.classpath文件,增加:[color=red]这个很重要[/color]
[color=blue]<classpathentry kind="src" path="src/main/java" including="**/*.java"/>[/color]
否则,导入之后,创建java的代码,总是在eclipse上位置现实不对。
倒入到eclipse, 导入的时候选择Maven已存在项目,选择父工程的根目录。
会看见导入三个工程,其中[color=blue]子工程有错误,点击错误右键,快速修复(Quick Fix)[/color],即可解决问题
如果要在eclipse中使用m2eclipse,需要使用:
mvn eclipse:m2eclipse
[color=red][b]4. 编译过程:[/b][/color]
到父工程右键--运行方式--Maven clean , Maven install。
这个过程
A: 自动会编译子模块C1,并打包成jar,
B: 自动会编译子模块C2,并打包成war, 并把C1生成的jar放到C2.war下面去。
[color=red][b]5. eclipse运行webapp工程:[/b][/color]
因为C2是Webapp的工程,所以必须跑到C2下面执行: [color=blue]jetty:run[/color]
[color=red][b]问题:如何实时调试C1的java代码[/b][/color]?
[color=darkblue][b]关于部署,可以参考[/b][/color]:[url]http://panyongzheng.iteye.com/blog/1866757[/url]
[color=red][b]其他:[/b][/color]
按照上面的带的三个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节点,已经定义了。