javamavenapache-torque

How to define project structure correctly when dealing with torque


I have my maven file defined as follows to generate source code from apache torque.

 <build>
<plugins>
  <plugin>
    <groupId>org.apache.torque</groupId>
    <artifactId>torque-maven-plugin</artifactId>
    <version>4.0-beta1</version>
    <executions>
      <execution>
        <id>generate-sources</id>
        <phase>generate-sources</phase>
        <goals>
          <goal>generate</goal>
        </goals>
        <configuration>
          <packaging>classpath</packaging>
          <configPackage>org.apache.torque.templates.om</configPackage>
          <sourceDir>src/main/schema</sourceDir>
          <defaultOutputDir>src/main/java/generated-java</defaultOutputDir>
          <options>
            <torque.om.package>com.project.om</torque.om.package>
            <torque.database>mysql</torque.database>
          </options>
        </configuration>
      </execution>
      <execution>
        <id>generate-sql</id>
        <phase>generate-sources</phase>
        <goals>
          <goal>generate</goal>
        </goals>
        <configuration>
          <packaging>classpath</packaging>
          <configPackage>org.apache.torque.templates.sql</configPackage>
          <sourceDir>src/main/schema</sourceDir>
          <defaultOutputDir>src/main/resources/generated-sql</defaultOutputDir>
          <defaultOutputDirUsage>none</defaultOutputDirUsage>
          <options>
            <torque.database>mysql</torque.database>
          </options>
        </configuration>
      </execution>
    </executions>
    <dependencies>
      <dependency>
        <groupId>org.apache.torque</groupId>
        <artifactId>torque-templates</artifactId>
        <version>4.0-beta1</version>
      </dependency>
    </dependencies>
  </plugin>

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>sql-maven-plugin</artifactId>
    <version>1.4</version>
    <configuration>
      <driver>org.gjt.mm.mysql.Driver</driver>
      <url>jdbc:mysql://localhost:3306/daydiary</url>
      <username>root</username>
      <password>mysql</password>
      <onError>continue</onError>
      <autocommit>true</autocommit>
      <fileset>
        <basedir>${basedir}/target/generated-sql</basedir>
        <includes>
          <include>*.sql</include>
        </includes>
      </fileset>
    </configuration>
    <dependencies>
      <dependency>
        <artifactId>mysql-connector-java</artifactId>
        <groupId>mysql</groupId>
        <version>5.0.4</version>
      </dependency>
    </dependencies>
  </plugin>

  <plugin>
    <!-- setting java version to 1.5 --> 
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
      <source>1.5</source>
      <target>1.5</target>
    </configuration>
  </plugin>

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
      <execution>
        <id>copy-dependencies</id>
        <phase>package</phase>
        <goals>
          <goal>copy-dependencies</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
</plugins>

For this maven creates the following folder structure as shown in the picture I need every code generated under src/main/java folder.How can i achieve that.?

enter image description here


Solution

  • Your generated classes are going to <defaultOutputDir>src/main/java/generated-java</defaultOutputDir> but all folders under src/main/java in maven are treated as package names. So It thinks that generated-java is a package name, but In classes the package name is specified as com.project.om and thats why it shows as an error in you IDE.

    I'm not sure how this plugin works but you could try changing the output directory to 'src/main/java' It might not work because plugin might be cleaning this dir before generating src files and you don't want your files to be deleted.

    Other option would be generate those files outside the src/main/java folder (in default location for example) and then: copy-override those files to the src/main/java directory for example with maven-antrun-plugin add the location of those generated files as a source directory with build-helper-maven-plugin.