Monday November 26, 2007
Project-Level License Settings in NetBeans IDE 6.0
In my earlier series on FreeMarker support (especially this one), I discussed the many flexible ways in which templating support in NetBeans IDE 6.0 can be of benefit to you. Importantly, the flexibility can be made use of either by the developer providing the templates or by the user of those templates. (That series discusses this, and the differences, in some detail.) One point that I hadn't discussed yet relates to licensing. The short story is: in NetBeans IDE 6.0 you can define a license for your project and then all files within that project will be created with that license embedded in the code. OK, that's the short story.
The long story makes sense in the context of a step-by-step procedure:
- Go to the Tools menu. Choose Templates. Open the Java|Java Class template in the editor:
- In general, the template above, and the ramifications of defining it in FreeMarker, have been discussed in the earlier blog entries (starting here). However, lets look (this time) at the first four lines:
<#assign licenseFirst = "/*"> <#assign licensePrefix = " * "> <#assign licenseLast = " */"> <#include "../Licenses/license-${project.license}.txt">What do these lines mean? They all have to do with licensing. The last line determines the license that will be used, per project. The first three determine the characters in front of and behind each line in the license. Above are the four lines for Java source files. Here's the same set of definitions you'll find for the Properties file template:
<#assign licensePrefix = "# "> <#include "../Licenses/license-${project.license}.txt">What's the difference? Nothing in the second line (which we'll look at later). The first line, however, tells us that each line in the license will be prefixed with a "# ", instead of with a " * ", which is the prefix for Java source files (with "/*" for the first line and " */" for the last line). To verify this, create a Java source file and then create a Properties file. You'll see a license in both cases. However, the characters prefixing and postfixing each line is different. Now you know why.
- Next, let's look at the license itself. Notice this line in the templates above:
<#include "../Licenses/license-${project.license}.txt">In particular, notice this bit:
${project.license}Put that, as a key, in your application's nbproject/project.properties file. Now add a value. For example:
project.license=apache
Now look in the Template Manager again, in the Licenses folder. You see some templates there. Create a new one called "license-apache.txt". For now, you can just copy an existing one and paste it in the same category in the Template Manager. Then, next time that you create a file that is defined by a FreeMarker template that includes this line:
<#include "../Licenses/license-${project.license}.txt">...you will have the specified license embedded within the newly created file.
What's the purpose of all this? Simple: NetBeans IDE 6.0 lets you define, per project, the license that each of its files should display. Plus, imagine if you need to create a new project with a different license. Assuming you have all your licenses defined in the Template Manager, using a new license is as simple as adding that one key/value pair to the nbproject/project.properties file. That was not possible before but, thanks to FreeMarker support in NetBeans IDE 6.0, is possible now.
Nov 26 2007, 09:01:54 AM PST Permalink
please! improve the text aditor! please!
Posted by bruno on November 28, 2007 at 03:43 AM PST #
What are you talking about, Bruno? "Improve the text editor" is really not enough information to be able to do anything with it. Also, this is the correct place to file enhancement requests, if that is what you're trying to do:
http://www.netbeans.org/community/issues.html
Posted by Geertjan on November 28, 2007 at 03:58 AM PST #
Just tried this in NB6 final on an existing prohject with its own Ant build script and it doesn't appear to work? There was no project.properties file in the nbproject folder, so I created one, but I'm still getting the default license text.
How is this supposed to work for projects with their own Ant build scripts...?
Posted by Bob on December 04, 2007 at 09:34 AM PST #
Hi Bob, I haven't tried this with projects that have their own Ant script. I'm not sure if/how it would work there, but I will try and find out. Also, you can write to nbusers@netbeans.org about this. Thanks for your question. Once I have the answer I will blog about it.
Posted by Geertjan on December 04, 2007 at 09:38 AM PST #
I'd really like to know why non of this is mentioned in the documents coming with NB6.
Templates (__TIME__, __DATE__, ...) weren't documented in 5.x, and now the new templates aren't documented in NB6. See a pattern?
It just took me two hours to convert my old templates to NB6, and things like ${application.title} in a template still don't work :-( That new template system you seem to be so proud of is royally broken.
Posted by Son Ying on December 12, 2007 at 01:40 PM PST #
I'd like to help, but you've given no description at all of what is going wrong. Feel free to write to me at geertjan DOT wielenga AT sun DOT com.
Posted by Geertjan on December 12, 2007 at 01:46 PM PST #
for ant projects, can also specify in project.xml using <project-license> element under <general-data>
Posted by Duncan on January 09, 2008 at 04:24 AM PST #
Geertjan, the problem I'm running into is that modifications to the project.properties file result in the following code insertion in my classes from templates:
Error reading included file Templates/Classes/../Licenses/license-apache.txt
As I cannot find the directory Templates/Classes.. I suspect I need to put a license-apache.txt file there but I'm wondering if this is inside a nbm file and therefore not easily found? Got any advice?
Posted by John Resler on January 10, 2008 at 04:13 PM PST #
John, look in the Template Manager under the Tools menu.
Posted by Geertjan on January 10, 2008 at 05:27 PM PST #
If I put project.license=mylicense in project.properties and create a license file, the template stuff works. The correct license text is inserted in my source files.
But I get a Resolve Reference Problems message that says that the project 'license' cannot be found.
This happen on one of my projects, not all of them. And I cannot find a difference. Very strange...
Posted by Goran Ehrsson on January 12, 2008 at 03:51 PM PST #
I have the same issue with Goran, and seems like different type of project is taking different approach[1] for specifying this property.
Should there be a consistent way for specifying this property?
[1] http://www.netbeans.org/issues/show_bug.cgi?id=115058
Posted by Henry Jen on February 06, 2008 at 11:31 AM PST #
What is the syntax of the <project-license> tag in the project.xml file? I haven't been able to find any documentation on that.
Thanks.
Posted by Tom Mueller on March 24, 2008 at 09:57 AM PDT #
I also have the precise same problem reported here with ${application.title} i.e. I get:
Expression application is undefined on line 4, column 28 in Templates/Licenses/license-default.txt..
Posted by Tiago Antao on April 03, 2008 at 12:12 PM PDT #
For setting the project license template to use with a Maven project in Netbeans see:
http://mevenide.codehaus.org/m2-site/mevenide2-netbeans/customizations.html
Posted by Michael Bedward on May 12, 2008 at 09:19 PM PDT #
This is not working.
I created a template called GPL3 under Tools/Templates. I then created nbproject/project.properties and put project.license = GPL3... didn't work. Projects still got the default license.
Then I tried putting it in the XML. I tried <project-license> under <general-data>. I tried <license> as well, as I noticed <name> wasn't <project-name>. I tried variations using "licence" instead of "license", in case someone had made it English instead of American. I tried putting it under the <properties> element too but I don't really know the syntax because it is empty to start with, so this may have worked if I knew how.
Really it wouldn't hurt to document these things, guys. If you're not going to have UI for setting such a vital variable, at least make the file format simple enough to edit, and make it the properties format. Or give us something like project-level templates, so that we don't have to fool around with variables and crap which don't work.
I think it's time to give up on this and go register for an open source developer licence for IDEA. There is only so much development time one can waste trying to make an IDE not screw up.
Posted by Trejkaz on March 13, 2009 at 04:42 PM PDT #
Would like to help you, but please write to nbusers@netbeans.org where more people will be able to do so.
Posted by Geertjan on March 13, 2009 at 04:48 PM PDT #
By the way, seems to me that your license file should be named "license-GPL3.txt", based on the text in this blog entry. I just tried that and it works. Why not follow the instructions as described here? Really it wouldn't hurt to follow the instructions guy.
Posted by Geertjan on March 13, 2009 at 05:23 PM PDT #
Trejkaz, putting the license under Tools/Templates is wrong too. Really, read this blog entry a few more times. I did, I followed the instructions, and everything works.
Posted by Chuck Kingsley on March 13, 2009 at 05:25 PM PDT #
But maybe it's time for him to go and join IDEA, good luck getting info on this in IDEA, Trejkaz. Their docs totally suck and they have no bloggers that describe as much stuff as is done in this one single blog by one single person.
Posted by Fred on March 13, 2009 at 05:27 PM PDT #
Turns out to have been not so painful in IDEA, actually. Project Settings, create a Copyright Profile, copy the boilerplate text into there, Source | Update Copyrights, done. I didn't even need to search help, let alone search the net for an hour.
Posted by Trejkaz on March 14, 2009 at 05:06 AM PDT #


