5

I am trying to build my pom.xml so that I can automatically create my database schema when running 'mvn install'. I'm using the "maven-cayenne-plugin" to do this. This is plugin is being called (at the integration-test phase), as I can see the output. But the mojo fails with the exception: (I used the -e and -X flag to see this).

java.lang.ClassNotFoundException: org.apache.derby.jdbc.ClientDriver

(I get the same if I try and use the EmbeddedDriver and whether or not I include 'derbyclient' or simply 'derby' as my dependency).

Here's a pom.xml that should replicate the issue. I'm using MVN 3 on Windows. [ Apache Maven 3.0.4 (r1232337; 2012-01-17 08:44:56+0000) ]

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany</groupId>
    <artifactId>myproject</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.cayenne.plugins</groupId>
                <artifactId>maven-cayenne-modeler-plugin</artifactId>
                <version>3.2M1</version>
            </plugin>
            <plugin>
                <groupId>org.apache.cayenne.plugins</groupId>
                <artifactId>maven-cayenne-plugin</artifactId>
                <version>3.2M1</version>
                <executions>
                    <execution>
                        <id>cgen</id>
                        <configuration>
                            <superPkg>com.mycompany.model.generated</superPkg>
                            <map>${project.build.sourceDirectory}/../resources/datamap.map.xml</map>
                            <destDir>${project.build.sourceDirectory}</destDir>
                        </configuration>
                        <goals>
                            <goal>cgen</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>cdbgen</id>
                        <configuration>
                            <map>${project.build.sourceDirectory}/../resources/datamap.map.xml</map>
                            <driver>org.apache.derby.jdbc.ClientDriver</driver>
                            <url>jdbc:derby:memory:tracedb;create=true</url>
                            <username>test</username>
                        </configuration>
                        <goals>
                            <goal>cdbgen</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <dependencies>
<dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derbyclient</artifactId>
            <version>10.10.1.1</version>
            <scope>compile</scope>
        </dependency>

    </dependencies>
</project>

This also requires a valid cayenne "datamap.map.xml" file (in src/main/resources), here's one I made earlier:

<?xml version="1.0" encoding="utf-8"?>
<data-map xmlns="http://cayenne.apache.org/schema/3.0/modelMap"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://cayenne.apache.org/schema/3.0/modelMap http://cayenne.apache.org/schema/3.0/modelMap.xsd"
     project-version="6">
    <db-entity name="TEST">
        <db-attribute name="ID" type="INTEGER" isPrimaryKey="true" isMandatory="true"/>
    </db-entity>
</data-map>

EDIT:

Adding more information.

The derbyclient-10.10.1.1.jar does contain the class 'org.apache.derby.jdbc.ClientDriver' (just expanded the JAR from Netbeans).

The -X flag seems to show that the CLASSPATH is correctly referencing the JAR:

[DEBUG]   (f) classpathElements = [<PROJECT-PATH>\mvn\target\classes, <HOME-DIR>\.m2\repository\org\apache\derby\derbyclient\10.10.1.1\derbyclient-10.10.1.1.jar]

SOLUTION:working pom.xml (see answer and my comment):

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany</groupId>
    <artifactId>myproject</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.cayenne.plugins</groupId>
                <artifactId>maven-cayenne-modeler-plugin</artifactId>
                <version>3.2M1</version>
            </plugin>
            <plugin>
                <groupId>org.apache.cayenne.plugins</groupId>
                <artifactId>maven-cayenne-plugin</artifactId>
                <version>3.2M1</version>
<dependencies>
      <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derby</artifactId>
        <version>10.10.1.1</version>
      </dependency>
   </dependencies>
                <executions>
                    <execution>
                        <id>cgen</id>
                        <configuration>
                            <superPkg>com.mycompany.model.generated</superPkg>
                            <map>${project.build.sourceDirectory}/../resources/datamap.map.xml</map>
                            <destDir>${project.build.sourceDirectory}</destDir>
                        </configuration>
                        <goals>
                            <goal>cgen</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>cdbgen</id>
                <configuration>
                            <map>${project.build.sourceDirectory}/../resources/datamap.map.xml</map>
                            <driver>org.apache.derby.jdbc.EmbeddedDriver</driver>
                            <url>jdbc:derby:memory:tracedb;create=true</url>
                            <username>test</username>
                        </configuration>
                        <goals>
                            <goal>cdbgen</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <dependencies>
<dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derbyclient</artifactId>
            <version>10.10.1.1</version>
            <scope>compile</scope>
        </dependency>

    </dependencies>
</project>
monojohnny
  • 5,894
  • 16
  • 59
  • 83
  • 1) Are you sure that `org.apache.derby.jdbc.ClientDriver` is actually located in the `derbyclient` dependency? 2) Try to run with `-X` command line parameter and see if classpath upon execution of the plugin in question contains this dependency. – Andrew Logvinov Aug 22 '13 at 19:34
  • Can you tell whether the maven tools are successfully retrieving a copy of derbyclient.jar from the maven repository? I don't generally use Maven, but I think that sometimes the version number is encoded in the jar name, so maybe you need to fetch something like derbyclient_10_10_1_1.jar? – Bryan Pendleton Aug 23 '13 at 01:58
  • I'll edit my original Post - but the quick answer to 1) and 2) is "yes" (it is in the JAR) and 2) Pretty sure the classpath is correct (from the -X output). – monojohnny Aug 23 '13 at 09:29
  • @BryanPendleton: pretty sure the name of dependency is correct - I used the Netbeans Maven front-end to look-up the artifact and version. Also if I manually edit the pom.xml and make a deliberate typo in the name/version of the dependency , Maven picks it up as an error. – monojohnny Aug 23 '13 at 09:34

1 Answers1

6

To ensure that the Derby driver is available during plugin execution (vs during your code compilation), you need to add it as a dependency of the plugin itself:

<plugin>
   <groupId>org.apache.cayenne.plugins</groupId>
   <artifactId>maven-cayenne-plugin</artifactId>
   <version>3.2M1</version>
   <dependencies>
      <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derbyclient</artifactId>
        <version>10.10.1.1</version>
      </dependency>
   </dependencies>
   ....
</plugin>
andrus_a
  • 2,528
  • 1
  • 16
  • 10
  • 1
    Great - I actually had to change back to making 'derby' (as opposed to just the derbyclient) the dependency and switch the JDBC driver to the EmbeddedDriver - just to make the pom run without errors. (The client JAR isn't able to actually create the in-memory Database). – monojohnny Aug 24 '13 at 12:56
  • Out of interest: is this a problem with Maven, or the Plugin ? Shouldn't the 'text' for the project dependencies be enough to ensure the plugin sees the dependency ? – monojohnny Aug 27 '13 at 13:09
  • The way I understand it this is a Maven feature and this is by design. "Regular" dependencies define what your own code needs to be compiled or executed. While dependencies under define what the plugin needs. Those are entirely different things. – andrus_a Aug 30 '13 at 08:49