`

Eclipse配置工程自动执行ant实现热部署

阅读更多

思路:配置 Eclipse 自动执行一个Ant脚本,在Ant脚本内自己定义热部署的步骤,这样在每个文件更新后就可以自动部署到服务的目录中了。

一、配置Eclipse 工程自动执行一个Ant 脚本。

1.右健项目工程--builders--import--选择当前项目下的build.xml,对于我现的工程,Ant 文件在D:\eclipse\workspace\工程名\plugins\hotcopy\hot-copy.xml

2.选中选择的build.xml--Edit--targets--Auto Build--set Targets--选择要自动build的targetName,对于我的工程,targetName为  工程名hot-copy.xml

3、配置build参数,在下面界面上可以看到

-Dresource_loc=${resource_loc}
-Dresource_path=${resource_path}
-Dresource_name=${resource_name}
-Dwar.deploy.dir=D:\jboss-4.0.2\server\default\deploy\bap.war

 

war.deploy.dir:表示服务器的路径,需要改变,其他三个参数只有原样复制即可。

 

其配置界面如下:

 

二、编写自动部署文件:D:\eclipse\workspace\工程名\plugins\hotcopy\hot-copy.xml

其内容如下:

<?xml version="1.0"?>
<project name="deploy">
         <!--引用了ant-contrib-1.0b3.jar包,需要放到当前目录\lib目录下-->
<path id="ant.contrib.lib">
		<fileset dir="">
			<include name="**/*jar" />
		</fileset>
	</path>
	<taskdef resource="net/sf/antcontrib/antcontrib.properties">
		<classpath>
			<path refid="ant.contrib.lib" />
		</classpath>
	</taskdef>
	<target name="deploy.hotcopy">
<!--resource_loc:需要传入的变量,在上的Eclipse图中可以看到,引用Eclipse的环境变量即可-->		<available property="is.present" file="${resource_loc}" />
		<available property="is.dir" file="${resource_loc}" type="dir" />
		<condition property="is.file">
			<and>
				<isset property="is.present" />
				<not>
					<isset property="is.dir" />
				</not>
			</and>
		</condition>
		<antcall target="deploy.hotcopy.webdocs" />
	</target>
	<target name="deploy.hotcopy.webdocs" if="is.file">
		<if>
	<!--只有当前的文件、文件夹的路径中存在webdocs才进行拷贝,只是根据工程目录规划决定的-->		
<contains string="${resource_path}" substring="\webdocs" />
			<then>
				<propertyregex property="webdocs-path" input="${resource_path}" regexp=".*\\webdocs(\\.*)\\${resource_name}" select="\1" casesensitive="true" />
				<if>
					<isset property="webdocs-path" />
					<then>
						<property name="target.dir" value="${war.deploy.dir}${webdocs-path}" />
					</then>
					<else>
						<property name="target.dir" value="${war.deploy.dir}" />
					</else>
				</if>
				<copy todir="${target.dir}" preservelastmodified="true" file="${resource_loc}" />
			</then>
		</if>
	</target>
</project>

 

对于一个普通的XML文件,打印出resource打的相关变量,有利于理解.

     [echo] resource_loc:D:\eclipse\workspace\工程名\src\webdocs\decorators\level-2-page-decorator.jsp
     [echo] resource_path:\工程名\src\webdocs\decorators\level-2-page-decorator.jsp
     [echo] resource_name:level-2-page-decorator.jsp

三、总结

做了上面的配置,Eclipse会检查每一个新修改的文件,看是否需要拷贝,所以Eclipse 的速度会变慢。所以实现的效果并不是太好,但是这种机制很好,Eclipse自动调用我们编写的Ant文件,可以执行任何操作。

  • 大小: 70.2 KB
分享到:
评论
2 楼 qingfeng825 2013-03-28  
shf675 写道
做了上面的配置,Eclipse会检查每一个新修改的文件
----  美女这个是可以设置的
Buid Options选项中勾选Specify working set of relevant resources 点击Specify Resources 选择工程指定目录,即选择的目录下文件修改后才自动执行Ant脚本

谢谢。
1 楼 shf675 2013-03-18  
做了上面的配置,Eclipse会检查每一个新修改的文件
----  美女这个是可以设置的
Buid Options选项中勾选Specify working set of relevant resources 点击Specify Resources 选择工程指定目录,即选择的目录下文件修改后才自动执行Ant脚本

相关推荐

Global site tag (gtag.js) - Google Analytics