Mapping directories to names in Ant
I deploy my packages in separate Jars (one package per Jar). This leads to less efficient Zip compression across the range of Jars, but is more efficient for launch using Java Web Start, given lazy downloads and incremental updates.
The build tool is Ant. At the moment, I have a separate task call for each directory of class files. That results in a lot of jar tasks and a long build file. I'd like to shorten it drastically by moving the manifest details into a separate file in each directory/package, and using a foreach (or similar) task to Jar the directory using the manifest, using the 'tail' of the package structure as the name of the Jar.
E.G. org/pscode/ui/widget/
package should be mapped to ui.widget.jar
(the org.pscode.
prefix is not required).
The thing I am having most trouble with, is creating the file name.
Solved
<target name="start" depends="properties">
<foreach target="printOut" param="package">
<path>
<dirset dir="${src}/java/org/pscode" includes="**/*" />
</path>
</foreach>
</target>
<target name="printOut">
<pathconvert property="prop" dirsep=".">
<path location="${package}"/>
</pathconvert>
<script language="javascript">
<![CDATA[
prop1 = projectName.getProperty("prop");
index = prop1.lastIndexOf("pscode");
prop2 = prop1.substring(index+7, prop.length);
projectName.setProperty("prop2", prop2);
]]>
</script>
<echo message="Package jar name: ${prop2}.jar"/>
</target>
This produces output along the lines of..
Buildfile: build.xml
properties:
[pathconvert] org
[pathconvert] pscode
[pathconvert] starzoom
[pathconvert] ui
..
[pathconvert] serialize
start:
printOut:
[echo] Package jar name: starzoom.jar
printOut:
[echo] Package jar name: ui.jar
...
printOut:
[echo] Package jar name: xui.security.serialize.jar
BUILD SUCCESSFUL
Total time: 0 seconds
Tool completed successfully
The JavaScript snippet was inspired by the post by pdeva on the thread ant string manipulation : extracting characte开发者_如何学Gors from a string.
See below for a slightly adjusted version, which hopefully is on the way to what you need.
Note that by default ant-contrib foreach
doesn't propagate properties
to the called target, so you need to nest an additional param
to pass the src
value. That value is needed in order to strip off
the leading part of the path.
I've added a sample properties target,
to illustrate that you might as well include
the full path to the 'root' directory of the jar naming scheme in the src
path.
You'll see that the pathconvert
task uses the passed in package
and src
values to construct the name (prop
) for the jar file. You should be able to cook up a jar
task to do the workfrom there.
I notice that as you've specified a dirset
, so you'll get all directories in
the hierarchy, which might include ones with no classes.
May not be a problem for you.
<target name="properties">
<property name="src" location="src/java/org/pscode" />
</target>
<target name="start" depends="properties">
<foreach target="printOut" param="package">
<param name="src" value="${src}" />
<path>
<dirset dir="${src}" includes="**/*" />
</path>
</foreach>
</target>
<target name="printOut">
<pathconvert property="prop" dirsep=".">
<path location="${package}"/>
<map from="${src}/" to=''/>
</pathconvert>
<echo message="Package jar name: ${prop}.jar"/>
</target>
精彩评论