在NetBeans5.0中使用xdoclet生成hibernate的*.hbm.xml文件
敬告:
------------------------------------------------------------
本文作者希望本文可以在網絡及任何媒體進行任何形式的傳播、復
制、引用、修改,在此過程中請保留此敬告內容。
謝謝!
popo
------------------------------------------------------------
NetBeans是基于ANT進行項目的build和run的,所以我們可以方便的加入自己的target對原有項目進行擴展。
啟動NetBeans,打開以前寫的SimpleWebSite_dao項目,因為我們要做的是利用XDoclet生成Hibernate的*.hbm.xml(ORM)文件,所以將XDoclet的所有Jar包解壓到lib\xdoclet-1.3-SNAPSHOT目錄下,不用在項目中引用XDoclet的包,因為我們是要通過修改Build文件來引用這些包。
選擇“Files”窗口,展開SimpleWebSite_dao項目。
打開build.xml文件,看該文件中的注釋,大概就是講NetBeans為我們生成的build文件是nbproject/build-impl.xml,build.xml已經import了build-impl文件,如果我們想加入自己的target或者對原有target進行修改,只要改動build文件就可以。
用NetBeans編輯XML文件很方便,可以對XML文件進行檢查和校驗,并且還有代碼提示及補齊的功能:)。導航窗口更是為我們查看XML內容結構提供了極大的方便。
在build.xml中加入以下內容:
<property name="lib.dir" location="../lib" />
<property name="xdoclet.dir" location="../lib/xdoclet-1.3-SNAPSHOT" />
<path id="xdoclet.classpath">
<fileset dir="${lib.dir}" includes="*.jar"/>
<fileset dir="${xdoclet.dir}" includes="*.jar"/>
</path>
<!-- =================================================================== -->
<!-- The "hibernatedoclet" target generates Hibernate mapping files -->
<!-- based on XDoclet marked-up Plain Old Java Object (POJO) -->
<!-- =================================================================== -->
<target name="hibernatedoclet" depends="compile" unless="hibernatedoclet.unnecessary"
description="Generate Hibernate mapping files">
<taskdef name="hibernatedoclet"
classname="xdoclet.modules.hibernate.HibernateDocletTask"
classpathref="xdoclet.classpath"/>
<!-- generate hibernate files -->
<hibernatedoclet destdir="${build.classes.dir}" mergedir="${build.classes.dir}"
excludedtags="@version,@author" addedtags="@xdoclet-generated at ${TODAY}"
force="${xdoclet.force}">
<fileset dir="src"/>
<hibernate validatexml="true" version="3.0"/>
</hibernatedoclet>
</target>
用右鍵點build.xml文件,在“Run target”中,會看到我們新加的target->“hibernatedoclet”。
現在寫一個POJO,用這個target生成.hbm.xml文件。
New 一個Java Class Customer(這個類的代碼是從網上拷貝來的,因為我對XDoclet還不太熟悉):
package hibernate;
import java.util.Set;
import java.util.Collections;
/**
* @author roson
* @since 1.0
* @version 1.0
* @hibernate.class tables="customers"
*/
public class Customer {
/**This customer's identifier field.
*/
private long id;
/**This customer's name field.
*/
private String name;
/**The customer's orders set.
*/
private Set orders=Collections.EMPTY_SET;
/**The default construtor for Hibernate to instantiate with.
*/
public Customer() {}
/**The getter method for this Customer's identifier.
*
* @hibernate.id generator-class="native"
*/
public long getId() {
return id;
}
/**The setter method for this Customer's identifier.
*/
public void setId(long id) {
this.id=id;
}
/**The getter method for this Customer's name.
*
* @hibernate.property
*/
public String getName() {
return name;
}
/**The setter method for this Customer's name.
*/
public void setName(String name) {
this.name=name;
}
/**The getter method for this Customer's orders.
*
* @hibernate.set role="orders"
*
* @hibernate.collection-key column="customer_id"
*
* @hibernate.collection-one-to-many class="Order"
*/
public Set getOrders() {
return orders;
}
/**The setter method for this Customer's orders.
*/
public void setOrders(Set orders) {
this.orders=orders;
}
}
現在右鍵點build.xml文件,在“Run target”中,選“hibernatedoclet”,BUILD SUCCESSFUL。
在build\classes\hibernate目錄下會看到編譯后的Customer.class文件和XDoclet生成的Customer.hbm.xml。
為了方便,我們進一步修改build.xml文件,將build-impl.xml中的“jar”這個target復制粘貼過來,并改為以下內容:
<target name="jar" depends="init,hibernatedoclet,-pre-jar,-do-jar-with-manifest,-do-jar-without-manifest,-do-jar-with-mainclass,-do-jar-with-libraries,-post-jar" description="Build JAR."/>
我只把depends中的compile改為了hibernatedoclet,這樣在編譯后和打包前的時候,會生成*.hbm.xml文件。
現在的build.xml文件內容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!-- You may freely edit this file. See commented blocks below for -->
<!-- some examples of how to customize the build. -->
<!-- (If you delete it and reopen the project it will be recreated.) -->
<project name="SimpleWebSite_Dao" default="default" basedir=".">
<description>Builds, tests, and runs the project SimpleWebSite_Dao.</description>
<import file="nbproject/build-impl.xml"/>
<property name="lib.dir" location="../lib" />
<property name="xdoclet.dir" location="../lib/xdoclet-1.3-SNAPSHOT" />
<path id="xdoclet.classpath">
<fileset dir="${lib.dir}" includes="*.jar"/>
<fileset dir="${xdoclet.dir}" includes="*.jar"/>
</path>
<!--
There exist several targets which are by default empty and which can be
used for execution of your tasks. These targets are usually executed
before and after some main targets. They are:
-pre-init: called before initialization of project properties
-post-init: called after initialization of project properties
-pre-compile: called before javac compilation
-post-compile: called after javac compilation
-pre-compile-single: called before javac compilation of single file
-post-compile-single: called after javac compilation of single file
-pre-compile-test: called before javac compilation of JUnit tests
-post-compile-test: called after javac compilation of JUnit tests
-pre-compile-test-single: called before javac compilation of single JUnit test
-post-compile-test-single: called after javac compilation of single JUunit test
-pre-jar: called before JAR building
-post-jar: called after JAR building
-post-clean: called after cleaning build products
(Targets beginning with '-' are not intended to be called on their own.)
Example of inserting an obfuscator after compilation could look like this:
<target name="-post-compile">
<obfuscate>
<fileset dir="${build.classes.dir}"/>
</obfuscate>
</target>
For list of available properties check the imported
nbproject/build-impl.xml file.
Another way to customize the build is by overriding existing main targets.
The targets of interest are:
-init-macrodef-javac: defines macro for javac compilation
-init-macrodef-junit: defines macro for junit execution
-init-macrodef-debug: defines macro for class debugging
-init-macrodef-java: defines macro for class execution
-do-jar-with-manifest: JAR building (if you are using a manifest)
-do-jar-without-manifest: JAR building (if you are not using a manifest)
run: execution of project
-javadoc-build: Javadoc generation
test-report: JUnit report generation
An example of overriding the target for project execution could look like this:
<target name="run" depends="SimpleWebSite_Dao-impl.jar">
<exec dir="bin" executable="launcher.exe">
<arg file="${dist.jar}"/>
</exec>
</target>
Notice that the overridden target depends on the jar target and not only on
the compile target as the regular run target does. Again, for a list of available
properties which you can use, check the target you are overriding in the
nbproject/build-impl.xml file.
-->
<!-- =================================================================== -->
<!-- The "hibernatedoclet" target generates Hibernate mapping files -->
<!-- based on XDoclet marked-up Plain Old Java Object (POJO) -->
<!-- =================================================================== -->
<target name="hibernatedoclet" depends="compile" unless="hibernatedoclet.unnecessary"
description="Generate Hibernate mapping files">
<taskdef name="hibernatedoclet"
classname="xdoclet.modules.hibernate.HibernateDocletTask"
classpathref="xdoclet.classpath"/>
<!-- generate hibernate files -->
<hibernatedoclet destdir="${build.classes.dir}" mergedir="${build.classes.dir}"
excludedtags="@version,@author" addedtags="@xdoclet-generated at ${TODAY}"
force="${xdoclet.force}">
<fileset dir="src"/>
<hibernate validatexml="true" version="3.0"/>
</hibernatedoclet>
</target>
<target name="jar" depends="init,hibernatedoclet,-pre-jar,-do-jar-with-manifest,-do-jar-without-manifest,-do-jar-with-mainclass,-do-jar-with-libraries,-post-jar" description="Build JAR."/>
</project>
在Projects窗口,選“Build Project”或者“Clean and Build Project”,
會看到用到了我們修改后的jar這個target進行的編譯和打包。
XDoclet的功能很強大,我會邊學邊在我的項目中利用它,可以省去很多編碼工作。
看Appfuse的build.xml,數據庫的表是用Hibernate提供的SchemaExportTask工具生成數據庫建表的SQL,我們也來照做,這樣只要在寫POJO時加上注釋,用ANT的target就可以生成*.hbm.xml文件和創建數據表的SQL語句。方便吧?
對build.xml進行修改,把生成*.hbm.xml文件的目標路徑改為SRC目錄,不再depends="compile",改為在jar的時候先“hibernatedoclet”再“compile”,增加create_tables_sql的target,修改后的build.xml文件為:
<?xml version="1.0" encoding="UTF-8"?>
<!-- You may freely edit this file. See commented blocks below for -->
<!-- some examples of how to customize the build. -->
<!-- (If you delete it and reopen the project it will be recreated.) -->
<project name="SimpleWebSite_Dao" default="default" basedir=".">
<description>Builds, tests, and runs the project SimpleWebSite_Dao.</description>
<import file="nbproject/build-impl.xml"/>
<property name="src.dir" location="./src" />
<property name="lib.dir" location="../lib" />
<property name="xdoclet.dir" location="../lib/xdoclet-1.3-SNAPSHOT" />
<path id="xdoclet.classpath">
<fileset dir="${lib.dir}" includes="*.jar"/>
<fileset dir="${xdoclet.dir}" includes="*.jar"/>
</path>
<!--
There exist several targets which are by default empty and which can be
used for execution of your tasks. These targets are usually executed
before and after some main targets. They are:
-pre-init: called before initialization of project properties
-post-init: called after initialization of project properties
-pre-compile: called before javac compilation
-post-compile: called after javac compilation
-pre-compile-single: called before javac compilation of single file
-post-compile-single: called after javac compilation of single file
-pre-compile-test: called before javac compilation of JUnit tests
-post-compile-test: called after javac compilation of JUnit tests
-pre-compile-test-single: called before javac compilation of single JUnit test
-post-compile-test-single: called after javac compilation of single JUunit test
-pre-jar: called before JAR building
-post-jar: called after JAR building
-post-clean: called after cleaning build products
(Targets beginning with '-' are not intended to be called on their own.)
Example of inserting an obfuscator after compilation could look like this:
<target name="-post-compile">
<obfuscate>
<fileset dir="${build.classes.dir}"/>
</obfuscate>
</target>
For list of available properties check the imported
nbproject/build-impl.xml file.
Another way to customize the build is by overriding existing main targets.
The targets of interest are:
-init-macrodef-javac: defines macro for javac compilation
-init-macrodef-junit: defines macro for junit execution
-init-macrodef-debug: defines macro for class debugging
-init-macrodef-java: defines macro for class execution
-do-jar-with-manifest: JAR building (if you are using a manifest)
-do-jar-without-manifest: JAR building (if you are not using a manifest)
run: execution of project
-javadoc-build: Javadoc generation
test-report: JUnit report generation
An example of overriding the target for project execution could look like this:
<target name="run" depends="SimpleWebSite_Dao-impl.jar">
<exec dir="bin" executable="launcher.exe">
<arg file="${dist.jar}"/>
</exec>
</target>
Notice that the overridden target depends on the jar target and not only on
the compile target as the regular run target does. Again, for a list of available
properties which you can use, check the target you are overriding in the
nbproject/build-impl.xml file.
-->
<!-- =================================================================== -->
<!-- The "hibernatedoclet" target generates Hibernate mapping files -->
<!-- based on XDoclet marked-up Plain Old Java Object (POJO) -->
<!-- =================================================================== -->
<target name="hibernatedoclet" unless="hibernatedoclet.unnecessary"
description="Generate Hibernate mapping files">
<taskdef name="hibernatedoclet"
classname="xdoclet.modules.hibernate.HibernateDocletTask"
classpathref="xdoclet.classpath"/>
<!-- generate hibernate files -->
<hibernatedoclet destdir="${src.dir}" mergedir="${src.dir}"
excludedtags="@version,@author" addedtags="@xdoclet-generated at ${TODAY}"
force="${xdoclet.force}">
<fileset dir="${src.dir}"/>
<hibernate validatexml="true" version="3.0"/>
</hibernatedoclet>
</target>
<!-- =================================================================== -->
<!-- The "create_tables_sql" target generates the database schema and -->
<!-- creates tables based on the mapping files -->
<!-- =================================================================== -->
<target name="create_tables_sql" depends="hibernatedoclet" description="creates database tables">
<taskdef name="schemaexport"
classname="org.hibernate.tool.hbm2ddl.SchemaExportTask"
classpathref="xdoclet.classpath"/>
<schemaexport quiet="yes" text="true" drop="no" delimiter=";"
properties="src/database.properties" output="../database/sql/create-tables.sql">
<fileset dir="${src.dir}" includes="**/*.hbm.xml"/>
</schemaexport>
</target>
<target name="jar" depends="init,create_tables_sql,compile,-pre-jar,-do-jar-with-manifest,-do-jar-without-manifest,-do-jar-with-mainclass,-do-jar-with-libraries,-post-jar" description="Build JAR."/>
</project>
這樣,每次“Build Project”或者“Clean and Build Project”時,都會先在源程序目錄創建*.hbm.xml文件,再在../database/sql目錄下生成創建數據表的SQL文件,再進行編譯,打包。
用上面的示例文件有幾個問題
1.Customer類中 * @hibernate.class tables="customers"應該是 * @hibernate.class table="customers",
2.生成的Customer.hbm.xml文件“create_tables_sql”時失敗,提示信息為:
D:\ProjectNetBeans\SimpleWebSite\SimpleWebSite_Dao\build.xml:107: Schema text failed: Could not read mapping document from file: D:\ProjectNetBeans\SimpleWebSite\SimpleWebSite_Dao\src\hibernate\Customer.hbm.xml
BUILD FAILED (total time: 4 seconds)
為字段加上數據類型(type="java.lang.Long")后可以讀取文件,但還是失敗:
D:\ProjectNetBeans\SimpleWebSite\SimpleWebSite_Dao\build.xml:107: Schema text failed: Association references unmapped class: Order
因為對XDoclet不熟,對Hibernate的配置也快忘記差不多了,所以省點事兒,先把Order屬性去掉。
“Clean and Build Project”,哈哈,看看,*.hbm.xml文件和建數據庫表的Sql語句都生成啦。
不過還沒有試過Debug等target,如果那些target依賴生成的*.hbm.xml文件的話,還要對相應的target加上depends...
剩下的就是要多看看XDoclet和Hibernate配置的文檔啦。
敬告:
------------------------------------------------------------
本文作者希望本文可以在網絡及任何媒體進行任何形式的傳播、復
制、引用、修改,在此過程中請保留此敬告內容。
謝謝!
popo
------------------------------------------------------------
NetBeans是基于ANT進行項目的build和run的,所以我們可以方便的加入自己的target對原有項目進行擴展。
啟動NetBeans,打開以前寫的SimpleWebSite_dao項目,因為我們要做的是利用XDoclet生成Hibernate的*.hbm.xml(ORM)文件,所以將XDoclet的所有Jar包解壓到lib\xdoclet-1.3-SNAPSHOT目錄下,不用在項目中引用XDoclet的包,因為我們是要通過修改Build文件來引用這些包。
選擇“Files”窗口,展開SimpleWebSite_dao項目。
打開build.xml文件,看該文件中的注釋,大概就是講NetBeans為我們生成的build文件是nbproject/build-impl.xml,build.xml已經import了build-impl文件,如果我們想加入自己的target或者對原有target進行修改,只要改動build文件就可以。
用NetBeans編輯XML文件很方便,可以對XML文件進行檢查和校驗,并且還有代碼提示及補齊的功能:)。導航窗口更是為我們查看XML內容結構提供了極大的方便。
在build.xml中加入以下內容:
<property name="lib.dir" location="../lib" />
<property name="xdoclet.dir" location="../lib/xdoclet-1.3-SNAPSHOT" />
<path id="xdoclet.classpath">
<fileset dir="${lib.dir}" includes="*.jar"/>
<fileset dir="${xdoclet.dir}" includes="*.jar"/>
</path>
<!-- =================================================================== -->
<!-- The "hibernatedoclet" target generates Hibernate mapping files -->
<!-- based on XDoclet marked-up Plain Old Java Object (POJO) -->
<!-- =================================================================== -->
<target name="hibernatedoclet" depends="compile" unless="hibernatedoclet.unnecessary"
description="Generate Hibernate mapping files">
<taskdef name="hibernatedoclet"
classname="xdoclet.modules.hibernate.HibernateDocletTask"
classpathref="xdoclet.classpath"/>
<!-- generate hibernate files -->
<hibernatedoclet destdir="${build.classes.dir}" mergedir="${build.classes.dir}"
excludedtags="@version,@author" addedtags="@xdoclet-generated at ${TODAY}"
force="${xdoclet.force}">
<fileset dir="src"/>
<hibernate validatexml="true" version="3.0"/>
</hibernatedoclet>
</target>
用右鍵點build.xml文件,在“Run target”中,會看到我們新加的target->“hibernatedoclet”。
現在寫一個POJO,用這個target生成.hbm.xml文件。
New 一個Java Class Customer(這個類的代碼是從網上拷貝來的,因為我對XDoclet還不太熟悉):
package hibernate;
import java.util.Set;
import java.util.Collections;
/**
* @author roson
* @since 1.0
* @version 1.0
* @hibernate.class tables="customers"
*/
public class Customer {
/**This customer's identifier field.
*/
private long id;
/**This customer's name field.
*/
private String name;
/**The customer's orders set.
*/
private Set orders=Collections.EMPTY_SET;
/**The default construtor for Hibernate to instantiate with.
*/
public Customer() {}
/**The getter method for this Customer's identifier.
*
* @hibernate.id generator-class="native"
*/
public long getId() {
return id;
}
/**The setter method for this Customer's identifier.
*/
public void setId(long id) {
this.id=id;
}
/**The getter method for this Customer's name.
*
* @hibernate.property
*/
public String getName() {
return name;
}
/**The setter method for this Customer's name.
*/
public void setName(String name) {
this.name=name;
}
/**The getter method for this Customer's orders.
*
* @hibernate.set role="orders"
*
* @hibernate.collection-key column="customer_id"
*
* @hibernate.collection-one-to-many class="Order"
*/
public Set getOrders() {
return orders;
}
/**The setter method for this Customer's orders.
*/
public void setOrders(Set orders) {
this.orders=orders;
}
}
現在右鍵點build.xml文件,在“Run target”中,選“hibernatedoclet”,BUILD SUCCESSFUL。
在build\classes\hibernate目錄下會看到編譯后的Customer.class文件和XDoclet生成的Customer.hbm.xml。
為了方便,我們進一步修改build.xml文件,將build-impl.xml中的“jar”這個target復制粘貼過來,并改為以下內容:
<target name="jar" depends="init,hibernatedoclet,-pre-jar,-do-jar-with-manifest,-do-jar-without-manifest,-do-jar-with-mainclass,-do-jar-with-libraries,-post-jar" description="Build JAR."/>
我只把depends中的compile改為了hibernatedoclet,這樣在編譯后和打包前的時候,會生成*.hbm.xml文件。
現在的build.xml文件內容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!-- You may freely edit this file. See commented blocks below for -->
<!-- some examples of how to customize the build. -->
<!-- (If you delete it and reopen the project it will be recreated.) -->
<project name="SimpleWebSite_Dao" default="default" basedir=".">
<description>Builds, tests, and runs the project SimpleWebSite_Dao.</description>
<import file="nbproject/build-impl.xml"/>
<property name="lib.dir" location="../lib" />
<property name="xdoclet.dir" location="../lib/xdoclet-1.3-SNAPSHOT" />
<path id="xdoclet.classpath">
<fileset dir="${lib.dir}" includes="*.jar"/>
<fileset dir="${xdoclet.dir}" includes="*.jar"/>
</path>
<!--
There exist several targets which are by default empty and which can be
used for execution of your tasks. These targets are usually executed
before and after some main targets. They are:
-pre-init: called before initialization of project properties
-post-init: called after initialization of project properties
-pre-compile: called before javac compilation
-post-compile: called after javac compilation
-pre-compile-single: called before javac compilation of single file
-post-compile-single: called after javac compilation of single file
-pre-compile-test: called before javac compilation of JUnit tests
-post-compile-test: called after javac compilation of JUnit tests
-pre-compile-test-single: called before javac compilation of single JUnit test
-post-compile-test-single: called after javac compilation of single JUunit test
-pre-jar: called before JAR building
-post-jar: called after JAR building
-post-clean: called after cleaning build products
(Targets beginning with '-' are not intended to be called on their own.)
Example of inserting an obfuscator after compilation could look like this:
<target name="-post-compile">
<obfuscate>
<fileset dir="${build.classes.dir}"/>
</obfuscate>
</target>
For list of available properties check the imported
nbproject/build-impl.xml file.
Another way to customize the build is by overriding existing main targets.
The targets of interest are:
-init-macrodef-javac: defines macro for javac compilation
-init-macrodef-junit: defines macro for junit execution
-init-macrodef-debug: defines macro for class debugging
-init-macrodef-java: defines macro for class execution
-do-jar-with-manifest: JAR building (if you are using a manifest)
-do-jar-without-manifest: JAR building (if you are not using a manifest)
run: execution of project
-javadoc-build: Javadoc generation
test-report: JUnit report generation
An example of overriding the target for project execution could look like this:
<target name="run" depends="SimpleWebSite_Dao-impl.jar">
<exec dir="bin" executable="launcher.exe">
<arg file="${dist.jar}"/>
</exec>
</target>
Notice that the overridden target depends on the jar target and not only on
the compile target as the regular run target does. Again, for a list of available
properties which you can use, check the target you are overriding in the
nbproject/build-impl.xml file.
-->
<!-- =================================================================== -->
<!-- The "hibernatedoclet" target generates Hibernate mapping files -->
<!-- based on XDoclet marked-up Plain Old Java Object (POJO) -->
<!-- =================================================================== -->
<target name="hibernatedoclet" depends="compile" unless="hibernatedoclet.unnecessary"
description="Generate Hibernate mapping files">
<taskdef name="hibernatedoclet"
classname="xdoclet.modules.hibernate.HibernateDocletTask"
classpathref="xdoclet.classpath"/>
<!-- generate hibernate files -->
<hibernatedoclet destdir="${build.classes.dir}" mergedir="${build.classes.dir}"
excludedtags="@version,@author" addedtags="@xdoclet-generated at ${TODAY}"
force="${xdoclet.force}">
<fileset dir="src"/>
<hibernate validatexml="true" version="3.0"/>
</hibernatedoclet>
</target>
<target name="jar" depends="init,hibernatedoclet,-pre-jar,-do-jar-with-manifest,-do-jar-without-manifest,-do-jar-with-mainclass,-do-jar-with-libraries,-post-jar" description="Build JAR."/>
</project>
在Projects窗口,選“Build Project”或者“Clean and Build Project”,
會看到用到了我們修改后的jar這個target進行的編譯和打包。
XDoclet的功能很強大,我會邊學邊在我的項目中利用它,可以省去很多編碼工作。
看Appfuse的build.xml,數據庫的表是用Hibernate提供的SchemaExportTask工具生成數據庫建表的SQL,我們也來照做,這樣只要在寫POJO時加上注釋,用ANT的target就可以生成*.hbm.xml文件和創建數據表的SQL語句。方便吧?
對build.xml進行修改,把生成*.hbm.xml文件的目標路徑改為SRC目錄,不再depends="compile",改為在jar的時候先“hibernatedoclet”再“compile”,增加create_tables_sql的target,修改后的build.xml文件為:
<?xml version="1.0" encoding="UTF-8"?>
<!-- You may freely edit this file. See commented blocks below for -->
<!-- some examples of how to customize the build. -->
<!-- (If you delete it and reopen the project it will be recreated.) -->
<project name="SimpleWebSite_Dao" default="default" basedir=".">
<description>Builds, tests, and runs the project SimpleWebSite_Dao.</description>
<import file="nbproject/build-impl.xml"/>
<property name="src.dir" location="./src" />
<property name="lib.dir" location="../lib" />
<property name="xdoclet.dir" location="../lib/xdoclet-1.3-SNAPSHOT" />
<path id="xdoclet.classpath">
<fileset dir="${lib.dir}" includes="*.jar"/>
<fileset dir="${xdoclet.dir}" includes="*.jar"/>
</path>
<!--
There exist several targets which are by default empty and which can be
used for execution of your tasks. These targets are usually executed
before and after some main targets. They are:
-pre-init: called before initialization of project properties
-post-init: called after initialization of project properties
-pre-compile: called before javac compilation
-post-compile: called after javac compilation
-pre-compile-single: called before javac compilation of single file
-post-compile-single: called after javac compilation of single file
-pre-compile-test: called before javac compilation of JUnit tests
-post-compile-test: called after javac compilation of JUnit tests
-pre-compile-test-single: called before javac compilation of single JUnit test
-post-compile-test-single: called after javac compilation of single JUunit test
-pre-jar: called before JAR building
-post-jar: called after JAR building
-post-clean: called after cleaning build products
(Targets beginning with '-' are not intended to be called on their own.)
Example of inserting an obfuscator after compilation could look like this:
<target name="-post-compile">
<obfuscate>
<fileset dir="${build.classes.dir}"/>
</obfuscate>
</target>
For list of available properties check the imported
nbproject/build-impl.xml file.
Another way to customize the build is by overriding existing main targets.
The targets of interest are:
-init-macrodef-javac: defines macro for javac compilation
-init-macrodef-junit: defines macro for junit execution
-init-macrodef-debug: defines macro for class debugging
-init-macrodef-java: defines macro for class execution
-do-jar-with-manifest: JAR building (if you are using a manifest)
-do-jar-without-manifest: JAR building (if you are not using a manifest)
run: execution of project
-javadoc-build: Javadoc generation
test-report: JUnit report generation
An example of overriding the target for project execution could look like this:
<target name="run" depends="SimpleWebSite_Dao-impl.jar">
<exec dir="bin" executable="launcher.exe">
<arg file="${dist.jar}"/>
</exec>
</target>
Notice that the overridden target depends on the jar target and not only on
the compile target as the regular run target does. Again, for a list of available
properties which you can use, check the target you are overriding in the
nbproject/build-impl.xml file.
-->
<!-- =================================================================== -->
<!-- The "hibernatedoclet" target generates Hibernate mapping files -->
<!-- based on XDoclet marked-up Plain Old Java Object (POJO) -->
<!-- =================================================================== -->
<target name="hibernatedoclet" unless="hibernatedoclet.unnecessary"
description="Generate Hibernate mapping files">
<taskdef name="hibernatedoclet"
classname="xdoclet.modules.hibernate.HibernateDocletTask"
classpathref="xdoclet.classpath"/>
<!-- generate hibernate files -->
<hibernatedoclet destdir="${src.dir}" mergedir="${src.dir}"
excludedtags="@version,@author" addedtags="@xdoclet-generated at ${TODAY}"
force="${xdoclet.force}">
<fileset dir="${src.dir}"/>
<hibernate validatexml="true" version="3.0"/>
</hibernatedoclet>
</target>
<!-- =================================================================== -->
<!-- The "create_tables_sql" target generates the database schema and -->
<!-- creates tables based on the mapping files -->
<!-- =================================================================== -->
<target name="create_tables_sql" depends="hibernatedoclet" description="creates database tables">
<taskdef name="schemaexport"
classname="org.hibernate.tool.hbm2ddl.SchemaExportTask"
classpathref="xdoclet.classpath"/>
<schemaexport quiet="yes" text="true" drop="no" delimiter=";"
properties="src/database.properties" output="../database/sql/create-tables.sql">
<fileset dir="${src.dir}" includes="**/*.hbm.xml"/>
</schemaexport>
</target>
<target name="jar" depends="init,create_tables_sql,compile,-pre-jar,-do-jar-with-manifest,-do-jar-without-manifest,-do-jar-with-mainclass,-do-jar-with-libraries,-post-jar" description="Build JAR."/>
</project>
這樣,每次“Build Project”或者“Clean and Build Project”時,都會先在源程序目錄創建*.hbm.xml文件,再在../database/sql目錄下生成創建數據表的SQL文件,再進行編譯,打包。
用上面的示例文件有幾個問題
1.Customer類中 * @hibernate.class tables="customers"應該是 * @hibernate.class table="customers",
2.生成的Customer.hbm.xml文件“create_tables_sql”時失敗,提示信息為:
D:\ProjectNetBeans\SimpleWebSite\SimpleWebSite_Dao\build.xml:107: Schema text failed: Could not read mapping document from file: D:\ProjectNetBeans\SimpleWebSite\SimpleWebSite_Dao\src\hibernate\Customer.hbm.xml
BUILD FAILED (total time: 4 seconds)
為字段加上數據類型(type="java.lang.Long")后可以讀取文件,但還是失敗:
D:\ProjectNetBeans\SimpleWebSite\SimpleWebSite_Dao\build.xml:107: Schema text failed: Association references unmapped class: Order
因為對XDoclet不熟,對Hibernate的配置也快忘記差不多了,所以省點事兒,先把Order屬性去掉。
“Clean and Build Project”,哈哈,看看,*.hbm.xml文件和建數據庫表的Sql語句都生成啦。
不過還沒有試過Debug等target,如果那些target依賴生成的*.hbm.xml文件的話,還要對相應的target加上depends...
剩下的就是要多看看XDoclet和Hibernate配置的文檔啦。