且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Java将Java项目导入Eclipse

更新时间:2023-09-18 11:06:46

你有这里是相同的想法,由 Liran Orevi ,但有一些更多的细节和代码示例:

  / ** 
*导入给定的路径作为项目进入工作区。如果
*操作成功,则返回true,如果由于重叠导致无法导入,则返回false。
*
* @param projectPath
* @return
* @throws如果操作失败,CoreException
* /
private boolean importExisitingProject(IPath projectPath)throws CoreException {
//加载项目描述文件
final IProjectDescription description = workspace.loadProjectDescription(
projectPath.append(IPath.SEPARATOR + IProjectDescription.DESCRIPTION_FILE_NAME));
final IProject project = workspace.getRoot()。getProject(description.getName());

//仅导入项目似乎不存在才导入。如果它看起来像
//存在,告诉用户它。
if(project.exists()){
System.err.println(SKTBuildPlugin.getFormattedMessage(
Build.commandLine.projectExists,// $ NON-NLS-1 $
project.getName()));
返回false;
}
IWorkspaceRunnable runnable = new IWorkspaceRunnable(){
public void run(IProgressMonitor monitor)throws CoreException {
project.create(description,monitor);
project.open(IResource.NONE,monitor);
}
};
workspace.run(runnable,
workspace.getRuleFactory()。modifyRule(workspace.getRoot()),
IResource.NONE,null);
返回true;
}






此线程,您还可以导入压缩的项目,并启用一些代码从$ code> org.eclipse.ui.internal.wizards.datatransfer.WizardProjectsImportPage.java 。


I've written a java program that writes another java project. However, I want to add a piece of specific code that will import the project into the workspace. Can this be done?

You have here the same idea expressed by Liran Orevi but with some more details and a code example:

/**
* Imports the given path into the workspace as a project. Returns true if the
* operation succeeded, false if it failed to import due to an overlap.
*
* @param projectPath
* @return
* @throws CoreException if operation fails catastrophically
*/
private boolean importExisitingProject(IPath projectPath) throws CoreException {
    // Load the project description file
    final IProjectDescription description = workspace.loadProjectDescription(
    projectPath.append(IPath.SEPARATOR + IProjectDescription.DESCRIPTION_FILE_NAME));
    final IProject project = workspace.getRoot().getProject(description.getName());

    // Only import the project if it doesn't appear to already exist. If it looks like it
    // exists, tell the user about it.
    if (project.exists()) {
        System.err.println(SKTBuildPlugin.getFormattedMessage(
        "Build.commandLine.projectExists",  //$NON-NLS-1$
        project.getName()));
        return false;
    }
    IWorkspaceRunnable runnable = new IWorkspaceRunnable() {
        public void run(IProgressMonitor monitor) throws CoreException {
            project.create(description, monitor);
            project.open(IResource.NONE, monitor);
        }
    };
    workspace.run(runnable,
    workspace.getRuleFactory().modifyRule(workspace.getRoot()),
    IResource.NONE, null);
    return true;
}


In this thread, you can also import projects which are zipped, with some code inspired from mostly scraped from org.eclipse.ui.internal.wizards.datatransfer.WizardProjectsImportPage.java.