Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

Eclipse Mobile Java Development Tools
Previous Page Home Next Page

Extending the build process with custom build hooks.

In this section, you will be learning how to extend MTJ build process in order do add SDK specific actions into it.

Attaching a build hook is very simple, extend the org.eclipse.mtj.core.mtjbuildhook extension point in your plug-in project:

Creating Extension

Implement your instance of the org.eclipse.mtj.core.build.IMTJBuildHook interface. The interface has as single callback for every state transition of the build process:

/**
 * This method is called upon state transitions within the MTJ build
 * process. This method implementation must be lightweight
 * in order to avoid introducing overhead to the build process. In case the
 * hook has nothing to do on the new state it must return
 * as soon as possible.
 * 
 * The project instance passed during the invocation of this method provides
 * everything a build hook might require, including access to all resources
 * available.
 * 
 * 
 * A progress monitor is provided to report in the user interface the
 * current statuses inside the hook.
 * Note: Progress messages should be "user readable" to be
 * displayed in the User Interface.
 * 
 * 
 * In case an error has occurred, clients implementing this interface must
 * throw a {@link CoreException} that will be treated by MTJ and correctly
 * displayed.
 *
 * Note: Exception messages should be "user readable" to be
 * displayed in the User Interface.
 * 
 * @param project the IMTJProject being built.
 * @param state new build state. For more info on available build states see
 * MTJBuildState.
 * @param monitor a progress monitor.
 * @throws CoreException any error occurred.
 * @see org.eclipse.mtj.core.build.MTJBuildState
 */
public void buildStateChanged(IMTJProject project, MTJBuildState state,
        IProgressMonitor monitor) throws CoreException;

The following states are available to be extended:

  • PRE_BUILD [Before build process starts]
  • PRE_PREPROCESS [Before preprocessing]
  • POST_PREPROCESS [After preprocessing]
  • PRE_LOCALIZATION [Before localization]
  • POST_LOCALIZATION [After localization]
  • PRE_COMPILE [Before JDT builder starts]
  • POST_COMPILE [After JDT builder ends]
  • PRE_PREVERIFICATION [Before preverifying]
  • POST_PREVERIFICATION [After preverifying]
  • PRE_PACKAGING [Before packaging]
  • Only available on deployment builds:
    • PRE_OBFUSCATION [Before obfuscation]
    • POST_OBFUSCATION [After obfuscation]
    • PRE_SIGNING [Before signing]
    • POST_SIGNING [After signing]
  • POST_PACKAGING [After packaging]
  • POST_BUILD [After build process ends]

The state diagram bellow illustrates the state transitions:

State Diagram

Follows an example of a hook that adds a license file (EPLV1.0.txt) from the project root into the binary folder in order to be packaged along with the other sources. It also adds a property to the JAD file.

public class LicenseBuildHook implements IMTJBuildHook {

	public void buildStateChanged(IMTJProject project, MTJBuildState state, IProgressMonitor monitor) throws CoreException {
		switch (state) {
			case PRE_COMPILATION:
				writeLicense(project, monitor);				
			break;
			case PRE_PREVERIFICATION:
				writeJadAttribute(project, monitor);
			break;
		}
	}
	
	private void writeJadAttribute(IMTJProject mtjProject, IProgressMonitor monitor) {
		if (!(mtjProject instanceof IMidletSuiteProject)) {
			return;
		}
		IMidletSuiteProject midletSuiteProject = (IMidletSuiteProject) mtjProject;
		IApplicationDescriptor descriptor = midletSuiteProject.getApplicationDescriptor();
		descriptor.getManifestProperties().put("License", "EPLv1.0");
		try {
			descriptor.store();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void writeLicense(IMTJProject mtjProject, IProgressMonitor monitor) throws CoreException {
		IProject project = mtjProject.getProject();
		IFile    file = project.getFile("EPLV1.0.txt");
		if (file.exists()) {
			IPath path = mtjProject.getJavaProject()
				.getOutputLocation().removeFirstSegments(1);
			IFolder folder = project.getFolder(path);
			File    target = folder.getLocation().append(file.getName()).toFile();
			if (target.exists()) {
				try {
					file.setContents(new FileInputStream(target), false, false , monitor);
				} catch (FileNotFoundException e) {
					return;
				}
			} else {				
				file.copy(folder.getProjectRelativePath().append(file.getName()), true, monitor);
			}
			file.refreshLocal(IResource.DEPTH_ZERO, monitor);
		}
	}
}


 
 
  Published under the terms of the Eclipse Public License Version 1.0 ("EPL") Design by Interspire