Setting up the Compile File Command


To be able to compile selected files in a free-form project in the IDE, you need to create an Ant target for the command.

Generating a Skeleton Target for Compile File

You can create this target by right-clicking a .java file and choosing Compile File. The IDE then offers to create a target for you. If you click Generate, the IDE generates a skeleton target in the ide-file-targets.xml file and creates a mapping in the project.xml file between the target and IDE command. The target might look something like this:

<target name="compile-selected-files-in-src">      <fail unless="files">Must set property 'files'</fail>      <mkdir dir="build"/>      <javac destdir="build" includes="${files}" source="1.5"          srcdir="src">          <classpath path="resources"/>      </javac>  </target>


In this example, the files property picks up the files that you have selected in the IDE. The value of files is passed from the project.xml file when you choose the Compile File command. If no files are selected in the IDE when Compile File is chosen (and, therefore, no value is passed to files from the project.xml file), the target does not complete successfully.

In NetBeans IDE 5.0, the IDE also offers to generate the Compile File target for you the first time you try to choose Compile Package on a package. However, the generated target does not actually work on packages. To compile a package, you can use the Ctrl or Shift key to select all of the classes in the package and then right-click and choose Compile Files.


Syntax for Mapping Targets to Commands

Targets in your build script are mapped to IDE commands in the project.xml file using the <action> element. These mappings are entered into the project.xml file auto-matically when you specify targets for commands (in the New Project wizard or the Project Properties dialog box) or when you have the IDE generate a target for you. If you write a target from scratch, you have to enter the mapping manually.

See Table 16-6 for a description of the parts of the <action> element in the project.xml file.


Table 16-6. Details of the project.xml <action> Element

Target, Task, or Property

Description

context

Parameter that the IDE uses to collect information about the files that the command is to be run on.

property

Parameter that defines the name of the property that is passed the names of the currently selected files in the IDE when the command is chosen. A target can then reference this property to determine what files to run the command on. For example, the target that you can have the IDE generate for the Compile File command references the files property to determine which files are to be compiled.

folder

Parameter that enables you to specify the directory in which the target is enabled. In the compile-selected-files-in-src example on the previous page, the value is provided as a reference to the src property.

pattern

Parameter that contains a regular expression to limit the kinds of files that the target can be run on. In this example, only files with the .java extension are passed to the target.

format

Parameter that specifies the form in which the selected files are passed to the target. Possible values for this element are

 

relative-pathpasses the filename with its path relative to the folder specified by the folder element

 

relative-path-noextlike relative-path except that the filename is passed without its extension

 

java-namelike relative-path-noext except that periods (.) are used instead of slashes to delimit the folders in the path

 

absolute-pathpasses the filename with its absolute path

 

absolute-path-noextlike absolute-path except that the filename is passed without its extension

arity

Parameter that specifies whether single or multiple files can be passed to the target. Possible values are

 

<separated-files>delimiter</separated-files> Multiple files can be passed.

 

<one-file-only>Only one file can be passed.


Handling Properties in the project.xml File

In the example in the preceding section, the src property is referenced from the project.xml file. This property needs to be defined, either in a file referenced by the project.xml file or directly in the project.xml file.

In the project.xml file, properties are defined in the <properties> element, which belongs between the <name> and <folders> elements. Within the <properties> element, use the <property> element and its name attribute to define an individual property, or use the <property-file> element to designate a .properties file. Note that this syntax is different from Ant's syntax for defining properties.

After completing the New Project wizard, where you have specified the build script to use, something like the following is generated in your project.xml file:

<properties>   <property name="project.dir">C:\MyNBProjects\SampleFreeForm</property>   <property name="ant.script">${project.dir}/build.xml</property> </properties>


You can add more property references or property file references within the <properties> element. Because the src property in this example is likely a property that can also be used in your build script, it might be useful to set the property in one place and let both the build script and project.xml file use it. A reference to a .properties file from the project.xml file would look something like the following line:

<property-file>${project.dir}/MyProject.properties</property-file>


File paths that are referenced from the project.xml file are relative to the project folder. For path references to work the same for both the project.xml file and the build script, the build script needs to be in the project folder (which is actually the folder that contains the nbproject folder).

If the build script is in a different folder, you might solve the path discrepancy by moving the project.dir property in the example above to a properties file that is common for both the project.xml file and build script, and use that property in the values of other properties that you define for your classpath, source path, and so on. For example, you could create a property to specify the location for compiled class files and give it the value ${project.dir}/build/classes.




NetBeans IDE Field Guide(c) Developing Desktop, Web, Enterprise, and Mobile Applications
NetBeans IDE Field Guide(c) Developing Desktop, Web, Enterprise, and Mobile Applications
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 279

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net