failed to compress javascript files using yuicompressor - java

I have an ant file which contains a task to compress certain js files. I am using yui compressor to compress my files. So I have defined the taskdef
<taskdef resource="yuicompressor.tasks" classpath="lib/yuicompressor-taskdef-1.0.jar;lib/yuicompressor-2.4.2.jar" />
and this is how I am using yui compressor ant task
<yuicompressor todir="./js/" verbose="true">
<fileset dir="./js/"
includes="**/*.js">
</fileset>
<mapper type="glob" from="*.js" to="*.js" />
</yuicompressor>
When I run this ant file directly it works fine, that is all js files get compressed.
But when I run this ant file from pom.xml then it shows
Failed to compress files file_name.js
This is my execution task in pom.xml
<execution>
<id>default</id>
<phase>generate-sources</phase>
<configuration>
<tasks>
<tstamp />
<ant antfile="build.xml" />
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
What could be the possible issue?

In my case I was using the relative path in yuicompressor
<yuicompressor todir="./js/" verbose="true">
<fileset dir="./js/"
includes="**/*.js">
</fileset>
<mapper type="glob" from="*.js" to="*.js" />
</yuicompressor>
I updated to
<yuicompressor todir="${basedir}/js/" verbose="true">
<fileset dir="${basedir}/js/"
includes="**/*.js">
</fileset>
<mapper type="glob" from="*.js" to="*.js" />
</yuicompressor>
And working perfectly fine!!

Related

How to set the install path on a self-contained app JavaFX .deb (Debian) package

I'm trying to generate a .deb file, containing a self-contained copy of my JavaFX application, using the Oracle JavaFX Ant fx:deploy Task.
I've followed the samples on official documentation and my pom.xml configuration looks like this:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>install</phase>
<configuration>
<target xmlns:fx="javafx:com.sun.javafx.tools.ant">
<manifestclasspath property="manifest.classpath" jarfile="${application.dist}/${jar.name}.jar">
<classpath>
<path id="build.classpath">
<fileset dir="${application.dist}/lib">
<include name="*.jar"/>
</fileset>
</path>
</classpath>
</manifestclasspath>
<taskdef resource="com/sun/javafx/tools/ant/antlib.xml"
uri="javafx:com.sun.javafx.tools.ant"
classpath="${project.basedir}:${javafx.lib.ant-javafx.jar}"/>
<fx:preferences id="fxPreferences" shortcut="true" menu="true" install="true"/>
<fx:jar destfile="${application.dist}/${jar.name}.jar">
<fx:application name="${application.title}"
mainClass="${application.main.class}"/>
<manifest>
<attribute name="Class-Path" value="${manifest.classpath}"/>
</manifest>
<!-- The target/classes folder which contains all resources and
class files -->
<fileset dir="${project.build.outputDirectory}"/>
</fx:jar>
<fx:resources id="appRes">
<fx:fileset dir="${classes.dir}" includes="*.ico"/>
<fx:fileset dir="${classes.dir}" includes="*.bat"/>
<fx:fileset dir="${classes.dir}" includes="*.conf"/>
<fx:fileset dir="${application.dist}" includes="*.jar"/>
<fx:fileset dir="${application.dist}" includes="lib/*.jar"/>
<fx:fileset dir="${java.home}/lib/ext" includes="sunjce_provider.jar" type="data"/>
<fx:fileset dir="${java.home}/bin" includes="java.exe" type="data"/>
<fx:fileset dir="${java.home}/bin" includes="javaw.exe" type="data"/>
<fx:fileset dir="${extra.dir}" includes="**"/>
</fx:resources>
<!-- This same fx:deploy task MUST be able to generate a Windows .EXE bundle as well -->
<fx:deploy verbose="true" embedJNLP="false" extension="false" includeDT="false"
offlineAllowed="true" outdir="${application.dist}"
outfile="${bundle.name}" nativeBundles="all" updatemode="background">
<fx:application name="${application.name}" mainClass="${application.main.class}"/>
<fx:resources refid="appRes"/>
<fx:preferences refId="fxPreferences" shortcut="true" menu="true"/>
<fx:info title="${application.title}" vendor="${application.vendor}"/>
<fx:platform javafx="8.0+">
<fx:jvmarg value="-Djava.security.debug=sunpkcs11"/>
</fx:platform>
</fx:deploy>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
At the end of the build (mvn clean install), the .deb file is properly generated at /target/dist/bundles folder.
But when I install it with dpkg -i, my application will be installed at /opt/${bundle.name}.
The DEB packaging options documentation says the default installation location will be /opt, but it don't tell me how can I change it.
My question is: how may I set another installation path to override the default /opt value?
For example, it would be great if I could install it on /opt/company/apps/${bundle.name}.
I've read about the usage of fx:bundleArgument inside fx:deploy, which has fixed options. But none of the linux.* options it's related about installation path.
Environment information:
Debian 8.10 Jessie
JDK 1.8.0_171 64bits
dpkg-deb 1.17.27 (amd64)
Apache Maven 3.0.5

Run testng suite just using jar or war file

I have created a testng test using selenium webdriver and java. Now I don't want to share my code to different users but I want my code to run by different users using jar or war file.
Can anyone help me out with this. Is it possible to run test without sharing testNG java code?
I haven't got an opportunity to work with maven but it should also be possible with maven
Let me tell you the idea by taking an example using ant
Assume you are running your tests using ant with the below build.xml(sample file change according to your needs) file
<project name="TestNG Demo" default="clean" basedir=".">
<!-- ========== Initialize Properties =================================== -->
<property environment="env"/>
<property name="ws.home" value="${basedir}"/>
<property name="ws.jars" value="${ws.home}/Jars"/>
<property name="test.dest" value="${ws.home}/build"/>
<property name="test.src" value="${ws.home}/src"/>
<property name="ng.result" value="test-output"/>
<target name="setClassPath" unless="test.classpath">
<path id="classpath_jars">
<fileset dir="${ws.jars}" includes="**/*.jar"/>
<pathelement path="${ws.jars}"/>
</path>
<pathconvert pathsep=":"
property="test.classpath"
refid="classpath_jars"/>
</target>
<target name="init" depends="setClassPath">
<tstamp>
<format property="start.time" pattern="MM/dd/yyyy hh:mm aa" />
</tstamp>
<condition property="ANT"
value="${env.ANT_HOME}/bin/ant.bat"
else="${env.ANT_HOME}/bin/ant">
<os family="windows" />
</condition>
<taskdef name="testng" classpath="${test.classpath}"
classname="org.testng.TestNGAntTask" />
</target>
<!-- all -->
<target name="all">
</target>
<!-- clean -->
<target name="clean">
<delete dir="${test.dest}"/>
</target>
<!-- compile -->
<target name="compile" depends="init, clean" >
<delete includeemptydirs="true" quiet="true">
<fileset dir="${test.dest}" includes="**/*"/>
</delete>
<echo message="making directory..."/>
<mkdir dir="${test.dest}"/>
<echo message="classpath------: ${test.classpath}"/>
<echo message="compiling..."/>
<javac
includeantruntime="false"
debug="true"
destdir="${test.dest}"
srcdir="${test.src}"
target="1.6"
classpath="${test.classpath}"
>
</javac>
</target>
<!-- build -->
<target name="build" depends="init">
</target>
<!-- run -->
<target name="run" >
<testng classpath="${test.classpath}:${test.dest}" suitename="suite">
<xmlfileset dir="${ws.home}" includes="testng.xml"/>
</testng>
</target>
<target name="usage">
<echo>
ant run will execute the test
</echo>
</target>
<path id="test.c">
<fileset dir="${ws.jars}" includes="*.jar"/>
</path>
<target name="makexsltreports">
<mkdir dir="${ws.home}/XSLT_Reports/output"/>
<xslt in="${ng.result}/testng-results.xml" style="src/demo/testng-results.xsl"
out="${ws.home}/XSLT_Reports/output/index.html" classpathref="test.c" processor="SaxonLiaison">
<param name="testNgXslt.outputDir" expression="${ws.home}/XSLT_Reports/output/"/>
<param name="testNgXslt.showRuntimeTotals" expression="true"/>
</xslt>
</target>
<!-- ****************** targets not used ****************** -->
</project>
Now after test compiles(use target ant compile to compile your tests) you will get the class files in build folder inside your project folder and now you can delete the src folder(your java files) and to use ant run to execute the tests(use target ant run) to run your tests.If your planning to give it your client then you can make a simple bat(windows) or shellscript(linux) to execute command ant run and on clicking on it the test will run
Hope it helps you..Kindly get back if you have any queries
As #vicky suggests, look at Maven, as it'll allow you to package up your project with both a production WAR/JAR and also the tests packaged up into a test JAR. Specifically at the test-jar JAR type you can label JARs with.
So add this as a plugin to the pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
And then others can pull it as a dependency using:
<dependency>
<groupId>com.foo</groupId>
<artifactId>bar</artifactId>
<version>1.2.3</version>
<type>test-jar</type>
<scope>test</scope>
A bit more information here: How do I install a test-jar in maven?

Maven concat files specific files in a directory

I have a directory with unknown depth and folder names.
>A
-> AB
--> configuration.xml
--> ABC
---> configuration.xml
-> AD
--> configuration.xml
-> allconfigurations.xml
I need a maven plugin to concat all the configuration.xml files and create allconfigurations.xml file in the root. Unfortunately folder names and depth is unknown. It would be great to accomplish it within the pom.xml file without needing any other files.
just a quick google for you: the maven-shade-plugin with the XmlAppendingTransformer could help.
sample config is here
While struggling I realized that the real problem is to put a header and footer for the compiled allconfigurations.xml file, since each configuration.xml file is a fragment and when I concat all of them together resulting xml is not a valid xml.
here is the case;
an xml file is something like:
<Configuration xmlns="abc">
...
<connectionTimeoutInMs>240000</connectionTimeoutInMs>
<socketTimeoutInMs>240000</socketTimeoutInMs>
<persist>false</persist>
<internal>false</internal>
...
</Configuration>
and putting many of them is not valid thus result xml must be something like;
<AllConfigurations xmlns="abc">
...
<Configuration xmlns="abc">
...
</Configuration >
...
<AllConfigurations xmlns="abc">
so the first and last lines must be added to the result.
here is the solution I came up with;
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>default-cli</id>
<phase>generate-resources</phase>
<goals>
<goal>run</goal>
</goals>
<!--<phase>process-resources</phase>-->
<!--<phase>compile</phase>-->
<configuration>
<target>
<concat destfile="${project.basedir}/.../allConfigurations.xml"
force="yes">
<fileset dir="${project.basedir}/...">
<include name="xmlHeaderForConfiguration"></include>
</fileset>
<fileset dir="${project.basedir}/...">
<include name="**/configuration.xml"></include>
</fileset>
<fileset dir="${project.basedir}/...">
<include name="xmlFooterForConfiguration"></include>
</fileset>
</concat>
</target>
</configuration>
</execution>
</executions>
</plugin>
where xmlHeaderForConfiguration is a file with content; <AllConfigurations xmlns="abc"> and xmlHeaderForConfiguration has </AllConfigurations>

Executable war file that starts jetty without maven

I'm trying to make an "executable" war file (java -jar myWarFile.war) that will start up a Jetty webserver that hosts the webapp contained in the WAR file I executed.
I found a page that described how to make what I'm looking for:
However, following that advice along with how I think I'm supposed to make an executable jar (war) isn't working.
I have an Ant task creating a WAR file with a manifest that looks like:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.1
Created-By: 1.5.0_18-b02 (Sun Microsystems Inc.)
Main-Class: Start
The contents of the WAR file look like:
> Start.class
> jsp
> build.jsp
> META-INF
> MANIFEST.MF
> WEB-INF
> lib
> jetty-6.1.22.jar
> jetty-util.6.1.22.jar
When I try to execute the WAR file, the error is:
Exception in thread "main" java.lang.NoClassDefFoundError: org/mortbay/jetty/Handler
Caused by: java.lang.ClassNotFoundException: org.mortbay.jetty.Handler
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: Start. Program will exit.
There appears to be two errors here: one where it seems the JAR files can't be found, and one where the Start class can't be found.
To fix the first one, I put the Jetty JAR files in the base of the WAR file and tried again -- same error. I also tried adding the WEB-INF/lib/<specific-JAR-files> to the Class-Path attribute of the manifest. That did not work either.
Does anyone have any insight as to what I'm doing right/wrong and how I can get this executable WAR file up and running?
The link you have in your question provides most of what you need. However, there are a few things that need to be done in addition to that.
Any class files that Jetty needs to start up will need to be located at the root of the war file when it's packaged. We can leverage Ant to do that for us before we <war> the file. The war's manifest file will also need a Main-Class attribute to execute the server.
Here's a step-by-step:
Create your Jetty server class:
This is adapted from the link you provided.
package com.mycompany.myapp;
import java.io.File;
import java.net.URL;
import java.security.ProtectionDomain;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.webapp.WebAppContext;
public final class EmbeddedJettyServer
{
public static void main(String[] args) throws Exception
{
int port = Integer.parseInt(System.getProperty("port", "8080"));
Server server = new Server(port);
ProtectionDomain domain = EmbeddedJettyServer.class.getProtectionDomain();
URL location = domain.getCodeSource().getLocation();
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
webapp.setDescriptor(location.toExternalForm() + "/WEB-INF/web.xml");
webapp.setServer(server);
webapp.setWar(location.toExternalForm());
// (Optional) Set the directory the war will extract to.
// If not set, java.io.tmpdir will be used, which can cause problems
// if the temp directory gets cleaned periodically.
// Your build scripts should remove this directory between deployments
webapp.setTempDirectory(new File("/path/to/webapp-directory"));
server.setHandler(webapp);
server.start();
server.join();
}
}
To see what all you can configure here, have a look at the Jetty API documentation.
Build the war with Ant:
This uses a staging directory to unpack the necessary class files into the root of the war so they're accessible when the war is executed.
<target name="war" description="--> Creates self-executing war">
<property name="staging.dir" location="${basedir}/staging"/>
<property name="webapp.dir" location="${basedir}/src/webapp"/>
<mkdir dir="${staging.dir}"/>
<!-- assumes you have all of your war content (excluding classes and libraries) already structured in a directory called src/webapp -->
<!-- e.g. -->
<!-- src/webapp/index.html -->
<!-- src/webapp/WEB-INF/web.xml -->
<!-- src/webapp/WEB-INF/classes/my.properties -->
<!-- etc ... -->
<copy todir="${staging.dir}">
<fileset dir="${webapp.dir}" includes="**/*"/>
</copy>
<unjar dest="${staging.dir}">
<!-- you'll have to locate these jars or appropriate versions; note that these include JSP support -->
<!-- you might find some of them in the downloaded Jetty .tgz -->
<fileset dir="path/to/jetty/jars">
<include name="ant-1.6.5.jar"/>
<include name="core-3.1.1.jar"/>
<include name="jetty-6.1.24.jar"/>
<include name="jsp-2.1-glassfish-2.1.v20091210.jar"/><!-- your JSP implementation may vary -->
<include name="jsp-api-2.1-glassfish-2.1.v20091210.jar"/><!-- your JSP implementation may vary -->
<include name="servlet-api-2.5-20081211.jar"/><!-- your Servlet API implementation may vary -->
</fileset>
<patternset><!-- to exclude some of the stuff we don't really need -->
<exclude name="META-INF/**/*"/>
<exclude name="images/**/*"/>
<exclude name=".options"/>
<exclude name="about.html"/>
<exclude name="jdtCompilerAdapter.jar"/>
<exclude name="plugin*"/>
</patternset>
</unjar>
<!-- copy in the class file built from the above EmbeddedJettyServer.java -->
<copy todir="${staging.dir}">
<fileset dir="path/to/classes/dir" includes="com/mycompany/myapp/EmbeddedJettyServer.class"/>
</copy>
<war destfile="myapp.war" webxml="${webapp.dir}/WEB-INF/web.xml">
<fileset dir="${staging.dir}" includes="**/*"/>
<classes dir="path/to/classes/dir"/><!-- your application classes -->
<lib dir="path/to/lib/dir"/><!-- application dependency jars -->
<manifest>
<!-- add the Main-Class attribute that will execute our server class -->
<attribute name="Main-Class" value="com.mycompany.myapp.EmbeddedJettyServer"/>
</manifest>
</war>
<delete dir="${staging.dir}"/>
</target>
Execute the war:
If everything's set up properly above, you should be able to:
java -jar myapp.war
// or if you want to configure the port (since we are using the System property in the code)
java -Dport=8443 -jar myapp.war
This is an adaptation for Maven of #RobHruska's answer. It just copies the files of the main class and merges the Jetty JAR files into the WAR file, nothing new, just to simplify your life if you are new -like me- to Maven:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>move-main-class</id>
<phase>compile</phase>
<configuration>
<tasks>
<copy todir="${project.build.directory}/${project.build.finalName}">
<fileset dir="${project.build.directory}/${project.build.finalName}/WEB-INF/classes/">
<include name="main/*.class" />
</fileset>
</copy>
<unjar dest="${project.build.directory}/${project.build.finalName}">
<!-- you'll have to locate these jars or appropriate versions; note that these include JSP support -->
<!-- you might find some of them in the downloaded Jetty .tgz -->
<fileset dir="${project.build.directory}/${project.build.finalName}/WEB-INF/lib/">
<include name="ant-1.6.5.jar"/>
<!--<include name="core-3.1.1.jar"/>-->
<include name="jetty*"/>
<include name="servlet-api*"/>
</fileset>
<patternset><!-- to exclude some of the stuff we don't really need -->
<exclude name="META-INF/**/*"/>
<exclude name="images/**/*"/>
<exclude name=".options"/>
<exclude name="about.html"/>
<exclude name="jdtCompilerAdapter.jar"/>
<exclude name="plugin*"/>
</patternset>
</unjar>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<archiveClasses>true</archiveClasses>
<archive>
<manifest>
<mainClass>main.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
We have figured this out by using jetty-console-maven-plugin.
Whenever you run mvn package it creates another war that can be used with java -jar whateverpackage-runnable.war
<plugin>
<groupId>org.simplericity.jettyconsole</groupId>
<artifactId>jetty-console-maven-plugin</artifactId>
<version>1.45</version>
<executions>
<execution>
<goals>
<goal>createconsole</goal>
</goals>
</execution>
</executions>
<configuration>
<additionalDependencies>
<additionalDependency>
<artifactId>jetty-console-requestlog-plugin</artifactId>
</additionalDependency>
<additionalDependency>
<artifactId>jetty-console-gzip-plugin</artifactId>
</additionalDependency>
<additionalDependency>
<artifactId>jetty-console-ajp-plugin</artifactId>
</additionalDependency>
<additionalDependency>
<artifactId>jetty-console-startstop-plugin</artifactId>
</additionalDependency>
</additionalDependencies>
</configuration>
</plugin>
It also generates the init.d scripts and everything for you!
Hudson solves this exact problem using the Winstone servlet container, which supports this use case directly. http://winstone.sourceforge.net/#embedding
Perhaps this would work for you?
Even though this is kind of old another alternative with Jetty 8 is to simply include the Jetty jars as dependencies in your pom and add the following in your pom (versus an ant script that unpackages the war and repackages it):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>JettyStandaloneMain</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<!-- The main class needs to be in the root of the war in order to be
runnable -->
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>move-main-class</id>
<phase>compile</phase>
<configuration>
<tasks>
<move todir="${project.build.directory}/${project.build.finalName}">
<fileset dir="${project.build.directory}/classes/">
<include name="JettyStandaloneMain.class" />
</fileset>
</move>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
I take it that by "without maven" you want a jar that you can run by itself and not with "mvn jetty:run"--not that you don't want to use maven at all.
It took me way to long to figure this out because I found many options--none of them dirt simple. Eventually I found this maven plugin from simplericity. It works wonderfully.
This is my example ANT extract. The idea is to unpackage the Jetty dependencies and then include them locally just like a normal JAR file:
<!-- Hack: Java doesn't support jars within jars/wars -->
<unjar src="${lib.dir}/container/jetty.jar" dest="${build.dir}/unjar"/>
<unjar src="${lib.dir}/container/jetty-util.jar" dest="${build.dir}/unjar"/>
<unjar src="${lib.dir}/container/servlet-api.jar" dest="${build.dir}/unjar"/>
<unjar src="${lib.dir}/container/jsp-api.jar" dest="${build.dir}/unjar"/>
<!-- Build war file as normal, just including the compiled and unjar'ed files -->
<war destfile="${war.file}" webxml="${config.dir}/web.xml">
<fileset dir="${build.dir}/classes"/>
<fileset dir="${build.dir}/unjar"/>
<fileset dir="${resources.dir}" excludes="*.swp"/>
<lib dir="${lib.dir}/runtime"/>
<manifest>
<attribute name="Main-Class" value="Start"/>
</manifest>
</war>
Note:
The WEB-INF/lib direcory is for the web applications dependencies. In this case we're packaging the WAR file so that it works like the normal Jetty JAR file on startup
Putting .jars inside a .war file root does nothing
Putting .jars inside WEB-INF/lib doesn't help the JVM find the Jetty files to even begin launching your .war. It's "too late" to put them there.
Putting .jars in the manifest Class-Path only works for external .jar files, not those contained in the .jar
So what to do?
Use a build script to simply merge all the .jar files you need into the .war file. This takes a little extra work. It's also a bit ugly in that the compiled code is part of the servable files in the .war
Add dependent .jars to the JVM's classpath with "java -cp jetty.jar:... ..." Works though this kind of defeats the purpose of one stand-alone .war
I have done a similar thing before but are you launchign the app as "java -jar xxx.war" ?. You have only 2 jars and it is not going to be enough I think. Also try using Jetty 7.0.0M1 (which is the latest version). When I added jetty-server and jetty-webapp as two dependencies (they are from org.eclipse.jetty) I get the following jar's in the lib directory. FYI the org.mortbay.jetty.Handler was in the jetty-server*.jar.
jetty-continuation-7.0.0.M1.jar
jetty-http-7.0.0.M1.jar
jetty-io-7.0.0.M1.jar
jetty-security-7.0.0.M1.jar
jetty-server-7.0.0.M1.jar
jetty-servlet-7.0.0.M1.jar
jetty-util-7.0.0.M1.jar
jetty-webapp-7.0.0.M1.jar
jetty-xml-7.0.0.M1.jar

Using ant, rename a directory without knowing the full path?

Given a zipfile with an unknown directory, how can I rename or move that directory to a normalized path?
<!-- Going to fetch some stuff -->
<target name="get.remote">
<!-- Get the zipfile -->
<get src="http://myhost.com/package.zip"
dest="package.zip"/>
<!-- Unzip the file -->
<unzip src="package.zip"
dest="./"/>
<!-- Now there is a package-3d28djh3 directory. The part after package- is
a hash and cannot be known ahead of time -->
<!-- Remove the zipfile -->
<delete file="package.zip"/>
<!-- Now we need to rename "package-3d28djh3" to "package". My best attempt
is below, but it just moves package-3d28djh3 into package instead of
renaming the directory. -->
<!-- Make a new home for the contents. -->
<mkdir dir="package" />
<!-- Move the contents -->
<move todir="package/">
<fileset dir=".">
<include name="package-*/*"/>
</fileset>
</move>
</target>
I'm not much of an ant user, any insight would be helpful.
Thanks much, -Matt
This will only work as long as the dirset only returns 1 item.
<project name="Test rename" basedir=".">
<target name="rename">
<path id="package_name">
<dirset dir=".">
<include name="package-*"/>
</dirset>
</path>
<property name="pkg-name" refid="package_name" />
<echo message="renaming ${pkg-name} to package" />
<move file="${pkg-name}" tofile="package" />
</target>
</project>
If there are no subdirectories inside the package-3d28djh3 directory (or whatever it is called once you extracted it) you can use
<move todir="package" flatten="true" />
<fileset dir=".">
<include name="package-*/*"/>
</fileset>
</move>
Otherwise, use the regexp mapper for the move task and get rid of the package-xxx directory:
<move todir="package">
<fileset dir=".">
<include name="package-*/*"/>
</fileset>
<mapper type="regexp" from="^package-.*/(.*)" to="\1"/>
</move>

Categories