【问题标题】:Dropwizard configuration not working with a Shaded jarDropwizard 配置不适用于 Shaded jar
【发布时间】:2018-03-28 01:19:43
【问题描述】:

在尝试运行带有 configuration.yaml 文件中属性的阴影 jar 时,我遇到了以下问题。 (直接跑主类时不会发生)

运行命令:java -jar target/mlsdata-1.0.jar server configuration.yaml

错误:

io.dropwizard.configuration.ConfigurationParsingException: configuration.yaml has an error:
  * Failed to parse configuration at: server.applicationConnectors.[0]; Could not resolve type id 'http' as a subtype of [simple type, class io.dropwizard.jetty.ConnectorFactory]: known type ids = [] (for POJO property 'applicationConnectors')
 at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: com.zaplabs.MlsConfiguration["server"]->io.dropwizard.server.DefaultServerFactory["applicationConnectors"]->java.util.ArrayList[0])

根据我从各种来源了解到的情况,这可能是一个 Maven 打包问题。但解决方案对我没有帮助。

这是我在 POM 中的构建设置。

`

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>com.zaplabs.MlsApplication</mainClass>
                    </manifest>
                </archive>
                <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>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <archive>
                            <manifest>
                                <addClasspath>true</addClasspath>
                                <classpathPrefix>lib/</classpathPrefix>
                                <mainClass>com.zaplabs.MlsApplication</mainClass>
                            </manifest>
                        </archive>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"/>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.zaplabs.MlsApplication</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>

`

Dropwizard 版本:

    <dependency>
        <groupId>io.dropwizard</groupId>
        <artifactId>dropwizard-core</artifactId>
        <version>1.3.0</version>
    </dependency>

配置.yaml

server:
  applicationConnectors:
    - type: http
      port: 9000
  adminConnectors:
    - type: http
      port: 9001

# Database settings.
database:
    # the name of the JDBC driver, mysql in our case
    driverClass: com.mysql.jdbc.Driver
    # the username
    user: xxx
    # the password
    password: xxx
    # the JDBC URL
    url: jdbc:mysql://xxx
    properties:
      charSet: UTF-8
      maxWaitForConnection: 1s
      validationQuery: "/* MyService Health Check */ SELECT 1"
      minSize: 8
      maxSize: 32
      checkConnectionWhileIdle: false
      evictionInterval: 10s
      minIdleTime: 1 minute
      hibernate.dialect: org.hibernate.dialect.H2Dialect
      hibernate.show_sql: true
      hibernate.generate_statistics: false
      hibernate.hbm2ddl.auto: validate # validates schema when service is started

【问题讨论】:

    标签: java rest dropwizard dropwizard-templates


    【解决方案1】:

    所以我遇到了同样的问题,我找到了解决方案here

    我还必须使用以下插件

    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.3</version>
    <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>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <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>
        </execution>
    </executions>
    

    【讨论】:

    • 谢谢,是因为hibernate和dropwizard-core之间的依赖问题,加上后就可以了。
    猜你喜欢
    • 2016-05-22
    • 1970-01-01
    • 2018-08-18
    • 2020-07-06
    • 1970-01-01
    • 1970-01-01
    • 2014-04-10
    • 2018-02-03
    • 2019-07-28
    相关资源
    最近更新 更多