2012-12-17 27 views
1

使用反射我使用reflectionsonejar maven plugin,我有運行所產生的罐子,當一個問題:與onejar插件

20204 [主]錯誤org.reflections.Reflections - 定的掃描URL是 空。在配置中設置網址

在我看來,反射並不支持onejar正在使用的類加載器。我在我的代碼中使用了Reflections的以下配置:

Reflections reflections = new Reflections("path.to.my.package"); 

任何想法?謝謝。

回答

2

是的一個已知的問題。有一個工作...

使用自定義UrlType和自定義Vfs.Dir;使用OneJarURLConnection正確讀取嵌入式JAR中的內容;使用'標準'Vfs.File。

問題和解決方案在這裏討論:http://code.google.com/p/reflections/issues/detail?id=128

來源,如果你需要它:

OneJarDir

package com.sdl.ws.integration.profserv.shared.onejar; 


import com.simontuffs.onejar.OneJarURLConnection; 
import org.reflections.vfs.Vfs; 
import org.reflections.vfs.ZipDir; 
import org.reflections.vfs.ZipFile; 

import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Enumeration; 
import java.util.List; 
import java.util.jar.JarEntry; 
import java.util.jar.JarFile; 


public class OneJarDir implements Vfs.Dir { 

    private JarFile oneJarFile = null; 
    private List<Vfs.File> oneJarClassFiles = new ArrayList<Vfs.File>(); 

    private OneJarURLConnection oneJarConnection; 

    public OneJarDir(OneJarURLConnection oneJarConnection) { 

     this.oneJarConnection = oneJarConnection; 

     try { 
      this.oneJarConnection.connect(); 
      this.oneJarFile = this.oneJarConnection.getJarFile(); 
      Enumeration<JarEntry> entries = oneJarFile.entries(); 

      while (entries.hasMoreElements()) { 
       oneJarClassFiles.add(new ZipFile(new ZipDir(oneJarFile), entries.nextElement())); 
      } 

     } catch (IOException e) { 
      throw new RuntimeException("Can't create One-Jar VFS directory", e); 
     } 
    } 

    public String getPath() { 
     return oneJarConnection.getURL().getPath(); 
    } 

    public Iterable<Vfs.File> getFiles() { 
     return oneJarClassFiles; 
    } 

    public void close() { 
     try { 
      if (oneJarConnection != null) 
       oneJarConnection.getInputStream().close(); 
     } catch (IOException e) { 
      throw new RuntimeException("Can't close VFS JAR stream", e); 
     } 
    } 
} 

OneJarUrlType

package com.sdl.ws.integration.profserv.shared.onejar; 

import com.simontuffs.onejar.OneJarURLConnection; 
import org.reflections.vfs.Vfs; 

import java.io.IOException; 
import java.net.URL; 

public class OneJarUrlType implements Vfs.UrlType { 

    private static final String _JAR_DIR = "jar!"; 

    public boolean matches(URL url) { 

     // check if "double-jarred' by one-jar; this would appear to conflict with the standard JAR loader, so it either needs to be first (which it is) 
     // OR the standard needs to be removed. This match assumes a nested JAR, unlike the standard JAR type. 
     String externalForm = url.toExternalForm(); 

     // ugly, but should be much faster than regex. 
     int idx1 = externalForm.indexOf(_JAR_DIR); 
     return (idx1 > 0 && externalForm.indexOf(_JAR_DIR, idx1 + _JAR_DIR.length()) > 0); 
    } 

    public Vfs.Dir createDir(URL url) { 
     try { 
      return new OneJarDir(new OneJarURLConnection(url)); 
     } catch (IOException e) { 
      throw new RuntimeException("Can't open One-Jar embedded JAR", e); 
     } 
    } 

} 
+0

謝謝!這看起來不錯。稍後需要時再試用。 –