eclipse插件开发:手动创建一个Java工程,只适合你我有缘者
这是些用来留作笔记的代码,只适合有缘者。
功能包括:
创建新的java工程
设置默认的JRE库
创建源文件夹
创建输出文件夹
创建包
创建Java类
代码未经任何重构和整理,只适合有缘者, 严禁乱喷!!
功能包括:
创建新的java工程
设置默认的JRE库
创建源文件夹
创建输出文件夹
创建包
创建Java类
代码未经任何重构和整理,只适合有缘者, 严禁乱喷!!
1
package
javame.model.action;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.List;
6
7 import org.eclipse.core.resources.ICommand;
8 import org.eclipse.core.resources.IFolder;
9 import org.eclipse.core.resources.IProject;
10 import org.eclipse.core.resources.IProjectDescription;
11 import org.eclipse.core.resources.IResource;
12 import org.eclipse.core.resources.IWorkspace;
13 import org.eclipse.core.resources.IWorkspaceRoot;
14 import org.eclipse.core.resources.ResourcesPlugin;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IPath;
17 import org.eclipse.core.runtime.NullProgressMonitor;
18 import org.eclipse.core.runtime.Path;
19 import org.eclipse.core.runtime.SubProgressMonitor;
20 import org.eclipse.jdt.core.IClasspathEntry;
21 import org.eclipse.jdt.core.ICompilationUnit;
22 import org.eclipse.jdt.core.IJavaProject;
23 import org.eclipse.jdt.core.IPackageFragment;
24 import org.eclipse.jdt.core.IPackageFragmentRoot;
25 import org.eclipse.jdt.core.JavaCore;
26 import org.eclipse.jdt.core.JavaModelException;
27 import org.eclipse.jdt.ui.PreferenceConstants;
28 import org.eclipse.jface.action.IAction;
29 import org.eclipse.jface.viewers.ISelection;
30 import org.eclipse.text.edits.TextEdit;
31 import org.eclipse.ui.IWorkbenchWindow;
32 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
33
34 public class JavaModelAction implements IWorkbenchWindowActionDelegate {
35 private IWorkbenchWindow window;
36
37 @Override
38 public void dispose() {
39 // TODO Auto-generated method stub
40
41 }
42
43 @Override
44 public void init(IWorkbenchWindow window) {
45 // TODO Auto-generated method stub
46 this .window = window;
47 }
48
49 @Override
50 public void run(IAction action) {
51 // 获取工作区
52 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
53
54 // ////////////////////////////////// /创建新项目 ////////////////////////// /
55 final IProject project = root.getProject( " xyz " );
56 // 为项目指定存放路径,默认放在当前工作区
57 IPath projectPath = new Path( " g:/myplugIn " );
58
59 // 设置工程的位置
60 IWorkspace workspace = root.getWorkspace();
61 final IProjectDescription description = workspace
62 .newProjectDescription(project.getName());
63 description.setLocation(projectPath);
64
65 // 设置工程标记,即为java工程
66 String[] javaNature = description.getNatureIds();
67 String[] newJavaNature = new String[javaNature.length + 1 ];
68 System.arraycopy(javaNature, 0 , newJavaNature, 0 , javaNature.length);
69 newJavaNature[javaNature.length] = " org.eclipse.jdt.core.javanature " ; // 这个标记证明本工程是Java工程
70 description.setNatureIds(newJavaNature);
71
72 // //////////////////////////// /
73 try {
74 NullProgressMonitor monitor = new NullProgressMonitor();
75 project.create(description, monitor);
76 project.open(IResource.BACKGROUND_REFRESH, new SubProgressMonitor(
77 monitor, 1000 ));
78 } catch (CoreException e) {
79 // TODO Auto-generated catch block
80 e.printStackTrace();
81 }
82
83 // 下面代码只在新建向导的情况下才可用
84 /*
85 * //创建新项目,WorkspaceModifyOperation位于org.eclipse.ui.ide中
86 * WorkspaceModifyOperation workspaceModifyOperation = new
87 * WorkspaceModifyOperation() {
88 *
89 * @Override protected void execute(IProgressMonitor monitor) throws
90 * CoreException, InvocationTargetException, InterruptedException { try
91 * { monitor.beginTask("", 2000); project.create(description, monitor);
92 *
93 * if(monitor.isCanceled()){ throw new OperationCanceledException(); }
94 *
95 * project.open(IResource.BACKGROUND_REFRESH, new
96 * SubProgressMonitor(monitor, 1000)); } catch (Exception e) {
97 * e.printStackTrace(); } finally{ monitor.done(); } } };
98 * //接下来调用IWizard的getContainer().run()方法.
99 */
100
101 // 转化成java工程
102 IJavaProject javaProject = JavaCore.create(project);
103 // ////////////////////////////////// 添加JRE库 ////////////////////////////
104 try {
105 // 获取默认的JRE库
106 IClasspathEntry[] jreLibrary = PreferenceConstants
107 .getDefaultJRELibrary();
108 // 获取原来的build path
109 IClasspathEntry[] oldClasspathEntries = javaProject
110 .getRawClasspath();
111 List < IClasspathEntry > list = new ArrayList < IClasspathEntry > ();
112 list.addAll(Arrays.asList(jreLibrary));
113 list.addAll(Arrays.asList(oldClasspathEntries));
114
115 javaProject.setRawClasspath(list.toArray( new IClasspathEntry[list
116 .size()]), null );
117 } catch (JavaModelException e) {
118 // TODO Auto-generated catch block
119 e.printStackTrace();
120 }
121
122 // ////////////////////////////////// 创建输出路径 //////////////////////////// /
123 IFolder binFolder = javaProject.getProject().getFolder( " bin " );
124 try {
125 binFolder.create( true , true , null );
126 javaProject.setOutputLocation(binFolder.getFullPath(), null );
127 } catch (CoreException e) {
128 // TODO Auto-generated catch block
129 e.printStackTrace();
130 }
131
132 // //////////////////////// /设置Java生成器 ////////////////////// /
133 try {
134 IProjectDescription description2 = javaProject.getProject()
135 .getDescription();
136 ICommand command = description2.newCommand();
137 command.setBuilderName( " org.eclipse.jdt.core.javabuilder " );
138 description2.setBuildSpec( new ICommand[] { command });
139 description2
140 .setNatureIds( new String[] { " org.eclipse.jdt.core.javanature " });
141 javaProject.getProject().setDescription(description2, null );
142 } catch (CoreException e1) {
143 // TODO Auto-generated catch block
144 e1.printStackTrace();
145 }
146
147 // //////////////////////////// /创建源代码文件夹 //////////////////////////
148 // ////////// /源文件夹和文件夹相似,只是使用PackageFragmentRoot进行了封装 ////////
149 IFolder srcFolder = javaProject.getProject().getFolder( " src " );
150 try {
151 srcFolder.create( true , true , null );
152 // this.createFolder(srcFolder);
153 // 创建SourceLibrary
154 IClasspathEntry srcClasspathEntry = JavaCore
155 .newSourceEntry(srcFolder.getFullPath());
156
157 // 得到旧的build path
158 IClasspathEntry[] oldClasspathEntries = javaProject
159 .readRawClasspath();
160
161 // 添加新的
162 List < IClasspathEntry > list = new ArrayList < IClasspathEntry > ();
163 list.addAll(Arrays.asList(oldClasspathEntries));
164 list.add(srcClasspathEntry);
165
166 // 原来存在一个与工程名相同的源文件夹,必须先删除
167 IClasspathEntry temp = JavaCore.newSourceEntry( new Path( " /xyz " ));
168 if (list.contains(temp)) {
169 list.remove(temp);
170 }
171
172 System.out.println(list.size());
173
174 javaProject.setRawClasspath(list.toArray( new IClasspathEntry[list
175 .size()]), null );
176 } catch (CoreException e) {
177 // TODO Auto-generated catch block
178 e.printStackTrace();
179 }
180
181 // ////////////////////////////// /创建包 //////////////////////////
182 // IPackageFragmentRoot packageFragmentRoot =
183 // javaProject.getPackageFragmentRoot(javaProject.getResource());
184
185 try {
186 // 先找指定的源文件夹所在的IPackageFragmentRoot
187 IPackageFragmentRoot packageFragmentRoot = javaProject
188 .findPackageFragmentRoot( new Path( " /xyz/src " ));
189 // 根据IPackageFragmentRoot创建IPackageFragment,IPackageFragment就是包了
190 IPackageFragment packageFragment = packageFragmentRoot
191 .createPackageFragment( " com.aptech.plugin " , true , null );
192 } catch (JavaModelException e) {
193 // TODO Auto-generated catch block
194 e.printStackTrace();
195 }
196
197 // ////////////////////////////////// 创建Java文件 ////////////////////////
198 try {
199 String javaCode = " package com.aptech.plugin;public class HelloWorld{public static void main(String[] args){System.out.println(\ " 中华人民共和国\ " );}} " ;
200 IPackageFragmentRoot packageFragmentRoot = javaProject
201 .findPackageFragmentRoot( new Path( " /xyz/src " ));
202 IPackageFragment packageFragment = packageFragmentRoot
203 .getPackageFragment( " com.aptech.plugin " );
204 packageFragment.createCompilationUnit( " HelloWorld.java " , javaCode,
205 true , new NullProgressMonitor());
206
207 } catch (JavaModelException e) {
208 // TODO Auto-generated catch block
209 e.printStackTrace();
210 } catch (Exception e) {
211 // TODO Auto-generated catch block
212 e.printStackTrace();
213 }
214
215 // ////////////////////////////// /格式化java代码 //////////////////
216 }
217
218 @Override
219 public void selectionChanged(IAction action, ISelection selection) {
220 // TODO Auto-generated method stub
221 }
222
223 }
224
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.List;
6
7 import org.eclipse.core.resources.ICommand;
8 import org.eclipse.core.resources.IFolder;
9 import org.eclipse.core.resources.IProject;
10 import org.eclipse.core.resources.IProjectDescription;
11 import org.eclipse.core.resources.IResource;
12 import org.eclipse.core.resources.IWorkspace;
13 import org.eclipse.core.resources.IWorkspaceRoot;
14 import org.eclipse.core.resources.ResourcesPlugin;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IPath;
17 import org.eclipse.core.runtime.NullProgressMonitor;
18 import org.eclipse.core.runtime.Path;
19 import org.eclipse.core.runtime.SubProgressMonitor;
20 import org.eclipse.jdt.core.IClasspathEntry;
21 import org.eclipse.jdt.core.ICompilationUnit;
22 import org.eclipse.jdt.core.IJavaProject;
23 import org.eclipse.jdt.core.IPackageFragment;
24 import org.eclipse.jdt.core.IPackageFragmentRoot;
25 import org.eclipse.jdt.core.JavaCore;
26 import org.eclipse.jdt.core.JavaModelException;
27 import org.eclipse.jdt.ui.PreferenceConstants;
28 import org.eclipse.jface.action.IAction;
29 import org.eclipse.jface.viewers.ISelection;
30 import org.eclipse.text.edits.TextEdit;
31 import org.eclipse.ui.IWorkbenchWindow;
32 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
33
34 public class JavaModelAction implements IWorkbenchWindowActionDelegate {
35 private IWorkbenchWindow window;
36
37 @Override
38 public void dispose() {
39 // TODO Auto-generated method stub
40
41 }
42
43 @Override
44 public void init(IWorkbenchWindow window) {
45 // TODO Auto-generated method stub
46 this .window = window;
47 }
48
49 @Override
50 public void run(IAction action) {
51 // 获取工作区
52 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
53
54 // ////////////////////////////////// /创建新项目 ////////////////////////// /
55 final IProject project = root.getProject( " xyz " );
56 // 为项目指定存放路径,默认放在当前工作区
57 IPath projectPath = new Path( " g:/myplugIn " );
58
59 // 设置工程的位置
60 IWorkspace workspace = root.getWorkspace();
61 final IProjectDescription description = workspace
62 .newProjectDescription(project.getName());
63 description.setLocation(projectPath);
64
65 // 设置工程标记,即为java工程
66 String[] javaNature = description.getNatureIds();
67 String[] newJavaNature = new String[javaNature.length + 1 ];
68 System.arraycopy(javaNature, 0 , newJavaNature, 0 , javaNature.length);
69 newJavaNature[javaNature.length] = " org.eclipse.jdt.core.javanature " ; // 这个标记证明本工程是Java工程
70 description.setNatureIds(newJavaNature);
71
72 // //////////////////////////// /
73 try {
74 NullProgressMonitor monitor = new NullProgressMonitor();
75 project.create(description, monitor);
76 project.open(IResource.BACKGROUND_REFRESH, new SubProgressMonitor(
77 monitor, 1000 ));
78 } catch (CoreException e) {
79 // TODO Auto-generated catch block
80 e.printStackTrace();
81 }
82
83 // 下面代码只在新建向导的情况下才可用
84 /*
85 * //创建新项目,WorkspaceModifyOperation位于org.eclipse.ui.ide中
86 * WorkspaceModifyOperation workspaceModifyOperation = new
87 * WorkspaceModifyOperation() {
88 *
89 * @Override protected void execute(IProgressMonitor monitor) throws
90 * CoreException, InvocationTargetException, InterruptedException { try
91 * { monitor.beginTask("", 2000); project.create(description, monitor);
92 *
93 * if(monitor.isCanceled()){ throw new OperationCanceledException(); }
94 *
95 * project.open(IResource.BACKGROUND_REFRESH, new
96 * SubProgressMonitor(monitor, 1000)); } catch (Exception e) {
97 * e.printStackTrace(); } finally{ monitor.done(); } } };
98 * //接下来调用IWizard的getContainer().run()方法.
99 */
100
101 // 转化成java工程
102 IJavaProject javaProject = JavaCore.create(project);
103 // ////////////////////////////////// 添加JRE库 ////////////////////////////
104 try {
105 // 获取默认的JRE库
106 IClasspathEntry[] jreLibrary = PreferenceConstants
107 .getDefaultJRELibrary();
108 // 获取原来的build path
109 IClasspathEntry[] oldClasspathEntries = javaProject
110 .getRawClasspath();
111 List < IClasspathEntry > list = new ArrayList < IClasspathEntry > ();
112 list.addAll(Arrays.asList(jreLibrary));
113 list.addAll(Arrays.asList(oldClasspathEntries));
114
115 javaProject.setRawClasspath(list.toArray( new IClasspathEntry[list
116 .size()]), null );
117 } catch (JavaModelException e) {
118 // TODO Auto-generated catch block
119 e.printStackTrace();
120 }
121
122 // ////////////////////////////////// 创建输出路径 //////////////////////////// /
123 IFolder binFolder = javaProject.getProject().getFolder( " bin " );
124 try {
125 binFolder.create( true , true , null );
126 javaProject.setOutputLocation(binFolder.getFullPath(), null );
127 } catch (CoreException e) {
128 // TODO Auto-generated catch block
129 e.printStackTrace();
130 }
131
132 // //////////////////////// /设置Java生成器 ////////////////////// /
133 try {
134 IProjectDescription description2 = javaProject.getProject()
135 .getDescription();
136 ICommand command = description2.newCommand();
137 command.setBuilderName( " org.eclipse.jdt.core.javabuilder " );
138 description2.setBuildSpec( new ICommand[] { command });
139 description2
140 .setNatureIds( new String[] { " org.eclipse.jdt.core.javanature " });
141 javaProject.getProject().setDescription(description2, null );
142 } catch (CoreException e1) {
143 // TODO Auto-generated catch block
144 e1.printStackTrace();
145 }
146
147 // //////////////////////////// /创建源代码文件夹 //////////////////////////
148 // ////////// /源文件夹和文件夹相似,只是使用PackageFragmentRoot进行了封装 ////////
149 IFolder srcFolder = javaProject.getProject().getFolder( " src " );
150 try {
151 srcFolder.create( true , true , null );
152 // this.createFolder(srcFolder);
153 // 创建SourceLibrary
154 IClasspathEntry srcClasspathEntry = JavaCore
155 .newSourceEntry(srcFolder.getFullPath());
156
157 // 得到旧的build path
158 IClasspathEntry[] oldClasspathEntries = javaProject
159 .readRawClasspath();
160
161 // 添加新的
162 List < IClasspathEntry > list = new ArrayList < IClasspathEntry > ();
163 list.addAll(Arrays.asList(oldClasspathEntries));
164 list.add(srcClasspathEntry);
165
166 // 原来存在一个与工程名相同的源文件夹,必须先删除
167 IClasspathEntry temp = JavaCore.newSourceEntry( new Path( " /xyz " ));
168 if (list.contains(temp)) {
169 list.remove(temp);
170 }
171
172 System.out.println(list.size());
173
174 javaProject.setRawClasspath(list.toArray( new IClasspathEntry[list
175 .size()]), null );
176 } catch (CoreException e) {
177 // TODO Auto-generated catch block
178 e.printStackTrace();
179 }
180
181 // ////////////////////////////// /创建包 //////////////////////////
182 // IPackageFragmentRoot packageFragmentRoot =
183 // javaProject.getPackageFragmentRoot(javaProject.getResource());
184
185 try {
186 // 先找指定的源文件夹所在的IPackageFragmentRoot
187 IPackageFragmentRoot packageFragmentRoot = javaProject
188 .findPackageFragmentRoot( new Path( " /xyz/src " ));
189 // 根据IPackageFragmentRoot创建IPackageFragment,IPackageFragment就是包了
190 IPackageFragment packageFragment = packageFragmentRoot
191 .createPackageFragment( " com.aptech.plugin " , true , null );
192 } catch (JavaModelException e) {
193 // TODO Auto-generated catch block
194 e.printStackTrace();
195 }
196
197 // ////////////////////////////////// 创建Java文件 ////////////////////////
198 try {
199 String javaCode = " package com.aptech.plugin;public class HelloWorld{public static void main(String[] args){System.out.println(\ " 中华人民共和国\ " );}} " ;
200 IPackageFragmentRoot packageFragmentRoot = javaProject
201 .findPackageFragmentRoot( new Path( " /xyz/src " ));
202 IPackageFragment packageFragment = packageFragmentRoot
203 .getPackageFragment( " com.aptech.plugin " );
204 packageFragment.createCompilationUnit( " HelloWorld.java " , javaCode,
205 true , new NullProgressMonitor());
206
207 } catch (JavaModelException e) {
208 // TODO Auto-generated catch block
209 e.printStackTrace();
210 } catch (Exception e) {
211 // TODO Auto-generated catch block
212 e.printStackTrace();
213 }
214
215 // ////////////////////////////// /格式化java代码 //////////////////
216 }
217
218 @Override
219 public void selectionChanged(IAction action, ISelection selection) {
220 // TODO Auto-generated method stub
221 }
222
223 }
224