万达换票券 发表于 2017-2-10 07:57:58

[置顶]Maven多模块项目 eclipse热部署 Maven项目实现 tomcat热部署 .

  Maven 多模块项目在eclipse下面热部署,即你可以体验下无论你修改整个项目里面的任何模块的代码,都不需要用maven打包就可以看到效果,
  1、首先准备好创建一个maven多项目的代码,准备好一个eclipse,一个tomcat,什么java环境,tomcat环境我就不多说了,如果这个你都没有配好就别往下看了,
  2、创建项目,mvn archetype:generate -DgroupId=com.laoshuisheng -DartifactId=test -Dversion=1.0
  创建成功后到pom.xml文件里面修改pom.xml
view plaincopy to clipboardprint?


[*]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.laoshuisheng</groupId>  
[*]  <artifactId>test</artifactId>  
[*]  <version>1.0</version>  
[*]  <packaging>jar</packaging>  
[*]  
[*]  <name>test</name>  
[*]  <url>http://maven.apache.org</url>  
[*]  
[*]  <properties>  
[*]    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
[*]  </properties>  
[*]  
[*]  <dependencies>  
[*]    <dependency>  
[*]      <groupId>junit</groupId>  
[*]      <artifactId>junit</artifactId>  
[*]      <version>3.8.1</version>  
[*]      <scope>test</scope>  
[*]    </dependency>  
[*]  </dependencies>  
[*]</project>  

  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.laoshuisheng</groupId><artifactId>test</artifactId><version>1.0</version><packaging>jar</packaging><name>test</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency></dependencies></project>
  修改成下面
view plaincopy to clipboardprint?


[*]oject 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.laoshuisheng</groupId>  
[*]  <artifactId>test</artifactId>  
[*]  <version>1.0</version>  
[*]  <packaging>pom</packaging>  
[*]  
[*]  <name>test</name>  
[*]  <url>http://maven.apache.org</url>  
[*]  
[*]  <properties>  
[*]    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
[*]  </properties>  
[*]  
[*]  <dependencies>  
[*]    <dependency>  
[*]      <groupId>junit</groupId>  
[*]      <artifactId>junit</artifactId>  
[*]      <version>3.8.1</version>  
[*]      <scope>test</scope>  
[*]    </dependency>  
[*]  </dependencies>  
[*]</project>  

  oject 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.laoshuisheng</groupId><artifactId>test</artifactId><version>1.0</version><packaging>pom</packaging><name>test</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency></dependencies></project>
  进入该项目里面随便的创建几个模块,我这里为了方便就只创建两个模块好了,一个就是web项目,一个就是业务代码
  .../test>mvn archetype:generate -DgroupId=com.laoshuisheng.web -DartifactId=test-web -DarchetypeArti
factId=maven-archetype-webapp -Dversion=1.0
  创建业务代码项目:
  ../test>mvn archetype:generate -DgroupId=com.laoshuisheng.web -DartifactId=test-core -DarchetypeArt
ifactId=maven-archetype-quickstart -Dversion=1.0
  创建完了如下图:

  好了现在项目创建成功了,我们要导入eclipse了,这一步也很重要,大家要细心的看了,导入的命令是:
  ...test>mvn eclipse:eclipse -Dwtpversion=1.0
  记得不要缺少 -Dwtpversion=1.0,这个说明了eclipse认出它就是web项目,而不是简单的java项目,大家应该知道web项目在eclipse可以做一些其他的事情吧。

  记住了,是一般的导入,不是maven项目导入。

  看见了吧,项目上面有个小地球,证明eclipse认出了他就是java web项目,没错,就是这个会给我们意想不到的效果
  接下来我们去把项目依赖加上,在web项目想的pom.xml里面增加对test-core的依赖:
view plaincopy to clipboardprint?


[*]<?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>  
[*]        <artifactId>test</artifactId>  
[*]        <groupId>com.laoshuisheng</groupId>  
[*]        <version>1.0</version>  
[*]    </parent>  
[*]    <groupId>com.laoshuisheng.web</groupId>  
[*]    <artifactId>test-web</artifactId>  
[*]    <version>1.0</version>  
[*]    <packaging>war</packaging>  
[*]    <name>test-web Maven Webapp</name>  
[*]    <url>http://maven.apache.org</url>  
[*]    <dependencies>  
[*]        <dependency>  
[*]            <groupId>junit</groupId>  
[*]            <artifactId>junit</artifactId>  
[*]            <version>3.8.1</version>  
[*]            <scope>test</scope>  
[*]        </dependency>  
[*]        <dependency>  
[*]            <groupId>com.laoshuisheng.web</groupId>  
[*]            <artifactId>test-core</artifactId>  
[*]            <version>1.0</version>  
[*]        </dependency>  
[*]    </dependencies>  
[*]    <build>  
[*]        <finalName>test-web</finalName>  
[*]    </build>  
[*]</project>  

  <?xml version="1.0"?><projectxsi: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><artifactId>test</artifactId><groupId>com.laoshuisheng</groupId><version>1.0</version></parent><groupId>com.laoshuisheng.web</groupId><artifactId>test-web</artifactId><version>1.0</version><packaging>war</packaging><name>test-web Maven Webapp</name><url>http://maven.apache.org</url><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><dependency><groupId>com.laoshuisheng.web</groupId><artifactId>test-core</artifactId><version>1.0</version></dependency></dependencies><build><finalName>test-web</finalName></build></project>
  加上后重新 mvn eclipse:clean eclipse:eclipse下刷新下eclipse的项目发现:

  这个时候就该配置到服务器上了,因为虽然说是maven项目,但是我们开发的时候服务器运行的是web下面的classes下的class文件和
  webapp下面的jsp/css/js等文件,所以并不需要打包什么的,如果是依赖如test-core也是跑classes的编译后的代码,所以也不需要编译,但是这里有个问题就是配置文件必须是放在classpath下面,因为我刚刚已经说了原因了,,废话不多说,看是配服务器,

  这就是我们创建好的tomcat服务器,然后大家点下   右键 ,记得是在服务器上点右键哦, 弹出个对话框,不错是弹出对话框,上面有个add and remove 的选项,你会发现有个test-web

  看到了吧,知道为啥我一定要强调导入项目的时候加上 -Dwtpversion=1.0了吧,这样他才能在这里加,eclipse才认出它就是web项目,哈哈,到这里基本上已经成功了,看看加后的效果:

  不错~不错,看到这样说明连带依赖的模块也成功加载了,嗯,现在你可以随意的修改这两块里面的代码,都不需要重新打包、不需要重新启动服务器,因为我们是运用了eclipse的热编译来实现的,或者说我们根本不需要在开发中运用到maven的功能。
  好了。到这里maven的多模块的项目的热编译就完全可以无任何插件的实现了,如果大家有什么不明白的直接来问我,谢谢!
  转自:http://blog.csdn.net/laoshuisheng/article/details/6420003
页: [1]
查看完整版本: [置顶]Maven多模块项目 eclipse热部署 Maven项目实现 tomcat热部署 .