How to change name of Hibernate Tools reverse engineerd DAO class? - java

So im trying to use Hibernate Tools to reverse engineer my database and I am just getting into using Freemarker templates to weak the code it generates. The problem is I want to change the name of the DAO classes it generates. By default the DAO classes are named in the form PersonHome however to change the name to PersonDAO i modified the dao/daohome.ftl.
While this did change the generated class name to PersonDAO the java file was still called PersonHome.java.
Is there a place I can also change the generated file name to match the source code?

Ok well I have got to the bottom of it myself. It seems while hibernate tools does support changing the filename the feature is not exposed in the Hibernate tools plugin for Eclipse which is frustrating. Instead I have had to create an ant build script to run the reverse engineering like follows.
<project name="Reverse Engineer" basedir=".">
<path id="toolslib">
<path location="lib/hibernate3.jar" />
<path location="lib/hibernate-tools.jar" />
<path location="lib/freemarker.jar" />
...
<path location="${jdbc.driver.jar}" />
</path>
<taskdef name="hibernatetool"
classname="org.hibernate.tool.ant.HibernateToolTask"
classpathref="toolslib" />
<hibernatetool destdir="src">
<jdbcconfiguration
configurationfile="src/hibernate.cfg.xml"
packagename="my.package.name"
revengfile="hibernate.reveng.xml">
</jdbcconfiguration>
<hbmtemplate destdir="src"
templatepath="templates"
template="dao/daohome.ftl"
filepattern="{package-name}/{class-name}DAO.java">
<property key="ejb3" value="false" />
<property key="jdk5" value="true" />
<property key="sessionFactoryName" value="my.HibernateSessionFactory" />
</hbmtemplate>
</hibernatetool>

Use "Generic Exporter <hibernatetemplate>" tool instead of "DAO Code(.java)".
Along with it set the following attributes in the
templatename[customtemplate.ftl] for this we may use the existing daohome.ftl which is available in hibernate-tools.jar.
filePattern as ${package-name}\${class-name}DAO.java
and required properties like sessionFactoryName.

I didn't look closely at this but I think you'll have to modify the DAONewExporter class (see HBX-343 for some inspiration).

I am adding the bits that were missing.
Use case:
Modify DAO names class generated by hibernate tools
Solution:
I used mvn with ant to do this.A roundabout way but its easier for those who have mvn set up.
<taskdef name="hibernatetool"
classname="org.hibernate.tool.ant.HibernateToolTask">
<classpath>
<fileset dir="${basedir}/lib">
<include name="*.jar"/>
</fileset>
</classpath>
</taskdef>
<target name="gen_hibernate"
description="generate hibernate classes">
<hibernatetool destdir="${basedir}/src/main/java">
<jdbcconfiguration
configurationfile="${basedir}/hibernate.cfg.xml"
packagename="com.bcbsmt.eie.framework.dto"
revengfile="${basedir}/hibernate.reveng.xml">
</jdbcconfiguration>
<hbmtemplate destdir="${basedir}/src/main/java"
templatepath="${basedir}/src/main/resources/template"
template="dao/daohome.ftl"
filepattern="{package-name}/{class-name}DAO.java">
<property key="ejb3" value="false" />
<property key="jdk5" value="true" />
</hbmtemplate>
<hbmtemplate destdir="${basedir}/src/main/java"
templatepath="${basedir}/src/main/resources/template"
template="pojo/Pojo.ftl"
filepattern="{package-name}/{class-name}.java">
<property key="ejb3" value="false" />
<property key="jdk5" value="true" />
</hbmtemplate>
</hibernatetool>
</target>
maven POM:
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.bcbsmt
HibernateAnnnotationSample
0.0.1-SNAPSHOT
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>install</phase>
<configuration>
<target>
<ant antfile="${basedir}/build.xml">
<target name="gen_hibernate" />
</ant>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Jars needed:
These jars should be in ${basedir}/lib folder.Versions are as per your wish
asm-1.5.3.jar
cglib-2.1_3.jar
commons-collections-20030418.083655.jar
commons-logging-1.1.1.jar
dom4j-1.6.1.jar
freemarker-2.3.8.jar
hibernate-3.3.2.jar
hibernate-annotations-3.5.6-Final.jar
hibernate-tools-3.2.4.GA.jar
jtidy-r8-20060801.jar
log4j-1.2.17.jar
slf4j-api-1.5.8.jar
slf4j-log4j12-1.5.8.jar
sqljdbc-4.0.jar

Related

sonar's code coverage using ANt (build.xml)

I am trying to use my junit test for sonar's code coverage. I am using Ant. I am trying to update build.xml like this:
<!-- ========= Define SonarQube Scanner for Ant Target ========= -->
<target name="sonar" depends="compile">
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
<!-- Update the following line, or put the "sonar-ant-task-*.jar" file in your "$HOME/.ant/lib" folder -->
<classpath path="/lib/sonar-ant-task-2.0.jar" />
</taskdef>
<!-- Execute SonarQube Scanner for Ant Analysis -->
<sonar:sonar />
</target>
But it is showing "resource="org/sonar/ant/antlib.xml" not found. I have sonar-ant-task-2.0.jar in my lib folder. and I am using intellij.
This . are the properties i added in build.xml
<property name="sonar.projectKey" value="org.codehaus.sonar:example-java-ant" />
<property name="sonar.projectName" value="SIML project" />
<property name="sonar.projectVersion" value="1.0" />
<property name="sonar.test" value="test" />
Please help :) :)
And let me know if you need any other information to make question clear. :)

I'm trying to utilizing SASS in an ant file while removing ruby dependencies

I am currently using a ruby gem SASS in order to convert my *.scss files to *.css files on a large project. Here is a mockup of the code I am using:
<?xml version="1.0"?>
<!-- scss to CSS -->
<project name="StackOverflowScssCss" default="sass-compile-to-css" basedir=".">
<property file="build.properties" />
<target name="sass-compile-to-css">
<echo message="Compiling scss files to css..." />
<!-- create the css destination dir if it doesn't already exist -->
<property name="css-dest" location="${css.dir}" />
<echo message="Creating directory at ${css.dir} [if it doesn't yet exist]" />
<mkdir dir="${css-dest}" />
<!-- create subdirs if necessary -->
<echo message="Creating css directories (and temporary .css files) for .scss to be compiled..." />
<touch mkdirs="true">
<fileset dir="${sass.dir}" includes="**/*.scss" excludes="**/_*" />
<mapper type="glob" from="*.scss" to="${css.dir}/*.css" />
</touch>
<echo message="Running sass executable against sass files and compiling to CSS directory [${css-dest}] " />
<!-- run sass executable -->
<apply executable="sass" dest="${css-dest}" verbose="true" force="true" failonerror="true">
<arg value="--unix-newlines" />
<!-- Disable creation of map file. THIS SHOULD BE A FLAG -->
<arg value="--sourcemap=none" />
<srcfile />
<targetfile />
<fileset dir="${sass.dir}" includes="**/*.scss" excludes="**/_*" />
<mapper type="glob" from="*.scss" to="*.css" />
</apply>
<echo message="Done compiling scss files!" />
</target>
</project>
Ultimately I want to remove the ruby dependency so I have been looking at this libsass maven plugin. I know that there are many options for libsass but I am trying to stick strictly to Java. Does anyone have any experience with doing this? I don't want to run Node.js, Sass.js ot anything, and I have racked my brain all day on how to do this. Any help is much appreciated!
There is a Java wrapper that can be used with Maven.
If you really want to stick with Java you could either
Use that wrapper and run the mvn task from inside your ant script (see here)
Adapt that wrapper and build your own Ant Task out of it
I ended up using a JRuby dependency in order to run on the JVM. My code is as follows:
<!-- Jruby Dependent SCSS to CSS conversion -->
<path id="JRuby">
<fileset file="packages/jruby-complete-1.7.20.1.jar"/> <!-- Location of JRuby jar file -->
</path>
<target name="compileSass" depends="cleanSass">
<echo message="Compiling scss files..." />
<!-- JRuby Script to convert files into new directory -->
<property name="filesIn" value="${dir.scss}/**/[^_]*.scss" />
<property name="projectDirectory" value="${user.dir}"/>
<script language="ruby" classpathref="JRuby">
<![CDATA[
require ($project.getProperty('projectDirectory')) + '/packages/sass-3.4.14/lib/sass'
require ($project.getProperty('projectDirectory')) + '/packages/sass-3.4.14/lib/sass/exec'
files = Dir.glob($project.getProperty('filesIn'))
files.each do
| file |
newOutDir = File.dirname(file).sub! 'scss', 'css'
FileUtils::mkdir_p newOutDir
puts "[sass compiler] From:" + file
puts "[sass compiler] To:" + newOutDir + "/" + File.basename(file, ".*") + ".css"
opts = Sass::Exec::SassScss.new(["--load-path", File.dirname(file), file, File.join(newOutDir, File.basename(file, ".*") + ".css")], 'scss')
opts.parse
end
]]>
</script>
<echo message="Done compiling scss files!" />
</target>
<target name="cleanSass">
<echo message="removing .css files..." />
<delete includeemptydirs="true" failonerror="false">
<fileset dir="${dir.css}" includes="**/*.css" />
</delete>
<echo message="removing .css.map files..." />
<delete includeemptydirs="true" failonerror="false">
<fileset dir="${dir.css}" includes="**/*.css.map" />
</delete>
</target>

How can I get the Javadoc class descriptions at compile time?

I'm trying to build up some documentation for my Wicket Web Application. I have created a page to grab all of my mounted pages and display them in /sitemap.xml.
In the vein of documentation I've added a new tag to the file <siteMap:Description>
now I want to fill that description with the javadoc entry that describes the class file.
I know there is know direct way to access them at runtime. So Instead I'm hoping to copy them at compile time into a List where they will then be accessible from runtime. How would I do that?
I'm using Maven for my build.
EDIT
I should probably Also mention that I do have an AntTask Already defined as part of my build process to save the compile Dates/times to a property file.
It seems to me an Task to scan my Class and then put the information into a file is probably the way to go. Problem is I'm not sure how to proceed.
My Ant-Task is defined like in my pom.xml so:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<dependencies>
<dependency>
<groupId>ant</groupId>
<artifactId>ant-nodeps</artifactId>
<version>1.6.5</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>set-build-time</id>
<phase>process-sources</phase>
<configuration>
<tasks>
<tstamp>
<format property="build.timestamp" pattern="yyyy/MM/dd HH:mm:ss"/>
<format property="build.time" pattern="HH:mm:ss" />
<format property="build.date" pattern="MM/dd/yyyy" />
<format property="build.year" pattern="yyyy"/>
</tstamp>
<replaceregexp byline="true">
<regexp pattern="copyYear\=.*" />
<!--suppress MavenModelInspection -->
<substitution expression="copyYear=${build.year}" />
<fileset dir="src/main/java/" includes="**/*.properties" />
</replaceregexp>
<replaceregexp byline="true">
<regexp pattern="buildTime\=.*" />
<!--suppress MavenModelInspection -->
<substitution expression="buildTime=${build.date} ${build.time}" />
<fileset dir="src/main/java/" includes="**/*.properties" />
</replaceregexp>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
After doing more research I determined I was barking up the wrong tree.
I since I was trying to get Javadoc comments A Doclet was the better answer.
So I implemented a custom doclet and wired it up to run automatically as described in
the follow up question and answer below.
How can I compile and run my Custom Doclet class in my project?

Ant Nested Update

This is a follow up question related to my question from yesterday:
Ant Ear Update Without Full Exploding Ear
I'm using Ant 1.8.2 and am able to update files in an ear, using the example I made in the provided link above.
I have a war file inside my ear file, and I'm hoping to see if there is a way to do a nested update (e.g. update a file in the war that is in the ear).
My other option is to extract the war, update the war, then update the ear with the updated war. If there is a way to do the nested update, I think it would save me time, as my war file is pretty big.
The following is my alternative approach POC, in case anyone is interested. I will use this if we cannot find a "nested" update.
<property name="ear.file1" value="file1.ear"/>
<property name="war.file1" value="war1.war"/>
<property name="war.file" value="war.war"/>
<property name="war.file.backup" value="warBk.war"/>
<property name="text.file1" value="1.txt"/>
<property name="text.file2" value="2.txt"/>
<property name="xml.application1" value="application.xml"/>
<target name="clean">
<delete file="${ear.file1}"/>
<delete file="${war.file}"/>
<delete file="${war.file.backup}"/>
</target>
<target name="run">
<!-- Our war file contains 1.txt, allows us add 2.txt and verify updates properly -->
<copy file="${war.file1}" tofile="${war.file}"/>
<!-- simple ear that will be updated -->
<ear earfile="${ear.file1}" appxml="${xml.application1}">
<fileset dir="." includes="${text.file1}"/>
<fileset dir="." includes="${war.file}"/>
</ear>
<!-- Backup war, for comparision purposes -->
<move file="${war.file}" tofile="${war.file.backup}" overwrite="true" />
<!-- Extact the war we just added -->
<unzip dest="." src="${ear.file1}" overwrite="true" >
<patternset>
<include name="${war.file}" />
</patternset>
</unzip>
<!-- Update the war by adding a file -->
<war destfile="${war.file}" update="true">
<fileset dir="." includes="${text.file2}"/>
</war>
<!-- Update the ear with our updated war -->
<ear earfile="${ear.file1}" appxml="${xml.application1}" update="true">
<fileset dir="." includes="${war.file}"/>
</ear>
</target>
<target name="main" depends="clean,run"/>

Why is Sonar design view not showing any dependencies for my project?

I've successfully set up Sonar against a large Java project that is built using Ant. I've finally hooked up the JUnit test results and the Cobertura code coverage report.
I now see all the packages in the design view but there are no values for dependencies between any of the packages or classes (see example below).
Does anyone have any idea what I have missed here?
UPDATE
Looking at the output of the Sonar Ant task I also notice that the package design task is completing very quickly for what is a fairly large and complicated project. From the Ant output:
[sonar:sonar] [INFO] Package design analysis...
[sonar:sonar] [INFO] Package design analysis done: 66 ms
The Ant task is as follows:
<target name="sonar" depends="collate-xml-reports">
<sonar:sonar workDir="src/build/sonarTemp" key="myProjectKeyWhichHasBeenChangedToHideMyClient" version="1.0" xmlns:sonar="antlib:org.sonar.ant">
<!-- source directories (required) -->
<sources>
<path location="src/common/src" />
<path location="src/commonWidgets/src" />
<path location="src/compositionWidget/src" />
<path location="src/nativeLib/src" />
<path location="src/services/src" />
</sources>
<!-- list of properties (optional) -->
<property key="sonar.projectName" value="RPS Nightly" />
<property key="sonar.dynamicAnalysis" value="reuseReports" />
<property key="sonar.surefire.reportsPath" value="src/reports/junit" />
<property key="sonar.cobertura.reportPath" value="src/reports/cobertura/coverage.xml" />
<!-- test source directories (optional) -->
<tests>
<path location="src/common/test" />
<path location="src/commonWidgets/test" />
<path location="src/compositionWidget/test" />
<path location="src/services/test" />
</tests>
<!-- binaries directories, which contain for example the compiled Java bytecode (optional) -->
<binaries>
<path location="src/common/build" />
<path location="src/commonWidgets/build" />
<path location="src/compositionWidget/build" />
<path location="src/services/build" />
</binaries>
<!-- path to libraries (optional). These libraries are for example used by the Java Findbugs plugin -->
<libraries>
<path location="src/common/lib/**/" />
<path location="src/commonWidgets/lib/**/" />
<path location="src/compositionWidget/lib/**/" />
<path location="src/services/lib/" />
</libraries>
</sonar:sonar>
</target>
Being a Java n00b it turns out that this was a misunderstanding on my part. The binaries directory locations need to point to the root folder of the compiled Java bytecode but I was pointing it to the jar file location. In this case the bytecode is written to the build/classes folder so I needed to modify my ant target (shown in the question) from:
<binaries>
<path location="src/common/build" />
<path location="src/commonWidgets/build" />
<path location="src/compositionWidget/build" />
<path location="src/services/build" />
</binaries>
To:
<binaries>
<path location="src/common/build/classes" />
<path location="src/commonWidgets/build/classes" />
<path location="src/compositionWidget/build/classes" />
<path location="src/services/build/classes" />
</binaries>
This fixed the problem and the design view is now correctly populated.

Categories

Resources