Avoid duplicate file generation by comparing timestamps

Source: Internet
Author: User

Recently, there is a need in the project, in order to analyze the property rights information of some files, need to the project all the file generation corresponding to the. ip file, when selectedchange in Eclipse Package Explorer, call the project/ Folder/file all files to generate the corresponding. ip file. But the problem arises, every time package Explorer SelectedChange, you need to generate a. ip file, and most of the time, this is not necessary (for files that have been built, if the file has not changed since the last build, Build is meaningless), and for larger projects, this is difficult to accept in an efficient way, so that we can no longer build the files that have been build and have not changed after build, here, We can borrow Ant's Solution: Ant minimizes compilation by comparing the timestamps of class files and source files.     Similarly, we can do this by comparing the build-generated file with the timestamp of the original file: If the original file was modified before the build generated file modification time, it would not need to be built, or it would require a build (the nonexistent file timestamp is set to-1). Abstract, this solution is very useful, in many cases can be used to determine whether the need to generate new files, to avoid duplicate generation of files, improve efficiency.

Here is the program fragment:

Package com.ibm.crl.ariadne.ipmanagement.util;

Import Java.io.File; Import Org.eclipse.core.resources.IFile; Import Org.eclipse.core.resources.IWorkspaceRoot; Import Org.eclipse.core.resources.ResourcesPlugin; Import Org.eclipse.core.runtime.IPath; Import Org.eclipse.core.runtime.Path;

public class Buildutil {/** * @author Suqiang * @param artifact * @return The value whether the artifact to be Built/public static Boolean needbebuilt (IFile artifact) {//if the ' file is ' not the ' Java/htm/gif/jpg/jar file,   It need not built.   String artifactname = Artifact.getname (); if (!) ( Artifactname.endswith (". Java") |artifactname.endswith (". Jar")) return false;

IPath Relativeprojectpath = Artifact.getprojectrelativepath ();      IPath Projectfullpath = Artifact.getproject (). GetFullPath ();     Iworkspaceroot root = Resourcesplugin.getworkspace (). Getroot (); IFile ipfile = (ifile) root.getfile (New Path (projectfullpath.tostring () + "/.ip/" +relativeprojectpath.tostring () + ". IP "));

Transform from IFile to File into the LastModified time,the method Getlocaltimestamp the Iresource of Le//more in eclipse document:note which due to varying file system timing granularities,//this value is not guarantee   D To change every time the ' file is modified.   File artifactfile = Artifact.getlocation (). ToFile (); File ipfilefile = Ipfile.getlocation (). ToFile ();

Long Buildfiletimestamp = ipfilefile.lastmodified ();      Long Sourcetimestamp = artifactfile.lastmodified (); If the artifact has been built before and the artifact itself is not modified,it needn ' t being build again if (Buildfilet   Imestamp>=sourcetimestamp) return false;  else return true; } }

Before optimization: The build process cost 31828ms  (CORE) The build process cost 31109ms  (CORE) The build process cost 114 640ms (UI) The build process cost 113406ms (UI) Optimization: The IP file is exist:the builds process cost 94ms  (CORE) the BU ILD process cost 78ms  (CORE) builds process cost 374ms (UI) The build process cost 359ms (UI) deletes the IP file: The build process cost 32219ms  (CORE) builds process cost 37828ms  (CORE) The build process cost 133906ms ( UI) The build process cost 134438ms (UI) below is the result data for some tests (two very large project core and UI)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.