添加删除项目的builder

*每次从svn或clean项目时,都会在刷新工作区后执行项目构件,而在项目很多时,就浪费很多时间.

想到的解决的方法之一,是去除项目的构建器.

*构建器是定义在".project"中,如下是java项目的构建器定义

builder定义<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
	<name>cb</name>
	<comment></comment>
	<projects>
	</projects>
	<buildSpec>
		<buildCommand>
			<name>org.eclipse.jdt.core.javabuilder</name>
			<arguments>
			</arguments>
		</buildCommand>
	</buildSpec>
	<natures>
		<nature>cn.agree.ab.ide.abideProject</nature>
		<nature>org.eclipse.jdt.core.javanature</nature>
	</natures>
</projectDescription>

*添加和删除构建器

AddOrRemove Builder/**
 * Copyright(C) 2011 Agree Tech, All rights reserved.
 * 
 * Created on 2011-4-14  by dzh
 */
package cn.com.agree.ide.navigator.action;

import org.eclipse.core.resources.ICommand;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.PlatformUI;

public class ToggleBuilderAction implements IViewActionDelegate {
	
	private static final String  NAME_JAVABUILDER ="org.eclipse.jdt.core.javabuilder"; //$NON-NLS-1$
	
	private boolean isExist;
	
	private IProject project;

	@Override
	public void init(IViewPart view) {
		
	}

	@Override
	public void run(IAction action) {
		Shell shell =PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
		boolean code =MessageDialog.openConfirm(shell, Messages.getString("ToggleBuilderAction.JavaBuilder_Configuration"), action.getText()); //$NON-NLS-1$
		if(!code)
			return;
		
		try {
			if(isExist){
				deconfigure();
			}else{
				configure();
			}
		} catch (CoreException e) {
			e.printStackTrace();
		}
	}
	
	public void configure() throws CoreException {
		IProjectDescription desc = project.getDescription();
		ICommand[] commands = desc.getBuildSpec();

		for (int i = 0; i < commands.length; ++i) {
			if (commands[i].getBuilderName().equals(NAME_JAVABUILDER)) {
				return;
			}
		}

		ICommand[] newCommands = new ICommand[commands.length + 1];
		System.arraycopy(commands, 0, newCommands, 0, commands.length);
		ICommand command = desc.newCommand();
		command.setBuilderName(NAME_JAVABUILDER);
		newCommands[newCommands.length - 1] = command;
		desc.setBuildSpec(newCommands);
		project.setDescription(desc, null);
	}
	
	public void deconfigure() throws CoreException {
		IProjectDescription description = project.getDescription();
		ICommand[] commands = description.getBuildSpec();
		for (int i = 0; i < commands.length; ++i) {
			if (commands[i].getBuilderName().equals(NAME_JAVABUILDER)) {
				ICommand[] newCommands = new ICommand[commands.length - 1];
				System.arraycopy(commands, 0, newCommands, 0, i);
				System.arraycopy(commands, i + 1, newCommands, i,
						commands.length - i - 1);
				description.setBuildSpec(newCommands);
				project.setDescription(description, null);			
				return;
			}
		}
	}


	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		action.setEnabled(false);
		
		if(!selection.isEmpty()){
			if(selection instanceof IStructuredSelection){
				Object obj =((IStructuredSelection)selection).getFirstElement();
				if(obj instanceof IProject){
					action.setEnabled(true);
					changeAction((IProject) obj,action);
				}
			}
		}
	}
	
	/**
	 * 
	 * @param project
	 */
	private void changeAction(IProject project,IAction action){
		this.project =project;
		try {
			IProjectDescription desc =project.getDescription();
			ICommand[] commands =desc.getBuildSpec();
			for(ICommand cmd:commands){
				if(cmd.getBuilderName().equals(NAME_JAVABUILDER)){
					isExist =true;
					action.setText(Messages.getString("ToggleBuilderAction.remove_javabuilder")); //$NON-NLS-1$
					return;
				}
			}
			
			action.setText(Messages.getString("ToggleBuilderAction.add_javabuilder")); //$NON-NLS-1$
			isExist =false;
		} catch (CoreException e) {
			e.printStackTrace();
		}
		
	}

}

转载于:https://www.cnblogs.com/bronte/articles/2019251.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值