1. jvmarg 用法
使用jvmarg,可以设置java虚拟机的环境。
下面的代码是svn上的一个配置文件
<project name="digitaliser-dk"> <path id="ant.classpath"> <pathelement path="${tomcat.home}" /> <fileset dir="lib"> <include name="**/*.jar" /> </fileset> </path> <taskdef resource="net/sf/antcontrib/antcontrib.properties" classpathref="ant.classpath" /> <property environment="env" /> <echo message="looking for host specific properties file host_${env.COMPUTERNAME}.properties" /> <property file="host_${env.COMPUTERNAME}.properties" /> <echo message="looking for host specific properties file host_${env.AMAZON_SERVER}.properties" /> <property file="host_${env.AMAZON_SERVER}.properties" /> <property file="build.properties" /> <condition property="personal.config.isset"> <isset property="personal.config" /> </condition> <if> <equals arg1="${personal.config.isset}" arg2="true" /> <then> <property file="config_${personal.config}.properties" /> <echo>Using propertyfile config_${personal.config}.properties</echo> </then> <else> <property file="config.properties" /> <echo>Using propertyfile config.properties</echo> </else> </if> <property name="solr.plugins.jar" value="../solr-plugins/target/solr-plugins-1.0-SNAPSHOT.jar" /> <property name="resource.repository.dir" value="${basedir}/../resource-repository" /> <property name="mvn.executable" value="mvn" /> <property name="tomcat.port" value="8080" /> <property name="war.file.name" value="resourcerepository-1.0-SNAPSHOT.war" /> <property name="resourcerepository.war.file" value="${resource.repository.dir}/target/${war.file.name}" /> <macrodef name="maven"> <attribute name="dir" default="." /> <attribute name="args" default="" /> <sequential> <echo message="running mvn @{args}" /> <if> <os family="windows" /> <then> <exec executable="cmd.exe" dir="@{dir}"> <arg line="/c mvn.bat @{args}" /> </exec> </then> <else> <exec executable="mvn" dir="@{dir}"> <arg line="@{args}" /> </exec> </else> </if> </sequential> </macrodef> <target name="redeploy"> <antcall target="stop-tomcat" /> <deploy /> <antcall target="start-tomcat" /> </target> <macrodef name="deploy"> <attribute name="context" default="ROOT" /> <attribute name="war" default="${resource.repository.dir}/target/resourcerepository-1.0-SNAPSHOT.war" /> <sequential> <echo message="deploying @{war} to context @{context}" /> <delete dir="${tomcat.home}/webapps/@{context}" /> <delete file="${tomcat.home}/webapps/@{context}.war" /> <mkdir dir="target/tomcat" /> <copy file="${tomcat.context}" tofile="target/tomcat/@{context}.xml" /> <replace file="target/tomcat/@{context}.xml" token="WAR_FILE_NAME" value="@{war}" /> <copy file="target/tomcat/@{context}.xml" tofile="${tomcat.home}/conf/Catalina/localhost/@{context}.xml" /> <antcall target="configure-solr" /> </sequential> </macrodef> <target name="deploy-snapshot"> <if> <isset property="db.snapshot.source.file" /> <then /> <else> <property name="db.snapshot.source.file" value="${default.db.snapshot.source.file}" /> </else> </if> <antcall target="stop-tomcat" /> <echo message="dropping old database..." /> <exec executable="${postgresql.dropdb}" failonerror="false"> <arg value="--username=postgres" /> <arg value="digitaliserdk" /> </exec> <echo message="using snapshot file ${db.snapshot.source.file}" /> <echo message="restoring database..." /> <exec executable="${postgresql.pg_restore}"> <arg value="--username=postgres" /> <arg value="--create" /> <arg value="--dbname=postgres" /> <!-- db name does not matter --> <arg value="${db.snapshot.source.file}" /> </exec> <antcall target="start-tomcat" /> </target> <target name="take-snapshot"> <exec executable="${postgresql.pg_dump}"> <arg value="--username=postgres" /> <arg value="--format=c" /> <arg value="-f" /> <arg value="target/db_snapshot.dump" /> <arg value="digitaliserdk" /> </exec> </target> <target name="start-tomcat-debug"> <if> <os family="windows" /> <then> <echo message="Starting Fedora as Windows service" /> <exec executable="sc.exe"> <env key="BUILD_ID" value="dontKillMe" /> <arg value="start" /> <arg value="resourcerepository" /> </exec> </then> <else> <echo message="Starting Fedora using bootstrap.jar" /> <java jar="${tomcat.home}/bin/bootstrap.jar" fork="true"> <env key="BUILD_ID" value="dontKillMe" /> <jvmarg value="-Dcatalina.home=${tomcat.home}" /> <jvmarg value="-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager" /> <jvmarg value="-Djava.util.logging.config.file=${tomcat.home}/conf/logging.properties" /> <jvmarg value="-Dnet.sourceforge.cobertura.datafile=${cobertura.datafile}" /> <jvmarg value="-Xdebug" /> <jvmarg value="-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=1044" /> <!-- <jvmarg value="-Xbootclasspath/p:/home/ahj/Trifork-Profiler/lib/instrumentation-1.6.0_13-b03.jar" /> --> <jvmarg value="-XX:MaxPermSize=512m" /> </java> </else> </if> <sleep seconds="5" /> </target> <target name="start-tomcat"> <if> <os family="windows" /> <then> <echo message="Starting Fedora as Windows service" /> <exec executable="sc.exe"> <env key="BUILD_ID" value="dontKillMe" /> <arg value="start" /> <arg value="resourcerepository" /> </exec> </then> <else> <echo message="Starting Fedora using bootstrap.jar" /> <echo message="Running: ${tomcat.home}/bin/bootstrap.jar -Dcatalina.home=${tomcat.home} -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file=${tomcat.home}/conf/logging.properties -Dnet.sourceforge.cobertura.datafile=${cobertura.datafile}" /> <java jar="${tomcat.home}/bin/bootstrap.jar" fork="true" spawn="true"> <env key="BUILD_ID" value="dontKillMe" /> <jvmarg value="-Dcatalina.home=${tomcat.home}" /> <jvmarg value="-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager" /> <jvmarg value="-Djava.util.logging.config.file=${tomcat.home}/conf/logging.properties" /> <jvmarg value="-Dnet.sourceforge.cobertura.datafile=${cobertura.datafile}" /> </java> </else> </if> <sleep seconds="5" /> </target> <target name="stop-tomcat"> <if> <os family="windows" /> <then> <echo message="Stopping Fedora as Windows service" /> <exec executable="sc.exe"> <arg value="stop" /> <arg value="resourcerepository" /> </exec> </then> <else> <echo message="Stopping Fedora using bootstrap.jar" /> <java jar="${tomcat.home}/bin/bootstrap.jar" fork="true"> <jvmarg value="-Dcatalina.home=${tomcat.home}" /> <arg line="stop" /> </java> <sleep seconds="5" /> </else> </if> </target> <target name="reindex"> <property name="reindex.url" value="http://localhost:${tomcat.port}/rest/admin/index" /> <get src="${reindex.url}" dest="target/index.log" username="admin" password="oioadmin" /> </target> <target name="clean"> <delete file="${cobertura.datafile}" /> <delete dir="target" /> <delete dir="../integration-tests/target" /> <delete dir="../resource-repository/target" /> </target> <target name="build" depends="clean"> <mkdir dir="target" /> <attribute name="context" default="ROOT" /> <attribute name="war" default="${resource.repository.dir}/target/resourcerepository-1.0-SNAPSHOT.war" /> <copy file="${tomcat.context}" tofile="target/tomcat/@{context}.xml" /> <replace file="target/tomcat/@{context}.xml" token="WAR_FILE_NAME" value="@{war}" /> <maven dir=".." args="install -Dmaven.test.skip=true" /> </target> <target name="build-and-deploy" depends="build"> <antcall target="redeploy" /> </target> <target name="configure-solr"> <mkdir dir="${solr.home}/lib" /> <copy todir="${solr.home}/lib" overwrite="true"> <fileset file="${solr.plugins.jar}" /> </copy> <copy todir="${solr.home}/conf" overwrite="true"> <fileset dir="${resource.repository.dir}/resources/solr-config" /> </copy> <replace file="${solr.home}/conf/solrconfig.xml" token="<dataDir>INDEX_DIR</dataDir>" value="<dataDir>${solr.indexDir}</dataDir>" /> </target> <target name="coverage" depends="build"> <path id="cobertura.classpath"> <fileset dir="${cobertura.dir}"> <include name="cobertura.jar" /> <include name="lib/**/*.jar" /> </fileset> </path> <taskdef classpathref="cobertura.classpath" resource="tasks.properties" /> <property name="cobertura.datafile" value="${basedir}/target/cobertura.ser" /> <echo message="cobertura datafile: ${cobertura.datafile}" /> <property name="instrumented.war.file" value="target/${war.file.name}" /> <maven dir="${resource.repository.dir}" args="cobertura:cobertura" /> <cobertura-instrument todir="target" datafile="${cobertura.datafile}"> <ignore regex="dk.itst.oio.katalog.resourcerepository.jaxws.*" /> <ignore regex="dk.itst.oio.katalog.resourcerepository.management.*" /> <includeClasses regex="dk.itst.oio.*" /> <instrumentationClasspath> <pathelement location="${resourcerepository.war.file}" /> </instrumentationClasspath> </cobertura-instrument> <antcall target="stop-tomcat" /> <copy file="${cobertura.dir}/cobertura.jar" todir="${tomcat.home}/lib" /> <deploy war="${instrumented.war.file}" context="ROOT" /> <antcall target="start-tomcat" /> <antcall target="reindex" /> <maven dir="../integration-tests" args="integration-test" /> <get src="http://localhost:${tomcat.port}/rest/admin/flushcobertura" dest="target/flushcobertura.log" username="admin" password="oioadmin" /> <antcall target="stop-tomcat" /> <antcall target="start-tomcat" /> <cobertura-merge datafile="target/cobertura-merged.ser"> <fileset file="${cobertura.datafile}" /> <fileset file="${resource.repository.dir}/target/cobertura/cobertura.ser" /> </cobertura-merge> <cobertura-report datafile="target/cobertura-merged.ser" format="html" destdir="target/cobertura-report" srcdir="${resource.repository.dir}/src/main/java" /> <cobertura-report datafile="target/cobertura-merged.ser" format="xml" destdir="target/cobertura-report" srcdir="${resource.repository.dir}/src/main/java" /> </target> </project>
2.-Xbootclasspath 在cobertura中的使用方法
下面的代码是cobertura,ant配置文件中的一个片段。为了这个功能找了好久,使用jvmarg标签给java虚拟机加载jar
<!-- 单元测试 -->
<target name="test" depends="init,compile,copyxml">
<junit fork="true" dir="${basedir}" failureProperty="test.failed">
<!--
Note the classpath order: instrumented classes are before the
original (uninstrumented) classes. This is important.
-->
<jvmarg value="-Xbootclasspath/p:${project.lib.dir}/utmp/STAR-1.0.jar" />
<classpath location="${instrumented.dir}" />
<classpath location="${classes.dir}" />
<!--
The instrumented classes reference classes used by the
Cobertura runtime, so Cobertura and its dependencies
must be on your classpath.
-->
<classpath refid="project.classpath" />
<classpath refid="cobertura.classpath" />
<formatter type="xml" />
<test name="${testcase}" todir="${reports.xml.dir}" if="testcase" />
<batchtest todir="${reports.xml.dir}" unless="testcase">
<fileset dir="${testsrc.dir}">
<!-- 2.增加测试用例类,可以参考下面的例子写 -->
<!--
-->
<!-- 100% 行覆盖率
<include name="com/cvicse/inforguard/utmp/analysis/storage/machineroom/service/impl/ViewSrvImplTest.java" />
-->
<include name="com/cvicse/inforguard/utmp/analysis/storage/asset/service/impl/AssetWizardSrvImplTest.java" />
</fileset>
</batchtest>
</junit>
<junitreport todir="${reports.xml.dir}">
<fileset dir="${reports.xml.dir}">
<include name="TEST-*.xml" />
</fileset>
<report format="frames" todir="${reports.html.dir}" />
</junitreport>
</target>