Creating DDL scripts from JPA/Hibernate Annotation Classes Using ANT - sql

I would like to generate SQL DDL scripts from Hibernate/JPA Annotation Classes using ANT.
Below is the ANT script that I wrote based on Hibernate Dev Docs URL: http://docs.jboss.org/hibernate/orm/4.1/devguide/en-US/html_single/
<project name="yourmarketnet" default="all" basedir=".">
<target name="ddl_generation">
<!-- paths to required jars -->
<path location="web/WEB-INF/lib/hibernate-annotations.jar" />
<path location="web/WEB-INF/lib/ejb3-persistence.jar" />
<path location="web/WEB-INF/lib/hibernate-entitymanager.jar" />
<path location="web/WEB-INF/lib/javaassist.jar" />
<path location="web/WEB-INF/lib/hibernate-tools.jar"/>
<taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask.jar">
<classpath path="${build.dir}/web/WEB-INF/lib/hibernate-tools.jar"/>
</taskdef>
<!-- output destination -->
<hibernatetool destdir="${build.dir}">
<!-- were the annotation beans files are located-->
<classpath>
<path location="${build.dir}/web/WEB-INF/classes/com/yourmarketnet/beans" />
</classpath>
<!-- list exporters here -->
<hbm2ddl
export="false"
update="false"
drop="true"
create="true"
outputfilename="myApps.ddl"
delimiter=";"
format="false"
haltonerror="true"/>
</hibernatetool>
</target>
</project>
Im getting ERROR taskdef class org.hibernate.tool.ant.HibernateToolTask cannot be found
using the classloader AntClassLoader[]
I also checked to see if hibernate-tools.jar was actually in the path and it was (C:\Users\naim\Documents\NetBeansProjects\yourmarketnet\build\web\WEB-INF\lib\hibernate-tools.jar)
Can someone please tell me step by step how to fix/debug this issue , thanks.

Error message clearly tells you that ant can't find your tools. When you declare
<taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask" >
<classpath path="${build.dir}/web/WEB-INF/lib"/>
</taskdef>
it's already wrong because tool classes are not in directory but in jar. Secondly, your intent seems to be to use ${build.dir} as output location, so why would it contain tool classes? Anyway, you didn't even define this directory.
So if you really have your hibernate-tools.jar in web/WEB-INF/lib, you probably want something like
<taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask" >
<classpath path="web/WEB-INF/lib/hibernate-tools.jar"/>
</taskdef>
Note that it's relative to your project directory.

Related

ant junit selenium wont run in jenkins

I have junit tests that use selenium to test web server.
When i run the tests using ant from command line, everything is working fine, browser gets opened and tests are going as planed.Browser gets open and i can see tests running.
Recently ive tried to add automatic tests as part of Ci cycle running on jenkins.
I run it as ant build command.
I can see that ant is executing properly (test classes are built i can see output from tests to console) but browser window never gets opened and test fails because of it. here is my ant file
<?xml version="1.0"?>
<project name="JUNIT" default="main" basedir="../../project" xmlns:artifact="antlib:org.apache.maven.artifact.ant">
<!-- Sets variables which can later be used. -->
<!-- The value of a property is accessed via ${} -->
<property name="api.dir" location="src/java" />
<property name="build.api.dir" location="target/classes" />
<property name="test.dir" location="src/test/java" />
<property name="build.test.dir" location="target" />
<!-- Variables used for JUnit testin -->
<property name="test.report.dir" location="testreport" />
<!-- Define the classpath which includes the junit.jar and the classes after compiling-->
<path id="api.class.path">
<pathelement location="${build.api.dir}" />
</path>
<artifact:dependencies cacheDependencyRefs="true" pathId="pomdeps.path">
<pom file="pom.xml"/>
</artifact:dependencies>
<target name="clean">
<delete dir="${test.report.dir}" />
<delete dir="${build.api.dir}" />
<delete dir="${build.test.dir}" />
</target> <!-- Creates the build, docs and dist directory-->
<target name="makedir">
<echo message="Make dir"/>
<mkdir dir="${build.test.dir}" />
<mkdir dir="${build.api.dir}" />
<mkdir dir="${test.report.dir}" />
</target> <!-- Compiles the java code (including the usage of library for JUnit -->
<target name="compile" depends="clean, makedir">
<echo message="Compile"/>
<javac srcdir="${api.dir}" destdir="${build.api.dir}" includeantruntime="false">
<!--classpath refid="junit.class.path" />
<classpath refid="libs.class.path" /-->
<classpath refid="pomdeps.path" />
</javac>
<javac srcdir="${test.dir}" destdir="${build.test.dir}" includeantruntime="false">
<!--classpath refid="junit.class.path" /-->
<classpath refid="api.class.path" />
<classpath refid="pomdeps.path" />
</javac>
</target>
<!-- Run the JUnit Tests --> <!-- Output is XML, could also be plain-->
<echo message="Classes folder ${build.test.dir}"/>
<target name="junit" depends="compile" >
<echo message="junit"/>
<junit printsummary="on" fork="false" haltonfailure="no" showoutput="true">
<classpath refid="pomdeps.path" />
<classpath>
<pathelement location="${build.test.dir}"/>
<pathelement location="${build.api.dir}"/>
</classpath>
<formatter usefile="false" type="plain"/>
<batchtest fork="no" todir="${test.report.dir}">
<fileset dir="${test.dir}">
<include name="**/*Test*.java"/>
</fileset>
</batchtest>
</junit>
</target>
<target name="main" depends="compile, junit">
<description>Main target</description>
</target>
</project>
Jenkins is 1.591 i installed it with default parameters as windows installation downloaded from their site.
Can it be something wrong with jenkins? Do i miss something?
As i mentioned earlier the problem was lack of UI permissions for Jenkins server.
1.configure Jenkins to run as service and make it login with real user name
2.Make sure that windows host that runs Jenkins logs on automaticaly after restart.

Sonar Integration Tests Coverage not show + Selenium + Jboss +Jacoco

I use Sonar 4.1.1, Jboss 6.x, Jacoco 0.6.4, execute tasks with Ant I am not allowed to use Maven. In an eclipse workspace, I have two projects, one is the web application another is selenium test.
I am able to get unit test and code coverage for unit test. But sonar is not able to read the integration test file created by Jacoco. I think there might be something wrong with the way I create jacoco-it.exec file so sonar can't read it. Because sonar does read my jacoco-ut.exec file. And I am able to have both reportPath and itReportPath to read my jacoco-ut.exec file with no problem. Also thinking maybe is something wrong in my build file. I did a lot of research and tried many different ways to create the jacoco-it.exec file, different Jacoco settings and followed different examples from sonar, jacoco, other blogs but still doesn't work. I must be missing something Help!! Thanks!!
I have VM arguments for Jboss like this
-javaagent:/path to jar/jacocoagent.jar=destfile=/path for create/jacoco-it.exec
When I run selenium, the above code create a file with some data, size about 1.3MB
Here is the part of build relate to this issue
<property name="sonar.sourceEncoding" value="UTF-8" />
<property name="sonar.java.coveragePlugin" value="jacoco" />
<property name="sonar.core.codeCoveragePlugin" value="jacoco" />
<property name="sonar.dynamicAnalysis" value="reuseReports" />
<property name="sonar.jacoco.reportsPath" value="${reports.dir}/junit" />
<property name="sonar.jacoco.itReportPath" value="${reports.dir}/jacoco-it.exec" />
<property name="sonar.jacoco.reportPath" value="${reports.dir}/jacoco-ut.exec" />
<target name="unitTest" depends="compile">
<taskdef name="junit" classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask">
<classpath>
<path refid="classpath"/>
</classpath>
</taskdef>
<!-- Import the JaCoCo Ant Task -->
<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
<classpath refid="classpath"/>
</taskdef>
<!-- Run your unit tests, adding the JaCoCo agent -->
<jacoco:coverage destfile="reports/jacoco-ut.exec" xmlns:jacoco="antlib:org.jacoco.ant">
<junit printsummary="yes" haltonfailure="yes" forkmode="once" fork="true" dir="${basedir}" failureProperty="test.failed">
<classpath location="${classes.dir}" />
<classpath refid="classpath"/>
<formatter type="plain" />
<formatter type="xml" />
<batchtest fork="true" todir="${reports.junit.xml.dir}">
<fileset dir="src">
<include name="**/*TestAdd.java" />
</fileset>
</batchtest>
</junit>
</jacoco:coverage>
</target>
<target name="coverageTest" depends="compile">
<taskdef name="junit" classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask">
<classpath>
<path refid="classpath"/>
</classpath>
</taskdef>
<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
<classpath refid="classpath"/>
</taskdef>
<!--Run your unit tests, adding the JaCoCo agent-->
<jacoco:coverage xmlns:jacoco="antlib:org.jacoco.ant" dumponexit="true" >
<junit printsummary="yes" haltonfailure="yes" forkmode="once" fork="true" dir="${basedir}" failureProperty="test.failed">
<classpath location="${classes.dir}"/>
<classpath refid="classpath"/>
<formatter type="plain" />
<formatter type="xml" />
<formatter type="plain" usefile="false"/>
<batchtest todir="${reports.junit.xml.dir}">
<fileset dir="../HelloAppTest/src">
<include name="**/answerTest.java"/>
</fileset>
</batchtest>
</junit>
</jacoco:coverage>
</target>
The reason for that is, may be you are NOT attaching Jacocoagent.jar file to the "TARGET" (ex: JBoss / Tomcat) JVM's scope and stopping it so that it can flush the final code coverage data to the jacoco it exec file.
Once you do that (instead of using Maven/ANT's JVM scope), run your non-Unit (IT) tests and then STOP the target JVM.
After the target JVM is stopped, you'll get the final jacoco .exec file genreated for the IT tests. Use that file for sonar.jacoco.itReportPath variable and it'll work.
For ex: I pass/have this variable to the Tomcat's startup.sh script and while starting tomcat (target JVM), I use this variable within the Tomcat's actual start command.
PROJ_EXTRA_JVM_OPTS=-javaagent:tomcat/jacocoagent.jar=destfile=build/jacoco/IT/jacocoIT.exec,append=false

How can I specify the classpath in the build file of an ftp tasks

I would like to specify the classpath for an fpt task directly in my Ant Build script. I tried the following:
<target name="ftp-upload" depends="build-html">
<echo message="Ftp upload started with user ${user}" />
<ftp verbose="yes"
remotedir="${ftp.dir}"
server="${server}"
userid="${user}"
password="${password}"
depends="yes">
<fileset dir="${mystuff.dir}/.." />
<classpath refid="build.classpath" />
</ftp>
</target>
and
<target name="ftp-upload" depends="build-html">
<echo message="Ftp upload started with user ${user}" />
<ftp verbose="yes"
remotedir="${ftp.dir}"
server="${server}"
userid="${user}"
password="${password}"
depends="yes"
classpathref="build.classpath"
>
<fileset dir="${mystuff.dir}/.." />
</ftp>
</target>
The first approach gives me the following error message:
Could not create type ftp due to java.lang.NoClassDefFoundError: org/apache/commons/net/ftp/FTPClientConfig
The second approach gives me the following error message:
ftp doesn't support the "classpathref" attribute
Is it possible to set the classpath in the build script for the ftp tasks or do I have to do it outside the build script on my server? Would be nice to have the build script self-contained.
Solution:
Just re-define the ftp task with your classpath reference:
<taskdef name="ftp" classname="org.apache.tools.ant.taskdefs.optional.net.FTP">
<classpath>
<pathelement location="\lib\commons-net-1.4.0.jar"/>
</classpath>
</taskdef>
Have you tried the solution commented at How to load an optional task into ant without -lib or global installation? ?
I had a similar problem some time ago and I solved it adding a new classloader.
Regards

How to do OpenJPA enhancement with Ant + IntelliJ IDEA

This is driving me crazy and I'm shocked that official documentation is absolutely useles.
Here is what I have:
IntelliJ IDEA 11
OpenJPA 2.1.1
Since openjpa is added into list of used libraries I already had classpath to OpenJPA which looks like this
<path id="library.openjpa.classpath">
<fileset dir="${basedir}/lib/openjpa">
<patternset refid="library.patterns"/>
</fileset>
</path>
According to official documentation I added following target
<target name="enhance">
<copy includeemptydirs="false" todir="${basedir}/lib/openjpa">
<fileset dir="src" excludes="**/*.launch, **/*.java"/>
</copy>
<taskdef name="openjpac" classname="org.apache.openjpa.ant.PCEnhancerTask">
<classpath refid="library.openjpa.classpath"/>
</taskdef>
<openjpac>
<classpath refid="library.openjpa.classpath"/>
</openjpac>
</target>
It gives me exception
C:\work\prj\build.xml:283: org.apache.openjpa.util.MetaDataException:
MetaDataFactory could not be configured
(conf.newMetaDataFactoryInstance() returned null). This might mean
that no configuration properties were found. Ensure that you have a
META-INF/persistence.xml file, that it is available in your classpath,
or that the properties file you are using for configuration is
available. If you are using Ant, please see the or
attributes of the task's nested element.
This can also occur if your OpenJPA distribution jars are corrupt, or
if your security policy is overly strict.
I tested with Process Monitor and can see that it opens and reads persistence.xml.
Some person filed bug having problems I have and the answer he got was that finding persistence.xml is not a source of problem.
Questions are:
What can I do to make it work ?
Can I make it work by skipping need for persistence.xml and just specifying pattern for .class files I want to be enhanced ?
It's more Ant question. How can I make OpenJPA enhancer to look for persistence.xml in directory other than where openjpa-2.1.1.jar resides ?
So I couldn't make it work without undocumented propertiesFile. Here is version that works for me. Also specifying persistence-unit via # makes it fail with NullReferenceException.
<target name="enhance">
<taskdef name="openjpac" classname="org.apache.openjpa.ant.PCEnhancerTask">
<classpath refid="library.openjpa.classpath"/>
</taskdef>
<openjpac>
<classpath refid="library.openjpa.classpath"/>
<classpath location="${reporting.output.dir}"/>
<config propertiesFile = "${basedir}/src/META-INF/persistence.xml"/>
</openjpac>
</target>
It appears that you might have missed an important part from the documentation. Your library.openjpa.classpath is missing a reference to your Entities, and the location of the persistence.xml file. Try adding that and see how it goes.
<path id="jpa.enhancement.classpath">
<pathelement location="bin"/> <!-- add something like this -->
<!-- lib contains all of the jars that came with the OpenJPA binary download -->
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
</path>

How to run liquibase on linux system

I am working with Liquibase on Linux, does anyone know how to run the datbasechangelog.xml file from the Linux prompt step by step? And what's the idea behind databasechangelog and how it works?
For our projects, we have set up ant tasks to do this. So, for example, if you want to run the migrations, the ant file may look like the following:
ant-migrations.xml
<project name="Migrations" basedir="." default="update-database">
<property file="./liquibasetasks.properties" />
<path id="master-classpath" description="Master classpath">
<fileset dir="..\lib">
<include name="*.jar" />
</fileset>
</path>
<target name="update-database">
<fail unless="db.changelog.file">db.changelog.file not set</fail>
<fail unless="database.url">database.url not set</fail>
<fail unless="database.username">database.username not set</fail>
<fail unless="database.password">database.password not set</fail>
<taskdef resource="liquibasetasks.properties">
<classpath refid="master-classpath"/>
</taskdef>
<updateDatabase
changeLogFile="${db.changelog.file}"
driver="${database.driver}"
url="${database.url}"
username="${database.username}"
password="${database.password}"
promptOnNonLocalDatabase="${prompt.user.if.not.local.database}"
dropFirst="false"
classpathref="master-classpath"
/>
</target></project>
Make sure your liquibase jar file(s) are referenced in the classpath element.
The properties file contains the references that are particular to your environment:
liquibasetasks.properties
db.changelog.file=YOUR_MIGRATION_FILE.xml
#################################
## DB Settings
#################################
database.driver=
database.username=
database.password=
database.url=
Ok, so now we have the ant task set up and configured.. with all of that saved, you should be able to run the migration by typing the following at the command prompt:
linux>ant -f ant-migrations.xml update-database
Hope that helps!