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)