ant etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
ant etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

7 Ekim 2011 Cuma

ant create database(sql) task

here is an ant target to create the specified database... unfortunately, sql does not close the connection properly..



<target name="create-db" >
<sql driver="org.postgresql.Driver" classpath="${build.lib}/postgresql-8.4-702.jdbc4.jar" url="jdbc:postgresql://localhost:5432/template1" userid="userid" password="password" autocommit="true" onerror="continue">


         CREATE DATABASE ${database.prefix}_mydatabase;
       </sql>
</target>

lowercase ant task

here is ant task to lowercase a property:


<target name="lowercase-NAME" depends="init">
    <taskdef name="stringutil" classname="ise.antelope.tasks.StringUtilTask">
<classpath path="${build.lib}/antelopetasks-3.2.10.jar" />
  </taskdef>
    <property name="NAME.lowercase" value="${NAME}" />
  <stringutil string="${NAME.lowercase}" property="SURUM.lowercase">
  <lowercase />
  </stringutil>
</target>

tomcat deploy ant task

here is tomcat deploy-undeploy targets:


<target name="tomcat-start">
<exec executable="${tomcat.dir}/bin/startup.sh">
<env key="CATALINA_PID" value="${tomcat.pidfile}" />
</exec>
<sleep seconds="2" />
</target>


<target name="tomcat-deploy" depends="war">
<taskdef resource="org/apache/catalina/ant/catalina.tasks" classpathref="tomcat.classpath" />
<deploy url="${tomcat.manager.url}" username="${tomcat.manager.username}" password="${tomcat.manager.password}" 
war="file:${build.dir}/${name}.war" path="/${name}}" failonerror="true" />
<sleep seconds="2" />
</target>


<target name="tomcat-undeploy">
<taskdef resource="org/apache/catalina/ant/catalina.tasks" classpathref="tomcat.classpath" />
<undeploy url="${tomcat.manager.url}" username="${tomcat.manager.username}" password="${tomcat.manager.password}" 
path="/${name}" failonerror="false" />
</target>


<target name="tomcat-stop" depends="tomcat-undeploy">
<exec executable="${tomcat.dir}/bin/shutdown.sh">
<env key="CATALINA_PID" value="${tomcat.pidfile}" />
</exec>
<delete file="${tomcat.pidfile}" />
</target>


13 Eylül 2011 Salı

proguard ant task

proguard (http://proguard.sourceforge.net/) promises for  the following:

  • Creating more compact code, for smaller code archives, faster transfer across networks, faster loading, and smaller memory footprints.
  • Making programs and libraries harder to reverse-engineer.
  • Listing dead code, so it can be removed from the source code.
  • Retargeting and preverifying existing class files for Java 6, to take full advantage of Java 6's faster class loading.

here is code sample:
<target name="proguard" depends="jar">
<taskdef resource="proguard/ant/task.properties" classpath="${lib}/proguard-4.4.jar" />
<proguard configuration="${config}/obfuscate.pro">
<injar file="${build.dir}/prod.jar" />
<outjar file="${build.dir}/prod_obf.jar" />
<libraryjar refid="externals" />
</proguard>
</target>

checkstyle ant task

checkstyle (http://checkstyle.sourceforge.net/) is a tool that hels java code to adhere to coding standards.

for checkstyle ant task: http://checkstyle.sourceforge.net/anttask.html



<property name="build.checkstyle.config" value="${config}/sun_checks.xml" />
<property name="build.checkstyle.output" value="${build.dir}/checkstyle_report.xml" />


<path id="checkstyle.classpath">
<pathelement location="${lib}/checkstyle-5.3.jar" />
<pathelement location="${lib}/google-collections-1.0.jar" />
<pathelement location="${lib}/commons-beanutils-core-1.8.3.jar" />
<pathelement location="${lib}/commons-logging-1.1.1.jar" />
<pathelement location="${lib}/antlr-2.7.7.jar" />
</path>


<target name="checkstyle" depends="init">
<taskdef resource="checkstyletask.properties" classpathref="checkstyle.classpath" />
<checkstyle failOnViolation="false" config="${build.checkstyle.config}">
<fileset dir="${src.dir}">
<include name="**/*.java" />
</fileset>
<formatter type="xml" tofile="${build.checkstyle.output}" />
</checkstyle>
</target>



about sun-checks.xml configuration file: http://checkstyle.sourceforge.net/config.html
here is a sample config file: http://code.google.com/p/checkstyle-idea/source/browse/trunk/src/main/resources/sun_checks.xml?r=120

cobertura ant task

cobertura (http://cobertura.sourceforge.net/) is a coverage analysis tool which reports what percentage of source code is covered by unit tests.

for cobertura ant task: http://cobertura.sourceforge.net/anttaskreference.html

and here is code sample:


<property name="build.coverage.datafile" value="${build.coverage}/cobertura.ser" />


<path id="instrument.classpath">
<pathelement location="${build.classes}" />
</path>


<path id="cobertura.classpath">
<pathelement location="cobertura-1.9.4.1.jar" />
<pathelement location="log4j-1.2.4.jar" />
<pathelement location="oro-2.0.8.jar" />
<pathelement location="asm-3.0.jar" />
<pathelement location="asm-tree-3.0.jar" />
</path>


<target name="instrument" depends="compile-test">
<taskdef classpathref="cobertura.classpath" resource="tasks.properties" />
<cobertura-instrument todir="${build.instrumented}" datafile="${build.coverage.datafile}">
<includeClasses regex=".*" />
<instrumentationClasspath>
<path refid="instrument.classpath" />
</instrumentationClasspath>
</cobertura-instrument>
</target>



<target name="test" depends="compile-test, instrument">
<junit ..;>
...
<sysproperty key="net.sourceforge.cobertura.datafile" file="${build.coverage.datafile}" />
..
</junit>
</target>

<target name="coverage" depends="test">
<taskdef classpathref="cobertura.classpath" resource="tasks.properties" />
<cobertura-report format="xml" destdir="${build.coverage}" srcdir="${src.dir}" datafile="${build.coverage.datafile}" />
<cobertura-check haltonfailure="false" datafile="${build.coverage.datafile}" />
</target>

12 Eylül 2011 Pazartesi

findbugs ant task

Findbugs is a static analysis tool that finds existing bugs in java code. Here is a link on how to use it from ant script: http://findbugs.sourceforge.net/manual/anttask.html

and code sample is as follows:



<property name="findbugs.home" value="/home/hilal.tarakci/Desktop/htarakci/environment/other/findbugs-1.3.9"/>
<target name="findbugs-define">
<taskdef name="findbugs" classname="edu.umd.cs.findbugs.anttask.FindBugsTask">
<classpath path="${findbugs.home}/lib/findbugs.jar" />
</taskdef>
<!-- ensure that findbugs taskdef is only run once, otw ant will error -->
<property name="findbugs.initialized" value="true" />
</target>
<target name="findbugs" if="findbugs.present" depends="findbugs-define, jar" description="Run findbugs">
<findbugs output="xml:withMessages" home="${findbugs.home}" effort="max" outputFile="${build.dir}/findbugs.report.xml">
<auxClasspath>
<fileset dir="${ivy.lib}">
<include name="**/*.jar" />
<exclude name="**/ivy-*.jar" />
</fileset>
</auxClasspath>
<sourcePath path="${src.dir}" />
<class location="${build.dir}/${final.name}.jar" />
</findbugs>
<xslt style="${findbugs.home}/src/xsl/fancy-hist.xsl" in="${build.dir}/findbugs.report.xml" out="${build.dir}/findbugs.report.html" />
</target>


25 Ağustos 2011 Perşembe

how to continue build with ant despite failure

I am aware of two ways to continue build process despite failure.. The first way is the easy one.. If the task that may fail has failonerror property, simply set it to false. However, not all tasks have that property. In those situations, subant task( http://ant.apache.org/manual/Tasks/subant.html) which is generally used for batch builds come into play. In order to use subant, you have to redesign your build file by moving targets which may-fail during build process in seperate build files. The target names that will be executed wihin subant in main build.xml must be identical..

Here is a subant example:


<subant failonerror="false">
            <fileset dir="." includes="**/build.xml" excludes="build.xml"/>
            <target name="clean"/>
            <target name="build"/>
        </subant>

By the way, if the execution order is important, filelist must be used instead of fileset as follows:


<subant failonerror="false">
<filelist dir="${parent.dir}">
    <file name="project1/build.xml"/>
    <file name="project2/bıild.xml"/>
    ...
    <file name="projectn/build.xml"/>
  </filelist>
            <target name="clean"/>
            <target name="build"/>
        </subant>




24 Ağustos 2011 Çarşamba

ant deploy to glassfish for service assemblies

The previous post is about ant deploy of web services to glassfish v2.1 application server. If you are curious, please read http://hilaltarakci.blogspot.com/2011/08/ant-deploy-to-glassfish.html.
This post is about deploying service assemblies to glassfish via ant..

Here is my solution:
jbi_admin.xml, copied from ${glassfish.home}/jbi/bin, modified as follows:


<?xml version="1.0" encoding="UTF-8"?>
<!--
 # BEGIN_HEADER - DO NOT EDIT
 #
 # The contents of this file are subject to the terms
 # of the Common Development and Distribution License
 # (the "License").  You may not use this file except
 # in compliance with the License.
 #
 # You can obtain a copy of the license at
 # https://open-esb.dev.java.net/public/CDDLv1.0.html.
 # See the License for the specific language governing
 # permissions and limitations under the License.
 #
 # When distributing Covered Code, include this CDDL
 # HEADER in each file and include the License file at
 # https://open-esb.dev.java.net/public/CDDLv1.0.html.
 # If applicable add the following below this CDDL HEADER,
 # with the fields enclosed by brackets "[]" replaced with
 # your own identifying information: Portions Copyright
 # [year] [name of copyright owner]
-->


<!--
 # @(#)jbi_admin.xml
 # Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
 #
 # END_HEADER - DO NOT EDIT
-->
<!--
 Copyright (c) 2004-2007 Sun Microsystems Inc., All Rights Reserved.
-->
<!--
@author chikkala
-->
<project basedir="." name="jbi_admin" xmlns:jbi="http://www.sun.com/ns/jbi/jbi-ant/1">
<target name="-pre-jbi-admin-setup">
</target>


<target name="-jbi-admin-setup" depends="-pre-jbi-admin-setup">
<taskdef resource="com/sun/jbi/ui/ant/antlib.xml" />
<!-- properties used as attribute values for ant tasks -->
<property name="jbi.target" value="server" />
<property name="jbi.secure" value="false" />
<property name="jbi.username" value="admin" />
<property name="jbi.password" value="adminadmin" />
<property name="jbi.host" value="localhost" />
<property name="jbi.port" value="4848" />
<property name="jbi.show.descriptor" value="false" />
<property name="jbi.task.fail.on.error" value="true" />
<property name="jbi.force.task" value="false" />
<property name="jbi.keep.archive" value="false" />
</target>


<target name="init-jbi-admin" depends="-jbi-admin-setup" />


<!-- deployment targets -->
<!-- deploy service assembly targets -->


<target name="-init-deploy-sa">
<condition property="deploy.sa">
<and>
<isset property="jbi.deploy.file" />
<not>
<isset property="jbi.service.assembly.name" />
</not>
</and>
</condition>
<condition property="deploy.sa.from.domain">
<and>
<isset property="jbi.service.assembly.name" />
<not>
<isset property="jbi.deploy.file" />
</not>
</and>
</condition>
<condition property="deploy.sa.option.error1">
<and>
<isset property="jbi.service.assembly.name" />
<isset property="jbi.deploy.file" />
</and>
</condition>
<condition property="deploy.sa.option.error2">
<and>
<not>
<isset property="jbi.service.assembly.name" />
</not>
<not>
<isset property="jbi.deploy.file" />
</not>
</and>
</condition>
</target>


<target name="-do-deploy-sa" depends="init-jbi-admin, -init-deploy-sa" if="deploy.sa">
<jbi-deploy-service-assembly username="${jbi.username}" password="${jbi.password}" secure="${jbi.secure}" host="${jbi.host}" port="${jbi.port}" target="${jbi.target}" 
failOnError="${jbi.task.fail.on.error}" file="${jbi.deploy.file}" />
</target>


<target name="-do-deploy-sa-from-domain" depends="init-jbi-admin, -init-deploy-sa" if="deploy.sa.from.domain">
<jbi-deploy-service-assembly username="${jbi.username}" password="${jbi.password}" secure="${jbi.secure}" host="${jbi.host}" port="${jbi.port}" target="${jbi.target}" 
failOnError="${jbi.task.fail.on.error}" name="${jbi.service.assembly.name}" />
</target>


<target name="-do-deploy-sa-option-error1" depends="init-jbi-admin, -init-deploy-sa" if="deploy.sa.option.error1">
<jbi-deploy-service-assembly username="${jbi.username}" password="${jbi.password}" secure="${jbi.secure}" host="${jbi.host}" port="${jbi.port}" target="${jbi.target}" 
failOnError="${jbi.task.fail.on.error}" file="${jbi.deploy.file}" name="${jbi.service.assembly.name}" />
</target>


<target name="-do-deploy-sa-option-error2" depends="init-jbi-admin, -init-deploy-sa" if="deploy.sa.option.error2">
<jbi-deploy-service-assembly username="${jbi.username}" password="${jbi.password}" secure="${jbi.secure}" host="${jbi.host}" port="${jbi.port}" target="${jbi.target}" 
failOnError="${jbi.task.fail.on.error}" />
</target>


<!-- main install target for deploy service assembly -->
<target name="deploy-service-assembly" depends="init-jbi-admin, -init-deploy-sa,
        -do-deploy-sa, -do-deploy-sa-from-domain, 
        -do-deploy-sa-option-error1, -do-deploy-sa-option-error2" description="Deploys Service assembly">
</target>


<!-- undeploy service assembly target -->
<target name="-init-undeploy-sa" depends="init-jbi-admin">
<!-- default service assembly name value. null or empty for no service assembly dependency in query tasks-->
<property name="jbi.service.assembly.name" value="" />
</target>


<target name="undeploy-service-assembly" depends="init-jbi-admin, -init-undeploy-sa" description="Undeploys service assembly">
<jbi-undeploy-service-assembly username="${jbi.username}" password="${jbi.password}" secure="${jbi.secure}" host="${jbi.host}" port="${jbi.port}" target="${jbi.target}" 
failOnError="${jbi.task.fail.on.error}" force="${jbi.force.task}" keepArchive="${jbi.keep.archive}" name="${jbi.service.assembly.name}" />
</target>


<target name="-init-sa-lifecycle-options" depends="init-jbi-admin">
<!-- default service assembly name value. null or empty for no service assembly dependency in query tasks-->
<property name="jbi.service.assembly.name" value="" />
</target>


<!-- start service assembly target -->
<target name="start-service-assembly" depends="init-jbi-admin, -init-sa-lifecycle-options" description="starts service assembly">
<jbi-start-service-assembly username="${jbi.username}" password="${jbi.password}" secure="${jbi.secure}" host="${jbi.host}" port="${jbi.port}" target="${jbi.target}" 
failOnError="${jbi.task.fail.on.error}" name="${jbi.service.assembly.name}" />
</target>


<!-- stop service assembly target -->
<target name="stop-service-assembly" depends="init-jbi-admin, -init-sa-lifecycle-options" description="stops service assembly">
<jbi-stop-service-assembly username="${jbi.username}" password="${jbi.password}" secure="${jbi.secure}" host="${jbi.host}" port="${jbi.port}" target="${jbi.target}" 
failOnError="${jbi.task.fail.on.error}" name="${jbi.service.assembly.name}" />
</target>


<!-- shutdown service assembly target -->
<target name="shut-down-service-assembly" depends="init-jbi-admin, -init-sa-lifecycle-options" description="shuts down service assembly">
<jbi-shut-down-service-assembly username="${jbi.username}" password="${jbi.password}" secure="${jbi.secure}" host="${jbi.host}" port="${jbi.port}" target="${jbi.target}" 
failOnError="${jbi.task.fail.on.error}" force="${jbi.force.task}" name="${jbi.service.assembly.name}" />
</target>


</project>

and the actual build.xml is as follows:

<?xml version="1.0"?>
<project name="myproject" default="zip" xmlns:ivy="antlib:org.apache.ivy.ant" xmlns:cs="antlib:com.puppycrawl.tools.checkstyle">


<import file="./jbi_admin.xml" />
        <!-- some stuff deleted.. -->
<!-- ==================================== -->
<!-- Glassfish tasks                      -->
<!-- ==================================== -->
<target name="glassfish-init" unless="glassfish.initialized">
<path id="jbi.ant.tasks.classpath.id">
<pathelement location="${glassfish.home}/lib/sun-appserv-ant.jar" />
<pathelement location="${glassfish.home}/jbi/lib/jbi-ant-tasks.jar" />
</path>
<!-- Load jbi task definitions -->
<taskdef resource="com/sun/jbi/ui/ant/antlib.xml" classpathref="jbi.ant.tasks.classpath.id" />
<taskdef name="unset" classname="ise.antelope.tasks.Unset">
<classpath path="${build.lib}/antelopetasks-3.2.10.jar" />
</taskdef>
<!-- ensure that glassfish taskdef is only run once, otw ant will error -->
<property name="glassfish.initialized" value="true" />
</target>


<target name="glassfish-password-init" depends="init, glassfish-init">
<!-- do not forget to delete this! -->
<echo message="AS_ADMIN_PASSWORD=adminadmin" file="${build.dir}/password.file" />
</target>


<target name="glassfish-prod-deploy-pre" depends="glassfish-init">
<!--<property name="jbi.service.assembly.name" value="" />-->
<unset name="jbi.service.assembly.name"/>
<property name="jbi.task.fail.on.error" value="true" />
<property name="jbi.host" value="${prod.host}" />
<property name="jbi.deploy.file" value="${build.dir}/${name}-${SURUM}.zip" />
</target>


<target name="glassfish-start-assembly-pre" depends="glassfish-init">
<property name="jbi.task.fail.on.error" value="true" />
<property name="jbi.service.assembly.name" value="${name}-${SURUM}" />
<property name="jbi.host" value="${prod.host}" />
<unset name="jbi.deploy.file"/>
<!--<property name="jbi.deploy.file" value="" />-->
</target>


<target name="glassfish-prod-undeploy-pre" depends="glassfish-init">
<property name="jbi.task.fail.on.error" value="false" />
<property name="jbi.service.assembly.name" value="${name}-${SURUM}" />
<property name="jbi.host" value="${prod.host}" />
<unset name="jbi.deploy.file"/>
<!--<property name="jbi.deploy.file" value="" />-->
</target>


<target name="exec-glassfish-prod-deploy" depends="zip, glassfish-prod-deploy-pre, deploy-service-assembly">
</target>


<target name="exec-glassfish-start-assembly" depends="glassfish-start-assembly-pre, start-service-assembly">
</target>


<target name="glassfish-prod-deploy" depends="exec-glassfish-prod-deploy, exec-glassfish-start-assembly">
</target>


<target name="glassfish-prod-undeploy" depends="glassfish-prod-undeploy-pre, shut-down-component, undeploy-service-assembly">
</target>


</project>


glassfish-prod-deploy target deploys the specified assembly to the application server and starts it.
Likewise, glassfish-prod-undeploy target shutdowns the assembly and undeploys it.

19 Ağustos 2011 Cuma

ant deploy to glassfish

In order to deploy the web service war to glassfish using ant, you can use the following ant snippet:



<target name="glassfish-init" unless="glassfish.initialized">
<taskdef name="sun-appserv-deploy" classname="org.apache.tools.ant.taskdefs.optional.sun.appserv.DeployTask">
<classpath path="${glassfish.home}/lib/sun-appserv-ant.jar" />
</taskdef>
<taskdef name="sun-appserv-undeploy" classname="org.apache.tools.ant.taskdefs.optional.sun.appserv.UndeployTask">
<classpath path="${glassfish.home}/lib/sun-appserv-ant.jar" />
</taskdef>
<!-- ensure that glassfish taskdef is only run once, otw ant will error -->
<property name="glassfish.initialized" value="true" />
</target>


<target name="glassfish-password-init" depends="init, glassfish-init">
<!-- do not forget to delete this! -->
<echo message="AS_ADMIN_PASSWORD=adminadmin" file="${build.dir}/password.file" />
</target>


<target name="glassfish-remote-deploy" depends="glassfish-init, glassfish-password-init, war">
<sun-appserv-deploy file="${build.dir}/${name}-${version}.war" name="${name}-${version}" 
user="admin" passwordfile="${build.dir}/password.file" host="remotemachine" port="4848"
asinstalldir="${glassfish.home}"/>
</target>


<target name="glassfish-remote-undeploy" depends="glassfish-init, glassfish-password-init">
<sun-appserv-undeploy name="${name}-${version}" 
user="admin" passwordfile="${build.dir}/password.file" host="remotemachine" port="4848" 
asinstalldir="${glassfish.home}"/>
</target>



The point is to use sun-appserv-ant.jar file, which includes glassfish specific ant task definitions, inside glassfish v2.1 home directory. Otherwise, there will be ClassCouldNotBeFound exceptions..


2 Ağustos 2011 Salı

running ant JUnitTask

message from ant:


BUILD FAILED
.../build.xml:346: Problem: failed to create task or type junit
Cause: the class org.apache.tools.ant.taskdefs.optional.junit.JUnitTask was not found.
        This looks like one of Ant's optional components.
....



this is due to lack of  ant-junit.jar file.

one method is to download ant-junit.jar from here: http://www.java2s.com/Code/Jar/ABC/Downloadantjunitjar.htm
and copy it under $ANT_HOME/lib

another method is to download a newer version of ant (http://ant.apache.org/)
ant 1.8.2 comes with that jar file..

http://ant.apache.org/manual/Tasks/junit.html explains how to run junit task..