问题描述
我正在使用Apache CXF JAX-RS在Eclipse中构建项目。当我在Eclipse中运行我的主类时,它工作正常。当我在maven中构建一个具有依赖关系的jar时,它不起作用。这是我的pom.xml(我通过运行mvn clean compile assembly:single来构建):
I am building a project in Eclipse, using Apache CXF JAX-RS. When I run my main class in Eclipse, it works fine. When I build a jar with dependencies in maven, it doesn't work. This is my pom.xml (I am building by running "mvn clean compile assembly:single"):
<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.theopentutorials.jaxrs</groupId>
<artifactId>JsonCxfProvider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>2.7.4</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>2.7.4</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-extension-providers</artifactId>
<version>2.7.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.theopentutorials.jaxrs.calc.CalcRESTStartUp</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>
这是我的主要课程:
public static void main(String[] args) {
JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
sf.setResourceClasses(ResultsXml.class);
sf.setResourceProvider(ResultsXml.class, new SingletonResourceProvider(new ResultsXml()));
sf.setAddress("http://localhost:9999/open/");
Server server = sf.create();
}
其中ResultsXml基本上是带注释的pojo类。在Eclipse中运行时,我可以在localhost:9999 / open /上发出请求,然后我得到了我期望的JSON。但是,当我在maven中构建然后使用java -jar myjarfile.jar运行时,我收到以下错误:
Where ResultsXml is basically an annotated pojo class. When running in Eclipse, I can make requests on localhost:9999/open/ and I get the JSON back that I expect. However, when I build in maven and then run with java -jar myjarfile.jar, I get the following error:
到目前为止,我在Google / StackOverflow上找到的所有内容都表明此错误来自缺少的META-INF / cxf / cxf.xml文件 - 这是有道理的,因为我没有其中之一。但是它如何在Eclipse中起作用呢?
Everything I have been able to find on Google / StackOverflow so far suggests that this error comes from a missing META-INF/cxf/cxf.xml file - which makes sense, as I don't have one of those. But how then does it work in Eclipse?
可能是Eclipse正在从其中一个碰巧拥有我需要的配置的依赖jar中获取一个cxf.xml文件,但是当使用maven打包时它会选择他们以不同的顺序,所以不起作用?我试图创建自己的cxf.xml文件,但我不确定哪一个(maven构建日志表明我所有的依赖项之间,大约有12个文件的副本)要使用 - 有没有办法找到Eclipse正在挑选哪一个?
Could it be that Eclipse is picking up a cxf.xml file from one of the dependency jars that happens to have the config I need, but when packaging with maven it picks them up in a different order and so doesn't work? I have tried to create my own cxf.xml file, but I'm not sure which one (the maven build logs suggest that between all my dependencies, there are about 12 copies of the file) to use - is there a way to find out which one Eclipse is picking up?
EDIT1
我尝试使用eclipse导出带有解压缩依赖项的可运行jar文件,当eclipse导出的jar我得到一个类似但略有不同的消息:
I tried using eclipse to export a runnable jar file with unpacked dependencies, when the eclipse-exported jar I get a similar but subtly different message:
线程main
org.apache.cxf.service.factory中的异常。 ServiceConstructionException
at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.create(JAXRSServerFactoryBean.java:199)
at com.theopentutorials.jaxrs.calc.CalcRESTStartUp.main(CalcRESTStartUp.java:15)
引起:java.io.IOException:无法从总线中找到任何已注册的
HttpDestinationFactory。
at org.apache.cxf.transport.http.HTTPTransportFactory.getDestination(HTTPTransportFactory.java:295)
at org.apache.cxf.endpoint.ServerImpl.initDestination(ServerImpl.java:93)
at org.apache.cxf.endpoint.ServerImpl。(ServerImpl.java:72)
at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.create(JAXRSServerFactoryBean.java:155)
Exception in thread "main" org.apache.cxf.service.factory.ServiceConstructionException at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.create(JAXRSServerFactoryBean.java:199) at com.theopentutorials.jaxrs.calc.CalcRESTStartUp.main(CalcRESTStartUp.java:15) Caused by: java.io.IOException: Cannot find any registered HttpDestinationFactory from the Bus. at org.apache.cxf.transport.http.HTTPTransportFactory.getDestination(HTTPTransportFactory.java:295) at org.apache.cxf.endpoint.ServerImpl.initDestination(ServerImpl.java:93) at org.apache.cxf.endpoint.ServerImpl.(ServerImpl.java:72) at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.create(JAXRSServerFactoryBean.java:155)
EDIT2
我尝试使用eclipse导出一个可运行的jar文件'将所需的库打包到jar中 - 到目前为止,这似乎正在起作用。是否有可能在maven中复制它?
I tried using eclipse to export a runnable jar file with 'package required libraries into jar' - so far this seems to be working. Is it possible to replicate this in maven?
推荐答案
这似乎是正确的解决方案,我在确切的时候发现了一些Spring库的问题是使用Maven Shade插件:
It seems the correct fix for this, which I discovered when I had the exact same problem with some Spring libraries, is to use the Maven Shade plugin:
因为几个cxf - * jars中包含相同名称的文件(例如META-INF / cxf / cxf.xml),正常的jar-with-dependencies构建将包含第一个,然后忽略它找到的所有后续的重复。 shade插件会将这些文件连接在一起,因此最终会在最后包含一个包含所需条目的大文件。
Because several cxf-* jars have files in them with the same names (eg META-INF/cxf/cxf.xml) a normal jar-with-dependencies build will include the first one, and then ignore all subsequent ones it finds as 'duplicate'. The shade plugin will concatenate these files together, so you end up with one large file at the end that contains all of the entries that you needed.
这篇关于缺少cxf.xml?在maven中失败,在Eclipse中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!