Copying file from plugin project into the local system

Imagine a situation where you want to copy a file from a simple eclipse plugin project into local system as soon as the eclipse workbench opens or when the software containing the said plugin is being installed. One way to achieve this task is to use org.eclipse.ui.startup extension. This blog explains how to use this extension and to copy file from the plugin project into local system.

Eclipse plugins are loaded whenever the user needs their functionalities. This is called lazy plug-in activation. The extension org.eclipse.ui.startup is designed to break this behavior. It forces the workbench to activate the plug-in as soon as the workbench window opens. And this extension should be used only in exceptional situations as it can affect start-up performance.

Note that wherever I used the term code it meant Java code.

Do the following steps to copy a file attached with a plugin project into the local system.

Create a plugin project, say demo.startup in eclipse IDE.

Make sure that you have org.eclipse.ui as dependency.

Add the extension, org.eclipse.ui.IStartup.

Remove the entry Bundle-ActivationPolicy: lazy from MANIFEST.MF.

Attach the file, say settings.xml, which you want to copy to the project.

Create a package, say demo.startup.action under src folder.

Create a java class, say FileCopier.

The FileCopier class should implement the interface IStartup (org.eclipse.ui.IStartup) and should override the method, earlyStartup() as given below.

import org.eclipse.ui.IStartup;

publicclass FileCopier implements IStartup {

@Override

public void earlyStartup() {

<Java code to copy file into local system during start up>

}

}

Note that whenever the workbench opens, the plugin is loaded immediately and earlyStartup() method is called automatically.

Now open the plugin.xml file and insert your startup class name as given below.

Create a local file (i.e. target file) where you want to store the content of settings.xml.

File targetFile = new File(“C:/temp/settings.xml”);

Before copying make sure that the target file already exists. If exists check if the contents are same. If same, do not copy. Otherwise either overwrite or make a copy of existing file and then do copying. I prefer the second approach.

Restart the eclipse IDE. The file must be copied into the specified location in your local system. In case the file already exists and the content is not the same as that of the new file then it will create a copy like settings – Copy.xml. If settings – copy.xml file also exists then it will create settings – Copy (2).xml and so on.