上一篇,说到了ant脚本如何分类以及何组织的,现在让我们看一下如何用ant脚本来实现上面所说的过程。
在此同样先是基于小的分类,然后是整合,其实按照上篇的分类是非常细的,因此对于一些经常用到的,在此就不赘述,仅说几个我认为不常用的,但是很关键的。
首先定义weblogic的相关ant任务
<path id="weblogic.ant.classpath">
<pathelement location="${weblogic.home}/server/lib/weblogic.jar"/>
</path>
<target name="init" description="">
<taskdef name="wlserver" classname="weblogic.ant.taskdefs.management.WLServer" classpathref="weblogic.ant.classpath"/>
<taskdef name="wlconfig" classname="weblogic.ant.taskdefs.management.WLConfig" classpathref="weblogic.ant.classpath"/>
<taskdef name="wldeploy" classname="weblogic.ant.taskdefs.management.WLDeploy" classpathref="weblogic.ant.classpath"/>
</target>
然后是weblogic的启动与关闭,此处并没有通过wlserver来进行weblogic的startup与shutdown,是因为在我的环境里面总是报错,实在解决不了了,就采用了如下的这种替代方案。
<target name="startup">
<echo message="-------- Starting Weblogic AdminServer -----"/>
<exec executable="${domain.home}/startWebLogic.sh">
</exec>
<echo message="${weblogic.adminserver} started successfully"/>
</target>
<target name="shutdown">
<echo message="-------- Shutdown Weblogic server --------"/>
<exec executable="${domain.home}/bin/stopWebLogic.sh"/>
<sleep seconds="20" />
</target>
接下来是创建weblogic domain
<target name="create-test" depends="init" description="">
<echo message="--------Create weblogic domain:odmain--------"/>
<delete dir="${domain.home}"/>
<mkdir dir="${domain.home}"/>
<wlserver
weblogichome="${weblogic.home}"
username="${weblogic.username}"
password="${weblogic.password}"
servername="${weblogic.adminserver}"
domainname="${domain.name}"
dir="${domain.home}"
host="${weblogic.host}"
port="${weblogic.port.adminserver}"
productionmodeenabled="${weblogic.domain.isproduction}"
generateconfig="true"
classpath="${weblogic.jar}:${java.home}/lib/tools.jar">
<jvmarg value="-server"/>
<jvmarg value="-Xms256m"/>
<jvmarg value="-Xmx512m"/>
<jvmarg value="-XX:PermSize=128m"/>
<jvmarg value="-XX:MaxPermSize=256m"/>
<jvmarg value="-Dweblogic.ThreadPoolPercentSocketReaders=75"/>
<jvmarg value="-Djava.security.policy=${weblogic.home}/server/lib/weblogic.policy"/>
</wlserver>
</target>
再接下来是配置weblogic domain
<target name="config-test" description="Add Weblogic domain DB/JMS configurations" depends="init">
<echo message="--------Config all domain weblogic resources--------"/>
<mkdir dir="${domain.home}/rmfilestore"/>
<wlconfig url="t3://${weblogic.host}:${weblogic.port.adminserver}"
username="${weblogic.username}" password="${weblogic.password}">
<query domain="${domain.name}" type="Server" name="${weblogic.adminserver}"
property="prop.admin.server"/>
<create type="JDBCConnectionPool" name="Test Connection Pool">
<set attribute="InitialCapacity" value="${jdbc.pool.initcapacity}"/>
<set attribute="CapacityIncrement" value="1"/>
<set attribute="MaxCapacity" value="${jdbc.pool.maxcapacity}"/>
<set attribute="DriverName" value="${oracle.driver}"/>
<set attribute="Properties" value="user=${oracle.username.core2}"/>
<set attribute="Password" value="${oracle.password.core2}"/>
<set attribute="TestConnectionsOnRelease" value="true"/>
<set attribute="TestConnectionsOnReserve" value="true"/>
<set attribute="URL" value="${oracle.url.core2}"/>
<set attribute="TestTableName" value="SQL SELECT 1 FROM DUAL"/>
<set attribute="Targets" value="${prop.admin.server}"/>
</create>
<create type="JDBCDataSource" name="Test Data Source">
<set attribute="JNDIName" value="${datasource.jndi.name.test}"/>
<set attribute="PoolName" value="Test Connection Pool"/>
<set attribute="Targets" value="${prop.admin.server}"/>
</create>
<create type="JMSConnectionFactory" name="jms/JMSConnectionFactory">
<set attribute="JNDIName" value="jms/JMSConnectionFactory"/>
<set attribute="XAConnectionFactoryEnabled" value="true"/>
<set attribute="DefaultDeliveryMode" value="Non-Persistent"/>
<set attribute="Targets" value="${prop.admin.server}"/>
</create>
<create type="JMSFileStore" name="TestPersistentStore" property="test.persist.store">
<set attribute="Directory" value="rmfilestore"/>
</create>
<create type="JMSFileStore" name="TestPagingStore" property="test.paging.store">
<set attribute="Directory" value="rmfilestore"/>
</create>
<create type="JMSServer" name="TestJMSServer">
<set attribute="Store" value="${test.persist.store}"/>
<set attribute="PagingStore" value="${test.paging.store}"/>
<set attribute="Targets" value="${prop.admin.server}"/>
<create type="JMSQueue" name="jms/TestQueue">
<set attribute="JNDIName" value="jms/TestQueue"/>
</create>
<create type="JMSTopic" name="jms/TestTopic">
<set attribute="JNDIName" value="jms/TestTopic"/>
</create>
</create>
</wlconfig>
</target>
再接下来是deploy
<target name="deploy-test" depends="init">
<echo message="--------Deploy all odomain war and ear--------"/>
<wldeploy action="deploy" source="${deploy.package.folder}/od/test.ear" name="test"
user="${weblogic.username}" password="${weblogic.password}" verbose="true" nostage="false"
adminurl="t3://${weblogic.host}:${weblogic.port.adminserver}" targets="${weblogic.adminserver}">
</wldeploy>
<wldeploy action="deploy" source="${deploy.package.folder}/od/portal.war" name="portal"
user="${weblogic.username}" password="${weblogic.password}" verbose="true" nostage="false"
adminurl="t3://${weblogic.host}:${weblogic.port.adminserver}" targets="${weblogic.adminserver}"/>
</target>
最后,findbugs
<target name="findbugs-test">
<findbugs home="${findbugs.home}"
output="xml"
outputFile="findbugs/findbugs-test.xml"
jvmargs="-Xmx640m">
<auxClasspath>
<fileset dir="${basedir}/test/lib" >
<include name="*.jar" />
</fileset>
<pathelement location="${emma.jar}" />
</auxClasspath>
<sourcePath path="${basedir}/test/src" />
<class location="${basedir}/test/dist/classes" />
</findbugs>
</target>
有了这些分类以后,我们就简单得按照上篇的想法来进行组织,此处仅组织持续集成的,项目创建的根据各自不同可以自己组织。
<target name="ci" description="">
<antcall target="build-test"/>
<antcall target="findbugs-test"/>
<parallel>
<antcall target="startup"/>
<sequential>
<waitfor maxwait="5" maxwaitunit="minute" checkevery="1" checkeveryunit="second">
<http url="http://${weblogic.host}:${weblogic.port.adminserver}"/>
</waitfor>
<antcall target="integration-test"/>
<antcall target="shutdown"/>
</sequential>
</parallel>
</target>