tika解析器作为gradlejar中的运行时依赖项

6jygbczu  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(357)

我有一个gradle项目,我用tika的autodetectparser提取内容。当项目构建到胖jar中时,autodetectparser返回空字符串。我可以看到这是因为解析器不在运行时类路径中。如何将解析器添加到运行时类路径?
渐变生成文件:

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());
                }
yptwkmov

yptwkmov1#

使用shadow插件构建jar解决了这个问题

相关问题