MPHIBERNATE-12: new goal that generates POJOs from HBM files (hibernate:code-generation)

git-svn-id: https://svn.apache.org/repos/asf/maven/maven-1/plugins/trunk@125458 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
felipeal 2005-01-18 02:53:55 +00:00
parent 9e4136d82d
commit 38b4d952bb
17 changed files with 597 additions and 5 deletions

View File

@ -68,6 +68,17 @@
includes="${maven.hibernate.input.includes}"
excludes="${maven.hibernate.input.excludes}"/>
</goal>
<goal name="hibernate:code-generation" prereqs="hibernate:init" description="Generate POJOs from hbm files">
<ant:echo>Generating POJOs from hbm files</ant:echo>
<h:code-generation
basedir="${maven.hibernate.codeGeneration.input.dir}"
includes="${maven.hibernate.codeGeneration.input.includes}"
excludes="${maven.hibernate.codeGeneration.input.excludes}"
outputdir="${maven.hibernate.codeGeneration.output.dir}"/>
</goal>
</project>

View File

@ -24,4 +24,8 @@ maven.hibernate.output.file=${maven.hibernate.output.dir}/${maven.final.name}-sc
maven.hibernate.input.dir=${maven.build.dest}
maven.hibernate.input.includes=**/*.hbm.xml
maven.hibernate.input.excludes=
maven.hibernate.aggregate.output.file=${maven.hibernate.output.dir}/aggregated-mappings.hbm.xml
maven.hibernate.aggregate.output.file=${maven.hibernate.output.dir}/aggregated-mappings.hbm.xml
maven.hibernate.codeGeneration.input.dir=${maven.src.dir}/hibernate
maven.hibernate.codeGeneration.output.dir=${maven.src.dir}/hibernate
maven.hibernate.codeGeneration.input.includes=${maven.hibernate.input.includes}
maven.hibernate.codeGeneration.input.excludes=

View File

@ -95,6 +95,12 @@
<version>2.1.3</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>hibernate</groupId>
<artifactId>hibernate-tools</artifactId>
<version>2.1.3</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>ant</groupId>
<artifactId>ant</artifactId>
@ -179,5 +185,12 @@
<version>1.4</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>jdom</groupId>
<artifactId>jdom</artifactId>
<version>1.0</version>
<type>jar</type>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,61 @@
package org.apache.maven.hibernate.beans;
/* ====================================================================
* Copyright 2001-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
import net.sf.hibernate.tool.hbm2java.CodeGenerator;
/**
* @author <a href="paulkearney@gmail.com">Paul Kearney</a>
* @version $Id$
*/
public class CodeGenerationBean extends CommonOperationsBean {
private static final String OUTPUT_SWITCH = "--output=";
private String outputdir = null;
public void execute() {
// Construct output directory argument
StringBuffer switchArg= new StringBuffer();
switchArg.append(OUTPUT_SWITCH).append(getOutputdir());
// Get list of files that are to be used to generate POJOs
final String[] files = getFileNames();
// Require new array to combine command args with hbm files array
String[] args = new String[files.length + 1];
// Add command arg to new array
args[0] = switchArg.toString();
// Copy list of hbm files to new array
System.arraycopy(files, 0, args, 1, files.length);
// Generate POJOs
CodeGenerator generator = new CodeGenerator();
generator.main(args);
}
public String getOutputdir() {
return this.outputdir;
}
public void setOutputdir(String outputdir) {
this.outputdir = outputdir;
}
}

View File

@ -0,0 +1,99 @@
package org.apache.maven.hibernate.jelly;
/* ====================================================================
* Copyright 2001-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
import org.apache.commons.jelly.JellyTagException;
import org.apache.commons.jelly.MissingAttributeException;
import org.apache.commons.jelly.TagSupport;
import org.apache.commons.jelly.XMLOutput;
import org.apache.maven.hibernate.beans.CodeGenerationBean;
/**
* @author <a href="paulkearney@gmail.com">Paul Kearney</a>
* @version $Id$
*/
public class CodeGenerationTag extends TagSupport {
private CodeGenerationBean bean = new CodeGenerationBean();
/**
* @see org.apache.commons.jelly.Tag#doTag(org.apache.commons.jelly.XMLOutput)
*/
public void doTag(XMLOutput arg0) throws MissingAttributeException, JellyTagException {
execute();
}
/**
*
*/
protected void execute() throws JellyTagException
{
try {
bean.execute();
} catch (Exception e) {
String msg = "Code generation operation failed";
throw new JellyTagException(msg, e);
}
}
public String getBasedir()
{
return bean.getBasedir();
}
public String getExcludes()
{
return bean.getExcludes();
}
public String getIncludes()
{
return bean.getIncludes();
}
public String getOutputdir()
{
return bean.getOutputdir();
}
public void setBasedir(String string)
{
bean.setBasedir(string);
}
public void setExcludes(String string)
{
bean.setExcludes(string);
}
public void setIncludes(String string)
{
bean.setIncludes(string);
}
public void setOutputdir(String dir)
{
bean.setOutputdir(dir);
}
public String toString()
{
return bean.toString();
}
}

View File

@ -24,7 +24,7 @@ import org.apache.commons.jelly.tags.core.CoreTagLibrary;
* Hibernate tag library.
*
* @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
* @version $Id: HibernateTagLibrary.java,v 1.4 2004/11/06 21:52:23 felipeal Exp $
* @version $Id$
*/
public class HibernateTagLibrary extends CoreTagLibrary
{
@ -37,5 +37,6 @@ public class HibernateTagLibrary extends CoreTagLibrary
registerTag( "schema-export", SchemaExportTag.class );
registerTag( "schema-update", SchemaUpdateTag.class );
registerTag( "aggregate-mappings", AggregateMappingsTag.class );
registerTag( "code-generation", CodeGenerationTag.class );
}
}

View File

@ -0,0 +1,62 @@
<!--
/*
* Copyright 2001-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-->
<project xmlns:util="jelly:util"
xmlns:j="jelly:core"
xmlns:ant="jelly:ant"
xmlns:assert="assert"
xmlns:maven="jelly:maven"
xmlns:x="jelly:xml"
default="testPlugin">
<goal name="testPlugin" prereqs="test-hibernate-code-generation">
<attainGoal name="clean"/>
</goal>
<goal name="test-hibernate-code-generation">
<!-- definitions -->
<j:set var="maven.hibernate.codeGeneration.input.excludes" value="org/apache/maven/hibernate/Item.hbm.xml,org/apache/maven/hibernate/ExcludeItem.hbm.xml"/>
<j:set var="itemJavaPath" value="org/apache/maven/hibernate/Item.java"/>
<j:set var="itemJavaFullPath" value="${maven.hibernate.codeGeneration.input.dir}/${itemJavaPath}"/>
<j:set var="excludeItemJavaPath" value="org/apache/maven/hibernate/ExcludeItem.java"/>
<j:set var="excludeItemJavaFullPath" value="${maven.hibernate.codeGeneration.input.dir}/${excludeItemJavaPath}"/>
<j:set var="messageJavaFullPath" value="${maven.hibernate.codeGeneration.output.dir}/org/apache/maven/hibernate/Message.java"/>
<!-- makes sure there was an Item.java before -->
<assert:assertFileExists file="${itemJavaFullPath}"/>
<util:loadText var="preItemContent" file="${itemJavaFullPath}"/>
<attainGoal name="hibernate:code-generation"/>
<util:loadText var="postItemContent" file="${itemJavaFullPath}"/>
<!-- makes sure Item.java was not generated -->
<assert:assertEquals expected="${preItemContent}" value="${postItemContent}"/>
<!-- makes sure ExcludeItem.java was not generated -->
<assert:assertFileNotFound file="${excludeItemJavaFullPath}"/>
<!-- makes sure Message.java was generated -->
<assert:assertFileExists file="${messageJavaFullPath}"/>
<attainGoal name="java:compile"/>
<delete file="${messageJavaFullPath}"/>
</goal>
</project>

View File

@ -0,0 +1,20 @@
# -------------------------------------------------------------------
# Copyright 2004 The Apache Software Foundation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -------------------------------------------------------------------
maven.hibernate.quiet=false
maven.hibernate.codeGeneration.input.dir=${maven.src.dir}/main
maven.hibernate.codeGeneration.output.dir=${maven.hibernate.codeGeneration.input.dir}

View File

@ -0,0 +1,110 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/*
* Copyright 2001-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-->
<project>
<pomVersion>3</pomVersion>
<id>test-maven-hibernate-plugin-codeGenerationTest</id>
<name>Test Cases for Maven Hibernate Plugin's CodeGeneration Tag</name>
<groupId>maven</groupId>
<currentVersion>1.0-SNAPSHOT</currentVersion>
<organization>
<name>Apache Software Foundation</name>
<url>http://www.apache.org/</url>
<logo>http://maven.apache.org/images/jakarta-logo-blue.gif</logo>
</organization>
<inceptionYear>2005</inceptionYear>
<package>org.apache.maven</package>
<logo>http://maven.apache.org/images/maven.jpg</logo>
<description>Test for Maven Hibernate plugin</description>
<shortDescription>Test for Maven Hibernate plugin</shortDescription>
<url>http://maven.apache.org/reference/plugins/hibernate/</url>
<siteDirectory>/www/maven.apache.org/reference/plugins/hibernate/</siteDirectory>
<repository>
<connection>scm:svn:http://svn.apache.org/repos/asf/maven/maven-1/plugins/trunk/hibernate/src/plugin-test</connection>
<url>http://svn.apache.org/viewcvs.cgi/maven/maven-1/plugins/trunk/hibernate/src/plugin-test</url>
</repository>
<developers>
<developer>
<name>Felipe Leme</name>
<id>felipeal</id>
<email>maven@felipeal.net</email>
<organization>Falcon Informatica</organization>
<roles>
<role>Java Developer</role>
</roles>
<timezone>-3</timezone>
</developer>
</developers>
<dependencies>
<dependency>
<groupId>commons-jelly</groupId>
<artifactId>commons-jelly-tags-xml</artifactId>
<version>20030211.142705</version>
<url>http://jakarta.apache.org/commons/jelly/libs/xml/</url>
</dependency>
<dependency>
<groupId>hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>1.7.1</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>2.1.3</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.0.2</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.4</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.0</version>
<type>jar</type>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main</sourceDirectory>
<resources>
<resource>
<directory>src/main</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>

View File

@ -0,0 +1,21 @@
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class
name="org.apache.maven.hibernate.ExcludeItem"
table="EXCLUDE_ITEMS">
<id
name="id"
type="java.lang.Long"
column="EXCLUDE_ITEM_ID">
<generator class="increment"/>
</id>
<property
name="name"
type="java.lang.String"
column="EXCLUDE_ITEM_NAME"/>
</class>
</hibernate-mapping>

View File

@ -0,0 +1,16 @@
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping auto-import="false">
<class name="org.apache.maven.hibernate.Item" table="Item" >
<id name="id" type="int" column="ID" unsaved-value="0">
<generator class="native"/>
</id>
<property name="name" column="NAME" type="string"/>
</class>
</hibernate-mapping>

View File

@ -0,0 +1,56 @@
package org.apache.maven.hibernate;
/* ====================================================================
* Copyright 2001-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
/**
* Class to use with testing the plugin.
*
* @author <a href="mailto:epugh@opensourceconnections.com">Eric Pugh</a>
*
* @version $Id: Item.java 125453 2005-01-18 01:03:25Z felipeal $
*/
public class Item {
private int id;
private String name;
/**
* @return Returns the id.
*/
public int getId() {
return id;
}
/**
* @param id The id to set.
*/
public void setId(int id) {
this.id = id;
}
/**
* @return Returns the name.
*/
public String getName() {
return name;
}
/**
* @param name The name to set.
*/
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,30 @@
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class
name="org.apache.maven.hibernate.Message"
table="MESSAGES">
<id
name="id"
type="java.lang.Long"
column="MESSAGE_ID">
<generator class="increment"/>
</id>
<property
name="text"
type="org.apache.maven.hibernate.UserType"
column="FIELD_TEXT"/>
<property
name="field"
type="java.lang.String"
column="MESSAGE_TEXT"/>
<many-to-one
name="nextMessage"
class="org.apache.maven.hibernate.Message"
cascade="all"
column="NEXT_MESSAGE_ID"/>
</class>
</hibernate-mapping>

View File

@ -0,0 +1,41 @@
package org.apache.maven.hibernate;
/* ====================================================================
* Copyright 2001-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
/**
* Class to use with testing the plugin.
*
* @author <a href="paulkearney@gmail.com">Paul Kearney</a>
*
* @version $Id$
*/
public class UserType {
private Long field;
private Message message;
public Long getField() {
return field;
}
public void setField(Long field) {
this.field = field;
}
}

View File

@ -25,8 +25,9 @@
</properties>
<body>
<release version="1.3" date="in cvs">
<action dev="felipeal" type="add" issue="MPHIBERNATE-13">Added new goal <code>hibernate:schema-update</code></action>
<action dev="epugh" type="fix" issue="MPHIBERNATE-9">plugin:test fails without a network connection</action>
<action dev="felipeal" type="add" issue="MPHIBERNATE-12" due-to="Paul Kearney">Added new goal <code>hibernate:code-generation</code>.</action>
<action dev="felipeal" type="add" issue="MPHIBERNATE-13">Added new goal <code>hibernate:schema-update</code>.</action>
<action dev="epugh" type="fix" issue="MPHIBERNATE-9">plugin:test fails without a network connection.</action>
</release>
<release version="1.2" date="2004-08-14">
<action dev="epugh" type="fix" issue="MPHIBERNATE-10" due-to="Henning Schmiedehausen">maven-hibernate ignores the "config" attribute</action>

View File

@ -49,7 +49,14 @@
<td>
Aggregates multiple hibernate mappings into one
</td>
</tr>
</tr>
<a name="hibernate:code-generation" />
<tr>
<td>hibernate:code-generation</td>
<td>
Generates Java classes from set of *.hbm.xml files
</td>
</tr>
</table>
</section>
</body>

View File

@ -133,6 +133,45 @@
<td>Yes</td>
<td>When <code>aggregate-mappings</code> is run, this file will contain the aggregated mappings</td>
</tr>
<tr>
<td>maven.hibernate.codeGeneration.input.dir</td>
<td>Yes</td>
<td>Comma-separated list of directories that contains Hibernate mapping files which will be used to generated Java classes when the goal <code>code-generation</code> is used.
It defaults to <code>${maven.src.dir}/hibernate</code>.
</td>
</tr>
<tr>
<td>maven.hibernate.codeGeneration.input.includes</td>
<td>Yes</td>
<td>
Comma-separated list of patterns of Hibernate mapping files,
which will be included during the code generation process (goal <code>code-generation</code>).
<br/>
<b>Note</b>: Files are relative to <code>${maven.hibernate.codeGeneration.input.dir}</code>.
<br/>
Default value is <code>${maven.hibernate.input.includes}</code>.
</td>
</tr>
<tr>
<td>maven.hibernate.codeGeneration.input.excludes</td>
<td>Yes</td>
<td>
Comma-separated list of patterns of Hibernate mapping files,
which will be excluded during the code generation process (goal <code>code-generation</code>).
<br/>
<b>Note</b>: Files are relative to
<code>${maven.hibernate.codeGeneration.input.dir}</code>.
<br/>
By default no files are excluded.
</td>
</tr>
<tr>
<td>maven.hibernate.codeGeneration.output.dir</td>
<td>Yes</td>
<td>When <code>code-generation</code> is run, the generated Java files will be placed in
this directory. It defaults to <code>${maven.src.dir}/hibernate</code>.</td>
</tr>
</table>
</section>