从 Eclipse 插件视图启动外部工具
Launch External Tools from Eclipse Plug-in View
我正在从预先存在的 Java 应用程序项目中构建一个简单的 Eclipse 插件,该项目 依赖于 2 个外部文件,一个 x-executable/application 和一个 .sh 脚本。
该调用在这样的应用程序中实现,(在插件中不起作用):
Process p = new ProcessBuilder("external/application_name", "-d", path).start();
我使用 外部工具配置 来定义我希望如何启动此外部文件(当用户单击视图上的按钮时)并且我已经导出配置(一个示例) :
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.ui.externaltools.ProgramLaunchConfigurationType">
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_LOCATION" value="${workspace_loc:/softwareevolution/external/application_name}"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_TOOL_ARGUMENTS" value="-d ${project_loc}"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_WORKING_DIRECTORY" value="${project_loc}"/>
</launchConfiguration>
- 如何将此应用程序与 Eclipse 插件一起安装,
或者作为它的一部分? (请参阅@howlger 的回答)- 我将插件设置为安装为目录 /
将插件连接到 Feature 项目 - 检查后解压
安装 - 并导出功能项目。 Select 应用程序的
Build/Binary build.
上的文件夹
- 然后我可以使用这个导出的 .launch 文件吗?如果可以的话
我应该在哪个扩展点下将它们包含在 plugin.xml 中? -
没有。(参见@greg-449)
应用程序应该在它所在的路径上生成 2 文件
从执行。 我在尝试时面临权限被拒绝
从插件的安装目录在终端启动它,但不是什么时候
在工作区启动。 (请参阅@howlger 的回答)- 导出插件后,初始
应用程序的权限已更改。在 p2.inf 中使用的指令
将它们修改回来。
新生成的文件(来自运行 .sh 脚本)缺少写入权限。
ProcessBuilder
After finally setting up plugin correctly and adding ProcessBuilder I was getting exception message : Cannot run program "rfind_20" (in
directory
"home/adminuser/.p2/pool/plugins/rFindTest3_1.0.0.201809030453/external"
error=2:, No such file or directory
文件rfind_20确实存在,权限为777。目标项目也存在。
Although the working directory was set to applications folder, the application name was not enough, the absolute path was required as
command argument.
pb = new ProcessBuilder(url.getPath(), "-d", project.getProject().getLocation().toString());
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
IProject project= sampleGetSelectedProject();
ProcessBuilder pb;
Process rfind, ajust, copy;
Bundle bundle = FrameworkUtil.getBundle(getClass());//Bundle bundle = Platform.getBundle("rFindTest3");
URL url = FileLocator.find(bundle, new Path("external/rfind_20"), null);
URL dirurl = FileLocator.find(bundle, new Path("external/"), null);
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
try {
MessageDialog.openInformation(
window.getShell(),
"Test",
project.getProject().getLocation().toString());
url = FileLocator.toFileURL(url);
dirurl = FileLocator.toFileURL(dirurl);
pb = new ProcessBuilder(url.getPath(), "-d", project.getProject().getLocation().toString());
//no matter the working directory the absolute path was required!! sending "rfind_20" as command did not work as command
pb.directory(new File(dirurl.getFile()));
rfind = pb.start();
rfind.waitFor();
rfind.destroy();
}catch(Exception e) {
MessageDialog.openInformation(
window.getShell(),
"Test",
e.getMessage());
}
return null;
}
剩下的唯一谜团是为什么我的 sampleGetProject() 方法在 Plug-in Perspective 中不起作用。因此,请记住在测试您的插件时切换到其他 Perspectives。
如果工作区中有 xxx.launch
文件,您可以使用
启动它
IFile file = ... get IFile for workspace file
ILaunchConfiguration config = DebugPlugin.getDefault().getLaunchManager().getLaunchConfiguration(file);
DebugUITools.launch(config, ILaunchManager.RUN_MODE, false);
如果您有一个可执行文件作为插件的一部分,那么您不能使用 .launch 文件。而是使用 FileLocator
获取可执行文件的位置,并使用 ProcessBuilder
运行
Bundle bundle = FrameworkUtil.getBundle(getClass());
URL url = FileLocator.find(bundle, new Path("relative path to executable"));
url = FileLocator.toFileURL(url);
有两种方法可以通过ProcessBuilder
[=41=作为插件和运行发送应用程序的一部分](*.launch
文件不能在插件中使用):
- 将插件JAR中的可执行文件提取到一个(临时)目录,然后更改 他们的 文件权限 之前 运行 宁他们
- 将插件安装为目录:
- 在
META-INF/MANIFEST.MF
中添加行Eclipse-BundleShape: dir
(参见"The Eclipse-BundleShape Header" 在 Eclipse help - Platform Plug-in Developer Guide - OSGi Bundle Manifest Headers)
- 创建一个Feature Project并在Included Plug-in中连接你的插件,勾选“Unpack the plug-in archive after installation"
- 创建一个包含以下内容的
META-INF/p2.inf
文件(请参阅 Eclipse 帮助 - 平台插件开发人员指南:"Touchpoint Instruction Advice" 在 Customizing p2 metadata and "chmod" in Provisioning Actions and Touchpoints):
instructions.install = \
chmod(targetDir:${artifact.location},targetFile:path/to/executable1,permissions:755);\
chmod(targetDir:${artifact.location},targetFile:path/to-executale_which_generates_files/executable2,permissions:733);\
chmod(targetDir:${artifact.location},targetFile:path/to-executale_which_generates_files/,permissions:766);
instructions.install.import = org.eclipse.equinox.p2.touchpoint.eclipse.chmod
我正在从预先存在的 Java 应用程序项目中构建一个简单的 Eclipse 插件,该项目 依赖于 2 个外部文件,一个 x-executable/application 和一个 .sh 脚本。 该调用在这样的应用程序中实现,(在插件中不起作用):
Process p = new ProcessBuilder("external/application_name", "-d", path).start();
我使用 外部工具配置 来定义我希望如何启动此外部文件(当用户单击视图上的按钮时)并且我已经导出配置(一个示例) :
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.ui.externaltools.ProgramLaunchConfigurationType">
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_LOCATION" value="${workspace_loc:/softwareevolution/external/application_name}"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_TOOL_ARGUMENTS" value="-d ${project_loc}"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_WORKING_DIRECTORY" value="${project_loc}"/>
</launchConfiguration>
- 如何将此应用程序与 Eclipse 插件一起安装, 或者作为它的一部分? (请参阅@howlger 的回答)- 我将插件设置为安装为目录 / 将插件连接到 Feature 项目 - 检查后解压 安装 - 并导出功能项目。 Select 应用程序的 Build/Binary build. 上的文件夹
- 然后我可以使用这个导出的 .launch 文件吗?如果可以的话 我应该在哪个扩展点下将它们包含在 plugin.xml 中? - 没有。(参见@greg-449)
应用程序应该在它所在的路径上生成 2 文件 从执行。 我在尝试时面临权限被拒绝 从插件的安装目录在终端启动它,但不是什么时候 在工作区启动。 (请参阅@howlger 的回答)- 导出插件后,初始 应用程序的权限已更改。在 p2.inf 中使用的指令 将它们修改回来。
新生成的文件(来自运行 .sh 脚本)缺少写入权限。
ProcessBuilder
After finally setting up plugin correctly and adding ProcessBuilder I was getting exception message : Cannot run program "rfind_20" (in directory "home/adminuser/.p2/pool/plugins/rFindTest3_1.0.0.201809030453/external" error=2:, No such file or directory
文件rfind_20确实存在,权限为777。目标项目也存在。
Although the working directory was set to applications folder, the application name was not enough, the absolute path was required as command argument.
pb = new ProcessBuilder(url.getPath(), "-d", project.getProject().getLocation().toString());
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
IProject project= sampleGetSelectedProject();
ProcessBuilder pb;
Process rfind, ajust, copy;
Bundle bundle = FrameworkUtil.getBundle(getClass());//Bundle bundle = Platform.getBundle("rFindTest3");
URL url = FileLocator.find(bundle, new Path("external/rfind_20"), null);
URL dirurl = FileLocator.find(bundle, new Path("external/"), null);
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
try {
MessageDialog.openInformation(
window.getShell(),
"Test",
project.getProject().getLocation().toString());
url = FileLocator.toFileURL(url);
dirurl = FileLocator.toFileURL(dirurl);
pb = new ProcessBuilder(url.getPath(), "-d", project.getProject().getLocation().toString());
//no matter the working directory the absolute path was required!! sending "rfind_20" as command did not work as command
pb.directory(new File(dirurl.getFile()));
rfind = pb.start();
rfind.waitFor();
rfind.destroy();
}catch(Exception e) {
MessageDialog.openInformation(
window.getShell(),
"Test",
e.getMessage());
}
return null;
}
剩下的唯一谜团是为什么我的 sampleGetProject() 方法在 Plug-in Perspective 中不起作用。因此,请记住在测试您的插件时切换到其他 Perspectives。
如果工作区中有 xxx.launch
文件,您可以使用
IFile file = ... get IFile for workspace file
ILaunchConfiguration config = DebugPlugin.getDefault().getLaunchManager().getLaunchConfiguration(file);
DebugUITools.launch(config, ILaunchManager.RUN_MODE, false);
如果您有一个可执行文件作为插件的一部分,那么您不能使用 .launch 文件。而是使用 FileLocator
获取可执行文件的位置,并使用 ProcessBuilder
Bundle bundle = FrameworkUtil.getBundle(getClass());
URL url = FileLocator.find(bundle, new Path("relative path to executable"));
url = FileLocator.toFileURL(url);
有两种方法可以通过ProcessBuilder
[=41=作为插件和运行发送应用程序的一部分](*.launch
文件不能在插件中使用):
- 将插件JAR中的可执行文件提取到一个(临时)目录,然后更改 他们的 文件权限 之前 运行 宁他们
- 将插件安装为目录:
- 在
META-INF/MANIFEST.MF
中添加行Eclipse-BundleShape: dir
(参见"The Eclipse-BundleShape Header" 在 Eclipse help - Platform Plug-in Developer Guide - OSGi Bundle Manifest Headers) - 创建一个Feature Project并在Included Plug-in中连接你的插件,勾选“Unpack the plug-in archive after installation"
- 创建一个包含以下内容的
META-INF/p2.inf
文件(请参阅 Eclipse 帮助 - 平台插件开发人员指南:"Touchpoint Instruction Advice" 在 Customizing p2 metadata and "chmod" in Provisioning Actions and Touchpoints):
- 在
instructions.install = \
chmod(targetDir:${artifact.location},targetFile:path/to/executable1,permissions:755);\
chmod(targetDir:${artifact.location},targetFile:path/to-executale_which_generates_files/executable2,permissions:733);\
chmod(targetDir:${artifact.location},targetFile:path/to-executale_which_generates_files/,permissions:766);
instructions.install.import = org.eclipse.equinox.p2.touchpoint.eclipse.chmod