2015-12-02 78 views
0

我使用spring引導編寫了一個REST API,它使用了一些外部庫,這些庫通過依賴包含在我的pom.xml中。 如果我通過mvn spring-boot:運行在IntelliJ中啓動項目,一切正常,但如果我嘗試通過mvn包將所有內容打包到罐子中,除了spring-boot的所有外部依賴關係cals都會丟失。但是,相應的jar文件被複制到jar的lib文件夾中。 所以,如果我開始罐子一切工作得很好(回答的GetRequest的等),但只要我想初始化類型的變量FFmpegFrameGrabber(這是從bytedeco)我得到一個的NoClassDefFoundErrorNoClassDefFoundError spring boot maven

我POM看起來如下:

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

    <groupId>spring-boot-test</groupId> 
    <artifactId>test</artifactId> 
    <version>1.0-SNAPSHOT</version> 

    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.2.7.RELEASE</version> 
    </parent> 

    <properties> 
     <java.version>1.8</java.version> 
    </properties> 

    <dependencies> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-test</artifactId> 
      <scope>test</scope> 
     </dependency> 

     <dependency> 
      <groupId>org.bytedeco</groupId> 
      <artifactId>javacv</artifactId> 
      <version>1.1</version> 
     </dependency> 

    </dependencies> 

    <build> 
     <finalName>${project.artifactId}</finalName> 
     <plugins> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

的genrerated罐(它的一部分)的結構:

+你好
+ LIB
+ META-INF
+組織
---- + springframework的
---- +這裏shoould是bytedeco(?)

在此先感謝

編輯:最小(不)工作示例

package hello; 

import org.bytedeco.javacv.FFmpegFrameGrabber; 
import org.springframework.web.bind.annotation.RestController; 
import org.springframework.web.bind.annotation.RequestMapping; 

@RestController 
public class HelloController { 

    @RequestMapping("/") 
    public String index() { 
     String path = "D:\\TestVideos\\1\\original.mp4"; 
     FFmpegFrameGrabber frameGrabber; 
     System.out.println("Starting Frame Grabber for: " + path); 
     frameGrabber = new FFmpegFrameGrabber(path); 
     try { 
      frameGrabber.start(); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     return "Greetings from Spring Boot! Opening: " + path; 
    } 

} 

而且Application.java

package hello; 

import java.util.Arrays; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.context.ApplicationContext; 

@SpringBootApplication 
public class Application { 

    public static void main(String[] args) { 
     ApplicationContext ctx = SpringApplication.run(Application.class, args); 

     System.out.println("Let's inspect the beans provided by Spring Boot:"); 

     String[] beanNames = ctx.getBeanDefinitionNames(); 
     Arrays.sort(beanNames); 
     for (String beanName : beanNames) { 
      System.out.println(beanName); 
     } 
    } 

} 

這是直接從春季啓動教程。 坦克再次

+1

我不熟悉bytedeco,但它的罐子還應該在你的JAR的'lib'文件夾及其所有類應提供。也許它做了不尋常的事情?你能提供一個能夠再現問題的小樣本嗎? –

+0

它是ffmpeg的hava包裝。所有的ffmpeg jar都在libfolder中,但是在適當的地方沒有classfiles。 – TheElk

回答

0

添加下面的插件在您的pom.xml<build><plugins> ... </plugins></build>部分。 構建maven項目並執行jar命令。 這個插件會將你所有的依賴jar包裝到最終的可執行jar文件中。

 <plugin> 
      <artifactId>maven-assembly-plugin</artifactId> 
      <version>2.6</version> 
      <configuration> 
       <descriptorRefs> 
        <descriptorRef>jar-with-dependencies</descriptorRef> 
       </descriptorRefs> 
      </configuration> 
     </plugin> 

這個頁面有Maven的組裝插件的詳細信息 - https://maven.apache.org/plugins/maven-assembly-plugin/usage.html

+0

這不起作用,因爲它有點弄亂了春季啓動的路徑。通常,spring-boot:repackage會調整由maven生成的jar中的這些名稱(如果我正確的話)。因此,我需要調用spring-boot:在依賴項上重新打包jar。有任何想法嗎?使用dependecies啓動jar時的錯誤:org.springframework.context.ApplicationContextException:無法啓動embedde d容器;嵌套異常是org.springframework.context.ApplicationContextE xception:由於缺少EmbeddedS ervletContainerFactory bean而無法啓動EmbeddedWebApplicationContext。 – TheElk

+0

好的。你有沒有嘗試[this](https://docs.spring。io/spring-boot/docs/current/reference/html/build-tool-plugins-maven-plugin.html) –

+0

是的,我做過。還是行不通 :/ – TheElk