netbeans自定义文件模板(备忘)

本文以windows下自定义PHP文件模板为例。
点击顶部菜单:
Tools-> Templates ,

如编辑PHP模板,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
<?php
/**
 * @filename ${nameAndExt}
 * @encoding ${encoding}
 * @author ${author}
 * @link ${link}
 * @copyright ${copyright}
 * @license http://www.gnu.org/licenses/
 * @datetime ${date}  ${time}
 * @version 1.0
 * @Description
  */

如果要加上许可证,则可用如下方式:

1
2
3
4
<#assign licenseFirst = "/**">
<#assign licensePrefix = " * ">
<#assign licenseLast = " */">
<#include "../Licenses/license-${project.license}.txt">

既然上面指定了许可证的位置,那么得让许可证存在,要不然会抛出异常。

1
2
cd C:\Users\HuangYe\.netbeans\7.0\config\Templates
mkdir Licenses

以添加GPL许可证为例,在Template Manager 窗口中,选择 Licenses,再选择默认的DefaultLicense ,点击“Duplicate”复制一份,然后重命名为
license-gpl.txt (如果不这样做,而是直接在目录中新建文件的话,这个管理器里面是不可见的,不便于管理)

然后编辑之,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<#if licenseFirst??>
${licenseFirst}
</#if>
${licensePrefix} Copyright (C) <2011>  <荒野无灯>

${licensePrefix} This program is free software: you can redistribute it and/or modify
${licensePrefix} it under the terms of the GNU General Public License as published by
${licensePrefix} the Free Software Foundation, either version 3 of the License, or
${licensePrefix} (at your option) any later version.

${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} 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>

至于要在项目中使用哪个许可证,要在项目目录下的nbproject/project.properties 文件中定义,如:

1
project.license=gpl

在nb模板中,我们可以使用 FreeMarker 模板语言,如下是一些预定义的变量:

${date} 插入眼前日期,格式如: Feb 16, 2008
${encoding} 插入默认编码,如: UTF-8
${name} 插入文件名
${nameAndExt} 插入带扩展名的文件名
${package} inserts the name of the package where the file is created.
${time} 插入当前时间,如: 7:37:58 PM
${user} 插入用户名

我们也可以自定义一些量,Tools-> Templates -> Settings ,这里nb会为你打开User.properties 文件(在windows7下这个文件的位置是C:\Users\HuangYe\.netbeans\7.0\config\Templates\Properties\User.properties,如这是我的定义:

1
2
3
author=荒野无灯 <HuangYeWuDeng, admin@ihacklog.com>
copyright=Copyright (C) 2011 荒野无灯
link=http://ihacklog.com

参考:
http://platform.netbeans.org/tutorials/60/nbm-filetemplates.html

http://freemarker.org/docs/index.html

更多
2 Responses Post a comment
  1. 荒野无灯

    :lol: 至少支持5种语言吧,其中包括简体中文。不过我喜欢英文的,嘿嘿

Leave a Reply

Note: You may use basic HTML in your comments. Your email address will not be published.

Subscribe to this comment feed via RSS