<project 
	name="Ivy Dependency Management Template"
	xmlns:ivy="antlib:org.apache.ivy.ant"	
	default="report.dependencies" 
	basedir=".">
	
	<description>
		
		Base Ivy build file for all projects.
		
		Projects must import this file into their build.xml.
		
		This project exposes operations that publish artifacts to the corporate
		repository and retrieve artifacts from the corporate repository. If
		you wish to install an artifact from a public repository (e.g. Maven 2)
		into the corporate repository you must open an SSH session to the 
		corporate repository and run the install executable.
		
	</description>
	
	<property environment="env"/>
	
	<!-- property locations -->
       
	<property name="build.dir" 			location="build"/>
	<property name="dist.dir" 			location="dist"/>
    <property name="dist.client.jar"    location="dist/app-ws-client.jar"/>
	<property name="reports.dir" 		location="${build.dir}/reports"/>
	<property name="ivy.dir" 			location="ivy" />
	<property name="lib.dir" 			location="addlib"/>
	<property name="conf.dir"           location="conf" />
    <property name="src.meta.inf.dir"   location="src/META-INF" />
			
	<!-- this value gets parsed transparently when loading Ivy settings -->
	<property name="dest.repo.dir"		value="/opt/ivy-repository-2.1.0/myrepositoryNoDeps/no-namespace" />
		
	<!-- initialise -->
	<target name="init-ivy">	
		<ivy:settings id="basic.settings"    file="${ivy.dir}/ivysettings-basic.xml"/>
		<ivy:settings id="config.settings"    file="${ivy.dir}/ivysettings-config.xml"/>
		<ivy:settings id="advanced.settings" file="${ivy.dir}/ivysettings-advanced.xml"/>
	</target>

	<!-- delete any existing libraries -->
	<target name="clean">
		<delete>
			<fileset dir="${build.dir}/classes" />
		</delete>
		<antcall target="clean.ivy.cache"></antcall>
	</target>
			
	<!-- Resolve dependencies with Ivy -->
	<target name="resolve.dependencies" depends="init-ivy" description="Retrieve dependencies with ivy">		
		<ivy:retrieve			
			settingsRef="basic.settings"
			pattern="${lib.dir}/[artifact]-[revision].[ext]"
			conf="compile"/>
	    <ivy:retrieve          
	                settingsRef="basic.settings"
	                pattern="${lib.dir}/[artifact]-[revision].[ext]"
	                conf="default"/>
	    <ivy:retrieve          
            settingsRef="basic.settings"
            pattern="${lib.dir}/[artifact]-[revision].[ext]"
            conf="unversioned.jar"/>
	    <ivy:retrieve          
            settingsRef="basic.settings"
            pattern="${src.meta.inf.dir}/[artifact].[ext]"
            conf="src"/>
	</target>
	
	<!-- Publish a development build of a library to the corporate Ivy repository -->
	<!-- Important! You must call retrieve (in this case via client.deploy) before publishing. -->
	<!-- If you call publish without first calling retrieve, an IllegalStateException will be thrown. 
	
	
	-->
	<target name="publish.integration" depends="client.deploy" description="Publishes a library to the corporate Ivy repository">
	    <echo message="Ivy organisation : ${ivy.organisation}" />
        <echo message="Ivy module : ${ivy.module}" />
        
        <!-- auto-increment the next release number -->
        <ivy:buildnumber
            organisation="${ivy.organisation}" 
            module="${ivy.module}"
            revision="1.0"
            default="1.0.0"
        />
        <echo message="Ivy repository says that the current ivy.revision is : ${ivy.revision}" />
        <echo message="Ivy repository says that the current ivy.new.revision is : ${ivy.new.revision}" />
        <echo message="Ivy repository says that the current ivy.build.number is : ${ivy.build.number}" />
        <echo message="Ivy repository says that the current ivy.new.build.number is : ${ivy.new.build.number}" />
	    
	    
	    
		<!-- update manifest in the .jar file built during bem.jar -->
		<jar
			update="true"
			destfile="${dist.client.jar}">
			<manifest>
				<attribute name="Manifest-Version" value="${manifest.version}"/>
				<section name="uk/co/company/${ivy.module}/">
					<attribute name="Implementation-Title" value="Ivy Module:${ivy.module}"/>
					<attribute name="Implementation-Version" value="updated ${TODAY} ${ivy.new.revision} (Integration Build)"/>
					<attribute name="Implementation-Vendor" value="${implementation.vendor}"/>
				</section>
			</manifest>
		</jar>
		
		<ivy:publish 
		    organisation="${ivy.organisation}"
		    module="${ivy.module}"
			settingsRef="basic.settings"
			pubBranch="${ivy.branch.name}"
		    revision="${ivy.new.revision}"
			artifactspattern="${dist.dir}/[artifact].[ext]" 
			resolver="vm-repository" 			 
			status="integration"
			conf="unversioned.jar"
			forcedeliver="true"
			overwrite="true"
		/>						
		<echo message="project ${ivy.module} integration build published" />
	</target>
	
    <target name="buildnumbertest" depends="client.deploy" description="Publishes a library to the corporate Ivy repository">
        <echo message="Ivy organisation : ${ivy.organisation}" />
        <echo message="Ivy module : ${ivy.module}" />
        
        <!-- auto-increment the next release number -->
        <ivy:buildnumber
            settingsref="basic.settings"
            organisation="${ivy.organisation}" 
            module="${ivy.module}"
            revision="1.0"
        />
        <echo message="Ivy repository says that the current ivy.revision is : ${ivy.revision}" />
        <echo message="Ivy repository says that the current ivy.new.revision is : ${ivy.new.revision}" />
        <echo message="Ivy repository says that the current ivy.build.number is : ${ivy.build.number}" />
        <echo message="Ivy repository says that the current ivy.new.build.number is : ${ivy.new.build.number}" />
    </target>
    
	<!-- Publish a full release of a library to the corporate Ivy repository -->
	<!-- Important! You must call retrieve (in this case via client.deploy) before publishing. -->
	<!-- If you call publish without first calling retrieve, an IllegalStateException will be thrown. -->
    <target name="publish.release" depends="client.deploy" description="Publishes a library to the corporate Ivy repository">
        <!-- asks to ivy an available version number -->
        
		<!-- auto-increment the next release number -->
		<ivy:buildnumber
			organisation="uk.co.company" 
			module="AppWebServiceClient"
		/>
	    <echo message="Ivy repository says that the current ivy.revision is : ${ivy.revision}" />
	    <echo message="Ivy repository says that the current ivy.new.revision is : ${ivy.new.revision}" />
	    <echo message="Ivy repository says that the current ivy.build.number is : ${ivy.build.number}" />
	    <echo message="Ivy repository says that the current ivy.new.build.number is : ${ivy.new.build.number}" />
		
		<!-- update manifest in the .jar file built during bem.jar - - >
		<jar
			update="true"
			destfile="${dist.dir}/${ivy.module}.jar">
			<manifest>
				<attribute name="Manifest-Version" value="${manifest.version}"/>
				<section name="uk/co/company/${ivy.module}/">
				    <attribute name="Implementation-Title" value="Ivy Module:${ivy.module}"/>
                    <attribute name="Implementation-Version" value="updated ${TODAY} ${ivy.new.revision} (Integration Build)"/>
                    <attribute name="Implementation-Vendor" value="${implementation.vendor}"/>
				</section>
			</manifest>
		</jar -->	
				
		<ivy:publish 
			settingsRef="basic.settings"			
			artifactspattern="${dist.dir}/[artifact].[ext]" 
			resolver="vm-repository" 			 
			status="release"
			conf="unversioned.jar"
			forcedeliver="true"
			overwrite="true"
		    pubrevision="${ivy.new.revision}"
		/>					
		<echo message="project ${ivy.module} released with version ${ivy.new.revision}" />
	</target>		
	
	<!-- Does nothing -->
	<target name="client.deploy" description="Stub">
		<!-- the target of the same name in the build.xml will be called -->
	</target>		
	
	<!-- Report on dependencies with Ivy -->
	<target name="report.dependencies" depends="resolve.dependencies" description="Generate an Ivy report of dependencies">
		<ivy:report todir="${build.dir}/reports"/>
	</target>
	
	<!-- Clean Ivy Cache -->
	<target name="clean.ivy.cache" description="Clean the Ivy cache">		
		<ivy:cleancache />
	</target>	
		
</project>
