代码创建java工程
博客分类: eclipse plugin
.
package com.wisoft.magicube.codegenerator.popup.actions;
002
003 import java.util.ArrayList;
004 import java.util.Arrays;
005 import java.util.List;
006
007 import org.eclipse.core.resources.ICommand;
008 import org.eclipse.core.resources.IFolder;
009 import org.eclipse.core.resources.IProject;
010 import org.eclipse.core.resources.IProjectDescription;
011 import org.eclipse.core.resources.IResource;
012 import org.eclipse.core.resources.IWorkspace;
013 import org.eclipse.core.resources.IWorkspaceRoot;
014 import org.eclipse.core.resources.ResourcesPlugin;
015 import org.eclipse.core.runtime.CoreException;
016 import org.eclipse.core.runtime.IPath;
017 import org.eclipse.core.runtime.NullProgressMonitor;
018 import org.eclipse.core.runtime.Path;
019 import org.eclipse.core.runtime.SubProgressMonitor;
020 import org.eclipse.jdt.core.IClasspathEntry;
021 import org.eclipse.jdt.core.IJavaProject;
022 import org.eclipse.jdt.core.IPackageFragment;
023 import org.eclipse.jdt.core.IPackageFragmentRoot;
024 import org.eclipse.jdt.core.JavaCore;
025 import org.eclipse.jdt.core.JavaModelException;
026 import org.eclipse.jdt.ui.PreferenceConstants;
027 import org.eclipse.jface.action.IAction;
028 import org.eclipse.jface.viewers.ISelection;
029 import org.eclipse.ui.IObjectActionDelegate;
030 import org.eclipse.ui.IWorkbenchPart;
031
032 public class NewJavaProjectAction implements IObjectActionDelegate {
033
034 public void run(IAction action) {
035 // 获取工作区
036 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
037
038 // ///创建新项目///
039 final IProject project = root.getProject("xyz");
040
041 // 设置工程的位置
042 // 为项目指定存放路径,默认放在当前工作区
043 IPath projectPath = new Path("g:/myplugIn");
044 IWorkspace workspace = root.getWorkspace();
045 final IProjectDescription description = workspace.newProjectDescription(project.getName());
046 description.setLocation(projectPath);
047
048 // 设置工程标记,即为java工程
049 String[] javaNature = description.getNatureIds();
050 String[] newJavaNature = new String[javaNature.length + 1];
051 System.arraycopy(javaNature, 0, newJavaNature, 0, javaNature.length);
052 newJavaNature[javaNature.length] = "org.eclipse.jdt.core.javanature"; // 这个标记证明本工程是Java工程
053 description.setNatureIds(newJavaNature);
054
055 // /
056 try {
057 NullProgressMonitor monitor = new NullProgressMonitor();
058 project.create(description, monitor);
059 project.open(IResource.BACKGROUND_REFRESH, new SubProgressMonitor(monitor, 1000));
060 } catch (CoreException e) {
061 e.printStackTrace();
062 }
063
064 // 下面代码只在新建向导的情况下才可用
065 /*
066 * //创建新项目,WorkspaceModifyOperation位于org.eclipse.ui.ide中
067 * WorkspaceModifyOperation workspaceModifyOperation = new
068 * WorkspaceModifyOperation() {
069 *
070 * @Override protected void execute(IProgressMonitor monitor) throws
071 * CoreException, InvocationTargetException, InterruptedException { try
072 * { monitor.beginTask("", ); project.create(description, monitor);
073 *
074 * if(monitor.isCanceled()){ throw new OperationCanceledException(); }
075 *
076 * project.open(IResource.BACKGROUND_REFRESH, new
077 * SubProgressMonitor(monitor, )); } catch (Exception e) {
078 * e.printStackTrace(); } finally{ monitor.done(); } } };
079 * //接下来调用IWizard的getContainer().run()方法.
080 */
081
082 // 转化成java工程
083 IJavaProject javaProject = JavaCore.create(project);
084 // //添加JRE库
085 try {
086 // 获取默认的JRE库
087 IClasspathEntry[] jreLibrary = PreferenceConstants.getDefaultJRELibrary();
088 // 获取原来的build path
089 IClasspathEntry[] oldClasspathEntries = javaProject.getRawClasspath();
090 List list = new ArrayList();
091 list.addAll(Arrays.asList(jreLibrary));
092 list.addAll(Arrays.asList(oldClasspathEntries));
093
094 javaProject.setRawClasspath(list.toArray(new IClasspathEntry[list.size()]), null);
095 } catch (JavaModelException e) {
096 e.printStackTrace();
097 }
098
099 // //创建输出路径/
100 IFolder binFolder = javaProject.getProject().getFolder("bin");
101 try {
102 binFolder.create(true, true, null);
103 javaProject.setOutputLocation(binFolder.getFullPath(), null);
104 } catch (CoreException e) {
105 e.printStackTrace();
106 }
107
108 // /设置Java生成器///
109 try {
110 IProjectDescription description2 = javaProject.getProject().getDescription();
111 ICommand command = description2.newCommand();
112 command.setBuilderName("org.eclipse.jdt.core.javabuilder");
113 description2.setBuildSpec(new ICommand[] { command });
114 description2.setNatureIds(new String[] { "org.eclipse.jdt.core.javanature" });
115 javaProject.getProject().setDescription(description2, null);
116 } catch (CoreException e) {
117 e.printStackTrace();
118 }
119
120 // /创建源代码文件夹//
121 // ///源文件夹和文件夹相似,只是使用PackageFragmentRoot进行了封装
122 IFolder srcFolder = javaProject.getProject().getFolder("src");
123 try {
124 srcFolder.create(true, true, null);
125 // this.createFolder(srcFolder);
126 // 创建SourceLibrary
127 IClasspathEntry srcClasspathEntry = JavaCore.newSourceEntry(srcFolder.getFullPath());
128
129 // 得到旧的build path
130 IClasspathEntry[] oldClasspathEntries = javaProject.readRawClasspath();
131
132 // 添加新的
133 List list = new ArrayList();
134 list.addAll(Arrays.asList(oldClasspathEntries));
135 list.add(srcClasspathEntry);
136
137 // 原来存在一个与工程名相同的源文件夹,必须先删除
138 IClasspathEntry temp = JavaCore.newSourceEntry(new Path("/xyz"));
139 if (list.contains(temp)) {
140 list.remove(temp);
141 }
142
143 System.out.println(list.size());
144
145 javaProject.setRawClasspath(list.toArray(new IClasspathEntry[list.size()]), null);
146 } catch (CoreException e) {
147 e.printStackTrace();
148 }
149
150 // ///创建包//
151 // IPackageFragmentRoot packageFragmentRoot = javaProject.getPackageFragmentRoot(javaProject.getResource());
152 //此处得到的src目录只读
153
154 try {
155 // 先找指定的源文件夹所在的IPackageFragmentRoot
156 IPackageFragmentRoot packageFragmentRoot = javaProject.findPackageFragmentRoot(newPath("/xyz/src"));
157 // 根据IPackageFragmentRoot创建IPackageFragment,IPackageFragment就是包了
158 IPackageFragment packageFragment = packageFragmentRoot.createPackageFragment("com.aptech.plugin", true, null);
159
160 // //创建Java文件
161 String javaCode = "package com.aptech.plugin;public class HelloWorld{public static void main(String[] args){System.out.println(\"中华人民共和国\");}}";
162 packageFragment.createCompilationUnit("HelloWorld.java", javaCode, true, newNullProgressMonitor());
163
164 } catch (JavaModelException e) {
165 e.printStackTrace();
166 } catch (Exception e) {
167 e.printStackTrace();
168 }
169 }
170
171
172 public void setActivePart(IAction action, IWorkbenchPart targetPart) {
173 }
174
175
176 public void selectionChanged(IAction action, ISelection selection) {
177 }
178
179 }