<?xml version="1.0" encoding="iso-8859-1"?>

<!--
   build.xml - build file para a aplicacao exemplo do artigo 'Ant alem do basico' - Java Magazine 14
   
   Autor: Felipe Leme <pente-fino@felipeal.net>   
-->

<project name="JM14" default="targetPadrao">

<!-- le arquivo com propriedades da build -->
   <property file="build.properties"/>
<!-- importa propriedades do ambiente -->
   <property  environment="env"/>

<!-- classpath comum a todos targets -->
   <path id="classpath.comum">
   <pathelement location="${lib.dir}/log4j.jar"/>
   </path>
   
<!-- classpath usado apenas para compilacao  -->
   <path id="classpath.compilacao">
      <path refid="classpath.comum"/>
      <pathelement location="${lib.dir}/servlet.jar"/>
   </path>
   
<!-- classpath usado em tempo de execucao -->
   <path id="classpath.execucao">
   	  <path refid="classpath.comum"/>
      <pathelement location="${classes.dir}"/>
      <pathelement location="${lib.dir}/hsqldb.jar"/>
      <pathelement location="${resources.dir}"/>
   </path>
   
<!-- classpath usado pela aplicacao web-->
	<fileset  id="jars.web" dir="${lib.dir}">
		<include name="jstl.jar"/>
		<include name="standard.jar"/>
		<include name="hsqldb.jar"/>
   </fileset>
      
 <!-- faz reuso de targets comuns -->
   <import file="buildComum.xml"/>
     
 <!-- configura as tasks de terceiros usadas nesse buildfile -->
 <!-- <import file="configuracaoTasksTerceiros.xml"/> -->
   
 	<target name="targetPadrao" description="Target default: prepara banco, cria war e executa aplicacao" 
 		depends="emiteSons,mostraSplash,prepararBancoDados,deployTomcat,deployJBoss">
 	</target>
 	
 	<target name="debugaPadrao" description="Executa o target default, logando as acoes">
 		<echoproperties  destfile="saidaAnt.log"/>
 		<record name="saidaAnt.log" action="start" append="true"/>
 		<antcall target="targetPadrao"/>
 		<record  name="saidaAnt.log" action="stop"/>
 	</target>
     
   <target name="compilarClasses" depends="prepararAmbiente" description="Compila as classes do sistema">
      <javac destdir="${classes.dir}" srcdir="${src.dir}">
         <classpath refid="classpath.compilacao"/>
      </javac>
   </target>
   
   <target name="executarAplicacao" depends="compilarClasses" description="Executa a classe principal da aplicacao">
      <java classname="${aplicacao.main}">
         <classpath refid="classpath.execucao"/>
         <sysproperty  key="jm14.ant.nomeDriver" value="${jm14.ant.nomeDriver}"/>
      </java>
   </target>
         	  
   <target  name="prepararBancoDados" description="Cria as tabelas e popula o banco de dados">
        <property name="driver.dir" value="${sql.dir}/${jm14.ant.nomeDriver}"/>
        <!-- le as propriedades do driver (definido pela propriedade jm14.ant.nomeDriver) -->
   		<property file="${driver.dir}/${sql.driver.arquivo}"/>
   		<!-- checa se o tipo de driver a ser usado foi passado pela linha de comando -->
   		<antcall target="checaPropriedade">
      		<param name="propriedade" value="jm14.ant.nomeDriver"/>
      	</antcall>
   		<!-- cria as tabelas -->
   		<sql driver="${jm14.ant.jdbc.driver}" url="${jm14.ant.jdbc.url}" 
   				userid="${jm14.ant.jdbc.user}" password="${jm14.ant.jdbc.password}" 
   				src="${driver.dir}/${sql.criaTabelas.arquivo}" classpath="${lib.dir}/${jm14.ant.jdbc.jar}"/>
   		<!-- popula as tabelas -->
   		<sql driver="${jm14.ant.jdbc.driver}" url="${jm14.ant.jdbc.url}" 
   				userid="${jm14.ant.jdbc.user}" password="${jm14.ant.jdbc.password}" 
   				src="${driver.dir}/${sql.insereDados.arquivo}" classpath="${lib.dir}/${jm14.ant.jdbc.jar}"/>
   </target>
     
   <target name="criarJar" depends="compilarClasses" 
   			description="Cria um JAR com as classes compiladas">
      <jar jarfile="${jar.fullPath}" basedir="${classes.dir}" >
      	<manifest >
      		<attribute name="Main-Class" value="${aplicacao.main}"/>
      	</manifest>
      </jar>
   </target>
   
	<target name="checarDescritorWeb" description="Checa a sintaxe de web.xml">
		<xmlvalidate file="${web.descritor}">
 			<dtd publicId="-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
       				location="etc/web-app_2_3.dtd"/>
		</xmlvalidate>
	</target>
	
	<target name="preCompilarJsp" depends="prepararAmbiente" 
			description="Faz a pre-compilacao das paginas JSP">
		<jspc srcdir="${web.dir}"  destdir="${jsp.dir}" verbose="9">
  			<include name="**/*.jsp" />
		</jspc>
	</target>
   
   <target name="criarWar" depends="checarDescritorWeb,criarJar" 
   			description="Cria um WAR com a aplicacao web">
   		<war destfile="${war.arquivo}" webxml="${web.descritor}">
   			<fileset dir="${web.dir}" excludes="WEB-INF/web.xml"/>
			<classes dir="${resources.dir}" includes="**/*.properties"/> 
			<lib refid="jars.web"/> 
			<lib dir="${dist.dir}" includes="${jar.arquivo}"/>
   		</war>
   </target>
   
	<target name="deployTomcat" if="env.CATALINA_HOME" 
			description="Faz o deploy da aplicacao em um servidor Tomcat">
   		<antcall target="deployWar">
      		<param name="parametro.war" value="${war.arquivo}"/>
      		<param name="parametro.deploy.dir" value="${env.CATALINA_HOME}/webapps"/>
   		</antcall>
	</target>

	<target name="deployJBoss" if="env.JBOSS_HOME"
			description="Faz o deploy da aplicacao em um servidor JBoss" >
   		<antcall target="deployWar">
      		<param name="parametro.war" value="${war.arquivo}"/>
      		<param name="parametro.deploy.dir" value="${env.JBOSS_HOME}/server/default/deploy"/>
   		</antcall>
	</target>   

	<target name="checarFormatacao" description="Checa a formatacao do codigo usando Checkstyle">
		<checkstyle config="${checkstyle.home}/docs/sun_checks.xml">
   			<fileset dir="${src.dir}" includes="**/*.java"/>
		</checkstyle>
	</target>
	
	<target name="formatarCodigo" description="Formata o codigo usando Jalopy">
		<jalopy backup="true" loglevel="info">
   			<fileset dir="${src.dir}" includes="**/*.java"/>
		</jalopy>
	</target>
	
	<target name="checarMetricas" description="Checa metricas de codigo usando PMD">
     	<mkdir dir="${relatorios.dir}"/>
 		<pmd rulesetfiles="rulesets/imports.xml">
   			<formatter type="html" toFile="${relatorios.pmd.arquivo}"/>
   			<fileset dir="${src.dir}" includes="**/*.java"/>
		</pmd>
	</target>

	<target name="ofuscarCodigo" depends="criarJar" description="Ofuscar o codigo usando Proguard">	
		<proguard configuration="${proguard.configuracao.arquivo}"/>
 	</target>
 	
 <!--
   targets auxiliares
   -->
   
    <!-- faz o deploy de uma aplicacao web (no formato WAR) -->
	<target name="deployWar" depends="criarWar">
		<copy file="${parametro.war}" todir="${parametro.deploy.dir}"/>
	</target>   

 	<!-- mostra uma tela de splash enquanto o target e executado -->
 	<target name="mostraSplash">
 		<splash imageurl="${splash.url}"/>
 	</target>
   
</project>
