2017-02-16 7 views
1

Я новичок в Maven и, чтобы его повесить, я попытался создать проект Dropwizard и попытался добавить соединение с базой данных mySQL. Я сделал следующее в моем YML файл -Невозможно загрузить класс: com.mysql.jdbc.Driver error в проекте Maven Dropwizard

а) ут -

# Database settings. 
database: 
    # the name of the JDBC driver, mysql in our case 
    driverClass: com.mysql.jdbc.Driver 
    # the username 
    user: root 
    # the password 
    password: drivedge12 
    # the JDBC URL; the database is called Hotel2 
    url: jdbc:mysql://localhost:3306/hotelmanagement 

И следующий в моем XML файл - б) pom.xml -

<?xml version="1.0" encoding="UTF-8"?> 
<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> 
    <prerequisites> 
     <maven>3.0.0</maven> 
    </prerequisites> 

    <artifactId>dropwizard-example</artifactId> 
    <version>1.1.0-SNAPSHOT</version> 
    <groupId>io.dropwizard</groupId> 
    <name>Dropwizard Example Application</name> 

    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 
     <maven.compiler.source>1.8</maven.compiler.source> 
     <maven.compiler.target>1.8</maven.compiler.target> 

     <!-- You'll probably want to remove this for your project. We're just using it here 
      so that dropwizard-example doesn't get deployed as a library. --> 
     <maven.deploy.skip>true</maven.deploy.skip> 
     <!-- You'll probably want to remove this for your project. We'Re just using it here 
      so that dropwizard-example site doesn't get staged and deployed. --> 
     <maven.site.skip>true</maven.site.skip> 
     <maven.site.deploy.skip>true</maven.site.deploy.skip> 
    </properties> 

    <repositories> 
     <repository> 
      <id>sonatype-nexus-snapshots</id> 
      <name>Sonatype Nexus Snapshots</name> 
      <url>http://oss.sonatype.org/content/repositories/snapshots</url> 
     </repository> 
    </repositories> 

    <dependencyManagement> 
     <dependencies> 
     <dependency> 
      <groupId>mysql</groupId> 
      <artifactId>mysql-connector-java</artifactId> 
      <version>5.1.37</version> 
     </dependency> 
      <dependency> 
       <groupId>io.dropwizard</groupId> 
       <artifactId>dropwizard-bom</artifactId> 
       <version>${project.version}</version> 
       <type>pom</type> 
       <scope>import</scope> 
      </dependency> 
     </dependencies> 
    </dependencyManagement> 

    <dependencies> 
     <dependency> 
      <groupId>io.dropwizard</groupId> 
      <artifactId>dropwizard-core</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>io.dropwizard</groupId> 
      <artifactId>dropwizard-auth</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>io.dropwizard</groupId> 
      <artifactId>dropwizard-assets</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>io.dropwizard</groupId> 
      <artifactId>dropwizard-http2</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>io.dropwizard</groupId> 
      <artifactId>dropwizard-hibernate</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>io.dropwizard</groupId> 
      <artifactId>dropwizard-migrations</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>io.dropwizard</groupId> 
      <artifactId>dropwizard-views-freemarker</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>io.dropwizard</groupId> 
      <artifactId>dropwizard-views-mustache</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>io.dropwizard</groupId> 
      <artifactId>dropwizard-metrics-graphite</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>com.h2database</groupId> 
      <artifactId>h2</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>io.dropwizard</groupId> 
      <artifactId>dropwizard-testing</artifactId> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.mockito</groupId> 
      <artifactId>mockito-core</artifactId> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.glassfish.jersey.test-framework.providers</groupId> 
      <artifactId>jersey-test-framework-provider-inmemory</artifactId> 
      <scope>test</scope> 
      <exclusions> 
       <exclusion> 
        <groupId>javax.servlet</groupId> 
        <artifactId>javax.servlet-api</artifactId> 
       </exclusion> 
       <exclusion> 
        <groupId>junit</groupId> 
        <artifactId>junit</artifactId> 
       </exclusion> 
      </exclusions> 
     </dependency> 
     <dependency> 
      <groupId>org.glassfish.jersey.test-framework.providers</groupId> 
      <artifactId>jersey-test-framework-provider-grizzly2</artifactId> 
      <scope>test</scope> 
      <exclusions> 
       <exclusion> 
        <groupId>javax.servlet</groupId> 
        <artifactId>javax.servlet-api</artifactId> 
       </exclusion> 
       <exclusion> 
        <groupId>junit</groupId> 
        <artifactId>junit</artifactId> 
       </exclusion> 
      </exclusions> 
     </dependency> 
    </dependencies> 

    <build> 
     <pluginManagement> 
      <plugins> 
       <plugin> 
        <groupId>org.apache.maven.plugins</groupId> 
        <artifactId>maven-clean-plugin</artifactId> 
        <version>2.6.1</version> 
       </plugin> 
       <plugin> 
    <groupId>org.eclipse.jetty</groupId> 
    <artifactId>jetty-maven-plugin</artifactId> 
    <version>9.4.2-SNAPSHOT</version> 
</plugin> 
       <plugin> 
        <groupId>org.apache.maven.plugins</groupId> 
        <artifactId>maven-install-plugin</artifactId> 
        <version>2.5.2</version> 
       </plugin> 
       <plugin> 
        <groupId>org.apache.maven.plugins</groupId> 
        <artifactId>maven-resources-plugin</artifactId> 
        <version>2.7</version> 
       </plugin> 
       <plugin> 
        <groupId>org.apache.maven.plugins</groupId> 
        <artifactId>maven-enforcer-plugin</artifactId> 
        <version>1.4.1</version> 
       </plugin> 
       <plugin> 
        <groupId>org.apache.maven.plugins</groupId> 
        <artifactId>maven-compiler-plugin</artifactId> 
        <version>3.6.0</version> 
       </plugin> 
       <plugin> 
        <groupId>org.apache.maven.plugins</groupId> 
        <artifactId>maven-source-plugin</artifactId> 
        <version>2.4</version> 
       </plugin> 
       <plugin> 
        <groupId>org.apache.maven.plugins</groupId> 
        <artifactId>maven-jar-plugin</artifactId> 
        <version>2.6</version> 
       </plugin> 
       <plugin> 
        <groupId>org.apache.maven.plugins</groupId> 
        <artifactId>maven-shade-plugin</artifactId> 
        <version>2.4.3</version> 
       </plugin> 
       <plugin> 
        <groupId>org.apache.maven.plugins</groupId> 
        <artifactId>maven-deploy-plugin</artifactId> 
        <version>2.8.2</version> 
       </plugin> 
       <plugin> 
        <groupId>org.apache.maven.plugins</groupId> 
        <artifactId>maven-site-plugin</artifactId> 
        <version>3.4</version> 
       </plugin> 
      </plugins> 
     </pluginManagement> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-enforcer-plugin</artifactId> 
       <executions> 
        <execution> 
         <id>enforce</id> 
         <configuration> 
          <rules> 
           <DependencyConvergence /> 
          </rules> 
         </configuration> 
         <goals> 
          <goal>enforce</goal> 
         </goals> 
        </execution> 
       </executions> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-source-plugin</artifactId> 
       <executions> 
        <execution> 
         <id>attach-sources</id> 
         <goals> 
          <goal>jar</goal> 
         </goals> 
        </execution> 
       </executions> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-jar-plugin</artifactId> 
       <configuration> 
        <archive> 
         <manifest> 
          <addDefaultImplementationEntries>true</addDefaultImplementationEntries> 
         </manifest> 
        </archive> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-shade-plugin</artifactId> 
       <configuration> 
        <createDependencyReducedPom>true</createDependencyReducedPom> 
        <filters> 
         <filter> 
          <artifact>*:*</artifact> 
          <excludes> 
           <exclude>META-INF/*.SF</exclude> 
           <exclude>META-INF/*.DSA</exclude> 
           <exclude>META-INF/*.RSA</exclude> 
          </excludes> 
         </filter> 
        </filters> 
        <transformers> 
         <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" /> 
         <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> 
          <mainClass>com.example.helloworld.HelloWorldApplication</mainClass> 
         </transformer> 
        </transformers> 
       </configuration> 
       <executions> 
        <execution> 
         <phase>package</phase> 
         <goals> 
          <goal>shade</goal> 
         </goals> 
        </execution> 
       </executions> 
      </plugin> 
     </plugins> 
    </build> 

    <profiles> 
     <profile> 
      <id>dev</id> 
      <build> 
       <plugins> 
        <plugin> 
         <groupId>org.apache.maven.plugins</groupId> 
         <artifactId>maven-shade-plugin</artifactId> 
         <executions> 
          <execution> 
           <phase>none</phase> 
          </execution> 
         </executions> 
        </plugin> 
       </plugins> 
      </build> 
     </profile> 
    </profiles> 
</project> 

Но я продолжайте получать следующую ошибку при попытке запуска в качестве приложения Java -

WARN [2017-02-16 04:44:52,491] org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator: HHH000342: Could not obtain connection to query metadata : Unable to load class: com.mysql.jdbc.Driver from ClassLoader:[email protected];ClassLoader:[email protected] 
Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] 

Может ли кто-нибудь сообщить мне об этом где я здесь и что мне нужно сделать, чтобы исправить это?

ответ

0

В pom.xml все зависимости проходят под тегом xml dependencies. Так что в вашем pom.xml двигаться ниже mysql-connector зависимость

    <dependency> 
       <groupId>mysql</groupId> 
       <artifactId>mysql-connector-java</artifactId> 
       <version>5.1.37</version> 
       </dependency> 

из dependencyManagement XML тега dependencies тег ниже его. Также вы можете проверить, загружен ли барабан mysql-connector или нет в вашем ideвнешний ящик раздел.

Ниже приведено изображение от IntelliJ Idea, где будут показаны все баночки, загруженные maven.

enter image description here

Позвольте мне знать, если у вас есть какие-либо сомнения.

+0

@ Code.rhyme это решило вашу проблему? –

Смежные вопросы