Usage

To use the Torque Maven 2 plugin in your project, you need to configure it in the pom.xml of your project. Here is an example:

...
<build>
  <plugins>
     <plugin>
      <groupId>org.apache.db.torque</groupId>
      <artifactId>torque-maven-plugin</artifactId>
      <configuration>
        <targetDatabase>mysql</targetDatabase>
        <targetPackage>org.apache.torque.test</targetPackage>
        <driver>org.gjt.mm.mysql.Driver</driver>
        <url>jdbc:mysql://localhost:3306/bookstore</url>
        <user>root</user>
        <password></password>
      </configuration>
      <executions>
        <execution>
          <phase>generate-sources</phase>
          <goals>
            <goal>om</goal>
            <goal>documentation</goal>
          </goals>
        </execution>
      </executions>
      <dependencies>
        <dependency>
          <artifactId>mysql-connector-java</artifactId>
          <groupId>mysql</groupId>
          <version>3.1.12</version>
        </dependency>
      </dependencies>
    </plugin>
  </plugins>
</build>
...

Generating code

In the example configuration above, the goals om and documentation are specified. Therefore, the om java classes and the html documentation of the table structure will be generated in the "generate sources" phase. This phase will be executed e.g. when running mvn compile or mvn package.

The om classes are generated in two locations:

  • The base classes (which should not be modified by the user) are generated by default in the directory target/generated-sources/torque. They will be deleted if the command mvn clean is run.
  • The non-base classes (which can be modified by the user) will be generated ba default in the directory src/main/generated-java. Once generated, they will not be overwritten and will also not be deleted if mvn clean is run.
Both paths will be added to the compile source path of maven 2, so the jave files in these directories will be compiled along with the other java source.

The documentation is generated per default in the directory target/generated-docs/torque.

Talking to the database

To allow the Torque maven 2 plugin to communicate with the database, the correct database url, database user and password must be given in the Torque Maven 2 plugin's configuration. Also, the correct database driver must be added as a dependency of the plugin. See the example configuration above how this is done.

For example, one can use the Torque Maven 2 plugin to create the tables corresponding to the om classes. Usually, one does not want to do this every time the project is compiled or built, so specifying these goals in the pom.xml makes no sense. Instead, one can execute the command

mvn torque:sql torque:sqlExec

in the project's home directory to generate the sql for creating the database table and afterwards executing the sql.