Tika Parser作为gradle jar中的运行时依赖项

时间:2019-02-04 15:50:53

标签: java gradle apache-tika

我有一个gradle项目,正在使用Tika的AutoDetectParser提取内容。当项目内置到胖子罐中时,AutoDetectParser返回空字符串。我看到这是因为解析器不在运行时类路径中。如何将解析器添加到运行时类路径?

Gradle构建文件:

    dependencies {
    compile 'org.apache.tika:tika-parsers:1.20'
    testImplementation 'junit:junit:4.12'
}

jar {
  manifest {
      attributes (
          'Main-Class': 'com.superna.tikatest.TikaTestApp'
      )
  }
  from { 
      configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } 
  } {
    exclude 'META-INF/*.RSA', 'META-INF/*.SF', 'META-INF/*.DSA'
  }
}

代码段:

                Metadata metadata = new Metadata();
                AutoDetectParser parser = new AutoDetectParser();
                BodyContentHandler handler = new BodyContentHandler();          

                try(FileInputStream fis = new FileInputStream(localPath.toString());
                        BufferedInputStream bis = new BufferedInputStream(fis);
                        TikaInputStream stream = TikaInputStream.get(bis)) {
                    parser.parse(stream, handler, metadata);
                    System.out.println(handler.toString());
                }

1 个答案:

答案 0 :(得分:1)

使用阴影插件构建我的jar可以解决问题