My blog has moved!

You should automatically be redirected in 6 seconds. If not, visit
http://blogs.i2m.dk/allan
and update your bookmarks.

Monday, 4 February 2008

Using the "License" functionality in NetBeans 6

NetBeans 6 comes with a brand new functionality for including project-based licenses in source files. This enables you to easily use different licenses in the headers depending on the project you are working on.

Using the functionality is simple, here are the steps:

  1. Add your licenses to the NetBeans templates
  2. Update the project.properties to specify the desired license
Simple, eh?

Okay, first we add a license to the NetBeans template. For the sake of this example I'll add the GPL license:

  1. Click Tools -> Templates
  2. Expand the "Licenses" folder
  3. Select "Default License"
  4. Click the "Duplicate" button (a new template is created called license-default_1.txt)
  5. Select license-default_1.txt
  6. Click the "Rename" button
  7. Name the new license license-gpl.txt (notice, the name should follow the format: license-.txt)
  8. Select license-gpl.txt
  9. Click "Open in Editor"
  10. Enter the following text (GPL license) into the editor:

    <#if licenseFirst??>
    ${licenseFirst}
    </#if>
    ${licensePrefix}${name}.java
    ${licensePrefix}
    ${licensePrefix}Copyright (C) ${date?date?string("yyyy")} Allan Lykke Christensen
    ${licensePrefix}
    ${licensePrefix}This program is distributed in the hope that it will be useful,
    ${licensePrefix}but WITHOUT ANY WARRANTY; without even the implied warranty of
    ${licensePrefix}MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    ${licensePrefix}GNU General Public License for more details.
    ${licensePrefix}
    ${licensePrefix}You should have received a copy of the GNU General Public License
    ${licensePrefix}along with this program. If not, see <http://www.gnu.org/licenses/>.
    <#if licenseLast??>
    ${licenseLast}
    </#if>


  11. Replace the author name

  12. Save and close the template (CTRL+S followed by CTRL+W)


You have successfully added the template to NetBeans for use in future projects.

To use the license in a project do the following:

  1. Open the "Files" view (CTRL+2)
  2. Expand the nbproject folder of your project
  3. Open the project.properties file
  4. Add project.license=gpl in the end of the file, or if you already have a line starting project.license replace it.
  5. Save and close the file


Alright, you are ready to try out the license. Create a new Java class in your Source Packages and viola, the source file is using the newly created license.

In a future version of NetBeans I expect that it will be possible to select the license from the project properties page rather than you having to tinker around with project.properties.

6 comments:

Anonymous said...

Allan,

Sorry it does not work for me. After a first mistake (I typed licence instead of license) I did all the steps, and I verified several times : all is now OK in the configuration files.
But it is still the default license when I create a new Java Class...
Any idea?
Thanks in advance.
Olivier

Allan Lykke Christensen said...

Hi Olivier,

Hm, sounds strange. I think you might have a difference in spelling in the license template and the license identifier in project.properties.

Ensure that in the templates the license it called license-YOUR_ID.txt and in project.properties you have a corresponding line:
project.license=YOUR_ID

If you still have problems feel free to e-mail me and I'll see what I can do to help.

Kind regards,
Allan

Anonymous said...

Dear Allan,

The method succeeds for me but then i have a "resolve reference problem" : netbeans wants me to provide a "license" project. Do you know this issue? It is Netbeans 6.0.1
Thanks.
Regards,
Cyril.

Allan Lykke Christensen said...

Hi Cyril,

It sounds like you might have accidentally added a line containing reference=license or similar. Does the reference problem disappear when you remove the line from project.properties?

Kind regards,
Allan

Anonymous said...

It appears this works fine for regular Java projects but not for Enterprise Application projects.

Allan Lykke Christensen said...

It also works for Enterprise applications, but you will have to add it to each of the EJB and WAR projects that is part of the Enterprise application and not just the project.properties for the Enterprise application.