在运行着Resources插件的RCP环境中,只需提供projectName,通过代码的方式创建一个具有Java语言特征的项目。
本例子通过一个简单的带有action的例子,创建一个固定名字的Java项目。
将创建项目的代码绑定到Create Sample Java Project 的action上。
String projectName =
"Sample"
;
|
1、创建一个带有.project文件的项目
IProject prj = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
NullProgressMonitor monitor= new NullProgressMonitor();
if (!prj.exists()){
IProjectDescription desc = prj.getWorkspace().newProjectDescription(projectName);
desc.setLocationURI( null );
try {
prj.create(desc, monitor);
if (!prj.isOpen()) {
prj.open(monitor);
monitor= null ;
}
} catch (CoreException e) {
e.printStackTrace();
}
}
2、添加Java项目的特性
IProjectDescription desc = prj.getDescription();
String oldnatureID[] = desc.getNatureIds();
boolean hasJavaNature = prj.hasNature(JavaCore.NATURE_ID );
String newnatureID[] = new String[hasJavaNature?oldnatureID.length :oldnatureID. length + 1];
System. arraycopy(oldnatureID, 0, newnatureID, 0, oldnatureID.length );
if (!hasJavaNature)
newnatureID[oldnatureID. length ]=JavaCore. NATURE_ID;
desc.setNatureIds(newnatureID);
prj.setDescription(desc, new NullProgressMonitor());
IJavaProject javaProject = JavaCore.create(prj);
prj.setDefaultCharset( "UTF-8" ,new NullProgressMonitor());
3、添加src目录
IFolder src = javaProject.getProject().getFolder( "src" );
File srcfile = src.getLocation().toFile();
if (!src.exists()&& !srcfile.exists()){
src.create( false , true , null);
}
4、添加bin目录
IFolder bin = javaProject.getProject().getFolder( "bin" );
File binfile = bin.getLocation().toFile();
if (!bin.exists()&& !binfile.exists()){
bin.create( false , true , null);
}
5、添加.classpath关于jre、output、src的设值
//create .classpath
List< IClasspathEntry> classpathEntries = new ArrayList<IClasspathEntry >();
//jre
String jre = JavaRuntime.getDefaultVMInstall().getName();
IClasspathEntry[] entrties = getJREEntities(jre);
//output
final IPath projectPath = prj.getFullPath();
IPath outputLocation = projectPath.append("/bin" );
//src
@SuppressWarnings ("restriction" )
ClasspathEntry newSourceEntry = (ClasspathEntry) JavaCore
. newSourceEntry(src.getFullPath(),
null );
classpathEntries.add(newSourceEntry);
classpathEntries.addAll(Arrays. asList(entrties));
IClasspathEntry[] classpath = (IClasspathEntry[]) classpathEntries
.toArray( new IClasspathEntry[classpathEntries.size()]);
//Sets the both the classpath of this project and its default output location at once
javaProject.setRawClasspath(classpath, outputLocation,
new NullProgressMonitor());
工具方法
private IClasspathEntry[] getJREEntities(String jreName){
IVMInstallType[] types = JavaRuntime. getVMInstallTypes();
for (int i = 0; i < types. length; i++) {
IVMInstallType type = types[i];
IVMInstall[] installs = type.getVMInstalls();
for (int j = 0; j < installs. length; j++) {
IVMInstall install = installs[j];
if (install.getName().equals(jreName)){
IPath jrePath=JavaRuntime.newJREContainerPath(install);
return new IClasspathEntry[] { JavaCore.newContainerEntry(jrePath) };
}
}
}
return PreferenceConstants.getDefaultJRELibrary();
}