开发表单映射的实体(entity)bean
2009-08-19 13:50 Jvpy 阅读(411) 评论(0) 编辑 收藏 举报
Entity bean属于JPA的技术,entity bean通过元数据在Java bean和数据库表之间建立映射关系。
根据JPA规范的要求,在entity bean的应用中,需要在应用的类路径下的META-INF目录下建立持久化配置文件persistence.xml。
persistence.xml模板如下:
大部分的注释写在了代码中,以下就从简表达。
最终工程结构如下:
首先需要配置jboss数据源:
Enity bean:
需要通过session bean来操纵entity bean,以下是session bean的接口和实现。
jndi.properties:
persistence.xml值得注意,以下这种方式的写法是默认先把person表创建好了。
创建person表:create table person (id integer not null auto_increment, name varchar(20), primary key(id) )engine=InnoDB;
单元测试:
build.xml:
根据JPA规范的要求,在entity bean的应用中,需要在应用的类路径下的META-INF目录下建立持久化配置文件persistence.xml。
persistence.xml模板如下:
persistence.xml
1 <persistence xmlns="http://java.sun.com/xml/ns/persistence"
2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
4 version="1.0">
5
6 </persistence>
1 <persistence xmlns="http://java.sun.com/xml/ns/persistence"
2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
4 version="1.0">
5
6 </persistence>
大部分的注释写在了代码中,以下就从简表达。
最终工程结构如下:
首先需要配置jboss数据源:
mysql-ds.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 <!-- $Id: mysql-ds.xml 63175 2007-05-21 16:26:06Z rrajesh $ -->
4 <!-- Datasource config for MySQL using 3.0.9 available from:
5 http://www.mysql.com/downloads/api-jdbc-stable.html
6 -->
7
8 <datasources>
9 <local-tx-datasource>
10 <jndi-name>JvpyDS</jndi-name>
11 <connection-url>jdbc:mysql://localhost:3306/jvpy</connection-url>
12 <driver-class>com.mysql.jdbc.Driver</driver-class>
13 <user-name>root</user-name>
14 <password>root</password>
15 <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
16 <!-- should only be used on drivers after 3.22.1 with "ping" support
17 <valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker</valid-connection-checker-class-name>
18 -->
19 <!-- sql to call when connection is created
20 <new-connection-sql>some arbitrary sql</new-connection-sql>
21 -->
22 <!-- sql to call on an existing pooled connection when it is obtained from pool - MySQLValidConnectionChecker is preferred for newer drivers
23 <check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
24 -->
25
26 <!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml -->
27 <metadata>
28 <type-mapping>mySQL</type-mapping>
29 </metadata>
30 </local-tx-datasource>
31 </datasources>
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 <!-- $Id: mysql-ds.xml 63175 2007-05-21 16:26:06Z rrajesh $ -->
4 <!-- Datasource config for MySQL using 3.0.9 available from:
5 http://www.mysql.com/downloads/api-jdbc-stable.html
6 -->
7
8 <datasources>
9 <local-tx-datasource>
10 <jndi-name>JvpyDS</jndi-name>
11 <connection-url>jdbc:mysql://localhost:3306/jvpy</connection-url>
12 <driver-class>com.mysql.jdbc.Driver</driver-class>
13 <user-name>root</user-name>
14 <password>root</password>
15 <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
16 <!-- should only be used on drivers after 3.22.1 with "ping" support
17 <valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker</valid-connection-checker-class-name>
18 -->
19 <!-- sql to call when connection is created
20 <new-connection-sql>some arbitrary sql</new-connection-sql>
21 -->
22 <!-- sql to call on an existing pooled connection when it is obtained from pool - MySQLValidConnectionChecker is preferred for newer drivers
23 <check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
24 -->
25
26 <!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml -->
27 <metadata>
28 <type-mapping>mySQL</type-mapping>
29 </metadata>
30 </local-tx-datasource>
31 </datasources>
Enity bean:
Person.java
1 package cn.jvpy.bean;
2
3 import java.io.Serializable;
4
5 import javax.persistence.Column;
6 import javax.persistence.Entity;
7 import javax.persistence.GeneratedValue;
8 import javax.persistence.GenerationType;
9 import javax.persistence.Id;
10 import javax.persistence.Table;
11
12 //要想跟数据进行映射,可通过xml文件或者注解方式定义元数据,注解方式效率高。
13 @Entity
14 //Table中name指定要映射的数据库的表
15 @Table(name = "person")
16 //当客户端远程调用ejb时,在数据传输过程中要进行序列化
17 //如果某业务方法要返回Person对象至客户端,则该对象需要是序列化的
18 public class Person implements Serializable {
19 /**
20 *
21 */
22 private static final long serialVersionUID = 8825614031697833934L;
23 private Integer id;
24 private String name;
25
26 // JPA规范要求,必须要用无参数的ctor
27 public Person() {
28 }
29
30 public Person(String name) {
31 this.name = name;
32 }
33
34 // Id指定entity bean的对象标识属性,entity bean中必须有此属性
35 @Id
36 @Column(name = "id")
37 // 指定该属性的生成方式
38 // (对于mysql来说AUTO和IDENTITY是一样的,但没有SEQUENCE,
39 // 为了增强可移植性,最好指定用AUTO)
40 @GeneratedValue(strategy = GenerationType.AUTO)
41 public Integer getId() {
42 return id;
43 }
44
45 public void setId(Integer id) {
46 this.id = id;
47 }
48
49 // 注解可以标注在property上或者field上面,标注在property上面则只能标注getter
50 // 指定与数据库表中哪个字段进行映射
51 @Column(name = "name", length = 20, nullable = false)
52 public String getName() {
53 return name;
54 }
55
56 public void setName(String name) {
57 this.name = name;
58 }
59
60 // 一般通过属性标志符id进行比较两个对象,需要重载以下两个方法
61 // 以下代码可通过eclipse自动生成:source->Generate hashCode() and equals()
62 @Override
63 public int hashCode() {
64 final int prime = 31;
65 int result = 1;
66 result = prime * result + ((id == null) ? 0 : id.hashCode());
67 return result;
68 }
69
70 @Override
71 public boolean equals(Object obj) {
72 if (this == obj)
73 return true;
74 if (obj == null)
75 return false;
76 if (getClass() != obj.getClass())
77 return false;
78 final Person other = (Person) obj;
79 if (id == null) {
80 if (other.id != null)
81 return false;
82 } else if (!id.equals(other.id))
83 return false;
84 return true;
85 }
86 // 自动生成结束
87 }
88
1 package cn.jvpy.bean;
2
3 import java.io.Serializable;
4
5 import javax.persistence.Column;
6 import javax.persistence.Entity;
7 import javax.persistence.GeneratedValue;
8 import javax.persistence.GenerationType;
9 import javax.persistence.Id;
10 import javax.persistence.Table;
11
12 //要想跟数据进行映射,可通过xml文件或者注解方式定义元数据,注解方式效率高。
13 @Entity
14 //Table中name指定要映射的数据库的表
15 @Table(name = "person")
16 //当客户端远程调用ejb时,在数据传输过程中要进行序列化
17 //如果某业务方法要返回Person对象至客户端,则该对象需要是序列化的
18 public class Person implements Serializable {
19 /**
20 *
21 */
22 private static final long serialVersionUID = 8825614031697833934L;
23 private Integer id;
24 private String name;
25
26 // JPA规范要求,必须要用无参数的ctor
27 public Person() {
28 }
29
30 public Person(String name) {
31 this.name = name;
32 }
33
34 // Id指定entity bean的对象标识属性,entity bean中必须有此属性
35 @Id
36 @Column(name = "id")
37 // 指定该属性的生成方式
38 // (对于mysql来说AUTO和IDENTITY是一样的,但没有SEQUENCE,
39 // 为了增强可移植性,最好指定用AUTO)
40 @GeneratedValue(strategy = GenerationType.AUTO)
41 public Integer getId() {
42 return id;
43 }
44
45 public void setId(Integer id) {
46 this.id = id;
47 }
48
49 // 注解可以标注在property上或者field上面,标注在property上面则只能标注getter
50 // 指定与数据库表中哪个字段进行映射
51 @Column(name = "name", length = 20, nullable = false)
52 public String getName() {
53 return name;
54 }
55
56 public void setName(String name) {
57 this.name = name;
58 }
59
60 // 一般通过属性标志符id进行比较两个对象,需要重载以下两个方法
61 // 以下代码可通过eclipse自动生成:source->Generate hashCode() and equals()
62 @Override
63 public int hashCode() {
64 final int prime = 31;
65 int result = 1;
66 result = prime * result + ((id == null) ? 0 : id.hashCode());
67 return result;
68 }
69
70 @Override
71 public boolean equals(Object obj) {
72 if (this == obj)
73 return true;
74 if (obj == null)
75 return false;
76 if (getClass() != obj.getClass())
77 return false;
78 final Person other = (Person) obj;
79 if (id == null) {
80 if (other.id != null)
81 return false;
82 } else if (!id.equals(other.id))
83 return false;
84 return true;
85 }
86 // 自动生成结束
87 }
88
需要通过session bean来操纵entity bean,以下是session bean的接口和实现。
PersonService.java
1 package cn.jvpy.service;
2
3 import java.util.List;
4
5 import cn.jvpy.bean.Person;
6
7 //通过在stateless bean中用实体管理器实现对entity bean的添删改查
8 public interface PersonService {
9 public void save(Person person);
10
11 public void update(Person person);
12
13 public void delete(Integer personId);
14
15 public Person getPerson(Integer personId);
16
17 public List<Person> getPersons();
18 }
19
1 package cn.jvpy.service;
2
3 import java.util.List;
4
5 import cn.jvpy.bean.Person;
6
7 //通过在stateless bean中用实体管理器实现对entity bean的添删改查
8 public interface PersonService {
9 public void save(Person person);
10
11 public void update(Person person);
12
13 public void delete(Integer personId);
14
15 public Person getPerson(Integer personId);
16
17 public List<Person> getPersons();
18 }
19
PersonServiceBean.java
1 package cn.jvpy.service.impl;
2
3 import java.util.List;
4
5 import javax.ejb.Remote;
6 import javax.ejb.Stateless;
7 import javax.persistence.EntityManager;
8 import javax.persistence.PersistenceContext;
9
10 import cn.jvpy.bean.Person;
11 import cn.jvpy.service.PersonService;
12
13 @Stateless
14 @Remote(PersonService.class)
15 public class PersonServiceBean implements PersonService {
16 // 注入实体管理器
17 // unitName的值为persistence.xml中persistence-uint中指定的值
18 // 如果persistence.xml中存在多个持久化单元的时候必须指定unitName的名称
19 @PersistenceContext(unitName = "jvpy")
20 EntityManager em;
21
22 public void delete(Integer personId) {
23 // 实体bean处于托管状态才能被正确删除
24 // em.getReference方法返回的是代理对象,只要还不访问返回对象的方法,不进行数据的加载行为
25 em.remove(em.getReference(Person.class, personId));
26 }
27
28 public Person getPerson(Integer personId) {
29 // 查询不到时返回null值
30 return em.find(Person.class, personId);
31 }
32
33 @SuppressWarnings("unchecked")
34 public List<Person> getPersons() {
35 // JPQL,关键字不区分大小写,面向对象的查询,not sql
36 // 用到的是实体的名称,而不是数据库表,区分大小写
37 return em.createQuery("select o from Person o").getResultList();
38 }
39
40 public void save(Person person) {
41 // 保存新建状态的实体
42 // 实体bean中有四种状态:新建状态/托管状态/游离状态/删除状态
43 // 该方法往数据中添加一条新的纪录
44 // 把实体bean里面的持久化字段里面的值保存到对应的数据表的字段,而不是把整个对象保存到数据库
45 em.persist(person);
46 }
47
48 public void update(Person person) {
49 // 实体bean处于游离状态时才可以调用该方法,在游离状态下对该对象的数据做了修改
50 em.merge(person);
51 }
52
53 }
54
1 package cn.jvpy.service.impl;
2
3 import java.util.List;
4
5 import javax.ejb.Remote;
6 import javax.ejb.Stateless;
7 import javax.persistence.EntityManager;
8 import javax.persistence.PersistenceContext;
9
10 import cn.jvpy.bean.Person;
11 import cn.jvpy.service.PersonService;
12
13 @Stateless
14 @Remote(PersonService.class)
15 public class PersonServiceBean implements PersonService {
16 // 注入实体管理器
17 // unitName的值为persistence.xml中persistence-uint中指定的值
18 // 如果persistence.xml中存在多个持久化单元的时候必须指定unitName的名称
19 @PersistenceContext(unitName = "jvpy")
20 EntityManager em;
21
22 public void delete(Integer personId) {
23 // 实体bean处于托管状态才能被正确删除
24 // em.getReference方法返回的是代理对象,只要还不访问返回对象的方法,不进行数据的加载行为
25 em.remove(em.getReference(Person.class, personId));
26 }
27
28 public Person getPerson(Integer personId) {
29 // 查询不到时返回null值
30 return em.find(Person.class, personId);
31 }
32
33 @SuppressWarnings("unchecked")
34 public List<Person> getPersons() {
35 // JPQL,关键字不区分大小写,面向对象的查询,not sql
36 // 用到的是实体的名称,而不是数据库表,区分大小写
37 return em.createQuery("select o from Person o").getResultList();
38 }
39
40 public void save(Person person) {
41 // 保存新建状态的实体
42 // 实体bean中有四种状态:新建状态/托管状态/游离状态/删除状态
43 // 该方法往数据中添加一条新的纪录
44 // 把实体bean里面的持久化字段里面的值保存到对应的数据表的字段,而不是把整个对象保存到数据库
45 em.persist(person);
46 }
47
48 public void update(Person person) {
49 // 实体bean处于游离状态时才可以调用该方法,在游离状态下对该对象的数据做了修改
50 em.merge(person);
51 }
52
53 }
54
jndi.properties:
1 java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
2 java.naming.provider.url=localhost:1099
2 java.naming.provider.url=localhost:1099
persistence.xml值得注意,以下这种方式的写法是默认先把person表创建好了。
创建person表:create table person (id integer not null auto_increment, name varchar(20), primary key(id) )engine=InnoDB;
persistence.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <persistence xmlns="http://java.sun.com/xml/ns/persistence"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
5 version="1.0">
6
7 <!-- 持久化单元:一堆实体类的集合。事务类型有JTA(默认)和SOURCE_LOCAL。该文件中可以配置多个持久化单元 -->
8 <persistence-unit name="jvpy" transaction-type="JTA">
9 <!-- 指定要使用的数据源,当数据源发布到jboss时,会被放到“java”空间中 -->
10 <jta-data-source>java:JvpyDS</jta-data-source>
11 </persistence-unit>
12 </persistence>
这种写法会自己创建数和删除数据库表据库表:1 <?xml version="1.0" encoding="UTF-8"?>
2 <persistence xmlns="http://java.sun.com/xml/ns/persistence"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
5 version="1.0">
6
7 <!-- 持久化单元:一堆实体类的集合。事务类型有JTA(默认)和SOURCE_LOCAL。该文件中可以配置多个持久化单元 -->
8 <persistence-unit name="jvpy" transaction-type="JTA">
9 <!-- 指定要使用的数据源,当数据源发布到jboss时,会被放到“java”空间中 -->
10 <jta-data-source>java:JvpyDS</jta-data-source>
11 </persistence-unit>
12 </persistence>
peisistence.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <persistence xmlns="http://java.sun.com/xml/ns/persistence"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
5 version="1.0">
6
7 <!-- 持久化单元:一堆实体类的集合。事务类型有JTA(默认)和SOURCE_LOCAL。该文件中可以配置多个持久化单元 -->
8 <persistence-unit name="jvpy" transaction-type="JTA">
9 <!-- 指定要使用的数据源,当数据源发布到jboss时,会被放到“java”空间中 -->
10 <jta-data-source>java:JvpyDS</jta-data-source>
11
12 <properties>
13 <!-- 在deploy时自动创建数据表,在undeploy时自动删除数据库表,只在开发的时候用,发布一般用update -->
14 <property name="hibernate.hbm2ddl.auto" value="create-drop" />
15 <!-- 显示最终执行的SQL -->
16 <property name="hibernate.show_sql" value="true"/>
17 <!-- 格式化显示的SQL -->
18 <property name="hibernate.format_sql" value="true"/>
19 </properties>
20 </persistence-unit>
21 </persistence>
1 <?xml version="1.0" encoding="UTF-8"?>
2 <persistence xmlns="http://java.sun.com/xml/ns/persistence"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
5 version="1.0">
6
7 <!-- 持久化单元:一堆实体类的集合。事务类型有JTA(默认)和SOURCE_LOCAL。该文件中可以配置多个持久化单元 -->
8 <persistence-unit name="jvpy" transaction-type="JTA">
9 <!-- 指定要使用的数据源,当数据源发布到jboss时,会被放到“java”空间中 -->
10 <jta-data-source>java:JvpyDS</jta-data-source>
11
12 <properties>
13 <!-- 在deploy时自动创建数据表,在undeploy时自动删除数据库表,只在开发的时候用,发布一般用update -->
14 <property name="hibernate.hbm2ddl.auto" value="create-drop" />
15 <!-- 显示最终执行的SQL -->
16 <property name="hibernate.show_sql" value="true"/>
17 <!-- 格式化显示的SQL -->
18 <property name="hibernate.format_sql" value="true"/>
19 </properties>
20 </persistence-unit>
21 </persistence>
单元测试:
PersonServiceTest.java
1 package cn.jvpy.test;
2
3 import static org.junit.Assert.*;
4
5 import java.util.List;
6
7 import javax.naming.InitialContext;
8
9 import org.junit.BeforeClass;
10 import org.junit.Test;
11
12 import cn.jvpy.bean.Person;
13 import cn.jvpy.service.PersonService;
14
15 public class PersonServiceTest {
16 private static PersonService personService;
17
18 @BeforeClass
19 public static void setUpBeforeClass() throws Exception {
20 try {
21 InitialContext ctx = new InitialContext();
22 personService = (PersonService) ctx.lookup("PersonServiceBean/remote");
23 } catch (RuntimeException e) {
24 e.printStackTrace();
25 }
26 }
27
28 @Test
29 public void testSave() {
30 personService.save(new Person("Xiao Li"));
31 }
32
33 @Test
34 public void testUpdate() {
35 Person person = personService.getPerson(1);
36 person.setName("Xiao Ming");
37 personService.update(person);
38 }
39
40 @Test
41 public void testDelete() {
42 personService.delete(1);
43 }
44
45 @Test
46 public void testGetPerson() {
47 Person person = personService.getPerson(1);
48 System.out.println(person.getName());
49 }
50
51 @Test
52 public void testGetPersons() {
53 List<Person> persons = personService.getPersons();
54 for (Person person : persons) {
55 System.out.println(person.getName());
56 }
57 }
58
59 }
60
1 package cn.jvpy.test;
2
3 import static org.junit.Assert.*;
4
5 import java.util.List;
6
7 import javax.naming.InitialContext;
8
9 import org.junit.BeforeClass;
10 import org.junit.Test;
11
12 import cn.jvpy.bean.Person;
13 import cn.jvpy.service.PersonService;
14
15 public class PersonServiceTest {
16 private static PersonService personService;
17
18 @BeforeClass
19 public static void setUpBeforeClass() throws Exception {
20 try {
21 InitialContext ctx = new InitialContext();
22 personService = (PersonService) ctx.lookup("PersonServiceBean/remote");
23 } catch (RuntimeException e) {
24 e.printStackTrace();
25 }
26 }
27
28 @Test
29 public void testSave() {
30 personService.save(new Person("Xiao Li"));
31 }
32
33 @Test
34 public void testUpdate() {
35 Person person = personService.getPerson(1);
36 person.setName("Xiao Ming");
37 personService.update(person);
38 }
39
40 @Test
41 public void testDelete() {
42 personService.delete(1);
43 }
44
45 @Test
46 public void testGetPerson() {
47 Person person = personService.getPerson(1);
48 System.out.println(person.getName());
49 }
50
51 @Test
52 public void testGetPersons() {
53 List<Person> persons = personService.getPersons();
54 for (Person person : persons) {
55 System.out.println(person.getName());
56 }
57 }
58
59 }
60
build.xml:
build.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <project name="EntityBean" basedir=".">
3 <property name="src.dir" value="${basedir}\src" />
4 <property environment="env" />
5 <property name="jboss.home" value="${env.JBOSS_HOME}" />
6 <property name="jboss.server.config" value="default" />
7 <property name="build.dir" value="${basedir}\build" />
8
9 <path id="build.classpath">
10 <fileset dir="${jboss.home}\client">
11 <include name="*.jar" />
12 </fileset>
13 <pathelement location="${build.dir}" />
14 </path>
15
16 <target name="prepare">
17 <delete dir="${build.dir}" />
18 <mkdir dir="${build.dir}" />
19 </target>
20
21
22 <target name="compile" depends="prepare" description="compile">
23 <javac srcdir="${src.dir}" destdir="${build.dir}">
24 <classpath refid="build.classpath" />
25 </javac>
26 </target>
27
28 <target name="ejbjar" depends="compile" description="jar the ejb package">
29 <jar jarfile="${basedir}\${ant.project.name}.jar">
30 <fileset dir="${build.dir}">
31 <include name="**/*.class" />
32 </fileset>
33 <metainf dir="${src.dir}\META-INF"></metainf>
34 </jar>
35 </target>
36
37 <target name="deploy" depends="ejbjar" description="deploy the ejb">
38 <copy file="${basedir}\${ant.project.name}.jar" todir="${jboss.home}\server\${jboss.server.config}\deploy" />
39 </target>
40
41 <target name="undeploy" description="undeploy the ejb">
42 <delete file="${jboss.home}\server\${jboss.server.config}\deploy\${ant.project.name}.jar" />
43 </target>
44 </project>
1 <?xml version="1.0" encoding="UTF-8"?>
2 <project name="EntityBean" basedir=".">
3 <property name="src.dir" value="${basedir}\src" />
4 <property environment="env" />
5 <property name="jboss.home" value="${env.JBOSS_HOME}" />
6 <property name="jboss.server.config" value="default" />
7 <property name="build.dir" value="${basedir}\build" />
8
9 <path id="build.classpath">
10 <fileset dir="${jboss.home}\client">
11 <include name="*.jar" />
12 </fileset>
13 <pathelement location="${build.dir}" />
14 </path>
15
16 <target name="prepare">
17 <delete dir="${build.dir}" />
18 <mkdir dir="${build.dir}" />
19 </target>
20
21
22 <target name="compile" depends="prepare" description="compile">
23 <javac srcdir="${src.dir}" destdir="${build.dir}">
24 <classpath refid="build.classpath" />
25 </javac>
26 </target>
27
28 <target name="ejbjar" depends="compile" description="jar the ejb package">
29 <jar jarfile="${basedir}\${ant.project.name}.jar">
30 <fileset dir="${build.dir}">
31 <include name="**/*.class" />
32 </fileset>
33 <metainf dir="${src.dir}\META-INF"></metainf>
34 </jar>
35 </target>
36
37 <target name="deploy" depends="ejbjar" description="deploy the ejb">
38 <copy file="${basedir}\${ant.project.name}.jar" todir="${jboss.home}\server\${jboss.server.config}\deploy" />
39 </target>
40
41 <target name="undeploy" description="undeploy the ejb">
42 <delete file="${jboss.home}\server\${jboss.server.config}\deploy\${ant.project.name}.jar" />
43 </target>
44 </project>