So, I'm hoping this is a simple question. I've downloaded openjpa-all-2.3.0.jar (and mysql-connector-java-5.1.31-bin.jar) So, I have this as my build.xml
<?xml version="1.0" ?>
<project name="reverser" default="reversemap" basedir=".">
<taskdef name="reversemappingtool" classname="org.apache.openjpa.jdbc.ant.ReverseMappingToolTask">
<classpath>
<pathelement location="${basedir}/mysql-connector-java-5.1.31-bin.jar" />
<pathelement location="${basedir}/openjpa-all-2.3.0.jar" />
</classpath>
</taskdef>
<target name="reversemap">
<reversemappingtool package="com.whatever.dbresults" directory="${basedir}/src" customizerProperties="${basedir}/conf/reverse.properties" metadata="none" generateAnnotations="true" />
</target>
</project>
In my reverse.properties file, I have:
ConnectionDriverName=com.mysql.jdbc.Driver
ConnectionDriverURL=jdbc:mysql://localhost:3306/db
ConnectionUser=user
ConnectionPassword=pw
when I run it tho, I get:
/tmp/apache-openjpa-2.3.0/build.xml:10: <openjpa-2.3.0-r422266:1540826 fatal user error> org.apache.openjpa.util.UserException: The persistence provider is attempting to use properties in the persistence.xml file to resolve the data source. A Java Database Connectivity (JDBC) driver or data source class name must be specified in the openjpa.ConnectionDriverName or javax.persistence.jdbc.driver property. The following properties are available in the configuration: "org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl#ec63420c".
at org.apache.openjpa.jdbc.schema.DataSourceFactory.newDataSource(DataSourceFactory.java:72)
at org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl.createConnectionFactory(JDBCConfigurationImpl.java:849)
at org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl.getConnectionFactory(JDBCConfigurationImpl.java:732)
at org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl.getDataSource(JDBCConfigurationImpl.java:878)
at org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl.getDataSource2(JDBCConfigurationImpl.java:920)
at org.apache.openjpa.jdbc.schema.SchemaGenerator.<init>(SchemaGenerator.java:85)
at org.apache.openjpa.jdbc.meta.ReverseMappingTool.run(ReverseMappingTool.java:2009)
at org.apache.openjpa.jdbc.ant.ReverseMappingToolTask.executeOn(ReverseMappingToolTask.java:295)
This is probably the wrong way of debugging this problem, but tracked the properties file being loaded, at ReverseMappingToolTask.java line 282, and it then passes that to
flags.customizer.setConfiguration(customProps);
before calling
ReverseMappingTool.run(conf, files, flags, loader);
The error tho, seems to happen at line 2009 of ReverseMappingTool, at a point where it never seems to even touch the "flags".
Well. My "debugging" is probably a non-sequitur/red-herring/what have you. How can I get the reverse mapping to work in the simplest way possible? I just want to use it with ant, and not maven, etc.
EDIT 2014-08-13
OK, I managed to move the error along by changing my build.xml to
<?xml version="1.0" ?>
<project name="reverser" default="reversemap" basedir=".">
<taskdef name="reversemappingtool" classname="org.apache.openjpa.jdbc.ant.ReverseMappingToolTask">
<classpath>
<pathelement location="${basedir}/mysql-connector-java-5.1.31-bin.jar" />
<pathelement location="${basedir}/openjpa-all-2.3.0.jar" />
</classpath>
</taskdef>
<target name="reversemap">
<reversemappingtool package="com.whatever.dbresults" directory="${basedir}/src" metadata="none" generateAnnotations="true">
<config
connectionDriverName="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/db"
connectionUserName="user"
connectionPassword="pw"
/>
</reversemappingtool>
</target>
</project>
But, then now, I get:
<openjpa-2.3.0-r422266:1540826 fatal user error> 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 <properties> or <propertiesFile> attributes of the task's nested <config> element. This can also occur if your OpenJPA distribution jars are corrupt, or if your security policy is overly strict.
Thanks!
OK, well, I finally got it working. Of course, I don't know if this is the "right" way or not. But, it generates my classes, and that's really all I wanted.
So, my new build file looks like this:
<?xml version="1.0" ?>
<project name="reverser" default="reversemap" basedir=".">
<taskdef name="reversemappingtool" classname="org.apache.openjpa.jdbc.ant.ReverseMappingToolTask">
<classpath /> <!-- removed classpath for the sake of making this post short -->
</taskdef>
<target name="reversemap">
<reversemappingtool package="com.whatever.db" directory="${basedir}/src" metadata="none" generateAnnotations="true">
<config propertiesFile="persistence.xml" />
</reversemappingtool>
</target>
</project>
And, my persistence.xml looks like
<?xml version="1.0"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="openjpa">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<properties>
<property name="openjpa.ConnectionURL" value="jdbc:mysql://localhost:3306/db"/>
<property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver"/>
<property name="openjpa.ConnectionUserName" value="user"/>
<property name="openjpa.ConnectionPassword" value="password"/>
<property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO"/>
</properties>
</persistence-unit>
</persistence>
So. I think that does it. Hopefully if someone else comes along and wants to know how to do reversemap with just ant and java, this will help.
Related
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 am trying to read a property from properties file and assign to a different name but it is not working. I am new to ant so I guess I am missing something basic.
build.properties:
USERNAME=deter_dangler
build.xml:
<project name="Simple Ant example" default="test" basedir=".">
<property file="build.properties"/>
<property environment="env"/>
<property name="uname" value="${env.USERNAME}"/>
<target name="test">
<echo message="uname property value is ${uname}"/>
<echo message="env.USERNAME property value is ${env.USERNAME}"/>
</target>
</project>
The output when I run the build command:
javanoob#DELL:~/Desktop$ ant
Buildfile: /Desktop/build.xml
test:
[echo] uname property value is ${env.USERNAME}
[echo] env.USERNAME property value is ${env.USERNAME}
BUILD SUCCESSFUL
Total time: 0 seconds
Trying setting the environment variable as follows:
USERNAME=deter_dangler ant
Alternatively, if you want to use a properties file then simplify your ANT file as follows:
<project name="Simple Ant example" default="test" basedir=".">
<property file="build.properties"/>
<target name="test">
<echo message="uname property value is ${USERNAME}"/>
</target>
</project>
I believe that it could be simple incorrect environment variable being used i.e., by default USER is available on the linux machine without requiring to be explicitly defined by user and you are using USERNAME. I assume this is what user is expecting as he is trying to print value from property and another one from system defined default.
So, just replacing the above mentioned changes in the build.properties and build.xml files.
build.properties
USER=deter_dangler
build.xml
<project name="Simple Ant example" default="test" basedir=".">
<property file="build.properties"/>
<property environment="env"/>
<property name="uname" value="${USER}"/>
<target name="test">
<echo message="uname property value is ${uname}"/>
<echo message="env.USER property value is ${env.USER}"/>
</target>
</project>
Output
Buildfile: /home/apps/Documents/so/34553709/build.xml
test:
[echo] uname property value is deter_dangler
[echo] env.USER property value is apps
BUILD SUCCESSFUL
Total time: 1 second
In the above, if you notice, ${USER} is taken from property file and ${env.USER} from system logged in user.
Following the example given at http://www.jooq.org/doc/2.4/manual/META/Configuration/#N10607 on how to run the jooq code-generation I ran into the problem that the build fails with the message:
codegen.xml:7: taskdef class org.jooq.util.GenerationTask cannot be found
For a reference, here is codegen.xml
<project name="..." default="generate-test-classes"
basedir=".">
<property name="path.to.jooq.distribution" value="${basedir}/libs"/>
<property name="path.to.mysql.driver" value="${basedir}/libs"/>
<property name="mysql.driver" value="mysqlcon"/>
<!-- Task definition -->
<taskdef name="generate-classes" classname="org.jooq.util.GenerationTask">
<classpath>
<fileset dir="${path.to.jooq.distribution}">
<include name="jooq.jar" />
<include name="jooqmeta.jar" />
<include name="jooqcodegen.jar" />
</fileset>
<fileset dir="${path.to.mysql.driver}">
<include name="${mysql.driver}.jar" />
</fileset>
</classpath>
</taskdef>
<!-- Run the code generation task -->
<target name="generate-test-classes">
<generate-classes jdbcurl="jdbc:mysql://localhost:3306/crawler"
jdbcuser="..." jdbcpassword="..." generatordatabaseinputschema="..."
generatortargetpackage="model.persistence.jooq"
generatortargetdirectory="${basedir}/src" />
</target>
</project>
I triple checked the definition of the classpath and every file listed exists under the given location. So what would be the problem? Am I missing something? Do I need to configure ant somewhere to recognize the task?
Since I already checked ant: failed to create task or type, I tried to move the taskdef inside the target, but to no avail.
The ant task was an example implementation in jOOQ 2.x. It has been deprecated a long time ago and removed from jOOQ 3.0:
http://www.jooq.org/notes.php?version=3.0
https://github.com/jOOQ/jOOQ/issues/1118
http://www.jooq.org/doc/3.1/manual/code-generation/codegen-configuration/#N12E23
The version of the manual that you've linked is quite outdated.
I am using the following demonstration script:
<?xml version="1.0" encoding="UTF-8"?>
<project name="test" basedir="." xmlns:deploy="antlib:net.sf.antcontrib">
<target name="default">
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="lib/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
<deploy:if>
<isset property="defaultprop"/>
<then>
<echo message="it's set!"/>
</then>
</deploy:if>
</target>
</project>
When I run this build script (with target default), the error is
build.xml:9: Problem: failed to create task or type antlib:net.sf.antcontrib:if
The pathelement lib/ant-contrib-1.0b3.jar exists, and ant is picking it up. I'm thinking the problem is how I'm using the xmlns. I'm taking this from another example that I have that also doesn't work for me (it works on a particular server, though!), and trying to figure out what the magic sauce is.
Your taskdef where you're adding ant-contrib needs to declare a URI the same as the namespace you're defining and prefixing in the project. Similar to how the taskdef over here works.
<project name="test" basedir="." xmlns:deploy="antlib:net.sf.antcontrib">
<target name="default">
<taskdef uri="antlib:net.sf.antcontrib" resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="lib/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
<deploy:if>
<isset property="defaultprop"/>
<then>
<echo message="it's set!"/>
</then>
</deploy:if>
</target>
</project>
Well, the error has if on the end, and it's talking about line 9. I think it's a problem with the syntax of this tag:
<deploy:if>
I can't find any documentation on a "deploy:if" tag, or even a "deploy" tag. I think there is no 'deploy' task in Ant - you need to make a 'deploy' target.
How about trying this:
<if>
<isset property="defaultprop"/>
<then>
<antcall target="deploy" />
</then>
</if>
As I read it, this will check the isset, then call the "deploy" target if it's set.
Of course, you need to make the 'deploy' target now :)
I would like to get verbose console output while building from eclipse and hudson.
There seems to be no verbose property for <target> and <project> and it seems very wrong to call <exec> on ant from inside the script just to pass the verbose prop.
Is there a better way?
You could use Ant's <record> task (http://ant.apache.org/manual/Tasks/recorder.html) to get verbose logging to a file. If this task is defined early in the build file, you should get logging for all build tasks. You could also start and stop the recorder anywhere in your build file. This could, for example, allow you to not log the output of some task that you do not want to see in the log file.
Here's an example of a simple build file that uses the <record> task:
<?xml version="1.0" encoding="UTF-8"?>
<project default="all" basedir=".">
<record name="build.log" loglevel="verbose" action="start" />
<target name="all">
<path id="all.files">
<fileset dir="." includes="**/*" />
</path>
<property name="files" refid="all.files" />
<echo level="verbose">files=${files}</echo>
</target>
</project>
It will be an eclipse External Tools Configuration parameter (under Run -> External Tools). Please see the screenshot below: