How to parameterize a path in ANT? - ant

I have the following defined in a file called build-dependencies.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="build-dependencies">
...
<path id="common-jars">
<fileset file="artifacts/project-1/jar/some*.jar" />
<fileset file="artifacts/project-2/jar/someother*.jar" />
</path>
...
</project>
I include it at the top of my build.xml file. Now I need to make the artifacts folder a parameter so it can be changed during execution of different targets.
Having this...
<?xml version="1.0" encoding="UTF-8"?>
<project name="build-dependencies">
...
<path id="common-jars">
<fileset file="${artifacts}/project-1/jar/some*.jar" />
<fileset file="${artifacts}/project-2/jar/someother*.jar" />
</path>
...
</project>
...and defining an "artifacts" property (and changing it) in the target does not work because it seems that the property substitution happens when the path is defined in build-dependencies.xml
How can I solve this? One way I was thinking was to have a parameterized macro and call that before the path is actually used, but that seems not elegant. Something like this:
<macrodef name="create-common-jars">
<attribute name="artifacts"/>
<sequential>
<path id="common-jars">
<fileset file="#{artifacts}/project-1/jar/some*.jar" />
<fileset file="#{artifacts}/project-2/jar/someother*.jar" />
</path>
</sequential>
</macrodef>
EDIT: Ivy and command line parameters are not an option.

You don't want a parameterized path. You want a PatternSet. You can define the patternset at the top-level and then just refer to it in individual targets when you need it. For your example:
<?xml version="1.0" encoding="UTF-8"?>
<project name="build-dependencies">
...
<patternset id="common-jars">
<include name="project-1/jar/some*.jar" />
<include name="project-2/jar/someother*.jar" />
</patternset>
...
<path id="instrumented-jars">
<fileset dir="instrumented">
<patternset refid="common-jars" />
</fileset>
</path>
...
<path id="standard-jars">
<fileset dir="not-instrumented">
<patternset refid="common-jars" />
</fileset>
</path>
...
</project>

I'd recommend using ivy to manage your classpath dependencies. Ivy has a neat concept called configurations that allows you to group collections of artifacts based on their usage.
Here's an adaption from one of my own build files:
<target name="retrieve" description="3rd party dependencies">
<ivy:resolve/>
<ivy:cachepath pathid="build.path" conf="build"/>
<ivy:cachepath pathid="runtime.path" conf="runtime"/>
</target>
The configurations are managed in the ivy.xml file (Would replace your build-dependencies.xml file)
<ivy-module version="2.0">
<info organisation="com.myspotontheweb" module="HelloWorld"/>
<configurations>
<conf name="build" description="jars needed for build" />
<conf name="runtime" extends="build" description="jars needed at runtime" />
</configurations>
<dependencies>
<dependency org="org1" name="project1" rev="1.0" conf="build->default"/>
<dependency org="org2" name="project2" rev="1.0" conf="build->default"/>
<dependency org="org3" name="project3" rev="1.0" conf="runtime->default"/>
<dependency org="org4" name="project4" rev="1.0" conf="runtime->default"/>
</dependencies>
</ivy-module>
The jar artifacts associated with each project would be downloaded and cached automatically from the on-line maven repositories or you can create your own local repository to hold collections of locally owned artifacts.

Lets call your file build.xml. So you execute it by running ant command. In the first case the artifacts names is hardcoded in the property defined on the third line below
<?xml version="1.0" encoding="UTF-8"?>
<project name="build-dependencies">
<property name="artifacts" value="first-value" />
...
<path id="common-jars">
<fileset file="artifacts/project-1/jar/some*.jar" />
<fileset file="artifacts/project-2/jar/someother*.jar" />
</path>
...
</project>
Now when you want to change it and use another value for that artifacts property, we run the script thus
ant -Dartifacts=new-value
This will override the hardcoded artifacts value in build.xml
If working in terms of ant targets you can do something similar, in the target on first line define the property, and if you want to overwrite the default value then pass the property as a parameter when that target is called.
Your comment reminded me of something else. Have your developers create a artifacts-dir-name.xml file. It will have only one line:
<?xml version="1.0" encoding="UTF-8"?>
<project name="artifacts-file">
<property name="artifacts" value="new-value" />
</project>
Now in your build.xml file, before the line where artifacts property is defined, import that file thus:
<import file="artifacts-dir-name.xml" optional="true" />
Now in Eclipse if this file exists, then the property is read from it and artifacts is set to "new-value", else the property is read from build.xml and is set to "first-value". All the developers need to do is to ensure artifacts-dir-name.xml file exists in that directory. This can run within Eclipse too.

is using environment variables an option (if they are set when eclipse is launched they will be picked up)? If so, have each one set ARTIFACTS and this should work:
<?xml version="1.0" encoding="UTF-8"?>
<project name="build-dependencies">
<property environment="env"/>
<path id="common-jars">
<fileset file="${env.ARTIFACTS}/project-1/jar/some*.jar" />
<fileset file="${env.ARTIFACTS}/project-2/jar/someother*.jar" />
</path>
</project>

OK, I think there is no other obvious way for me to do what I am trying to do, except use a macro that takes a parameter and creates the path with the appropriate artifacts folder.
To give a bit of context, why I was trying to what I wanted is to have "instrumented" and "not-instrumented" artifacts in separate folders. And in my "targets" I could just vary the artifacts mode. So what I do now is I have a macro: <initialise-build-settings artifacts-mode="instrumented" /> that sets up all the paths and other variables.
Thanks for your answers guys.

You can do this with different dependencies:
setpath.xml:
<project name="setpath">
<target name="setCommonJars">
<path id="common-jars">
<fileset file="${param1}/some*.jar" />
<fileset file="${param1}/someother*.jar" />
</path>
</target>
</project>
build.xml:
<project name="Test path" basedir=".">
<import file="./setpath.xml" />
<target name="buildT1" depends="setT1,setCommonJars">
<property name="jar-str" refid="common-jars" />
<echo message="buildT1: ${jar-str}" />
</target>
<target name="buildT2" depends="setT2,setCommonJars">
<property name="jar-str" refid="common-jars" />
<echo message="buildT2: ${jar-str}" />
</target>
<target name="setT1">
<property name="param1" value="t1" />
</target>
<target name="setT2">
<property name="param1" value="t2" />
</target>
</project>
If you call target buildT1 then the t1 directory will be used, if you call buildT2 then the t2 directory will be used.

Related

my property file seems not to be implemented by build.xml

I have a problem with properties.
here is content of my build.xml file:
<?xml version="1.0" ?>
<project name="default" default="package">
<target name="init">
<mkdir dir="build/classes" />
<mkdir dir="dist" />
</target>
<property file="${basedir}/localproperties"/>
<property name="javac.debug" value="off"/>
<target name="compile" depends="init" description="Compiles JAVA files">
<echo message="Debug: ${javac.debug}"/>
<javac srcdir="src"
destdir="build/classes"
classpathref="compile.classpath"
debug="${javac.debug}"/>
</target>
<path id="compile.classpath">
<fileset dir="lib" includes="*.jar"/>
</path>
</project>
and here is content of my localproperties file:
javac.debug = on
please note that I have saved localproperties as .xml file and put it into the same directory as build.xml
the problem is that it does not work as the output I get is:
Debug: off
clearly it should be:
Debug: on
please advise.
It should be:
<property file="${basedir}/localproperties.xml"/>
There is no extension assumed by the property task. As long as the content follows the key-value convention, it can be any file extension. But it would be confusing to save as .xml. Just save it as localproperties.properties, or simply local.properties.

Liquibase gives different results from Ant and the command line

When I run Liquibase from the command line it populates the FILENAME column of DATABASECHANGELOG with the relative paths to the changelog files, as you'd hope. But when I run exactly the same changelog from exactly the same directory, using Ant, it populates the column with the absolute paths.
Among other things this means that the Ant and the command-line versions are not interoperable for me.
But I can't find anyone else having reported this problem, so I'm sure it's something I'm doing; something that I haven't set up correctly. I've seen some suggestions that the root directory of the changelog needs to be on the classpath, so I've included it in the Ant classpath, but it doesn't make any difference.
Here's my Ant build file:
<project name="Database Build" default="build" basedir="." xmlns:liquibase="antlib:liquibase.integration.ant">
<path id="liquibase.lib.path">
<fileset dir="liquibase/lib">
<include name="**/*.jar" />
</fileset>
<fileset dir="liquibase">
<include name="liquibase.jar" />
</fileset>
</path>
<path id="driver.classpath">
<filelist files="${classpath}" />
</path>
<path id="main.classpath">
<pathelement location="." />
<path refid="driver.classpath" />
</path>
<taskdef
resource="liquibase/integration/ant/antlib.xml"
uri="antlib:liquibase.integration.ant">
<classpath refid="liquibase.lib.path" />
</taskdef>
<liquibase:database
id="main-schema"
driver="${driver}"
url="${url}"
user="${username}"
password="${password}"
defaultSchemaName="${defaultSchemaName}"
/>
<target
name="build"
description="Builds the database based on values set in the properties file">
<echo message="Building DB..." />
<liquibase:updateDatabase
databaseref="main-schema"
changelogfile="${changeLogFile}"
classpathref="main.classpath"
logLevel="debug"
>
<!-- Here we're effectively passing the values set as Ant properties in as Liquibase parameters -->
<liquibase:changeLogParameters>
<liquibase:changeLogParameter name="main.schema" value="${defaultSchemaName}" />
<liquibase:changeLogParameter name="tablespace.data" value="${tablespace.data}" />
<liquibase:changeLogParameter name="tablespace.index" value="${tablespace.index}" />
<liquibase:changeLogParameter name="tablespace.long" value="${tablespace.long}" />
</liquibase:changeLogParameters>
</liquibase:updateDatabase>
</target>
<target
name="createSchema"
description="Create a schema on the database"
>
<echo>${toString:main.classpath}</echo>
<sql
driver="${driver}"
classpathref="main.classpath"
url="${url}"
userid="${username}"
password="${password}"
expandProperties="true"
>
<transaction>
CREATE SCHEMA ${defaultSchemaName};
</transaction>
</sql>
</target>
<target
name="createOracleUsers"
description="Create a user in Oracle"
>
<sql
rdbms="oracle"
print="true"
driver="${driver}"
classpathref="main.classpath"
url="${url}"
userid="${username}"
password="${password}"
expandProperties="true"
>
<transaction>
CREATE USER ${defaultSchemaName} IDENTIFIED BY ${defaultSchemaName}
default tablespace ${tablespace.data}
temporary tablespace TEMP quota unlimited on ${tablespace.data}
quota unlimited on ${tablespace.index};
GRANT create session, alter session, create sequence,
create table, create view to ${defaultSchemaName};
</transaction>
</sql>
</target>
</project>
Edited to add some changelog files.
Here's the root changelog file:
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.3.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
<include file="changes/sequences/sequences.xml" relativeToChangelogFile="true" />
<include file="changes/baseobjects/db-511.xml" relativeToChangelogFile="true" />
<include file="changes/data/data-511.xml" relativeToChangelogFile="true" />
</databaseChangeLog>
The first of the included ones starts like this:
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.3.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
<changeSet dbms="oracle,db2,db2i" author="mccallim (generated)" id="1419011907193-1">
<createSequence schemaName="${main.schema}" cacheSize="100" cycle="false" incrementBy="1" minValue="1" sequenceName="SEQ_ALLOWEDCURRENCIES" startValue="1"/>
</changeSet>
...
</databaseChangeLog>
It has lots of other sequences. The next one does the tables, indexes and views, and is pretty much as you'd expect.
It looks like a bug. I created https://liquibase.jira.com/browse/CORE-2290 to track the fix for 3.3.3

how to excludes and include task in ant script

I want to copy only specific jar file using ant script. It is very easy task but i am not able to do this. I have written the below ant script for this task. I want to excludes all jar file except aopalliance-.jar file. But when i run this script it excludes all the jar file. can someone correct this script .
<?xml version="1.0" encoding="UTF-8"?>
<project default="mvcexample" name="MVCExample" basedir=".">
<property name="web.dir" value="WebContent" />
<property name="webinf.dir" value="WebContent/WEB-INF" />
<property name="lib.dir" value="WebContent/WEB-INF/lib" />
<property name="lib2.dir" value="WebContent/WEB-INF/lib2" />
<target name="copyjar">
<copy todir="${lib2.dir}">
<fileset dir="${lib.dir}" excludes="**/*.jar">
<include name="${lib.dir}/aopalliance-.jar"></include>
</fileset>
</copy>
</target>
</project>
Here's what you need to do:
<target name="copyjar">
<copy todir="${lib2.dir}">
<fileset dir="${lib.dir}">
<!-- <include name="${lib.dir}/aopalliance-.jar"/> -->
<include name="aopalliance-.jar"/> <!-- Don't put the dir name! -->
</fileset>
</copy>
</target>
When you specify a directory, you merely specify the name pattern under that directory. What you were asking for was the jar ${lib.dir}/${lib.dir}/aopalliance-.jar. By the way, is there suppose to be a version number in there somewhere? Like aopalliance-3.4.jar? If so, you need <include name="aopalliance-*.jar"/>.
Also notice that if I used <include .../> instead of <include...></include>.

JSP is not getting copied while creating war using Ant

I am using following Ant script to create a war of simple web application.
<?xml version="1.0" encoding="UTF-8"?>
<project name="MyProject" default="war">
<path id="compile.classpath">
<fileset dir="WebContent/WEB-INF/lib">
<include name="*.jar" />
</fileset>
</path>
<target name="compile">
<javac destdir="WebContent/WEB-INF/classes" debug="true" srcdir="src">
<classpath refid="compile.classpath" />
</javac>
</target>
<target name="war" depends="compile">
<war destfile="build/myproject.war" webxml="WebContent/WEB-INF/web.xml">
<fileset dir="WebContent">
<include name="**/*.jsp" />
</fileset>
<lib dir="WebContent/WEB-INF/lib" />
<classes dir="WebContent/WEB-INF/classes" />
</war>
</target>
</project>
It's creating the war but when I am opening the war, it's not containing JSP files due to which application is not running. Any idea what is wrong?
Also, right now I am coping war manually in Weblogic. Is there any Ant command which can deploy war?
I don't know exact answer but here is my way of using Ant build.xml for webapps. Give it a try. This works inside Eclipse or run from the command line. Few key points are:
build.xml has reference to compile-time libraries, including servlet-api.jar
dynamic META-INF/MANIFEST.MF
separate targets for compile, jar and war tasks to allow easier per project custom rules
webapp war don't have individual .class files but compiled web-inf/lib/mywebapp.jar library to minimize filesystem noice
you may create web/WEB-INF/classes/ folder and put some .properties file or extreme case "binary provided" class files. They are put inside war package along with other jsp,html,js files.
folder structure is very streamlined, I can use mywebapp/web/ folder directly in Tomcat service during development. Each html, jsp etc changes are reflected at runtime. Compiling jar triggers Tomcat to reload webapp instance.
Use this common folder structure for webapp project.
/mywebapp/ant.bat
/mywebapp/build.xml
/mywebapp/classes/
/mywebapp/src/
/mywebapp/src/META-INF/MANIFEST.MF
/mywebapp/lib/
/mywebapp/web/
/mywebapp/web/WEB-INF/web.xml
/mywebapp/web/WEB-INF/lib/
/mywebapp/web/META-INF/context.xml
mywebapp/build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="mywebapp" default="build" basedir=".">
<property name="name" value="${ant.project.name}" />
<property name="classes" value="./classes" />
<property name="src" value="./src" />
<property name="webdir" value="./web" />
<property name="version" value="1.0"/>
<property environment="env"/>
<path id="libs">
<pathelement location="lib/servlet-api.jar" />
<pathelement location="web/WEB-INF/lib/somelib1.jar" />
<pathelement location="web/WEB-INF/lib/somelib2.jar" />
<pathelement location="web/WEB-INF/lib/gson-2.2.4.jar" />
</path>
<tstamp>
<format property="TODAY" pattern="yyyy-MM-dd HH:mm:ss" />
</tstamp>
<target name="updatemanifest" description="Update manifest">
<buildnumber file="build.num"/>
<copy file="${src}/META-INF/MANIFEST.MF"
todir="${classes}/META-INF/" overwrite="true" preservelastmodified="true"
/>
<manifest file="${classes}/META-INF/MANIFEST.MF" mode="update">
<attribute name="Implementation-Version" value="${version}.${build.number} (${TODAY})" />
<attribute name="Implementation-Title" value="${name}" />
</manifest>
</target>
<target name="clean" description="Clean compiled classes">
<delete dir="${classes}" />
</target>
<target name="compile" depends="clean" description="Compile classes">
<mkdir dir="${classes}"/>
<javac srcdir="${src}" destdir="${classes}" target="1.6" source="1.6" encoding="ISO-8859-1"
debug="true" debuglevel="lines,source"
excludes="" includeantruntime="false" >
<classpath refid="libs" />
<compilerarg value="-Xlint:deprecation" />
</javac>
</target>
<target name="jar" depends="updatemanifest" description="Create a .jar file">
<echo message="Build release: ${release}" />
<jar
manifest="${classes}/META-INF/MANIFEST.MF"
jarfile="${webdir}/WEB-INF/lib/${name}.jar" >
<fileset dir="${classes}">
</fileset>
</jar>
</target>
<target name="war" depends="compile,jar" description="Create a .war file">
<delete file="${name}.war" />
<zip destfile="${name}.war"
basedir="${webdir}"
excludes="
**/CVS*
"
/>
</target>
<target name="build" depends="war" description="Build lib">
</target>
</project>
src/META-INF/MANIFEST.MF
Implementation-Title: myappname
Implementation-Version: 1.0.0 (2010-03-01)
Implementation-Vendor: My Name Ltd.
Implementation-URL: http://www.myname.com
mywebapp/build.bat
call c:\apache-ant-1.7.0\bin\ant.bat build
pause
Build script creates war package and manifest.mf within web-inf/lib/mywebapp.jar is updated to have build number, title and version. Very handy you can use folder content as a template for new webapp projects. Just edit build.xml to have new project name.
Some compile-time dependencies point mywebapp/web-inf/lib folder. Non war-packaged libraries are put to mywebapp/lib/ folder for compile time only. I like keeping each dependency within project version control so thats a reason for this lib folder. You may use *.jar wildcard ant syntax but I explictly list each file for self documentation purpose.
Here is a bonus file to be used in Tomcat during development time. It publishes webapp on Tomcat and any changes in project folder is seen immediately, its very handy for client file changes (html,js,jsp).
this file is a copypaste from mywebapp/web/META-INF/context.xml file but an explicit docBase attribute is added.
It directs Tomcat to use files directly from project folder, no redeployment needed at runtime
Start tomcat and keep it running, you may run several webapp projects withing same Tomcat instance. Sometimes bigger development projects need it.
Remote debugging hook requires some java magic not included here
tomcat/conf/Catalina/localhost/mywebapp.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="C:/mywebapp/web"
debug="0" reloadable="true" crossContext="true" >
<!--
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127.0.0.1" />
-->
<!--
<Valve className="org.apache.catalina.valves.RequestDumperValve"/>
-->
<!-- pooled db connection -->
<Resource name="jdbc/mywebappDB" auth="Container" type="javax.sql.DataSource"
maxActive="10" maxIdle="2" maxWait="20000"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
username="myuserid" password="mypwd"
url="jdbc:sqlserver://mysqlserv1.com:1433;DatabaseName=MyDB;applicationName=mywebapp"
validationQuery="SELECT 1"
/>
<!-- <ResourceLink name="jdbc/mywebappDB" global="jdbc/mywebappDB" type="javax.sql.DataSource" /> -->
<Resource name="jdbc/mywebappDB2" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="20" maxWait="10000"
driverClassName="com.mysql.jdbc.Driver"
username="myuserid" password="mypwd"
url="jdbc:mysql://localhost:3306/myDB2?useUnicode=true&characterEncoding=utf8"
validationQuery="SELECT 1" removeAbandoned="true" removeAbandonedTimeout="300"
/>
</Context>
ps: Ant build system is fine no matter what some people may say. Go with it as you please.

Common jar task in ant build script

I have a bunch of ant projects that build plug-ins that all conform to the same API. The plug-ins incorporate the third-party libraries that they depend on so that they are self-contained. As much of the behaviour in the individual build scripts was similar, I decided to pull out the common parts to a common build script.
The original build scripts looked something like this:
ProjectA/build.xml:
<project name="ProjectA" basedir=".">
...
<target name="jar">
<jar destfile="${project.target}" manifest="manifest.mf">
<fileset dir="${project.build.bin.dir}" />
<zipfileset src="externlib1.jar" />
<zipfileset src="externlib2.jar" />
</jar>
</target>
...
</project>
ProjectB/build.xml:
<project name="ProjectB" basedir=".">
...
<target name="jar">
<jar destfile="${project.target}" manifest="manifest.mf">
<fileset dir="${project.build.bin.dir}" />
<zipfileset src="externlib2.jar" />
<zipfileset src="externlib3.jar" />
</jar>
</target>
...
</project>
Here is what my build scripts look like after refactoring:
Common.xml:
<project name="Common" basedir=".">
...
<target name="jar">
<jar destfile="${project.target}" manifest="common-manifest.mf">
<fileset dir="${project.build.bin.dir}" />
<zipfileset refid="extern.libs" />
</jar>
</target>
...
</project>
ProjectA/build.xml:
<project name="ProjectA" basedir=".">
...
<zipfileset id="extern.libs">
<file file="externlib1.jar" />
<file file="externlib2.jar" />
</zipfileset>
...
<import file="../common.xml" />
</project>
ProjectB/build.xml:
<project name="ProjectB" basedir=".">
...
<zipfileset id="extern.libs">
<file file="externlib2.jar" />
<file file="externlib3.jar" />
</zipfileset>
...
<import file="../common.xml" />
</project>
However, the refactored build does not work--I believe that the problem is that I cannot declare a zipfileset that has multiple files.
I cannot figure out a way that I can declare a fileset such that the behaviour with the common jar task is the same as the behaviour where the jar tasks are declared in each project's build script. Has anyone solved this problem before? Is there a different way I can accomplish the same thing?
It's a bit fiddly, but the zipgroupfileset of the jar task may help.
Something like this for ProjectA should work. I've guessed the directory, adjust as appropriate.
<fileset dir="${project.build.lib.dir}" id="extern.libs">
<include name="externlib1.jar" />
<include name="externlib2.jar" />
</fileset>
Then in the common.xml file, I've renamed your common-manifest.mf.
<target name="jar">
<jar destfile="${project.target}" duplicate="preserve" manifest="common-manifest.mf">
<zipgroupfileset refid="extern.libs"/>
</jar>
</target>

Resources