首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

《Maven实战》学习札记(6)-部署

2012-07-16 
《Maven实战》学习笔记(6)--部署一。远程部署1.使用cargo-maven2-plugin插件build??? ??? finalNamehello

《Maven实战》学习笔记(6)--部署

一。远程部署

1.使用cargo-maven2-plugin插件

<build>
??? ??? <finalName>hello</finalName>??? <!-- war包名称 -->
??? ??? <plugins>??? ??? ???
??? ??? ??? <plugin>
??? ??? ??? ??? <!-- 指定插件名称及版本号 -->
??? ??? ??? ??? <groupId>org.codehaus.cargo</groupId>
??? ??? ??? ??? <artifactId>cargo-maven2-plugin</artifactId>
??? ??? ??? ??? <version>1.0.3</version>

??? ??? ??? ??? <!-- 插件的Tomcat6.x配置 -->
??? ??? ??? ??? <configuration>
??? ??? ??? ??? ??? <wait>true</wait>?? <!--是否说明,操作start、stop等后续操作必须等前面操作完成才能继续 -->
??? ??? ??? ??? ??? <container>???????? <!-- 容器的配置 -->
??? ??? ??? ??? ??? ??? <containerId>tomcat6x</containerId> <!-- 指定tomcat版本,tomcat7x不支持远程部署,可以为tomcat5x,tomcat6x -->
??? ??? ??? ??? ??? ??? <type>remote</type>? <!-- 指定类型:表示远程部署-->
??? ??? ??? ??? ??? </container>

??? ??? ??? ??? ??? <configuration>?????????? <!-- 具体的配置 -->
??? ??? ??? ??? ??? ??? <type>runtime</type>?? <!-- 类型,tomcat运行时 -->
??? ??? ??? ??? ??? ??? <properties> <!-- 配置属性 -->
??? ??? ??? ??? ??? ??? <cargo.tomcat.manager.url>http://localhost:8080/manager</cargo.tomcat.manager.url> <!-- 管理地址 -->
??? ??? ??? ??? ??? ??? <cargo.remote.username>admin</cargo.remote.username>???????? <!-- Tomcat用户名 -->
??? ??? ??? ??? ??? ??? <cargo.remote.password>admin</cargo.remote.password>???????? <!-- Tomcat密码 -->
??? ??? ??? ??? ??? ??? </properties>
??? ??? ??? ??? ??? </configuration>
??? ??? ??? ??? </configuration>

??? ??? ??? ??? <executions>
??? ??? ??? ??? ??? <!-- 执行的动作,如果不指定,可以执行执行mvn cargo:redeploy ,需要在settings.xml中声明groupId? -->
??? ??? ??? ??? ??? <execution>
??? ??? ??? ??? ??? ??? <id>verify-deployer</id>
??? ??? ??? ??? ??? ??? <phase>install</phase>? <!-- 关联到install阶段,执行mvn install即可完成部署 -->
??? ??? ??? ??? ??? ??? <goals>
??? ??? ??? ??? ??? ??? ??? <goal>redeploy</goal><!-- 实际调用cargo的redeploy目标 -->
??? ??? ??? ??? ??? ??? </goals>
??? ??? ??? ??? ??? </execution>
??? ??? ??? ??? </executions>
??? ??? ??? </plugin>

??? ??? </plugins>
??? </build>

?

?

二。本地部署

1.使用cargo-maven2-plugin插件
1.1standalone
启动命令:mvn install
<build>
??? ??? <finalName>hello</finalName>
??? ???
??? ??? <plugins>
??? ??? ??? <plugin>
??? ??? ??? ??? <!-- 指定插件名称及版本号 -->
??? ??? ??? ??? <groupId>org.codehaus.cargo</groupId>
??? ??? ??? ??? <artifactId>cargo-maven2-plugin</artifactId>
??? ??? ??? ??? <version>1.0.3</version>

??? ??? ??? ??? <!-- 插件的Tomcat6.x配置 -->
??? ??? ??? ??? <configuration>
??? ??? ??? ??? ??? <wait>true</wait>?????? <!--是否说明,操作start、stop等后续操作必须等前面操作完成才能继续 -->
??? ??? ??? ??? ??? <container>????????? <!-- 容器的配置 -->
??? ??? ??? ??? ??? ??? <containerId>tomcat7x</containerId>???????? <!-- 指定tomcat版本 -->??? ??? ??? ????????????????????????
??? ??? ??? ??? ??? ??? <home>F:\maven_workspace\apache-tomcat-7.0.5-windows-x86\apache-tomcat-7.0.5</home>??? <!-- 指定Tomcat的位置 -->
??? ??? ??? ??? ??? </container>

??? ??? ??? ??? ??? <configuration>??? <!-- 具体的配置 -->
??? ??? ??? ??? ??? ??? <type>standalone</type> <!-- 类型,standalone:复制tomcat配置到指定位置???? existing:直接部署到tomcat的webapps -->
??? ??? ??? ??? ??? ??? <home>${project.build.directory}/tomcat7</home>?? <!--? 指定复制Tomcat到什么位置,真正的工作目录? -->
??? ??? ??? ??? ??? ??? <properties> <!-- 配置属性 -->
??? ??? ??? ??? ??? ??? ??? <cargo.servlet.port>8081</cargo.servlet.port>???? <!-- 端口 -->
??? ??? ??? ??? ??? ??? </properties>
??? ??? ??? ??? ??? ??? ??? ???
??? ??? ??? ??? ??? </configuration>

??? ??? ??? ??? </configuration>

??? ??? ??? ??? <executions>
??? ??? ??? ??? ??? <!-- 执行的动作,如果不指定,可以执行执行mvn cargo:start,需要在settings.xml中声明groupId? -->
??? ??? ??? ??? ??? <execution>
??? ??? ??? ??? ??? ??? <id>verify-deployer</id>
??? ??? ??? ??? ??? ??? <phase>install</phase>????? <!-- 关联到install阶段,执行mvn install即可完成部署 -->
??? ??? ??? ??? ??? ??? <goals>
??? ??? ??? ??? ??? ??? ??? <goal>start</goal>????? <!-- 实际调用cargo的start目标 -->
??? ??? ??? ??? ??? ??? </goals>
??? ??? ??? ??? ??? </execution>
??? ??? ??? ??? </executions>
??? ??? ??? </plugin>

??? ??? </plugins>
??? </build>
1.2existing
启动命令:mvn install
<build>
??? ??? <finalName>hello</finalName>
??? ???
??? ??? <plugins>
??? ??? ??? <plugin>
??? ??? ??? ??? <!-- 指定插件名称及版本号 -->
??? ??? ??? ??? <groupId>org.codehaus.cargo</groupId>
??? ??? ??? ??? <artifactId>cargo-maven2-plugin</artifactId>
??? ??? ??? ??? <version>1.0.3</version>

??? ??? ??? ??? <!-- 插件的Tomcat6.x配置 -->
??? ??? ??? ??? <configuration>
??? ??? ??? ??? ??? <wait>true</wait>?????? <!--是否说明,操作start、stop等后续操作必须等前面操作完成才能继续 -->
??? ??? ??? ??? ??? <container>????????? <!-- 容器的配置 -->
??? ??? ??? ??? ??? ??? <containerId>tomcat7x</containerId>???????? <!-- 指定tomcat版本 -->??? ??? ??? ????????????????????????
??? ??? ??? ??? ??? ??? <home>F:\maven_workspace\apache-tomcat-7.0.5-windows-x86\apache-tomcat-7.0.5</home>??? <!-- 指定Tomcat的位置 -->
??? ??? ??? ??? ??? </container>

??? ??? ??? ??? ??? <configuration>??? <!-- 具体的配置 -->
??? ??? ??? ??? ??? ??? <type>standalone</type> <!-- 类型,standalone:复制tomcat配置到指定位置???? existing:直接部署到tomcat的webapps -->
??? ??? ??? ??? ??? ??? <home>F:\maven_workspace\apache-tomcat-7.0.5-windows-x86\apache-tomcat-7.0.5</home>??? <!-- 指定Tomcat的位置 -->

??? ??? ??? ??? ??? ???
??? ??? ??? ??? ??? ??? ??? ???
??? ??? ??? ??? ??? </configuration>

??? ??? ??? ??? </configuration>

??? ??? ??? ??? <executions>
??? ??? ??? ??? ??? <!-- 执行的动作,如果不指定,可以执行执行mvn cargo:start,需要在settings.xml中声明groupId? -->
??? ??? ??? ??? ??? <execution>
??? ??? ??? ??? ??? ??? <id>verify-deployer</id>
??? ??? ??? ??? ??? ??? <phase>install</phase>????? <!-- 关联到install阶段,执行mvn install即可完成部署 -->
??? ??? ??? ??? ??? ??? <goals>
??? ??? ??? ??? ??? ??? ??? <goal>start</goal>????? <!-- 实际调用cargo的start目标 -->
??? ??? ??? ??? ??? ??? </goals>
??? ??? ??? ??? ??? </execution>
??? ??? ??? ??? </executions>
??? ??? ??? </plugin>

??? ??? </plugins>
??? </build>

2.使用jetty-maven-plugin
启动命令:mvn install
??? <build>
??? ??? <finalName>hello</finalName>
??? ???
??? ??? <plugins>
??? ???
??? ??? ??? <plugin>
??? ??? ??? ??? <groupId>org.mortbay.jetty</groupId>
??? ??? ??? ??? <artifactId>jetty-maven-plugin</artifactId>
??? ??? ??? ??? <version>8.0.4.v20111024</version>
??? ??? ??? ??? <configuration>
??? ??? ??? ??? ??? <scanIntervalSeconds>10</scanIntervalSeconds>
??? ??? ??? ??? ??? <webAppConfig>
??? ??? ??? ??? ??? ??? <contextPath>/test</contextPath><!-- 测试时没有起作用,只能通根路径访问,既localhost:8080,应该是maven版本的问题,maven2下使用其它版本的jetty-maven-plugin,构建时会报错,maven3下使用该插件没有问题 -->
??? ??? ??? ??? ??? </webAppConfig>
??? ??? ??? ??? </configuration>
??? ??? ??? ???
??? ??? ??? ??? <executions>
??? ??? ??? ??? ??? <!-- 执行的动作 -->
??? ??? ??? ??? ??? <execution>
??? ??? ??? ??? ??? ??? <id>verify-deployer</id>
??? ??? ??? ??? ??? ??? <phase>install</phase>?????
??? ??? ??? ??? ??? ??? <goals>
??? ??? ??? ??? ??? ??? ??? <goal>run</goal>
??? ??? ??? ??? ??? ??? </goals>
??? ??? ??? ??? ??? </execution>
??? ??? ??? ??? </executions>
??? ??? ??? </plugin>
??? ??? ???
??? ??? </plugins>
??? </build>

以上插件如果要在命令行执行其各自的命令简写,需要在settings.xml中配置
??? <pluginGroups>
??? ??? <pluginGroup>org.mortbay.jetty</pluginGroup>
??? ??? <pluginGroup>org.codehaus.cargo</pluginGroup>
??? </pluginGroups>

?

三。本地测试-tomcat热部署debug

?

maven tomcat eclipse debug 调试 + hot code replace 热部署

参考资料:http://jaymsimusic.iteye.com/blog/1160118

maven tomcat eclipse debug 调试 + hot code replace 热部署
文章分类:Java编程上网找了好久都没找到,网上只有运行mvn package 打包,并利用tomcat manager功能部署到tomcat 下的文章,如果我修改了一个java文件,不得不重新打包部署一下,这佯做开发效率极低,影响用maven的心情,经过自己的反复试验,终于可以实行hot code replace了,就是修改了一个文件不用重新部署了,就可以使用debug了。
试验环境:
eclipse+ tomcat+ tomcatPlugin+maven3 关于怎么安装,就得大家自己找文章了
我们用petclinic项目为大家演示热部署。petclinic是spring官方的一个非常经典的例子
petclinic svn路径 https://src.springframework.org/svn/spring-samples/petclinic/trunk
配置使用
1。修改tomcat的server.xml文件,加入

1
<Context docBase="E:\workspace\petclinic\src\main\webapp" path="/petclinic" reloadable="false"></Context>
意思是指定tomcat运行项目的目录,在这里我们指定E:\workspace\petclinic\src\main\webapp ,src\main\webapp 是maven web规范,存放java web的相关资源。
2。右键单击项目,选择build path,将default output folder设置为 petclinic/src/main/webapp/WEB-INF/classes,并将
petclinic/src/main/resource的Excluded设置为None,默认是**,意思是让eclipse编译java和resource文件编译到petclinic/src/main/webapp/WEB-INF/classes目录

3。 运行 mvn war:inplace petclinic/src/main/webapp/WEB-INF下,就有lib文件了
4。用eclipse插件运行tomcat,项目就可以运行了,修改任意一java类,可以实时生效,打断点也能够调试了。

注意:要关联tamcat下的jar包。

该配置可以 聚合项目的debug,在需要debug外部项目时手工关联即可。

举例:.classpath
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
??? <classpathentry kind="src" output="src/main/webapp/WEB-INF/classes" path="src/main/resources"/>
??? <classpathentry kind="src" output="src/main/webapp/WEB-INF/classes" path="src/main/java"/>
??? <classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
??? <classpathentry kind="src" output="target/test-classes" path="src/test/resources"/>
??? <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
??? <classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
??? <classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v7.0"/>
??? <classpathentry kind="output" path="target/classes"/>
</classpath>

但这样会有一个问题,就是如果有需要替换为maven属性的资源文件就不会被替换了。可以使用jetty-maven-plugin解决这个问题,不过就是path尚不好使。

所以,可以使用上面的配置方式与maven-antrun-plugin插件结合,进行资源文件的替换,各个环境,可以结合profile使用
也就是说,本地开发使用本地的资源进行开发和测试,其它环境在使用maven进行替换

<plugin>
??? ??? ??? ??? <groupId>org.apache.maven.plugins</groupId>
??? ??? ??? ??? <artifactId>maven-antrun-plugin</artifactId>
??? ??? ??? ??? <version>1.3</version>
??? ??? ??? ??? <executions>
??? ??? ??? ??? ??? <!-- 执行的动作 -->
??? ??? ??? ??? ??? <execution>
??? ??? ??? ??? ??? ??? <id>ant-copy</id>
??? ??? ??? ??? ??? ??? <phase>prepare-package</phase>????? <!-- 绑定到prepare-package 阶段-->
??? ??? ??? ??? ??? ??? <goals>
??? ??? ??? ??? ??? ??? ??? <goal>run</goal>
??? ??? ??? ??? ??? ??? </goals>
??? ??? ??? ??? ??? ??? <configuration>
??? ??? ??? ??? ??? ??? ??? <tasks> <!-- 调用ant的copy任务执行资源文件替换 -->
??? ??? ??? ??? ??? ??? ??? ??? <echo message="prepare-release run" />
??? ??? ??? ??? ??? ??? ??? ??? <copy todir="${project.build.directory}/classes/config" overwrite="true">
??? ??? ??? ??? ??? ??? ??? ??? ??? <fileset dir="${project.basedir}/config/release" />
??? ??? ??? ??? ??? ??? ??? ??? </copy>
??? ??? ??? ??? ??? ??? ??? ??? <echo message="prepare-release completed!" />
??? ??? ??? ??? ??? ??? ??? </tasks>
??? ??? ??? ??? ??? ??? </configuration>
??? ??? ??? ??? ??? </execution>
??? ??? ??? ??? ??? 可以定义多个动作
??? ??? ??? ??? </executions>
??? ??? ??? </plugin>

热点排行