您的位置:首页 > 编程语言 > Java开发

Eclipse launch configuration----Eclipse运行外部工具

2016-03-09 23:57 579 查看
Eclipse运行外部工具

工具条中点击如下按钮,可以看到External Tools Configurations,点击打开使用外部工具配置对话框。



也可以通过 Run
> External Tools > Open External Tools Dialog打开,默认支持Ant Build和Program两种。



一)运行Program

如写一个简单的bat,如下:

example.bat:

@time /t

@echo "%*"

按照上图在Main中做配置,点击配置的Run按钮运行,则在Eclipse的Console栏中显示如下结果:

22:43

"container_loc:D:\workspace container_name: container_path:""

在${workspace}\.metadata\.plugins\org.eclipse.debug.core\.launches中可以看到Eclipse的配置文件example_bat.launch,内容如下:

<?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:/practise/example.bat}"/>

<stringAttribute key="org.eclipse.ui.externaltools.ATTR_TOOL_ARGUMENTS" value=""container_loc:${container_loc}" "container_name:${container_name}" "container_path:${container_path}""/>

</launchConfiguration>

二)运行Ant

首先在工程主目录下编写Ant脚本build.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project name="myAntExample" default="publish">
<description>
Short description here.
</description>

<property name="versionName" value="1.0.0" />
<property name="dist.dir" value="dist" />
<property name="publish.dir" value="published" />
<property name="classes.jar" value="${dist.dir}/classes.jar" />
<property name="sources.jar" value="${dist.dir}/sources.jar" />

<target name="publish" depends="package" description="Published the files">
<copy file="${classes.jar}" tofile="${publish.dir}/classes-${version}.jar" />
<copy file="${sources.jar}" tofile="${publish.dir}/sources-${version.jar}" />
</target>

<target name="package" depends="prepare" description="Packages the jars">
<jar destfile="${classes.jar}">
<fileset dir="${classes.dir}">
<include name="**/*.class" />
</fileset>
</jar>
<jar destfile="${sources.jar}">
<fileset dir="${sources.dir}">
<include name="**/*.java" />
</fileset>
</jar>
</target>

<target name="prepare">
<mkdir dir="${dist.dir}" />
<mkdir dir="${publish.dir}" />
</target>

</project>

然后在Eclipse中做相应配置,如下图:

1)Main标签:



关于Arguments:

传送给Ant的属性需要以-D<property name>=<value>的格式,如果想要verbose模式的输出并重写dist.dir变量,则需要传入 -v -Ddist.dir=out.

除了像上面一样使用静态数据外,还可以在运行时动态设置参数,通过将上面的<value>改为${string_prompt}。如我们要在运行时动态设置版本号,可以这样配置:-DversionName=${string_prompt}.

其它,如-q, -v , -d 表示输出的模式:quiet, verbose 和 debug. 通过设置-logfile <filename>指定log的输出位置。

关于Variables:

可以配置的变量及含义见下表:


Table 1. Variables for launch configurations

VariableDescription
build_filesIf automatic building is turned on, this variable includes the absolute paths of the files that triggered the build. It support parameters to narrow the choices, such as 
f
 for
files only.
build_projectThis is the absolute path of the project currently being built.
build_typeThis variable represents the type of build being executed (incremental, full, auto, or none). The value isnone if
you try to use this variable when your launch configuration is not a builder.
container_locThis is the absolute name of the current resource's container, which is the folder or directory.
container_nameThis is the name of the container in which the current resource resides.
container_pathThis variable represents the path of the container relative to the workspace.
eclipse_homeThis is the location in which Eclipse is installed.
env_varThis variable inputs the value of the given environment variable, where the name of the variable can be supplied as an argument to the variable.
file_promptWhen Eclipse launches the program, it displays a file selector. The name of the file you select when prompted is put into the value of the variable.
folder_promptThis variable is the same as the file prompt, but is limited to folders.
java_extensions_regexThis is a regex-matching registered Java-like file extensions.
java_type_nameThis variable is the fully qualified Java type name of the primary type in the selected resource.
project_locThis is the absolute path of the project on the file system.
project_nameThis is the name of the project.
project_pathThis is the path of the current project relative to the workspace.
resource_locThis variable represents the absolute path of the current resource on the file system.
resource_nameThis is the name of the resource.
resource_pathThis is the path of the current resource relative to the workspace.
selected_textThis is the value of the text that is selected, if any.
string_promptIf used, Eclipse prompts you for a string value. Whatever you type in the prompt is substituted into the variable.
systemThis is the value of an Eclipse system variable (ARCH, ECLIPSE_HOME, NL, OS, WS).
system_pathThis variable is the absolute path of a tool — supplied as an argument — in the file system.
system_propertyThis is the value of a system property from the Eclipse runtime, where the name of the property is an argument to the variable.
target.archThis variable represents the target processor architecture (for example, x86 for Intel® processors).
target.nlThis is the target locale (for example, en_US or fr_FR).
target.osThis is the target operating system (for example, macosx).
target.wsThis variable represents the target windowing system (for example, Carbon for Mac).
target_homeThis is the target home, which is the same as the Eclipse home.
workspace_locThis is the absolute path of the workspace on the file system.
2)Refresh标签



选中Refresh resources upon completion.可以选择自动刷新的资源位置,省却每次手动刷新的烦恼。各选项的含义如下:

The entire workspace
When selected, Eclipse refreshes every resource in the workspace. If you have a large workspace, you may not want to select this checkbox for performance reasons if you don't absolutely need it.
The selected resource
For the example Ant build in this tutorial, this refresh wouldn't be very effective. The selected resource could often be the build file.
The project containing the selected resource
This option would effectively refresh the project containing the build file (if the build file were selected), but would not be useful if a resource in another project were selected, instead.
The folder containing the selected resource
The same applies here for the Ant build, but this option could be useful with other launch configurations.
Specific resources
This allows you to select specific resources to be refreshed. For this example, select this option, then click Specify Resources. Select
the out, published, and dist directories.
3)Targets标签

这里可以选择运行的Task。



4)ClassPath标签

可以修改默认的Ant Home路径,也可以添加JAR包等。



可以修改默认的Ant Home路径,也可以运行上述配置,即可使用Ant脚本编译。

5)Common标签

默认情况下,launch Configuration是在本地保存的。 可以通过Shared file选项选择保存路径。Eclipse会自动命名该文件为<configuration
name>.launch.好处之一是可以与他人共享该配置。但是需要注意使用到的变量的通用型。



三)定位问题
标准的输入,输出不能解决问题时,可以使用上面的脚本(example.bat)来打印出参数,大部分情况下错误都是由于参数不正确引起的。

三)定位问题
标准的输入,输出不能解决问题时,可以使用上面的脚本来打印出参数,大部分情况下错误都是由于参数不正确引起的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  eclipse