eclipse插件>基于java的项目,如何

b0x*_*0rz 4 java eclipse eclipse-plugin

我在eclipse中创建了一个新的插件,它添加了一个新项目条目,然后可以用它来添加一个新项目.

然而,当去项目属性我得到这个:

我的项目属性

而不是这个:

java项目属性

所以,我的问题是,如何让我的项目也包括所有java的东西(比如:java build path等等),因为我希望这个项目基于默认的java项目.

我目前如何制作项目(代码):

@Override
public boolean performFinish()
{
    if (project != null)
    {
        return true;
    }
    final IProject projectHandle = wizardPage.getProjectHandle();
    URI projectURI = (!wizardPage.useDefaults()) ? wizardPage.getLocationURI() : null;
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    final IProjectDescription desc = workspace.newProjectDescription(projectHandle.getName());
    desc.setLocationURI(projectURI);
    WorkspaceModifyOperation op = new WorkspaceModifyOperation()
    {
        protected void execute(IProgressMonitor monitor) throws CoreException
        {
            createProject(desc, projectHandle, monitor);
        }
    };
    try
    {
        getContainer().run(true, true, op);
    }
    catch (InterruptedException e)
    {
        return false;
    }
    catch (InvocationTargetException e)
    {
        Throwable realException = e.getTargetException();
        MessageDialog.openError(getShell(), "Error", realException.getMessage());
        return false;
    }
    project = projectHandle;
    if (project == null)
    {
        return false;
    }
    BasicNewProjectResourceWizard.updatePerspective(config);
    BasicNewProjectResourceWizard.selectAndReveal(project, workbench.getActiveWorkbenchWindow());
    return true;
}
Run Code Online (Sandbox Code Playgroud)

编辑***

好的,所以解决方案是在项目中添加一个方面.如果我通过我的插件创建一个新项目后手动完成 - 从右键单击,项目属性 - 它的工作原理.如何添加这个facet programmaticaly?

编辑2***

好的,所以通过以下方式完成:

description.setNatureIds
Run Code Online (Sandbox Code Playgroud)

但不完全.

这是我手动添加facet时项目的外观(这就是我想要的样子):

在此输入图像描述

当我添加自然id"org.eclipse.jdt.core.javanature"programmaticaly(不是我想要它)时,这就是它实际看起来的样子

在此输入图像描述

所以...如何解决这个问题?我需要另一种性质吗?

这是我手动添加facet时的.project文件内容:

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
    <name>test</name>
    <comment></comment>
    <projects>
    </projects>
    <buildSpec>
        <buildCommand>
            <name>org.eclipse.jdt.core.javabuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>org.eclipse.wst.common.project.facet.core.builder</name>
            <arguments>
            </arguments>
        </buildCommand>
    </buildSpec>
    <natures>
        <nature>org.eclipse.wst.common.project.facet.core.nature</nature>
        <nature>org.eclipse.jdt.core.javanature</nature>
    </natures>
</projectDescription>
Run Code Online (Sandbox Code Playgroud)

还添加了文件.classpath(手动添加facet时,但是在添加programmaticaly时没有这样的文件):

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
        <attributes>
            <attribute name="owner.project.facets" value="java"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="output" path="bin"/>
</classpath>
Run Code Online (Sandbox Code Playgroud)

这里也是我的方法createProject():

private void createProject(IProjectDescription description, IProject proj, IProgressMonitor monitor)
            throws CoreException, OperationCanceledException
    {
        try
        {
            monitor.beginTask("", 2000);
            proj.create(description, new SubProgressMonitor(monitor, 10));
            if (monitor.isCanceled())
            {
                throw new OperationCanceledException();
            }
            proj.open(IResource.BACKGROUND_REFRESH, new SubProgressMonitor(monitor, 10));
            IContainer container = (IContainer) proj;
            [ *** ]
}
        finally
        {
            monitor.done();
        }
}
Run Code Online (Sandbox Code Playgroud)

Max*_*Max 5

您创建的项目缺少Java和WST性质和构建器.

desc.setNatureIds(new String[] {org.eclipse.jdt.core.JavaCore.NATURE_ID, "org.eclipse.wst.common.project.facet.core.nature"});
org.eclipse.core.resources.ICommand[] commands = new ICommand[] { desc.newCommand(), desc.newCommand };
commands[0].setBuilderName(org.eclipse.jdt.core.JavaCore.BUILDER_ID);
commands[1].setBuilderName("org.eclipse.wst.common.project.facet.core.builder");
desc.setBuildSpec(commands);

createProject(...) {
...
proj.create(description, ...);
IFolder srcFolder = proj.getFolder(new Path("src"));
srcFolder.create(false, true, new NullProgressMonitor());
org.eclipse.jdt.core.IJavaProject javaProject = org.eclipse.jdt.core.JavaCore.create(proj);
org.eclipse.jdt.core.IClasspathEntry src = JavaCore.newSourceEntry(srcFolder.getFullPath());
IClasspathEntry jre = JavaCore.newContainerEntry(new Path(org.eclipse.jdt.launching.JavaRuntime.JRE_CONTAINER), new IAccessRule[0], new IClasspathAttribute[] { JavaCore.newClasspathAttribute("owner.project.facets", "java")}, false);
IClasspathEntry[] entries = new IClasspathEntry[] { src, jre };
javaProject.setRawClasspath(entries, proj.getFullPath().append("bin"), new NullProgressMonitor());
Run Code Online (Sandbox Code Playgroud)

干杯,马克斯