自动化的Java(安卓)的进口通过命令行项目到Eclipse工作区命令行、项目、工作、Java

2023-09-03 22:23:31 作者:自认与酒同醉

我试图导入项目通过命令行自动到Eclipse工作空间(使用bash脚本)。我看过很多帖子使用CDT无头构建即使对于非C / C ++项目的建议,但我想,以避免下载CDT作为我的项目全部的Java / Android的项目,我希望能够自动完成这个对于很多人而不必让他们所有的下载CDT。我曾尝试与JDT的无头的构建与无济于事如下:

I'm trying to automate importing projects to an Eclipse workspace via commandline (using a bash script). I have seen many posts suggesting using the CDT headless build even for non-C/C++ projects, but I want to avoid having to download CDT as my projects are all Java/Android projects and I want to be able to automate this for many people without having to make them all download CDT. I have tried the following with the JDT headless build with no avail:

eclipse -nosplash \
    -application org.eclipse.jdt.apt.core.aptBuild \
    -data [absolute_path_to_desired_workspace] \
    -import [absolute_path_to_project_directories]

输出显示建设工作空间,然后选择注销,但在工作区中打开一个Eclipse的会议没有显示在Package Explorer中。

Output shows "Building workspace" and then "logout," but opening a session of Eclipse in the workspace shows nothing in the Package explorer.

好像在看在工作区中./metadata/.log文件没有显示与输入的任何错误。

Looking at the ./metadata/.log file in the workspace doesn't seem to show any errors with the import.

这难道不是可以自动将现有的Java Eclipse项目到Eclipse的进口,而不使用CDT无头版本?

Is it not possible to automate the import of existing Java Eclipse projects into Eclipse without using the CDT headless build?

推荐答案

不幸的是,JDT分布不具备,将支持 -import 的说法,像CDT的任何应用程序组织.eclipse.cdt.managedbuilder.core.headlessbuild 。但是你可以很容易地编写一个简单的:

Unfortunately, JDT distribution doesn't have any application that would support -import argument, like CDT's org.eclipse.cdt.managedbuilder.core.headlessbuild. But you can easily write a simple one:

package test.myapp;

import java.util.LinkedList;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;

public class Application implements IApplication {

    public Object start(IApplicationContext context) throws Exception {

        String[] args = (String[]) context.getArguments().get(IApplicationContext.APPLICATION_ARGS);

        boolean build = false;

        // Determine projects to import
        List<String> projects = new LinkedList<String>();
        for (int i = 0; i < args.length; ++i) {
            if ("-import".equals(args[i]) && i + 1 < args.length) {
                projects.add(args[++i]);
            } else if ("-build".equals(args[i])) {
                build = true;
            }
        }

        if (projects.size() == 0) {
            System.out.println("No projects to import!");
        } else {
            for (String projectPath : projects) {
                System.out.println("Importing project from: " + projectPath);

                // Import project description:
                IProjectDescription description = ResourcesPlugin.getWorkspace().loadProjectDescription(
                        new Path(projectPath).append(".project"));
                IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(description.getName());
                project.create(description, null);
                project.open(null);
            }

            // Build all projects after importing
            if (build) {
                System.out.println("Re-building workspace");
                ResourcesPlugin.getWorkspace().build(IncrementalProjectBuilder.CLEAN_BUILD, new NullProgressMonitor());
                ResourcesPlugin.getWorkspace().build(IncrementalProjectBuilder.FULL_BUILD, new NullProgressMonitor());
            }
        }
        return null;
    }

    public void stop() {
    }
}

您plugin.xml中应该包含这样的:

Your plugin.xml should contain something like:

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
   <extension
         id="App"
         point="org.eclipse.core.runtime.applications">
      <application
            cardinality="singleton-global"
            thread="main"
            visible="true">
         <run
               class="test.myapp.Application">
         </run>
      </application>
   </extension>
</plugin>

创建,您的插件导出为test.myapp_1.0.0.jar。然后你可以使用它,如下所示:

Create, and export your plug-in as "test.myapp_1.0.0.jar". Then you can use it as follows:

复制test.myapp_1.0.0.jar到Eclipse /的dropins /文件夹

所有需要的插件复制到目标工作区目录: Copy test.myapp_1.0.0.jar to your Eclipse/dropins/ folder

Copy all needed plug-ins to the target workspace directory:

CP -r项目/ * NewWorkspace /

cp -r projects/* NewWorkspace/

导入所需的项目到工作空间:

Import needed projects into the workspace:

日食-nosplash -application test.myapp.App - 数据NewWorkspace -import /路径/要/ NewWorkspace / PROJECT1 -import /路径/要/ NewWorkspace /项目2等等...

eclipse -nosplash -application test.myapp.App -data NewWorkspace -import /path/to/NewWorkspace/project1 -import /path/to/NewWorkspace/project2 etc...

现在,您可以安全地从Eclipse /的dropins /文件夹中删除test.myapp_1.0.0.jar。

Now, you can safely remove test.myapp_1.0.0.jar from the Eclipse/dropins/ folder.

我已经上传所有的code,包括导出插件在这里: https://github.com/ spektom /月食进口

I've uploaded all the code, including the exported plug-in here: https://github.com/spektom/eclipse-import