SpringProjectNature
在org.springframework.ide.eclipse.core.internal.project.SpringProjectNature的configure()中添加org.springframework.ide.eclipse.core.springbuilder,在deconfigure()中删除springbuilder。
添加Nature
/** * Adds given nature as first nature to specified project. */ public static void addProjectNature(IProject project, String nature, IProgressMonitor monitor) throws CoreException { if (project != null && nature != null) { if (!project.hasNature(nature)) { IProjectDescription desc = project.getDescription(); String[] oldNatures = desc.getNatureIds(); String[] newNatures = new String[oldNatures.length + 1]; newNatures[0] = nature; if (oldNatures.length > 0) { System.arraycopy(oldNatures, 0, newNatures, 1, oldNatures.length); } desc.setNatureIds(newNatures); project.setDescription(desc, monitor); } } }
删除Nature
/** * Removes given nature from specified project. */ public static void removeProjectNature(IProject project, String nature, IProgressMonitor monitor) throws CoreException { if (project != null && nature != null) { if (project.exists() && project.hasNature(nature)) { // first remove all problem markers (including the // inherited ones) from Spring beans project if (nature.equals(SpringCore.NATURE_ID)) { project.deleteMarkers(SpringCore.MARKER_ID, true, IResource.DEPTH_INFINITE); } // now remove project nature IProjectDescription desc = project.getDescription(); String[] oldNatures = desc.getNatureIds(); String[] newNatures = new String[oldNatures.length - 1]; int newIndex = oldNatures.length - 2; for (int i = oldNatures.length - 1; i >= 0; i--) { if (!oldNatures[i].equals(nature)) { newNatures[newIndex--] = oldNatures[i]; } } desc.setNatureIds(newNatures); project.setDescription(desc, monitor); } } }
添加Builder
/** * Adds given builder to specified project. */ public static void addProjectBuilder(IProject project, String builderID, IProgressMonitor monitor) throws CoreException { IProjectDescription desc = project.getDescription(); ICommand oldBuilderCommand = getProjectBuilderCommand(desc, builderID); if (oldBuilderCommand == null) { // Add a new build spec ICommand command = desc.newCommand(); command.setBuilderName(builderID); // Commit the spec change into the project addProjectBuilderCommand(desc, command); project.setDescription(desc, monitor); } }
删除Builder
/** * Removes given builder from specified project. */ public static void removeProjectBuilder(IProject project, String builderID, IProgressMonitor monitor) throws CoreException { if (project != null && builderID != null) { IProjectDescription desc = project.getDescription(); ICommand[] commands = desc.getBuildSpec(); for (int i = commands.length - 1; i >= 0; i--) { if (commands[i].getBuilderName().equals(builderID)) { 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); // Commit the spec change into the project desc.setBuildSpec(newCommands); project.setDescription(desc, monitor); break; } } } }添加BuilderCommand
/** * Adds (or updates) a builder in given project description. */ public static void addProjectBuilderCommand(IProjectDescription description, ICommand command) throws CoreException { ICommand[] oldCommands = description.getBuildSpec(); ICommand oldBuilderCommand = getProjectBuilderCommand(description, command.getBuilderName()); ICommand[] newCommands; if (oldBuilderCommand == null) { // Add given builder to the end of the builder list newCommands = new ICommand[oldCommands.length + 1]; System.arraycopy(oldCommands, 0, newCommands, 0, oldCommands.length); newCommands[oldCommands.length] = command; } else { // Replace old builder with given new one for (int i = 0, max = oldCommands.length; i < max; i++) { if (oldCommands[i] == oldBuilderCommand) { oldCommands[i] = command; break; } } newCommands = oldCommands; } description.setBuildSpec(newCommands); }删除BuilderCommand
/** * Returns specified builder from given project description. */ public static ICommand getProjectBuilderCommand(IProjectDescription description, String builderID) throws CoreException { ICommand[] commands = description.getBuildSpec(); for (int i = commands.length - 1; i >= 0; i--) { if (commands[i].getBuilderName().equals(builderID)) { return commands[i]; } } return null; }
StProjectNature
/** * 配置项目的性质,主要目的是为项目添加构建器 */ public void configure() throws CoreException { IProjectDescription desc = project.getDescription(); ICommand builderCommand = getBuilderCommand(desc, SoTowerProjectPlugin.BUILDER_ID); if (builderCommand == null) { ICommand command = desc.newCommand(); command.setBuilderName(StProjectPlugin.BUILDER_ID); setBuilderCommand(desc, command); project.setDescription(desc, new NullProgressMonitor()); } } /** * 向项目添加构建器 * * @param description 项目描述 * @param command 与构建器对应的构建命令 * @throws CoreException */ private void setBuilderCommand(IProjectDescription description, ICommand command) throws CoreException { ICommand[] oldCommands = description.getBuildSpec(); ICommand[] newCommands = new ICommand[oldCommands.length + 1]; System.arraycopy(oldCommands, 0, newCommands, 0, oldCommands.length); newCommands[oldCommands.length] = command; description.setBuildSpec(newCommands); } /** * 获得项目的某个特定构建器 * * @param description 项目描述 * @param builderID 构建器ID * @return * @throws CoreException */ private ICommand getBuilderCommand(IProjectDescription description, String builderID) throws CoreException { for (ICommand command : description.getBuildSpec()) { if (command.getBuilderName().equals(builderID)) { return command; } } return null; } /** * 把性质从项目中移除 */ public void deconfigure() throws CoreException {}