Avoid duplicate file generation by comparing timestamps

Source: Internet
Author: User

Recently, there is a demand in the project. In order to analyze the property right information of some documents, it is necessary to generate the corresponding information for all files in the project. IP file. When selectedchange is in the package explorer of Eclipse, all files under the project/folder/file are called to generate the corresponding. IP file. But the problem arises. Each time selectedchange in package explorer is generated. IP files, but in most cases, this is unnecessary (for files that have been built, if the last build has not changed, the build is meaningless ), for large projects, this is unacceptable in terms of efficiency. For files that have been built and have not changed after the build, we can stop building. Here, we can use ant's solution: ant minimizes compilation by comparing the timestamps of class files and source files. Similarly, we can compare the time stamps of the build files and the original files to achieve this: if the original file modification time is before the Build File modification time, no build is required, otherwise, build (the timestamp of a non-existing file is set to-1 ). Abstract: This solution is still very useful. In many cases, you can use this method to determine whether to generate new files to avoid repeated file generation and improve efficiency.

Below isProgramFragment:

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 need 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 in order to get the lastmodified time, the method getlocaltimestamp of iresource is unreliable // more in eclipse document: note that due to varying file system timing granularities, // This value is not guaranteed 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 be build again if (buildfiletimestamp> = sourcetimestamp) return false; else return true ;}}

 

Before optimization: The build process cost 31828 ms (core) the build process cost 31109 ms (CORE) the build process cost 114640 ms (UI) the build process cost 113406 ms (UI) after optimization: the IP file is exist: the build process cost 94 Ms (CORE) the build process cost 78 ms (CORE) the build process cost 374 ms (UI) the build process cost 359 ms (UI) Delete the IP file: The build process cost 32219 ms (core) the build process cost 37828 ms (CORE) the build process cost 133906 ms (UI) the build process cost 134438 ms (UI)

the following are some test results (taking 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.