IBM Support

Ant Tasks to import and export .epf files to set the preferences of new workspaces

Question & Answer


Question

How can you automate the creation of a new workspace with a predefined set of preferences?

Cause

A common use case for some automated build systems requires new workspaces to be created on a regular basis. When creating a new workspace, it is time consuming to set all the preferences individually. It is possible to use standard Eclipse menus to export sets of non-default preference values to .epf files, and conversely, it is possible to import .epf files into new workspaces, but the import/export has to be done manually. To further automate the import/export process, it is possible to write Ant tasks that can then be run from the command line executing Eclipse in headless mode..

Answer



This technote provides an Eclipse plug-in that contains two Ant Tasks, one to export the preferences to a file, one to import the preferences from a file into a new workspace. The plug-in is attached in source and in deployable format.

For additional functionality see also the RAD 7 J2EE ant task "workspacePreferenceFile" described in the Related URL section.

A. Plug-in installation instructions:

    1. Copy the attached file:
    com.ibm.rational.support.anttasks.epf.preferencesmanager_1.0.0.jar to the folder:
      Windows:

      <InstallDir>\SDP70\plugins   where  by default <installDir>=C:\Program Files\IBM

      Linux:

      <installDir>/SDP70/plugins   where by default <installDir>=/opt

      (do not copy it into the SDP70Shared folder)
    2. Restart the product with the -clean option to ensure that the Eclipse configuration is refreshed
      Windows:

      <InstallDir>\SDP70\eclipse.exe -clean

      Linux:

      <InstallDir>/SDP70/eclipse -clean
    3. Close the product, as the execution will be in headless mode (no user interface visible)

B. Execution:
    1. Prepare a build.xml file for import or export
    2. Prepare and execute a batch file to launch the import or export
    B.1.1 Sample build.xml file to export preferences:

    <?xml version="1.0" encoding="UTF-8" ?>
    <project default="exportpreferences">
    <target name="exportpreferences" description="export preferences from file">
      <ExportPreferences preferencesFile="c:\\temp\\prefs.epf"/>
     </target>
    </project>


    The task is called ExportPreferences and it takes one parameter called preferencesFile which should contain the absolute path to the .epf file (adapt the path to the operating system used).

    B.1.2. Sample build.xml file to import preferences:
    <?xml version="1.0" encoding="UTF-8" ?>
    <project default="importpreferences">
    <target name="importpreferences" description="import preferences from file">
       <ImportPreferences preferencesFile="c:\\temp\\prefs.epf"/>
     </target>
    </project>
     

    The task is called: ImportPreferences and it takes one parameter called preferencesFile which should contain the absolute path to the .epf file (adapt the path to the operating system used).

    B.2. Sample batch file to invoke one ant task:
      Windows:

      @ECHO ON
      "C:\Program Files\IBM\SDP70\jdk\bin\java.exe" -classpath "C:\Program Files\IBM\SDP70\startup.jar" org.eclipse.core.launcher.Main -data "C:\temp\myworkspace" -application org.eclipse.ant.core.antRunner -buildfile  "C:\temp\build.xml"

      Linux:

      /opt/IBM/SDP70/jdk/bin/java -classpath /opt/IBM/SDP70/startup.jar org.eclipse.core.launcher.Main -data "/usr/temp/myworkspace" -application org.eclipse.ant.core.antRunner -buildfile  "/usr/temp/build.xml"

C. Technical overview:

The two Ant Tasks are defined by the following code:



package com.ibm.rational.support.anttasks.epf;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import org.apache.tools.ant.BuildException;
import org.eclipse.ant.core.Task;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.preferences.IPreferencesService;

/**
 * <p>
 * The following command line will start a headless
 * Eclipse instance
 * and run the specified Ant script.
 * </p>
 * <p>
 * java -classpath <i>eclipseDir</i>/startup.jar
 *   org.eclipse.core.launcher.Main
 *   -data <i>worspaceDir</i> -application
 *   org.eclipse.ant.core.antRunner
 *   -buildfile <i>antScript</i>
 * </p>
 */
public class ExportTask extends Task {

 private String preferencesFile;

 public ExportTask() {
  super();

 }

 public final void execute() throws BuildException {
  System.out
    .println("Calling preferences.manager.ant.tasks.ExportTask.execute()");
  IPreferencesService service = org.eclipse.core.runtime.Platform
    .getPreferencesService();
  System.out.println("File: " + this.getPreferencesFile());

  try {
   service.exportPreferences(service.getRootNode(),
     new FileOutputStream(new File(this.getPreferencesFile())),
     null);
  } catch (FileNotFoundException e) {

   e.printStackTrace();
  } catch (CoreException e) {
   e.printStackTrace();
  }

 }

 public String getPreferencesFile() {
  return preferencesFile;
 }

 public void setPreferencesFile(String preferencesFile) {
  this.preferencesFile = preferencesFile;
 }
}





package com.ibm.rational.support.anttasks.epf;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import org.apache.tools.ant.BuildException;
import org.eclipse.ant.core.Task;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.preferences.IPreferencesService;

/**
 * <p>
 * The following command line will start a headless
 * Eclipse instance
 * and run the specified Ant script.
 * </p>
 * <p>
 * java -classpath <i>eclipseDir</i>/startup.jar
 *   org.eclipse.core.launcher.Main
 *   -data <i>worspaceDir</i> -application
 *   org.eclipse.ant.core.antRunner
 *   -buildfile <i>antScript</i>
 * </p>
 */
public class ImportTask extends Task {

 private String preferencesFile;

 public ImportTask() {
  super();

 }

 public final void execute() throws BuildException {
  System.out
    .println("Calling preferences.manager.ant.tasks.ImportTask.execute()");
  IPreferencesService service = org.eclipse.core.runtime.Platform
    .getPreferencesService();
  System.out.println("File: " + this.getPreferencesFile());

  try {
   service.importPreferences(new FileInputStream(new File(this
     .getPreferencesFile())));
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (CoreException e) {
   e.printStackTrace();
  }

 }

 public String getPreferencesFile() {
  return preferencesFile;
 }

 public void setPreferencesFile(String preferencesFile) {
  this.preferencesFile = preferencesFile;
 }
}




In the plugin.xml file the two Ant Tasks are declared as follows:


<extension
         id="com.ibm.rational.support.anttasks.epf"
         name="Preference Manager Ant Tasks"
         point="org.eclipse.ant.core.antTasks">
      <antTask
            class="com.ibm.rational.support.anttasks.epf.ExportTask"
            eclipseRuntime="true"
            headless="true"
            library="runtime/PreferencesManagerTasks.jar"
            name="ExportPreferences"/>
      <antTask
            class="com.ibm.rational.support.anttasks.epf.ImportTask"
            eclipseRuntime="true"
            headless="true"
            library="runtime/PreferencesManagerTasks.jar"
            name="ImportPreferences"/>
   </extension>



Disclaimer

All source code and/or binaries attached to this document are referred to here as "the Program". IBM is not providing program services of any kind for the Program. IBM is providing the Program on an "AS IS" basis without warranty of any kind. IBM WILL NOT BE LIABLE FOR ANY ACTUAL, DIRECT, SPECIAL, INCIDENTAL, OR INDIRECT DAMAGES OR FOR ANY ECONOMIC CONSEQUENTIAL DAMAGES (INCLUDING LOST PROFITS OR SAVINGS), EVEN IF IBM, OR ITS RESELLER, HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.



preferencesmanager.zip

com.ibm.rational.support.anttasks.epf.preferencesmanager_1.0.0.jar

Due to the warning reported in the <workspace>\.metadata\.log with the above version:


!ENTRY org.eclipse.osgi 2 0 2009-05-14 18:27:16.609
!MESSAGE The activator com.ibm.rational.support.anttasks.epf.Activator for bundle com.ibm.rational.support.anttasks.epf.preferencesmanager is invalid
!STACK 0
org.osgi.framework.BundleException: The activator com.ibm.rational.support.anttasks.epf.Activator for bundle com.ibm.rational.support.anttasks.epf.preferencesmanager is invalid
at org.eclipse.osgi.framework.internal.core.AbstractBundle.loadBundleActivator(AbstractBundle.java:146)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.start(BundleContextImpl.java:980)
at org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(BundleHost.java:346)
at org.eclipse.osgi.framework.internal.core.AbstractBundle.start(AbstractBundle.java:265)
at org.eclipse.osgi.framework.util.SecureAction.start(SecureAction.java:400)
at org.eclipse.osgi.framework.internal.core.BundleHost.loadClass(BundleHost.java:234)
at org.eclipse.osgi.framework.internal.core.AbstractBundle.loadClass(AbstractBundle.java:1274)
at org.eclipse.ant.core.AntCorePreferences$WrappedClassLoader.findClass(AntCorePreferences.java:115)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.eclipse.ant.internal.core.AntClassLoader.loadClassPlugins(AntClassLoader.java:69)
at org.eclipse.ant.internal.core.AntClassLoader.findClass(AntClassLoader.java:47)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.eclipse.ant.core.AntRunner.run(AntRunner.java:510)
at org.eclipse.ant.core.AntRunner.start(AntRunner.java:600)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:193)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:386)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:549)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:504)
at org.eclipse.equinox.launcher.Main.run(Main.java:1236)
Caused by: java.lang.ClassNotFoundException: com.ibm.rational.support.anttasks.epf.Activator
at org.eclipse.osgi.framework.internal.core.BundleLoader.findClassInternal(BundleLoader.java:483)
at org.eclipse.osgi.framework.internal.core.BundleLoader.findClass(BundleLoader.java:399)
at org.eclipse.osgi.framework.internal.core.BundleLoader.findClass(BundleLoader.java:387)
at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:87)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.eclipse.osgi.framework.internal.core.BundleLoader.loadClass(BundleLoader.java:315)
at org.eclipse.osgi.framework.internal.core.BundleHost.loadClass(BundleHost.java:227)
at org.eclipse.osgi.framework.internal.core.AbstractBundle.loadBundleActivator(AbstractBundle.java:139)
... 27 more


a new version has been provided:

PreferencesManager_1.0.1.zip

com.ibm.rational.support.anttasks.epf.preferencesmanager_1.0.1.jar

[{"Product":{"code":"SSYK2S","label":"Rational Software Architect Designer"},"Business Unit":{"code":"BU053","label":"Cloud & Data Platform"},"Component":"Eclipse","Platform":[{"code":"PF016","label":"Linux"},{"code":"PF033","label":"Windows"}],"Version":"7.0;7.0.0.1;7.0.0.2;7.0.0.3;7.0.0.4;7.0.0.5;7.0.0.6","Edition":"","Line of Business":{"code":"LOB45","label":"Automation"}},{"Product":{"code":"SSRTLW","label":"Rational Application Developer for WebSphere Software"},"Business Unit":{"code":"BU053","label":"Cloud & Data Platform"},"Component":"Eclipse","Platform":[{"code":"PF033","label":"Windows"},{"code":"PF016","label":"Linux"}],"Version":"7.0;7.0.0.1;7.0.0.2;7.0.0.3;7.0.0.4;7.0.0.5;7.0.0.6;7.5","Edition":"All Editions","Line of Business":{"code":"LOB45","label":"Automation"}},{"Product":{"code":"SSCLKU","label":"Rational Software Modeler"},"Business Unit":{"code":"BU053","label":"Cloud & Data Platform"},"Component":"Eclipse","Platform":[{"code":"PF033","label":"Windows"},{"code":"PF016","label":"Linux"}],"Version":"7.0;7.0.0.1;7.0.0.2;7.0.0.3;7.0.0.4;7.0.5;7.0.5.1;7.5","Edition":"All Editions","Line of Business":{"code":"LOB36","label":"IBM Automation"}},{"Product":{"code":"SSJP3D","label":"Rational Systems Developer"},"Business Unit":{"code":"BU053","label":"Cloud & Data Platform"},"Component":"Eclipse","Platform":[{"code":"PF016","label":"Linux"},{"code":"PF033","label":"Windows"}],"Version":"7.0;7.0.0.1;7.0.0.2;7.0.0.3;7.0.0.4;7.0.5;7.0.5.1","Edition":"All Editions","Line of Business":{"code":"LOB45","label":"Automation"}}]

Document Information

Modified date:
10 September 2020

UID

swg21273017