You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Pavel Krupets <pk...@yahoo.com> on 2005/06/24 22:52:43 UTC

Unjar + Jar (check if up-to-date)

Hello,

I need to unjar jar files into directory and then pack this folder
into one jar. But I don't know how to check whether I need to update
this jar or not (I need to check whether source jars are older or
have same age as the target jar).

Thank you!

With regards,
Pavel Krupets


		
____________________________________________________ 
Yahoo! Sports 
Rekindle the Rivalries. Sign up for Fantasy Football 
http://football.fantasysports.yahoo.com

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


Re: Unjar + Jar (check if up-to-date)

Posted by "Alexey N. Solofnenko" <A....@mdl.com>.
I usually create a timestamp file with <touch> and use <uptodate> to see 
if zip file was updated.

- Alexey.

Pavel Krupets wrote:
> Hello,
>
> Problem is that it always unzip files. I want to check it before
> unzipping.
>
> With regards,
> Pavel Krupets
>
>   

-- 
------------------------------------------------------------------------
/ Alexey N. Solofnenko
home: http://trelony.cjb.net/
/

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


RE: Unjar + Jar (check if up-to-date)

Posted by Dominique Devienne <dd...@gmail.com>.
> From: Pavel Krupets [mailto:pkrupets@yahoo.com]
> 
> Problem is that it always unzip files. I want to check it before
> unzipping.

Pavel, I've used the custom extension of <unzip> included below
to deal with pretty much the same issue you're faced with.

Just compile and taskdef it to start using it instead of Ant's
regular unzip, and it will only unzip when new or modified files
are in the zip/jar compared to the extracted files on disk,
and thus <jar> will also only re-jar when something changes.

One caveat though, is that it doesn't 'sync', i.e. if a file
is removed from the zip (jar), if won't remove it from disk,
so you'll keep on jarring it. --DD

import java.io.File;
import java.io.IOException;

import java.util.Date;
import java.util.Enumeration;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.util.FileUtils;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.taskdefs.Expand;
import org.apache.tools.ant.types.PatternSet;

import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipEntry;

/**
 * A smarter &lt;unzip&gt;.
 * <p>
 * This version differs from the Ant one in that it preserves the zip
entries
 * modification dates and only expands zip entries which are different on
disk
 * compared to what the archive recorded (name, type, size, last
modification
 * date). It currently does not use the recorded CRC32 hash code against the
 * (would be) computed CRC32 of the disk file though.
 *
 * @author <a href="mailto:ddevienne@lgc.com">Dominique Devienne</a>
 * @version Apr 2004
 */
public class Unzip
             extends Expand {

    /** Whether this unzip filters what gets extracted. */
    private boolean _hasPatterns;

    /**
     * Expands a given ZIP file into the given directory.
     * <p>
     * Unfortunately, I need to duplicate the Ant method...
     *
     * @param  fileUtils the file utils instance to use
     * @param  srcF the source Zip file.
     * @param  dir the target destination directory.
     */
    protected void expandFile(FileUtils fileUtils, File srcF, File dir) {
        ZipFile zf = null;
        try {
            int expandedEntryCount = 0;
            zf = new ZipFile(srcF, "UTF-8"); // can't access encoding!
            for (Enumeration e = zf.getEntries(); e.hasMoreElements();) {
                ZipEntry entry = (ZipEntry) e.nextElement();

                // Systematically skip directories. Files expanded will
                // have their parent dirs created automatically anyways.
                if (entry.isDirectory()) {
                    continue;
                }

                File entryFile = fileUtils.resolveFile(dir,
entry.getName());

                // FIXME: Maybe we should do CRC checking too...
                if (entryFile.exists() &&
                    entryFile.length() == entry.getSize() &&
                    entryFile.lastModified() == entry.getTime()) {
                    log("Skipping " + entryFile + " as it is up-to-date",
                        Project.MSG_DEBUG);
                    continue;
                }

                if (++expandedEntryCount == 1) {
                    log("Expanding: " + srcF + " into " + dir,
                        Project.MSG_INFO);
                }

                // Delegate actual extraction to Ant's <unzip>
                extractFile(fileUtils, srcF, dir, zf.getInputStream(entry),
                            entry.getName(), new Date(entry.getTime()),
                            entry.isDirectory());

                if (!entryFile.setLastModified(entry.getTime())) {
                    // Output warning only if we extract all files.
                    // A ZIP entry could have been filtered out by
                    // a patternset, and we have no way to know...
                    int logLevel = _hasPatterns? Project.MSG_VERBOSE
                                               : Project.MSG_WARN;
                    log("Cannot preserve last modified: " + entryFile,
                        logLevel);
                }
            }

            log("expand complete", Project.MSG_VERBOSE);
        }
        catch (IOException ioe) {
            throw new BuildException("Error while expanding " +
srcF.getPath(),
                                     ioe);
        }
        finally {
            if (zf != null) {
                try {
                    zf.close();
                } catch (IOException e) {
                    //ignore
                }
            }
        }
    }

    /**
     * Forbids setting the overwrite mode.
     * 
     * @param  b ignored!
     * @throws BuildException always!
     */
    public void setOverwrite(boolean b) {
        throw new BuildException("Not supported by <bm:unzip>");
    }

    /**
     * Forbids setting the encoding.
     * <p>
     * The encoding is hard-coded to UTF-8.
     * 
     * @param  encoding ignored!
     * @throws BuildException always!
     */
    public void setEncoding(String encoding) {
        throw new BuildException("Not supported by <bm:unzip>");
    }

    /**
     * Adds a patternset.
     *
     * @param  set the patternset used to filter what gets extracted.
     */
    public void addPatternset(PatternSet set) {
        _hasPatterns = true;
        super.addPatternset(set);
    }

}


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


Re: Unjar + Jar (check if up-to-date)

Posted by Pavel Krupets <pk...@yahoo.com>.
Hello,

Problem is that it always unzip files. I want to check it before
unzipping.

With regards,
Pavel Krupets

--- Jeffrey E Care <ca...@us.ibm.com> wrote:

> Assuming that you preserve the timestamps on the files when you
> unzip the 
> JARs initially the <jar> task should take care of this for you 
> automagically.
> 
> JEC
> -- 
> Jeffrey E. Care (carej@us.ibm.com)
> WebSphere Build SWAT Team Lead
> WebSphere Build Tooling Lead (Project Mantis)
> https://w3.opensource.ibm.com/projects/mantis
> 
> 
> Pavel Krupets <pk...@yahoo.com> wrote on 06/24/2005 04:52:43 PM:
> 
> > Hello,
> > 
> > I need to unjar jar files into directory and then pack this
> folder
> > into one jar. But I don't know how to check whether I need to
> update
> > this jar or not (I need to check whether source jars are older or
> > have same age as the target jar).
> > 
> > Thank you!
> > 
> > With regards,
> > Pavel Krupets

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

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


Re: Unjar + Jar (check if up-to-date)

Posted by Jeffrey E Care <ca...@us.ibm.com>.
Assuming that you preserve the timestamps on the files when you unzip the 
JARs initially the <jar> task should take care of this for you 
automagically.

JEC
-- 
Jeffrey E. Care (carej@us.ibm.com)
WebSphere Build SWAT Team Lead
WebSphere Build Tooling Lead (Project Mantis)
https://w3.opensource.ibm.com/projects/mantis


Pavel Krupets <pk...@yahoo.com> wrote on 06/24/2005 04:52:43 PM:

> Hello,
> 
> I need to unjar jar files into directory and then pack this folder
> into one jar. But I don't know how to check whether I need to update
> this jar or not (I need to check whether source jars are older or
> have same age as the target jar).
> 
> Thank you!
> 
> With regards,
> Pavel Krupets
> 
> 
> 
> ____________________________________________________ 
> Yahoo! Sports 
> Rekindle the Rivalries. Sign up for Fantasy Football 
> http://football.fantasysports.yahoo.com
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>