mysql - Maven 和 mysql 依赖

标签 mysql maven ant

我目前有一个项目,其中有一个 build.xml。在此构建中,有命令 CreateDB,它调用 something.jar。这个 jar 运行一些 sql 脚本 (mysql db)。所以,现在,我可以通过执行来运行该命令

ant CreateDB

从命令行。但是现在,我需要做更多的事情。当我构建我的应用程序(使用 Maven)时,我想运行那个 ant 命令。所以,我创建了这个 pom.xml(见下文),但它不起作用,我收到了这个错误:

org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'org.gjt.mm.mysql.Driver'

根据此消息,我似乎没有 mySql 的连接器,但我在依赖项中指定了它。那么,我做错了什么?

Pom.xml:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
         xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.dl.test</groupId>
  <artifactId>toto</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <name>toto</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.23</version>
        <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.6</version>
        <executions>
          <execution>
            <id>mysql</id>
            <phase>integration-test</phase>
            <configuration>
              <tasks>
                <!-- For MySql -->
                <ant antfile="path\build.xml" target="createDB" />
              </tasks>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

最佳答案

maven antrun插件启动的ant任务不会自动继承maven依赖

您必须在 build.xml 代码中显式引用 Maven 测试类路径,这可通过 maven.test.classpath 属性获得。

因此您的执行可能类似于以下内容(test.classpath 也需要在您的 build.xml 中引用):

<execution>
   <id>mysql</id>
   <phase>integration-test</phase>
   <configuration>
      <tasks>
         <!-- For MySql -->
         <property name="test_classpath" refid="maven.test.classpath"/>
         <ant antfile="path\build.xml" target="createDB" />
      </tasks>
   </configuration>
   <goals>
       <goal>run</goal>
   </goals>
</execution>

另请参阅:Maven AntRun Plugin - Referencing the Maven Classpaths

关于mysql - Maven 和 mysql 依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23092117/

相关文章:

php - 哪个数据库用于处理非常大的结果集?

Java Spring + Hibernate "Cannot add or update a child row"

Java Spring MVC 无法在 Heroku 上启动应用程序。错误 H10(找不到 webapp 运行程序)

java - 将当前的 git commit id 注入(inject) Java webapp

php - 将文本列转换为时间

部署在 Tomcat 上的 Java 应用程序无法连接到 jdbc-mysql

java - 由于 java.lang.NoClassDefFoundError : com/fasterxml/jackson/annotation/JsonMerge 无法运行代码

android - 在项目中使用android swipelistview

java - csv 匹配中的 ANT 元素

java - Ant 和 Maven 之间的差异