Ant File, include another file - java

Let me describe the scenario first. I have a main build file called main_build.xml. This file calls 4 other build files defined for sub projects, copies the Jars generated by sub project builds and then creates a WAR and finally EAR.
I have my classpath dependencies defined in a XML file named my_clspath.xml.
Now I have two questions:
1. How do I include my_clspath.xml file within the main_build.xml?
2. After including the my_clspath.xml in main_build, how do I make the classpath available for all 4 sub builds which are called from this main_build?.
my_clspath.xml content:
<fileset dir="../myApp_Ear/build/">
<include name="**/*.jar" />
</fileset>
<fileset dir="../myApp_Ear/lib/">
<include name="**/*.jar" />
</fileset>

Try
<project name="Your Project">
<include file="my_clspath.xml" />
<ant antfile="sub1.xml" inheritAll="true" inheritRefs="true" />
</project>
Also, you should set an id on the Filesets. Just having them in your Ant file does not do anything.
<fileset dir="../myApp_Ear/build/" id="my.EAR.fileset">
<include name="**/*.jar" />
</fileset>
Then, in sub1.xml, you can reference the filesets with <fileset refid="my.EAR.fileset" />
Hope this helps.

You should really take a look at the <subant> task. This does exactly what you're asking: You have a master build.xml file that's calling some sub-ant build files to build basic components.

Related

Simultaneously copying and renaming directory in ant

I've looked through ant tasks like copy, but can't really seem to find a way. With this kind of file structure:
/Resources/Docs/SpecificDoc/ScalaDocs
I want to rename "ScalaDocs" to "TargetDocs", having an end structure like so:
/Resources/Docs/TargetDocs
Try this.
<copy todir="/Resources/Docs/TargetDocs">
<fileset dir="/Resources/Docs/SpecificDoc/ScalaDocs">
<include name="*" />
</fileset>
</copy>

Ant can't find classpath classes

So I'm extending my company's ant build script to add in a special module we want build in some cases. I've written an ant script that points to where I know the compiled class files for the rest of our codebase are, because they get compiled earlier in the build process. I know with 100% certainty the files are in this location.
However, whenever I try to compile this module, the classpath reference can't see those classes, and I get a bunch of "package does not exist" and "can't find symbol" errors.
I just can't seem to figure out what I'm doing wrong. Hoping for help here.
Here's my build script code:
<property name="classpath" value="${dir.dev}/out/production/Main"
<path id="pfClasspath">
<fileset dir="${classpath}">
<include name="**/*.class"/>
</fileset>
<fileset dir="${dir.dev.lib}">
<include name="**/*.jar" />
</fileset>
<fileset file="${lib.json}" /> <!-- TODO try removing this -->
</path>
<target name="compile" depends="prepare">
<javac source="1.7" classpathref="pfClasspath" srcdir="${dir.project}/src" destdir="${dir.project.build.classes}" />
</target>
The directory the "classpath" property is pointing at 100% contains all of the class files for the rest of the project. That level is the equivalent of the "src" directory on the sources side, immediately within it are the com/companyName/etc... folders.
My code contains references to the classes compiled at this location. Yet ant isn't finding them. Any help?
Try
<path id="pfClasspath">
<pathelement path="${classpath}" />
...
</path>
instead. Specifying the classpath does not mean to specify every single class file that's on the classpath, which is what you do when you define your <path> element using a <fileset>.

Ant copy classpath jars to a directory

I'm sure this has either been asked before or is pretty straightforward. But for whatever reason, I cannot seem to make it work. I want to use ant to copy the ${build.classpath} (which contains a colon separated list of jars) to the ${output.dir}/myapp/WEB-INF/lib.
I have this right now and it doesn't seem to work:
<copy toDir="${output.dir}/myapp/WEB-INF/lib">
<fileset file="${build.classpath}" />
</copy>
It treats the whole classpath as one file. How do I get this to work?
The Ant Manual on the copy task contains the answer for your problem. One of the example snippets it provides:
Collect all items from the current CLASSPATH setting into a destination directory, flattening the directory structure.
<copy todir="dest" flatten="true">
<path>
<pathelement path="${java.class.path}"/>
</path>
</copy>
I think somethink like this should work:
<copy todir="${output.dir}/myapp/WEB-INF/lib" verbose="yes" flatten="yes" failonerror="no">
<fileset dir="${build.classpath}">
<include name="*.jar" />
</fileset>
</copy>
or with wildcard in include: <include name="**/*.jar" />
I think you should put all your colon separated jar files to one root folder. If it is not possible then create a separate task that put those jar files into one folder(may be temporary). And assign ${build.classpath} to that folder. Use <fileset dir="${build.classpath}"/> in your copy clause.
I hope, it should help.

Linked folder not resolved by Ant in eclipse

In my build.xml, below works fine :-
<path id="build.classpath">
<fileset dir="lib [myUtils]" includes="*.jar" />
</path>
if lib [myUtils] is of folder type, but don't works, if it's of Linked Folder type.
Also, I found this when googled :-
https://bugs.eclipse.org/bugs/show_bug.cgi?id=265960
https://bugs.eclipse.org/bugs/show_bug.cgi?id=43081
https://bugs.eclipse.org/bugs/show_bug.cgi?id=265960
Is there any trick to achieve this, without copying the dependencies in work folder??
Note that ant should work outside of eclipse as well. So you can't reply in IDE abstractions. You can use symbolic links (if your OS supports them).
If not, you can use the FileSync plugin to synchronize eclipse project folders with external folders. Or you can simply use the <copy> ant task.
I resolved this using copy task:
<copy todir="target/web/linked1">
<fileset dir="../linkdProject/source1" />
</copy>
<copy todir="target/web/linked2">
<fileset dir="../linkedProject/source2" />
</copy>
....
<war destfile="target/webApp.war">
<fileset dir="WebContent" />
<fileset dir="target/web" /> <!-- copy linked resources -->
...
</war>
<delete dir="target"/>

How to perform Ant path mapping in a 'war' task?

I have several JAR file pattern sets, like
<patternset id="common.jars">
<include name="external/castor-1.1.jar" />
<include name="external/commons-logging-1.2.6.jar" />
<include name="external/itext-2.0.4.jar" />
...
</patternset>
I also have a 'war' task containing a lib element:
<lib dir="${src.dir}/jars">
<patternset refid="common.jars"/>
<patternset refid="web.jars"/>
...
</lib>
Like this however, I end up with WEB-INF/lib containing the subdirectories from my patterns:
WEB-INF/lib/external/castor-1.1.jar
WEB-INF/lib/external/...
Is there any way to flatten this, so the JAR files appear at the top-level under WEB-INF/lib, regardless of the directories specified in the patterns? I looked at mapper but it seems you cannot use them inside lib.
I was dissatisfied with having to manually generate a WAR directory or declare library subdirectories, so I came up with this. Seems to work, obviously you'll need to adjust it to your own needs:
<war destfile="build/output.war" webxml="${web.dir}/WEB-INF/web.xml">
<fileset dir="${web.dir}" />
<mappedresources>
<fileset dir="${lib.dir}" includes="**/*.jar" />
<chainedmapper>
<flattenmapper/>
<globmapper from="*.jar" to="WEB-INF/lib/*.jar" />
</chainedmapper>
</mappedresources>
</war>
You can try to use mappedresources element (Ant 1.8+)
<mappedresources>
<!-- Fileset here -->
<globmapper from="external/*.jar" to="*.jar"/>
</mappedresources>
http://ant.apache.org/manual/Types/resources.html#mappedresources
If you have a typical web project it is better to split the web-app libraries and general-purpose libraries in different folders and leave WEB-INF/lib to have only those needed at runtime. This way you'll avoid collisions and also your project will look much clearer to other developers.

Categories

Resources