★ appfuse ant setup执行过程
v 建立数据库:db-create,根据脚本文件Mysql-create,建立数据库
v clean and prepare:清除和建立目录
v 建立hibernate映射文件:hibernatedoclet,根据POJO,建立hibernate映射文件。
注:appfuse中的ORM,是现手工建立POJO,然后利用工具根据POJO生成映射文件,
数据库表等。POJO存储在org/appfuse/model下,生成的映射文件存储在
Build/Dao/gen下。使用xdoclet.modules.hibernate.HibernateDocletTask工具
v 编译dao:compile-dao,调用compile-module编译dao目录下的类文件
v package-dao:Dao打包
v db-prepare:根据hiberante映射文件,建立数据库表,
使用net.sf.hibernate.tool.hbm2ddl.SchemaExportTask工具
v db-load:利用dbunit工具,根据metadata/sql/sample-data.xml,向数据库表中插
入数据
v setup-tomcat:拷贝metadata/conf/tomcat-context.xml
到${tomcat.home}/conf/Catalina/localhost/appfuse.xml,还拷贝JDBC jar和jta.jar到tomcat。
v compile-service:编译service下的类
v copy-resources: 拷贝应用需要的各种配置文件到build\web\classes和
build\appfuse\WEB-INF,而且在此任务中会进行中文转换码工作,借助JDK的native2ascii工具将资源文件ApplicationResources_zh_CN.properties中的内容进行转码。
v gen-forms: 利用xdoclet 和struts_form.xdt,根据module目录中的POJO生成
Struts的ActionFrom
v compile-web:编译web目录中的类,appfuse中的web服务类是不被打包成jar文
件的,直接放在WEB-INF/class目录下调用。
v webdoclet:利用xdoclet.modules.web.WebDocletTask工具,生成了web.xml、
appfuse.tld、struts-config.xml 、struts_config_xml.xdt 、validation.xml 、
validation_xml.xdt等文件
BEGIN: Buildfile: build.xml
v init: defines custom task
v db-create: 建立数据库,授予用户访问权限
<!-- =================================================================== -->
<!-- The "db-create" target creates a database based on properties -->
<!-- from the database.properties file -->
<!-- =================================================================== -->
Ø Mysql-create(建立数据库脚本)的内容:
create database if not exists appfuse;
grant all privileges on appfuse.* to test@"%" identified by "test";
grant all privileges on appfuse.* to test@localhost identified by "test";
Ø运行结果:
[echo] Detected MySQL, creating database...
[echo] Creating database with: mysql-create.sql
[echo] URL: jdbc:mysql://localhost/mysql
[sql] Executing file: E:\app\appfuse\metadata\sql\mysql-create.sql
[sql] 3 of 3 SQL statements executed successfully
v clean:删除构建、发布目录和database.properties
Ødatabase.properties(hibernate环境属性)的内容
#Hibernate Configuration for JUnit tests
#Mon Jun 20 00:08:25 CST 2005
hibernate.connection.username=test
dao.type=hibernate
hibernate.connection.password=test
hibernate.dialect=net.sf.hibernate.dialect.MySQLDialect
hibernate.connection.url=jdbc\:mysql\://localhost/appfuse?autoReconnect\=true&useUnicode\=true&characterEncoding\=utf-8
hibernate.connection.show_sql=true
hibernate.connection.driver_class=com.mysql.jdbc.Driver
Ø运行结果:
[echo] Cleaning build and distribution directories
[delete] Deleting directory E:\app\appfuse\build
[delete] Deleting directory E:\app\appfuse\dist
[delete] Deleting: E:\app\appfuse\database.properties
build.properties.missing:
v prepare:
Ø 运行结果:
[echo] Preparing target directory 'E:\app\appfuse/build/appfuse'
[mkdir] Created dir: E:\app\appfuse\build\appfuse
[mkdir] Created dir: E:\app\appfuse\build\appfuse\WEB-INF
[mkdir] Created dir: E:\app\appfuse\dist\webapps
v hibernatedoclet:
<!-- =================================================================== -->
<!-- The "hibernatedoclet" target generates Hibernate mapping files -->
<!-- based on XDoclet marked-up Plain Old Java Object (POJO) -->
<!-- =================================================================== -->
Ø 任务定义
<target name="hibernatedoclet" depends="prepare"
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.dir}/dao/gen"
mergedir="metadata/dao"
excludedtags="@version,@author"
addedtags="@xdoclet-generated at ${TODAY}"
force="${xdoclet.force}">
<fileset dir="src/dao"/>
<hibernate validatexml="true" version="2.0"/>
</hibernatedoclet>
</target>
Ø 运行结果:
[hibernatedoclet] (XDocletMain.start 47 ) Running <hibernate/>
[hibernatedoclet] Generating mapping file for org.appfuse.model.User.
[hibernatedoclet] org.appfuse.model.User
[hibernatedoclet] Generating mapping file for org.appfuse.model.UserCookie.
[hibernatedoclet] org.appfuse.model.UserCookie
[hibernatedoclet] Generating mapping file for org.appfuse.model.Role.
[hibernatedoclet] org.appfuse.model.Role
v compile-dao:调用compile-module编译dao目录下的类文件,包括测试类。
v compile-module:编译工作的主任务,其他具体编译工作以参数调用其完成编译工作。
Ø 任务定义:
<target name="compile-module">
<!-- Inputs: module, compile.classpath, test.classpath -->
<echo level="info">Compiling ${module}...</echo>
<mkdir dir="${build.dir}/${module}/classes"/>
<mkdir dir="${test.dir}/${module}/classes"/>
<property name="excludes" value=""/>
<property name="additional.src.dirs" value=""/>
<javac srcdir="src/${module};${additional.src.dirs}"
destdir="${build.dir}/${module}/classes"
debug="${compile.debug}"
deprecation="${compile.deprecation}"
optimize="${compile.optimize}"
classpathref="compile.classpath"/>
<javac srcdir="test/${module}"
destdir="${test.dir}/${module}/classes"
debug="true">
<classpath>
<path refid="test.classpath"/>
<path location="${build.dir}/${module}/classes"/>
</classpath>
</javac>
</target>
Ø 运行结果:
[echo] Compiling dao...
[mkdir] Created dir: E:\app\appfuse\build\dao\classes
[mkdir] Created dir: E:\app\appfuse\build\test\dao\classes
[javac] Compiling 15 source files to E:\app\appfuse\build\dao\classes
[javac] Note: E:\app\appfuse\src\dao\org\appfuse\model\User.java uses unchecked or unsafe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.
[javac] Compiling 5 source files to E:\app\appfuse\build\test\dao\classes
[javac] Note: E:\app\appfuse\test\dao\org\appfuse\dao\BaseDAOTestCase.java uses unchecked or unsafe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.
v package-dao:Dao打包
Ø 运行结果:
[copy] Copying 1 file to E:\app\appfuse\build\dao\gen
[jar] Building jar: E:\app\appfuse\dist\appfuse-dao.jar
v db-prepare:
<!-- =================================================================== -->
<!-- The "db-prepare" target generates the database schema and creates -->
<!-- tables based on the mapping files -->
<!-- =================================================================== -->
Ø 任务定义:
<target name="db-prepare" depends="clean,package-dao"
description="creates database tables">
<taskdef name="schemaexport"
classname="net.sf.hibernate.tool.hbm2ddl.SchemaExportTask">
<classpath>
<path refid="xdoclet.classpath"/>
<path refid="hibernate.classpath"/>
</classpath>
</taskdef>
<antcall target="generate.database.properties"/>
<schemaexport quiet="no" text="no" drop="no" delimiter=";"
properties="database.properties" output="create-tables.sql">
<fileset dir="${build.dir}/dao/gen" includes="**/*.hbm.xml"/>
</schemaexport>
</target>
v generate.database.properties:
Ø 任务定义:
<target name="generate.database.properties">
<echo>generating database.properties from properties.properties</echo>
<propertyfile comment="Hibernate Configuration for JUnit tests"
file="${basedir}/database.properties">
<entry key="hibernate.dialect" value="${hibernate.dialect}"/>
<entry key="hibernate.connection.driver_class" value="${database.driver_class}"/>
<entry key="hibernate.connection.url" value="${database.url}"/>
<entry key="hibernate.connection.username" value="${database.username}"/>
<entry key="hibernate.connection.password" value="${database.password}"/>
<entry key="hibernate.connection.show_sql" value="${database.show_sql}"/>
<entry key="dao.type" value="${dao.type}"/>
</propertyfile>
<replace file="database.properties" token="amp;" value=""/>
<property file="database.properties"/>
</target>
Ø db-prepare运行结果:
[echo] generating database.properties from build.properties
[propertyfile] Creating new property file: E:\app\appfuse\database.properties
[schemaexport] (cfg.Environment 478 ) Hibernate 2.1.7
[schemaexport] (cfg.Environment 507 ) hibernate.properties not found
[schemaexport] (cfg.Environment 538 ) using CGLIB reflection optimizer
[schemaexport] (cfg.Environment 567 ) using JDK 1.4 java.sql.Timestamp handling
[schemaexport] (cfg.Configuration 169 ) Mapping file: E:\app\appfuse\build\dao\gen\org\appfuse\model\Role.hbm.xml
[schemaexport] (cfg.Binder 230 ) Mapping class: org.appfuse.model.Role -> role
[schemaexport] (cfg.Configuration 169 ) Mapping file: E:\app\appfuse\build\dao\gen\org\appfuse\model\User.hbm.xml
[schemaexport] (cfg.Binder 230 ) Mapping class: org.appfuse.model.User -> app_user
[schemaexport] (cfg.Binder 572 ) Mapping collection: org.appfuse.model.User.roles -> user_role
[schemaexport] (cfg.Configuration 169 ) Mapping file: E:\app\appfuse\build\dao\gen\org\appfuse\model\UserCookie.hbm.xml
[schemaexport] (cfg.Binder 230 ) Mapping class: org.appfuse.model.UserCookie -> user_cookie
[schemaexport] (dialect.Dialect 86 ) Using dialect: net.sf.hibernate.dialect.MySQLDialect
[schemaexport] (cfg.Configuration 632 ) processing one-to-many association mappings
[schemaexport] (cfg.Configuration 641 ) processing one-to-one association property references
[schemaexport] (cfg.Configuration 666 ) processing foreign key constraints
[schemaexport] (cfg.Configuration 632 ) processing one-to-many association mappings
[schemaexport] (cfg.Configuration 641 ) processing one-to-one association property references
[schemaexport] (cfg.Configuration 666 ) processing foreign key constraints
[schemaexport] (hbm2ddl.SchemaExport 98 ) Running hbm2ddl schema export
[schemaexport] (hbm2ddl.SchemaExport 112 ) writing generated schema to file: create-tables.sql
[schemaexport] (hbm2ddl.SchemaExport 117 ) exporting generated schema to database
[schemaexport] (connection.DriverManagerConnectionProvider 42 ) Using Hibernate built-in connection pool (not for production use!)
[schemaexport] (connection.DriverManagerConnectionProvider 43 ) Hibernate connection pool size: 20
[schemaexport] (connection.DriverManagerConnectionProvider 77 ) using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost/appfuse?autoReconnect=true&useUnicode=true&characterEncoding=utf-8
[schemaexport] (connection.DriverManagerConnectionProvider 78 ) connection properties: {user=test, password=test, show_sql=true}
[schemaexport] alter table user_role drop foreign key FK143BF46A14048CB4;
[schemaexport] alter table user_role drop foreign key FK143BF46AF02988D6;
[schemaexport] drop table if exists user_role;
[schemaexport] drop table if exists app_user;
[schemaexport] drop table if exists user_cookie;
[schemaexport] drop table if exists role;
[schemaexport] create table user_role (
[schemaexport] username varchar(20) not null,
[schemaexport] role_name varchar(20) not null,
[schemaexport] primary key (username, role_name)
[schemaexport] );
[schemaexport] create table app_user (
[schemaexport] username varchar(20) not null,
[schemaexport] version integer not null,
[schemaexport] password varchar(255) not null,
[schemaexport] first_name varchar(50) not null,
[schemaexport] last_name varchar(50) not null,
[schemaexport] address varchar(150),
[schemaexport] city varchar(50) not null,
[schemaexport] province varchar(100),
[schemaexport] country varchar(100),
[schemaexport] postal_code varchar(15) not null,
[schemaexport] email varchar(255) not null unique,
[schemaexport] phone_number varchar(255),
[schemaexport] website varchar(255),
[schemaexport] password_hint varchar(255),
[schemaexport] primary key (username)
[schemaexport] );
[schemaexport] create table user_cookie (
[schemaexport] id bigint not null,
[schemaexport] username varchar(30) not null,
[schemaexport] cookie_id varchar(100) not null,
[schemaexport] date_created datetime not null,
[schemaexport] primary key (id)
[schemaexport] );
[schemaexport] create table role (
[schemaexport] name varchar(20) not null,
[schemaexport] version integer not null,
[schemaexport] description varchar(255),
[schemaexport] primary key (name)
[schemaexport] );
[schemaexport] alter table user_role add index FK143BF46A14048CB4 (role_name), add constraint FK143BF46A14048CB4 foreign key (role_name) references role (name);
[schemaexport] alter table user_role add index FK143BF46AF02988D6 (username), add constraint FK143BF46AF02988D6 foreign key (username) references app_user (username);
[schemaexport] create index user_cookie_username_cookie_id on user_cookie (username, cookie_id);
[schemaexport] (hbm2ddl.SchemaExport 160 ) schema export complete
[schemaexport] (connection.DriverManagerConnectionProvider 143 ) cleaning up connection pool: jdbc:mysql://localhost/appfuse?autoReconnect=true&useUnicode=true&characterEncoding=utf-8