eclipse 插件开发 创建一个java工程

步骤:
1. 创建一个org.eclipse.core.resources.IProject
2. 给该project设置description,也就是生成.project文件
注意:a、JavaCore.NATURE_ID = "org.eclipse.jdt.core.javanature"表示是个java project
b、在将description置入project时,保证project是存在的并且打开中。
3. 将org.eclipse.core.resources.IProject转换为java project
IJavaProject javaProject=JavaCore.create(project);
4. 取得eclipse的jre路径和当前project的路径,设置源代码的存放目录以及*.class文件的存放目录
这块生成的是.classPath文件中的内容
5. 将一个字符串编译为java文件和对应的.class文件

例子:

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) {  
    }  
  
}

你可能感兴趣的:(java,eclipse插件,插件)