【问题标题】:How can I add a custom file(built for every created project) in Eclipse?如何在 Eclipse 中添加自定义文件(为每个创建的项目构建)?
【发布时间】:2019-12-14 02:43:22
【问题描述】:

我想为 Eclipse 创建一个插件,其中我创建的每个自定义项目都有一个特定的模板和特定的文件。我设法创建了模板(folder1/folder_name1、folder1/folder_name2、folder2/folder_name1 等),但我仍在试图弄清楚如何确保在创建此类项目时创建一些自定义文件(即在文件夹名称 1 和文件夹名称 2 中)。您认为最好的方法是什么?

我尝试过使用 IFile,但我不确定如何使用它。

这是创建项目的函数:

public static IProject createProject(String projectName, URI location) {
    Assert.isNotNull(projectName);
    Assert.isTrue(projectName.trim().length() > 0);

    IProject project = createBaseProject(projectName, location);
    try {
        addNature(project);

        String[] paths = {
                "folder1/folder_name1", //$NON-NLS-1$
                "folder1/folder_name2",
                "folder2/folder_name1",
                "folder2/folder_name2"}; //$NON-NLS-1$
        addToProjectStructure(project, paths);
    } catch (CoreException e) {
        e.printStackTrace();
        project = null;
    }

    return project;
}

我希望在 folder_name1 和 folder_name2 中为每个 folder1/2 创建一个文件(我们将其命名为 test.cpp)。

【问题讨论】:

    标签: java eclipse plugins


    【解决方案1】:

    您有项目对象,即IProject project = createBaseProject(projectName, location); 你可以使用下面的代码sn-p

    ....其他代码...

    IFile templateFile = project.getFile("/projectPath/folder1/folder_name1/templateFile1.txt"); 
    String fileContents = "Template File Contents";//You can get the contents from source
    InputStream source = new ByteArrayInputStream(contents.getBytes());
    templateFile .create(source, false, null);
    

    我希望您可以在此创建特定于项目文件夹的文件。你也可以从其他人那里得到答案。

    【讨论】:

      猜你喜欢
      • 2013-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-09
      • 2016-07-16
      • 1970-01-01
      • 2021-04-23
      相关资源
      最近更新 更多