Search code examples
javamysqljakarta-eeglassfishcargo

Attempting to setup/use mysql datasource with glassfish (using Cargo and Maven). How to deploy mysql connector?


Good evening!

Using Cargo via Maven, I've configured my pom.xml to create an instance of glassfish and then deploy my project to it, before running integration tests. I'm most of the way there, in that I've got my code deployed, I've setup a datasource and a JNDI resource for it, but when I attempt to actually use the resource, I get the following exception:

Wrong class name or classpath for Datasource Object 
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

On a regular installation of glassfish, I can easily install the connector, but the installation of glassfish in this case is automated and a new instance is created each time I run the tests.

Is there any way that I can provide the mysql connector, either on a classpath that glassfish can read, or by installing it in the new instance of glassfish that's created each build?

Thanks!


Solution

  • Scouring the docs a little more, I found out that I can do this by providing maven dependencies in the container configuration, like so:

    <configuration>
        <container>
            <containerId>glassfish3x</containerId>
            <artifactInstaller>
                <groupId>org.glassfish.main.distributions</groupId>
                <artifactId>glassfish</artifactId>
                <version>${glassfish.version}</version>
            </artifactInstaller>
            <dependencies>
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                </dependency>
            </dependencies>
        </container>
        <configuration>
            <properties>
                <cargo.datasource.datasource.mysql>
                    cargo.datasource.jndi=jdbc/MysqlPool|
                    cargo.datasource.driver=com.mysql.jdbc.Driver|
                    cargo.datasource.url=jdbc:mysql://localhost/databasename|
                    cargo.datasource.transactionsupport=LOCAL_TRANSACTION|
                    cargo.datasource.username=username|
                    cargo.datasource.password=password
                </cargo.datasource.datasource.mysql>
            </properties>
        </configuration>
    </configuration>
    

    Take note of the mysql dependency. It needs to reference a dependency already defined in my project (which, for my project has a scope of "provided"). This works as expected. :)