gpt4 book ai didi

java - Maven 可执行 jar 打包 vs Maven exec with Jackson 不工作

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:46:27 26 4
gpt4 key购买 nike

我需要一些帮助来尝试让可执行文件工作。这是一个棘手的问题,我已将其缩小到这样一个事实,即 maven exec 运行方式以及 maven shade 插件或 maven assembly 插件打包文件的方式有所不同。

我正在使用 Netty 和 JAX-RS 在 Java 中构建 REST 服务,并使用 Jackson 将 POJO 转换为 JSON。

服务器在执行 mvn exec:javajava -jar 时正确启动。但是当对 java -jar 文件发出请求时,出现以下错误:

Could not find MessageBodyWriter for response object of type: [Ljava.lang.Object; of media type: application/json

似乎有些东西没有正确包装,但我不确定是什么。也许缺少传递依赖性?

这是我的 POM:

<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.test.myserver</groupId>
<artifactId>myserver</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

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

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

<repositories>
<repository>
<id>oss-sonatype</id>
<name>oss-sonatype</name>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>

<build>
<plugins>
<!-- For executing in maven itself (mvn exec:java) -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.test.myserver.App</mainClass>
</configuration>
</plugin>
<plugin>
<!-- for packaging (mvn package) -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.test.myserver.App</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<!-- The configuration of maven-assembly-plugin -->
<plugin>
<!-- for packaging (mvn compile assembly:single) -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<!-- The configuration of the plugin -->
<configuration>
<archive>
<manifest>
<mainClass>com.test.myserver.App</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>LATEST</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.googlecode.plist</groupId>
<artifactId>dd-plist</artifactId>
<version>LATEST</version>
</dependency>

<!-- MongoDB -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.mongojack</groupId>
<artifactId>mongojack</artifactId>
<version>2.1.0-SNAPSHOT</version>
</dependency>

<!-- REST Server -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-netty</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>LATEST</version>
</dependency>
</dependencies>

Maven shade 使用mvn package 执行。Maven 程序集使用 mvn compile assembly:single 执行。

提前致谢!

编辑

我根据 mvn shade 的输出检查了 mvn dependency:tree 并且对于所有意图,它们看起来都一样。

依赖树的输出

$ mvn dependency:tree
[INFO] Scanning for projects...
[INFO]
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building myserver 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ myserver ---
[INFO] com.test.myserver:myserver:jar:1.0-SNAPSHOT
[INFO] +- junit:junit:jar:4.11:test
[INFO] | \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] +- com.googlecode.plist:dd-plist:jar:1.8:compile
[INFO] +- org.mongodb:mongo-java-driver:jar:2.12.0:compile
[INFO] +- org.mongojack:mongojack:jar:2.1.0-SNAPSHOT:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-databind:jar:2.2.3:compile
[INFO] | +- de.undercouch:bson4jackson:jar:2.2.0:compile
[INFO] | +- javax.persistence:persistence-api:jar:1.0.2:compile
[INFO] | \- commons-io:commons-io:jar:2.4:compile
[INFO] +- org.jboss.resteasy:resteasy-jaxrs:jar:3.0.8.Final:compile
[INFO] | +- org.jboss.resteasy:jaxrs-api:jar:3.0.8.Final:compile
[INFO] | +- org.jboss.spec.javax.annotation:jboss-annotations-api_1.1_spec:jar:1.0.1.Final:compile
[INFO] | +- javax.activation:activation:jar:1.1:compile
[INFO] | +- org.apache.httpcomponents:httpclient:jar:4.2.1:compile
[INFO] | | +- org.apache.httpcomponents:httpcore:jar:4.2.1:compile
[INFO] | | +- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] | | \- commons-codec:commons-codec:jar:1.6:compile
[INFO] | \- net.jcip:jcip-annotations:jar:1.0:compile
[INFO] +- org.jboss.resteasy:resteasy-netty:jar:3.0.8.Final:compile
[INFO] | \- io.netty:netty:jar:3.6.4.Final:compile
[INFO] +- org.jboss.resteasy:resteasy-jackson2-provider:jar:3.0.8.Final:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-core:jar:2.3.2:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.3.2:compile
[INFO] | \- com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:jar:2.3.2:compile
[INFO] | +- com.fasterxml.jackson.jaxrs:jackson-jaxrs-base:jar:2.3.2:compile
[INFO] | \- com.fasterxml.jackson.module:jackson-module-jaxb-annotations:jar:2.3.2:compile
[INFO] +- org.slf4j:slf4j-api:jar:1.7.7:compile
[INFO] +- org.slf4j:slf4j-simple:jar:1.7.7:compile
[INFO] \- org.reflections:reflections:jar:0.9.9-RC1:compile
[INFO] +- com.google.guava:guava:jar:11.0.2:compile
[INFO] | \- com.google.code.findbugs:jsr305:jar:1.3.9:compile
[INFO] +- org.javassist:javassist:jar:3.16.1-GA:compile
[INFO] \- dom4j:dom4j:jar:1.6.1:compile
[INFO] \- xml-apis:xml-apis:jar:1.0.b2:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.999 s
[INFO] Finished at: 2014-04-22T17:09:47-08:00
[INFO] Final Memory: 13M/310M
[INFO] ------------------------------------------------------------------------

阴影输出

$ mvn package
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ myserver ---
[INFO] Building jar: /myserver/target/myserver-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-shade-plugin:2.2:shade (default) @ myserver ---
[INFO] Including com.googlecode.plist:dd-plist:jar:1.8 in the shaded jar.
[INFO] Including org.mongodb:mongo-java-driver:jar:2.12.0 in the shaded jar.
[INFO] Including org.mongojack:mongojack:jar:2.1.0-SNAPSHOT in the shaded jar.
[INFO] Including com.fasterxml.jackson.core:jackson-databind:jar:2.2.3 in the shaded jar.
[INFO] Including de.undercouch:bson4jackson:jar:2.2.0 in the shaded jar.
[INFO] Including javax.persistence:persistence-api:jar:1.0.2 in the shaded jar.
[INFO] Including commons-io:commons-io:jar:2.4 in the shaded jar.
[INFO] Including org.jboss.resteasy:resteasy-jaxrs:jar:3.0.8.Final in the shaded jar.
[INFO] Including org.jboss.resteasy:jaxrs-api:jar:3.0.8.Final in the shaded jar.
[INFO] Including org.jboss.spec.javax.annotation:jboss-annotations-api_1.1_spec:jar:1.0.1.Final in the shaded jar.
[INFO] Including javax.activation:activation:jar:1.1 in the shaded jar.
[INFO] Including org.apache.httpcomponents:httpclient:jar:4.2.1 in the shaded jar.
[INFO] Including org.apache.httpcomponents:httpcore:jar:4.2.1 in the shaded jar.
[INFO] Including commons-logging:commons-logging:jar:1.1.1 in the shaded jar.
[INFO] Including commons-codec:commons-codec:jar:1.6 in the shaded jar.
[INFO] Including net.jcip:jcip-annotations:jar:1.0 in the shaded jar.
[INFO] Including org.jboss.resteasy:resteasy-netty:jar:3.0.8.Final in the shaded jar.
[INFO] Including io.netty:netty:jar:3.6.4.Final in the shaded jar.
[INFO] Including org.jboss.resteasy:resteasy-jackson2-provider:jar:3.0.8.Final in the shaded jar.
[INFO] Including com.fasterxml.jackson.core:jackson-core:jar:2.3.2 in the shaded jar.
[INFO] Including com.fasterxml.jackson.core:jackson-annotations:jar:2.3.2 in the shaded jar.
[INFO] Including com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:jar:2.3.2 in the shaded jar.
[INFO] Including com.fasterxml.jackson.jaxrs:jackson-jaxrs-base:jar:2.3.2 in the shaded jar.
[INFO] Including com.fasterxml.jackson.module:jackson-module-jaxb-annotations:jar:2.3.2 in the shaded jar.
[INFO] Including org.slf4j:slf4j-api:jar:1.7.7 in the shaded jar.
[INFO] Including org.slf4j:slf4j-simple:jar:1.7.7 in the shaded jar.
[INFO] Including org.reflections:reflections:jar:0.9.9-RC1 in the shaded jar.
[INFO] Including com.google.guava:guava:jar:11.0.2 in the shaded jar.
[INFO] Including com.google.code.findbugs:jsr305:jar:1.3.9 in the shaded jar.
[INFO] Including org.javassist:javassist:jar:3.16.1-GA in the shaded jar.
[INFO] Including dom4j:dom4j:jar:1.6.1 in the shaded jar.
[INFO] Including xml-apis:xml-apis:jar:1.0.b2 in the shaded jar.
[INFO] Replacing original artifact with shaded artifact.
[INFO] Replacing /myserver/target/myserver-1.0-SNAPSHOT.jar with /myserver/target/myserver-1.0-SNAPSHOT-shaded.jar
[INFO] Dependency-reduced POM written at: /myserver/dependency-reduced-pom.xml
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.658 s
[INFO] Finished at: 2014-04-22T17:05:50-08:00
[INFO] Final Memory: 39M/310M
[INFO] ------------------------------------------------------------------------

最佳答案

缺少阴影插件配置 the service transformer它合并了 the service discovery mechanism 使用的 META-INF/services 文件.

这是一个例子:

<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>

关于java - Maven 可执行 jar 打包 vs Maven exec with Jackson 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23232442/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com