ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dimitri PISSARENKO <dimitri.pissare...@gmx.net>
Subject Functions in Ant?
Date Mon, 08 Jul 2002 12:36:58 GMT
Hello!

I'm using Ant to create the jar files required for deployment of EJBs.

I've an Ant XML file, in which I set the properties of the jar file to
be created, among other things, I define what files should be put into
the jar file by means of the properties

...

<property name="related-class1" value="Util//RandomKeyGenerator"/>
<property name="related-class2" value="DataAccess//queries//Query"/>
<property name="related-class3"
value="DataAccess//queries//FindProjectByNameQuery"/>
<property name="related-class4"
value="DataAccess//queries//FindProjectByPrimaryKeyQuery"/>
<property name="related-class5"
value="DataAccess//queries//GetNumberOfProjectsQuery"/>
<property name="related-class6"
value="DataAccess//queries//InsertProjectQuery"/>
<property name="related-class7"
value="DataAccess//queries//UpdateProjectNameQuery"/>

...

Then, I call another file, in which the properties are checked and the
appropriate files put into the new jar file:


<!--
	copy the related class 1
	<copy file="${class-dir}//${related-class1}.class"
tofile="${bean-jar-dir}//${related-class1}.class"/>
-->
<available file="${class-dir}//${related-class1}.class"
property="related-class-1-found"/>

<copy todir="${bean-jar-dir}">
	<fileset dir="${class-dir}">
		<include name="${related-class1}.class"
if="related-class-1-found"/>
	</fileset>
</copy>


<!--
	copy the related class 2
	<copy file="${class-dir}//${related-class1}.class"
tofile="${bean-jar-dir}//${related-class1}.class"/>
-->
<available file="${class-dir}//${related-class2}.class"
property="related-class-2-found"/>

<copy todir="${bean-jar-dir}">
	<fileset dir="${class-dir}">
		<include name="${related-class2}.class"
if="related-class-2-found"/>
	</fileset>
</copy>

...
<jar jarfile="${bean-jar-dir}//${bean-full-name}.jar">
    <fileset dir="${bean-jar-dir}">
    ...
	<include name="${related-class2}.class"
if="related-class-2-found"/>
	<include name="${related-class3}.class"
if="related-class-3-found"/>
	<include name="${related-class4}.class"
if="related-class-4-found"/>
	<include name="${related-class5}.class"
if="related-class-5-found"/>
	<include name="${related-class6}.class"
if="related-class-6-found"/>
	<include name="${related-class7}.class"
if="related-class-7-found"/>
	<include name="${related-class8}.class"
if="related-class-8-found"/>
	<include name="${related-class9}.class"
if="related-class-9-found"/>
    ...
    </fileset>
</jar>
...

The problem is, that the code required for processing related class 1,
related class 2 etc is always the same and it is highly unelegant to
always repeat it.

Is it possible to implement the shown in a better way? Is is possible
to make an Ant function and call it several times?

Thanks

Dimitri Pissarenko

PS: Below is the full code of the Ant scripts




** BEGINNING OF build.xml (MAIN SCRIPT) **
<?xml version="1.0" encoding="UTF-8" ?>
<!--
	Ant build file for the enterprise java beans of the LCM
(basis) 
	application
-->
<project name="lcmbasis" default="jar-all"
basedir="C:\work\aut\sw\lcmbasis\sw">
	<!-- 
		ejb-dir: EJB main directory
		Each EJB used in the application has a subdirectory in
this
		directory which has the same name as the appropriate
EJB

		META-INF of the EJB is again a subdirectory of the
EJB's 
		directory
	-->
	<property name="ejb-dir"
value="C:\work\aut\sw\lcm_basis_ejb"/>

	<!--
		root class files directory
	-->
	<property name="class-dir"
value="C:\Programme\Together\out\classes\lcmBasis"/>

	<!--
		deployment directory to which the complete jar file of
the bean should
		be moved
	-->
	<property name="deploy-dir"
value="C:\Programme\jboss-3.0.0\server\default\deploy"/>


	<target name="init">
	</target>

	<target name="jar-all">
		<antcall
target="ejb-jar-deploy-HelloWorldBean"></antcall>
		<antcall
target="ejb-jar-deploy-ProjectBean"></antcall>
		<antcall
target="ejb-jar-deploy-GetProjectListBean"></antcall>
		<antcall
target="ejb-jar-deploy-MapProjectBean"></antcall>
	</target>

	<target name="undeploy-all">
		<available file="${deploy-dir}//HelloWorldBean.jar"
property="HelloWorldBean-available"/>
		<antcall target="undeploy-HelloWorldBean"/>

		<available file="${deploy-dir}//ProjectBean.jar"
property="ProjectBean-available"/>
		<antcall target="undeploy-ProjectBean"/>

		<available
file="${deploy-dir}//GetProjectListBean.jar"
property="GetProjectListBean-available"/>
		<antcall target="undeploy-GetProjectListBean"/>
	</target>

	<target name="undeploy-HelloWorldBean"
if="HelloWorldBean-available">
		<delete file="${deploy-dir}//HelloWorldBean.jar"/>
	</target>

	<target name="undeploy-ProjectBean"
if="ProjectBean-available">
		<delete file="${deploy-dir}//ProjectBean.jar"/>
	</target>

	<target name="undeploy-GetProjectListBean"
if="GetProjectListBean-available">
		<delete file="${deploy-dir}//GetProjectListBean.jar"/>
	</target>


	<target name="ejb-jar-deploy-HelloWorldBean" depends="init">
		
		<ant antfile="build-ejb.xml" target="ejb-jar-deploy"
inheritAll="true">
			<!--
				bean-short-name: Name of the bean
without 
				"Bean" suffix (eg HelloWorld for 
				HelloWorldBean)
			-->
			<property name="bean-short-name"
value="HelloWorld"/>

			<!--
				directory of the bean
				Note: this must be a relative path
reflecting
				the package affiliation of the bean.
This 
				property is used both to locate the
XML
				descriptor AND the class files of the
bean.
			-->
			<property name="bean-dir"
value="DataAccess//operations"/>

			<!--
				no related classes
			-->
			<property name="related-class1" value=""/>	
	    <property name="related-class2" value=""/>
	    <property name="related-class3" value=""/>
	    <property name="related-class4" value=""/>
	    <property name="related-class5" value=""/>

		</ant>
	</target>

	<target name="ejb-jar-deploy-ProjectBean" depends="init">
		
		<ant antfile="build-ejb.xml" target="ejb-jar-deploy"
inheritAll="true">
			<!--
				bean-short-name: Name of the bean
without 
				"Bean" suffix (eg HelloWorld for 
				HelloWorldBean)
			-->
			<property name="bean-short-name"
value="Project"/>

			<!--
				directory of the bean
				Note: this must be a relative path
reflecting
				the package affiliation of the bean.
This 
				property is used both to locate the
XML
				descriptor AND the class files of the
bean.
			-->
			<property name="bean-dir"
value="DataAccess//entities"/>

			<!--
				name of the primary key class (only
for 
				entity EJBs)
			-->
			<property name="pk-class" value=""/>

			<!--
				related class 1
			-->
			<property name="related-class1"
value="Util//RandomKeyGenerator"/>
			<property name="related-class2"
value="DataAccess//queries//Query"/>

	    <property name="related-class3"
value="DataAccess//queries//FindProjectByNameQuery"/>
	    <property name="related-class4"
value="DataAccess//queries//FindProjectByPrimaryKeyQuery"/>
	    <property name="related-class5"
value="DataAccess//queries//GetNumberOfProjectsQuery"/>
	    <property name="related-class6"
value="DataAccess//queries//InsertProjectQuery"/>
	    <property name="related-class7"
value="DataAccess//queries//UpdateProjectNameQuery"/>
	    <property name="related-class8"
value="DataAccess//queries//DeleteProjectQuery"/>
	    <property name="related-class9"
value="DataAccess//queries//UpdateProjectDataQuery"/>

		</ant>
	</target>

	<target name="ejb-jar-deploy-GetProjectListBean"
depends="init">
		
		<ant antfile="build-ejb.xml" target="ejb-jar-deploy"
inheritAll="true">
			<!--
				bean-short-name: Name of the bean
without 
				"Bean" suffix (eg HelloWorld for 
				HelloWorldBean)
			-->
			<property name="bean-short-name"
value="GetProjectList"/>

			<!--
				directory of the bean
				Note: this must be a relative path
reflecting
				the package affiliation of the bean.
This 
				property is used both to locate the
XML
				descriptor AND the class files of the
bean.
			-->
			<property name="bean-dir"
value="DataAccess//operations"/>

			<!--
				related classes
			-->
			<property name="related-class1"
value="DataAccess//DatabaseConnectionFactory"/>
			<property name="related-class2"
value="DataAccess//operations//BriefProjectInformation"/>
		    <property name="related-class3" value=""/>
		    <property name="related-class4" value=""/>
	    <property name="related-class5" value=""/>

		</ant>
	</target>

	<target name="ejb-jar-deploy-MapProjectBean" depends="init">
		
		<ant antfile="build-ejb.xml" target="ejb-jar-deploy"
inheritAll="true">
			<!--
				bean-short-name: Name of the bean
without 
				"Bean" suffix (eg HelloWorld for 
				HelloWorldBean)
			-->
			<property name="bean-short-name"
value="MapProject"/>

			<!--
				directory of the bean
				Note: this must be a relative path
reflecting
				the package affiliation of the bean.
This 
				property is used both to locate the
XML
				descriptor AND the class files of the
bean.
			-->
			<property name="bean-dir"
value="DataAccess//mappers"/>

			<!--
				related classes
			<property name="related-class1"
value="DataAccess//DatabaseConnectionFactory"/>
			<property name="related-class2"
value="DataAccess//operations//BriefProjectInformation"/>

			-->
            <property name="related-class1"
value="BusinessLogic//Project"/>
	    <property name="related-class2"
value="DataAccess//ServiceLocator"/>
	    <property name="related-class3"
value="DataAccess//entities//ProjectHome"/>
	    <property name="related-class4"
value="DataAccess//entities//Project"/>
	    <property name="related-class5" value=""/>

		</ant>
	</target>



	<target name="antstructure">
		<antstructure output="ant.dtd"/>
	</target>
</project>
** END OF build.xml (MAIN SCRIPT) **

** BEGINNING OF build-ejb.xml **
<?xml version="1.0" encoding="UTF-8" ?>
<!--
	Ant build file for the HelloWorldBean EJB
-->
<project name="jar-HelloWorldBean" default="ejb-jar">

	<target name="ejb-jar-deploy">
		<!--
			Full name of the bean (with the "Bean" suffix)
		-->
		<property name="bean-full-name"
value="${bean-short-name}Bean"/>


		<!--
			Directory where the JAR file of the bean will
be placed
		-->
		<property name="bean-jar-dir"
value="${ejb-dir}//${bean-full-name}"/>
		<!--
			META-INF subdirectory name
		-->
		<property name="bean-meta-inf-dir"
value="${bean-jar-dir}//META-INF"/>


		<!-- 
			delete the bean directory and its META-INF 
			subdirectory 
		-->
		<delete dir="${bean-jar-dir}"/>

		<!--
			create the target directory for the jar file
		-->
		<mkdir dir="${bean-meta-inf-dir}"/>

		<!--
			create the package-oriented subdirectory for
the
			class files
		-->
		<mkdir dir="${bean-jar-dir}//${bean-dir}"/>

		<!--
			copy the class files into the package-oriented
			subdirectory
		-->

		
		<copy
file="${class-dir}//${bean-dir}//${bean-short-name}.class"
tofile="${bean-jar-dir}//${bean-dir}//${bean-short-name}.class"/>
		<copy
file="${class-dir}//${bean-dir}//${bean-short-name}Home.class"
tofile="${bean-jar-dir}//${bean-dir}//${bean-short-name}Home.class"/>
		<copy
file="${class-dir}//${bean-dir}//${bean-full-name}.class"
tofile="${bean-jar-dir}//${bean-dir}//${bean-full-name}.class"/>

		<!--
			copy the primary key class file
		-->
		<available
file="${class-dir}//${bean-dir}//${bean-short-name}PK.class"
property="primary-key-class-found"/>

		<antcall target="copy-pkfile"></antcall>

		<!--
			copy the xml descriptor of the bean (its name
is equal
			to the name of the described bean) and rename
it to
			ejb-jar.xml
		-->
		<copy
file="${basedir}//${bean-dir}//${bean-full-name}.xml"
tofile="${bean-meta-inf-dir}//ejb-jar.xml"/>

		<!--
			copy the file containing the desired JNDI name
for the EJB
		-->
		<copy
file="${basedir}//${bean-dir}//${bean-full-name}.jboss.xml"
tofile="${bean-meta-inf-dir}//jboss.xml" />

		<!--
			copy the related class 1
			<copy
file="${class-dir}//${related-class1}.class"
tofile="${bean-jar-dir}//${related-class1}.class"/>
		-->
		<available
file="${class-dir}//${related-class1}.class"
property="related-class-1-found"/>

		<copy todir="${bean-jar-dir}">
			<fileset dir="${class-dir}">
				<include
name="${related-class1}.class" if="related-class-1-found"/>
			</fileset>
		</copy>


		<!--
			copy the related class 2
			<copy
file="${class-dir}//${related-class1}.class"
tofile="${bean-jar-dir}//${related-class1}.class"/>
		-->
		<available
file="${class-dir}//${related-class2}.class"
property="related-class-2-found"/>

		<copy todir="${bean-jar-dir}">
			<fileset dir="${class-dir}">
				<include
name="${related-class2}.class" if="related-class-2-found"/>
			</fileset>
		</copy>


		<!--
			copy the related class 3
		-->
		<available
file="${class-dir}//${related-class3}.class"
property="related-class-3-found"/>

		<copy todir="${bean-jar-dir}">
			<fileset dir="${class-dir}">
				<include
name="${related-class3}.class" if="related-class-3-found"/>
			</fileset>
		</copy>


		<!--
			copy the related class 4
		-->
		<available
file="${class-dir}//${related-class4}.class"
property="related-class-4-found"/>

		<copy todir="${bean-jar-dir}">
			<fileset dir="${class-dir}">
				<include
name="${related-class4}.class" if="related-class-4-found"/>
			</fileset>
		</copy>

		<!--
			copy the related class 5
		-->
		<available
file="${class-dir}//${related-class5}.class"
property="related-class-5-found"/>

		<copy todir="${bean-jar-dir}">
			<fileset dir="${class-dir}">
				<include
name="${related-class5}.class" if="related-class-5-found"/>
			</fileset>
		</copy>


		<!--
			copy the related class 6
		-->
		<available
file="${class-dir}//${related-class6}.class"
property="related-class-6-found"/>

		<copy todir="${bean-jar-dir}">
			<fileset dir="${class-dir}">
				<include
name="${related-class6}.class" if="related-class-6-found"/>
			</fileset>
		</copy>


		
		<!--
			copy the related class 7
		-->
		<available
file="${class-dir}//${related-class7}.class"
property="related-class-7-found"/>

		<copy todir="${bean-jar-dir}">
			<fileset dir="${class-dir}">
				<include
name="${related-class7}.class" if="related-class-7-found"/>
			</fileset>
		</copy>


		<!--
			copy the related class 8
		-->
		<available
file="${class-dir}//${related-class8}.class"
property="related-class-8-found"/>

		<copy todir="${bean-jar-dir}">
			<fileset dir="${class-dir}">
				<include
name="${related-class8}.class" if="related-class-8-found"/>
			</fileset>
		</copy>


		<!--
			copy the related class 9
		-->
		<available
file="${class-dir}//${related-class9}.class"
property="related-class-9-found"/>

		<copy todir="${bean-jar-dir}">
			<fileset dir="${class-dir}">
				<include
name="${related-class9}.class" if="related-class-9-found"/>
			</fileset>
		</copy>



		<jar jarfile="${bean-jar-dir}//${bean-full-name}.jar">
		    <fileset dir="${bean-jar-dir}">
			<include
name="${bean-dir}//${bean-short-name}.class"/>
			<include
name="${bean-dir}//${bean-short-name}Home.class"/>
			<include
name="${bean-dir}//${bean-full-name}.class"/>
			<include
name="${bean-dir}//${bean-short-name}PK.class"
if="primary-key-class-found"/>
			<include name="${related-class1}.class"
if="related-class-1-found"/>
			<include name="${related-class2}.class"
if="related-class-2-found"/>
			<include name="${related-class3}.class"
if="related-class-3-found"/>
			<include name="${related-class4}.class"
if="related-class-4-found"/>
			<include name="${related-class5}.class"
if="related-class-5-found"/>
			<include name="${related-class6}.class"
if="related-class-6-found"/>
			<include name="${related-class7}.class"
if="related-class-7-found"/>
			<include name="${related-class8}.class"
if="related-class-8-found"/>
			<include name="${related-class9}.class"
if="related-class-9-found"/>
			<include name="META-INF/ejb-jar.xml" />
			<include name="META-INF/jboss.xml" />
		    </fileset>
		</jar>
		
		<copy file="${bean-jar-dir}//${bean-full-name}.jar"
todir="${deploy-dir}"/>
	</target>

	<target name="copy-pkfile" if="primary-key-class-found">
		<copy
file="${class-dir}//${bean-dir}//${bean-short-name}PK.class"
tofile="${bean-jar-dir}//${bean-dir}//${bean-short-name}PK.class"/>
	</target>
</project>
** END OF build-ejb.xml **

--
To unsubscribe, e-mail:   <mailto:ant-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-user-help@jakarta.apache.org>


Mime
View raw message