Compiling a Project


Once you have set up your project, created your source files, and set up any necessary dependencies, you can compile your project by choosing Build | Build Main Project (F11).

By default, when you build a standard Java project in the IDE, the following occurs:

  • All modified files in the project are saved.

  • Any uncompiled files (or files that have been modified since they were last compiled) under the Source Packages node are compiled.

  • A JAR file (or other archive, depending on the project category) is created with your classes and other resources.

  • Any projects the main project depends on are built. (In fact, these projects are built first.)

When you build your project, output on the progress of the Ant script is printed to the Output window.

If you merely want to compile your project (without building JAR files or running any other postcompile steps), you can create a shortcut to the compile target in the project's build script. Open the Files window and expand the build.xml node. Rightclick the compile target and choose Create Shortcut. In the wizard, you can designate a menu item, toolbar button, and a keyboard shortcut for the target. The shortcut works only for that particular project.


Setting Compiler Options

You can set compiler options in the Project Properties dialog box for a project.

  1. Right-click the project's node and choose Properties.

  2. In the Project Properties dialog box, select the Build | Compile node.

  3. Select the checkbox for any options you want to have included.

  4. For options that are not covered by a checkbox, type the option in the Additional Compiler Options field as you would when compiling from the command line.

Compiling Selected Files or Packages

If you have a larger project, you might want to be able to compile a few files without rebuilding the entire project.

To compile a single file, right-click the file's node and choose Compile File (F9).

To compile a package, right-click the package's node in the Projects window and choose Compile Package (F9).

Doing a Fresh Build

The Build Project command produces its outputs incrementally. When you rebuild a project, any files that have changed or have been added are recompiled, and the JAR file (or other output file) is repackaged with these changed classes. As your project evolves, sometimes you need to explicitly erase your compiled classes and distributables, particularly if you have removed or renamed some source files. Otherwise, classes that are no longer part of your sources might linger in your compiled outputs and cause problems with running your program.

To clean your project, right-click the node for your project in the Projects window and choose Clean Project. This command deletes the build and dist folders in your project.

To do a fresh build, choose Build | Clean and Build Main Project (Shift-F11).

Stopping a Build

If you start a build and want to halt it before it completes, choose Build | Stop Build/ Run. The build will terminate, but any artifacts created by the build will remain.

Changing the Location of Compiled Classes and JAR Files

By default, the compiled class files and the packaged distributable (for example, a JAR or WAR file) are placed in folders (named build and dist, respectively) parallel to the src folder in your project. If you would like the outputs to appear elsewhere or simply would like to change the names of the folders, you can do so in the project.properties file.

If you want to have your class files placed in the same directory as their source files, you need to override the project's clean target so that only class files are deleted from the directory when the Clean command is run. You also need to adjust the IDE's Ignored Files property. See Compiling Classes into the Same Directories As Your Sources later in this chapter.


To access the project.properties file:

  1. Open the Files window by clicking the Files tab (or pressing Ctrl-2).

  2. Expand the project's nbproject folder and open the project.properties file.

Table 3-3 lists properties that determine where your outputs are created for general Java and web projects.

Table 3-3. Ant Properties for Build Outputs in Standard Projects

Property

Specifies the Directory...

build.classes.dir

Where compiled class files are created. You can change the name of the output folder here.

build.test.classes.dir

Where unit test files are created (for general Java and web projects).

build.test.results.dir

Where unit test results are stored (for general Java and web projects).

build.generated.dir

Where the IDE places various temporary files, such as servlet source and class files generated by the IDE when you run the Compile JSP command. These files are not included in the WAR file.

build.web.dir

Where the WEB-INF folder and other key folders are placed in the built web application (for web projects).

build.dir

Where the above directories for the previously listed properties are placed. You can also change the value of this property. If you remove this property from the values of other properties, you must also override any targets that use this property (such as the clean target).

dist.jar

Where the project's JAR file is created. Also specifies the name of the JAR file (for general Java projects).

dist.war

Where the project's WAR file is created. Also specifies the name of the WAR file (for web projects).

dist.javadoc.dir

Where Javadoc for the project is generated when you run the Generate Javadoc for Project command.

dist.dir

Where the generated Javadoc documentation and the project's JAR file or WAR file are placed.


If you move either the build folder or the dist folder outside of the project's folder, it will no longer be visible in the Files window. You can remedy this by setting up the Favorites window to display the moved folder. Choose Window | Favorites (Ctrl-3). Then right-click in the Favorites window and choose Add to Favorites. Navigate to the folder you want to display and click Add.


Compiling Classes into the Same Directories As Your Sources

If your build environment requires that you compile your classes into the same directories as your sources, you must:

  • Modify the properties that represent any build outputs that you want to have moved.

  • Override the IDE's do-clean target so that it deletes only the class files in your source directory and not your sources as well.

  • Optionally, update the IDE's Ignore Files property so that compiled class files are not displayed in the Projects window.

Here is the step-by-step procedure for having your classes compiled into the same directories as your sources for a general Java project:

1.

Open the Files window by clicking the Files tab (or pressing Ctrl-2).

2.

Expand the project's nbproject folder and open the project.properties file.

3.

Change the build.classes.dir property to point to your source directory.

4.

Open the build-impl.xml file and copy the do-clean target.

5.

Paste the do-clean target into your build.xml file.

6.

In the build.xml file, modify the do-clean target so that only class files are deleted. For example, you might replace

    <delete dir="${build.dir}"/>


with

    <delete includeEmptyDirs="true">         <fileset dir=".">         <include name="${build.classes.dir}/**/*.class"/>         </fileset>     </delete>


7.

If you do not want compiled class files to display in the Projects and Files windows, choose Tools | Options, click Advanced Options, expand IDE Configuration | System, and select the System Settings node. Select the Ignored Files property and add to the regular expression to designate the files to ignore. For example, you might add |class$ to the end of the expression.

Investigating Compilation Errors

If you get an error when compiling a class, you can jump to the source of the error by clicking the hyperlinked error line in the Output window or by pressing F12. If there are multiple errors, you can cycle through them by pressing F12 (next error) or Shift-F12 (previous error).

Saving Build Output

You can save build output to a file by right-clicking in the Output window and choosing Save As.

By default, every command that you run in the IDE that uses Ant re-uses the same tab in the Output window; thus, the output from the previous command is cleared every time you run a new build command. If you would like to preserve the Ant output in the UI, you can configure the IDE to open a new tab every time you run a build command.

To preserve the output from all build commands in the IDE:

  1. Choose Tools | Options and click Miscellaneous in the left pane and expand the Ant node.

  2. Deselect the Reuse Output Tabs From Finished Processes checkbox.



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