Revision as of 22:06, 4 November 2009 by Admin (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)

Can I exclude some classes or packages from compilation?

Sometimes you may have classes in your source tree which you do not wish to compile; perhaps they are broken (uncompilable), or perhaps they are just irrelevant to your purposes at the moment. By default, plain Java SE projects are configured to compile all sources.

NetBeans 6.0+

Just configure Includes/Excludes in project properties. See Excludes49026 for full details.

NetBeans 4/5.x

IDE versions prior to 6.0 do not provide a mechanism in the GUI for excluding some classes. But you can edit your project's build.xml file (see the Files tab) to skip over unwanted classes or packages. Just locate the -init-macrodef-javac target in nbproject/build-impl.xml, copy it to build.xml, and adjust it to include just what you want, according to the Ant documentation for the javac task.

For example, in NetBeans 5.5, you can add to build.xml:

    <target name="-init-macrodef-javac">
        <macrodef name="javac" uri="http://www.netbeans.org/ns/j2se-project/3">
            <attribute name="srcdir" default="${src.dir}"/>
            <attribute name="destdir" default="${build.classes.dir}"/>
            <attribute name="classpath" default="${javac.classpath}"/>
            <attribute name="debug" default="${javac.debug}"/>
            <element name="customize" optional="true"/>
                <javac srcdir="@{srcdir}" destdir="@{destdir}" debug="@{debug}" deprecation="${javac.deprecation}" source="${javac.source}" target="${javac.target}" includeantruntime="false" excludes="${excludes}">
                        <path path="@{classpath}"/>
                    <compilerarg line="${javac.compilerargs}"/>

The only change from the build-impl.xml version is excludes="${excludes}". Now edit nbproject/project.properties and define excludes for your needs; for example, to exclude the package named excluded, add


Now when you compile, javac will not be asked to compile classes in that package.

Some notes:

  1. You can specify multiple excludes and in fact any Ant pattern, e.g.


Note that / at the end of a pattern means that package and all packages "beneath" it.
  1. You can specify the includes attribute to include only enumerated classes and/or packages, rather than including everything by default.
  2. The Java compiler will normally attempt to compile any classes referred to statically from classes you ask it to compile, even if they are supposed to be excluded. If you want to treat this situation as an error instead, simply specify sourcepath="" on the <javac> task.
  3. If you also need to control what non-Java resources are copied, which unit tests are run, etc., you will need to override additional targets.

Applies to: NetBeans 5.5 through 6.5

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo