1

我正在使用带有onejar maven 插件的反射,并且在运行生成的 jar 时遇到问题:

20204 [main] 错误 org.reflections.Reflections - 给定的扫描 url 为空。在配置中设置 url

在我看来,反射不支持 onejar 正在使用的类加载器。我一直Reflections在我的代码中使用以下配置:

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

有任何想法吗?谢谢。

4

1 回答 1

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

}
于 2013-01-05T04:47:46.177 回答