Eclipse 플러그 인 개발 중 자바 프로젝트 를 만 드 는 코드

7845 단어
package com.wisoft.magicube.codegenerator.popup.actions;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.core.resources.ICommand;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.ui.PreferenceConstants;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

public class NewJavaProjectAction implements IObjectActionDelegate {

	public void run(IAction action) {
		//      
		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();

		// ///////////////////////////////////     ///////////////////////////
		final IProject project = root.getProject("xyz");
		
		//        
		//          ,         
		IPath projectPath = new Path("g:/myplugIn");
		IWorkspace workspace = root.getWorkspace();
		final IProjectDescription description = workspace.newProjectDescription(project.getName());
		description.setLocation(projectPath);

		//       ,  java  
		String[] javaNature = description.getNatureIds();
		String[] newJavaNature = new String[javaNature.length + 1];
		System.arraycopy(javaNature, 0, newJavaNature, 0, javaNature.length);
		newJavaNature[javaNature.length] = "org.eclipse.jdt.core.javanature"; //           Java  
		description.setNatureIds(newJavaNature);

		// /////////////////////////////
		try {
			NullProgressMonitor monitor = new NullProgressMonitor();
			project.create(description, monitor);
			project.open(IResource.BACKGROUND_REFRESH, new SubProgressMonitor(monitor, 1000));
		} catch (CoreException e) {
			e.printStackTrace();
		}

		//                  
		/*
		 * //     ,WorkspaceModifyOperation  org.eclipse.ui.ide 
		 * WorkspaceModifyOperation workspaceModifyOperation = new
		 * WorkspaceModifyOperation() {
		 * 
		 * @Override protected void execute(IProgressMonitor monitor) throws
		 * CoreException, InvocationTargetException, InterruptedException { try
		 * { monitor.beginTask("", ); project.create(description, monitor);
		 * 
		 * if(monitor.isCanceled()){ throw new OperationCanceledException(); }
		 * 
		 * project.open(IResource.BACKGROUND_REFRESH, new
		 * SubProgressMonitor(monitor, )); } catch (Exception e) {
		 * e.printStackTrace(); } finally{ monitor.done(); } } };
		 * //     IWizard getContainer().run()  .
		 */

		//    java  
		IJavaProject javaProject = JavaCore.create(project);
		// //////////////////////////////////  JRE ////////////////////////////
		try {
			//      JRE 
			IClasspathEntry[] jreLibrary = PreferenceConstants.getDefaultJRELibrary();
			//      build path
			IClasspathEntry[] oldClasspathEntries = javaProject.getRawClasspath();
			List list = new ArrayList();
			list.addAll(Arrays.asList(jreLibrary));
			list.addAll(Arrays.asList(oldClasspathEntries));

			javaProject.setRawClasspath(list.toArray(new IClasspathEntry[list.size()]), null);
		} catch (JavaModelException e) {
			e.printStackTrace();
		}

		// //////////////////////////////////      /////////////////////////////
		IFolder binFolder = javaProject.getProject().getFolder("bin");
		try {
			binFolder.create(true, true, null);
			javaProject.setOutputLocation(binFolder.getFullPath(), null);
		} catch (CoreException e) {
			e.printStackTrace();
		}

		// /////////////////////////  Java   ///////////////////////
		try {
			IProjectDescription description2 = javaProject.getProject().getDescription();
			ICommand command = description2.newCommand();
			command.setBuilderName("org.eclipse.jdt.core.javabuilder");
			description2.setBuildSpec(new ICommand[] { command });
			description2.setNatureIds(new String[] { "org.eclipse.jdt.core.javanature" });
			javaProject.getProject().setDescription(description2, null);
		} catch (CoreException e) {
			e.printStackTrace();
		}

		// /////////////////////////////        //////////////////////////
		// ///////////          ,    PackageFragmentRoot     ////////
		IFolder srcFolder = javaProject.getProject().getFolder("src");
		try {
			srcFolder.create(true, true, null);
			// this.createFolder(srcFolder);
			//   SourceLibrary
			IClasspathEntry srcClasspathEntry = JavaCore.newSourceEntry(srcFolder.getFullPath());

			//     build path
			IClasspathEntry[] oldClasspathEntries = javaProject.readRawClasspath();

			//     
			List list = new ArrayList();
			list.addAll(Arrays.asList(oldClasspathEntries));
			list.add(srcClasspathEntry);

			//                  ,     
			IClasspathEntry temp = JavaCore.newSourceEntry(new Path("/xyz"));
			if (list.contains(temp)) {
				list.remove(temp);
			}

			System.out.println(list.size());

			javaProject.setRawClasspath(list.toArray(new IClasspathEntry[list.size()]), null);
		} catch (CoreException e) {
			e.printStackTrace();
		}

		// ///////////////////////////////   //////////////////////////
		// IPackageFragmentRoot packageFragmentRoot = javaProject.getPackageFragmentRoot(javaProject.getResource());
		//     src    

		try {
			//             IPackageFragmentRoot
			IPackageFragmentRoot packageFragmentRoot = javaProject.findPackageFragmentRoot(new Path("/xyz/src"));
			//   IPackageFragmentRoot  IPackageFragment,IPackageFragment    
			IPackageFragment packageFragment = packageFragmentRoot.createPackageFragment("com.aptech.plugin", true, null);

		// //////////////////////////////////  Java  ////////////////////////
			String javaCode = "package com.aptech.plugin;public class HelloWorld{public static void main(String[] args){System.out.println(\"       \");}}";
			packageFragment.createCompilationUnit("HelloWorld.java", javaCode, true, new NullProgressMonitor());

		} catch (JavaModelException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
	}


	public void selectionChanged(IAction action, ISelection selection) {
	}

}

본 고 는 B3log Solo 를 사용 하여 Noday 에서 동시 발표 한 것 이다.
원본 주소:http://www.noday.net/articles/2011/03/24/1300951113758.html

좋은 웹페이지 즐겨찾기