使用代码生成插件工程,脱离eclipse本身的新建工程向导(1)
第一步,新建一个普通的Java工程。在网上,我找到了网友们的实现,经过测试是能正常使用的。
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.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;public class CreateProject {public static void createJavaProject(String projectName) {// 获取工作区IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();// 创建新项目final IProject project = root.getProject(projectName);// 设置工程的位置// 为项目指定存放路径,默认放在当前工作区// IPath projectPath = new Path("");//自己设置IWorkspace workspace = root.getWorkspace();final IProjectDescription description = workspace.newProjectDescription(project.getName());description.setLocation(null);// 设置工程标记,即为java工程String[] javaNature = description.getNatureIds();String[] newJavaNature = new String[javaNature.length + 1];System.arraycopy(javaNature, 0, newJavaNature, 0, javaNature.length);newJavaNature[javaNature.length] = JavaCore.NATURE_ID; // 这个标记证明本工程是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();}// 转化成java工程IJavaProject javaProject = JavaCore.create(project);// 添加JRE库try {// 获取默认的JRE库IClasspathEntry[] jreLibrary = PreferenceConstants.getDefaultJRELibrary();// 获取原来的build pathIClasspathEntry[] oldClasspathEntries = javaProject.getRawClasspath();List list = new ArrayList();list.addAll(Arrays.asList(jreLibrary));list.addAll(Arrays.asList(oldClasspathEntries));javaProject.setRawClasspath((IClasspathEntry[]) 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);// 创建SourceLibraryIClasspathEntry srcClasspathEntry = JavaCore.newSourceEntry(srcFolder.getFullPath());// 得到旧的build pathIClasspathEntry[] oldClasspathEntries = javaProject.readRawClasspath();// 添加新的List list = new ArrayList();list.addAll(Arrays.asList(oldClasspathEntries));list.add(srcClasspathEntry);// 原来存在一个与工程名相同的源文件夹,必须先删除IClasspathEntry temp = JavaCore.newSourceEntry(new Path("/"+ projectName));if (list.contains(temp)) {list.remove(temp);}javaProject.setRawClasspath((IClasspathEntry[]) list.toArray(new IClasspathEntry[list.size()]), null);} catch (CoreException e) {e.printStackTrace();}// ///////////////////////////////创建包//////////////////////////// IPackageFragmentRoot packageFragmentRoot =// javaProject.getPackageFragmentRoot(javaProject.getResource());// 此处得到的src目录只读try {// 先找指定的源文件夹所在的IPackageFragmentRootIPackageFragmentRoot packageFragmentRoot = javaProject.findPackageFragmentRoot(new Path("/" + projectName+ "/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();}}}