rcp(插件开发)How to add a folder to java build path as library

转载一下老外写的代码,时间有点久,具体出处忘记了,请原作者见谅。

具体大家好好读一下,会对自己有帮助的,既然是转载,我就不班门弄斧了。

扩展点的编写:

<extension
  point="org.eclipse.jdt.core.classpathContainerInitializer">
  <classpathContainerInitializer
        class="org.eclipse.jdt.internal.junit.buildpath.JUnitContainerInitializer"
        id="org.eclipse.jdt.junit.JUNIT_CONTAINER">
  </classpathContainerInitializer>
</extension>

以下是代码:

final String description = "My container";

IProject project = ResourcesPlugin.getWorkspace().getRoot()
        .getProject("foo");

//get the lib folder, TODO check if it exists!
final IFolder folder = project.getFolder("lib");

//define a unique path for the custom container
final IPath containerPath = new Path(
        "my.custom.CLASSPATH_CONTAINER").append(project
        .getFullPath());

IJavaProject javaProject = JavaCore.create(project);

//create a container that lists all jars in the lib folder
IClasspathContainer customContainer = new IClasspathContainer() {
    public IClasspathEntry[] getClasspathEntries() {
        List<IClasspathEntry> entryList = new ArrayList<IClasspathEntry>();
        try {
            // add any members that are files with the jar extension
            IResource[] members = folder.members();
            for (IResource resource : members) {
                if (IFile.class.isAssignableFrom(resource
                        .getClass())) {
                    if (resource.getName().endsWith(".jar")) {
                        entryList.add(JavaCore.newLibraryEntry(
                                new Path(resource.getFullPath()
                                        .toOSString()), null,
                                new Path("/")));
                    }
                }
            }
        } catch (CoreException e) {
            // TODO handle the exception
            e.printStackTrace();
        }
        // convert the list to an array and return it
        IClasspathEntry[] entryArray = new IClasspathEntry[entryList
                .size()];
        return entryList.toArray(entryArray);
    }

    public String getDescription() {
        return description;
    }

    public int getKind() {
        return IClasspathEntry.CPE_CONTAINER;
    }

    public IPath getPath() {
        return containerPath;
    }

    @Override
    public String toString() {
        return getDescription();
    }
};

//register the custom container so when we add its path it is discovered
JavaCore.setClasspathContainer(containerPath,
        new IJavaProject[] { javaProject },
        new IClasspathContainer[] { customContainer }, null);

IClasspathEntry[] entries = javaProject.getRawClasspath();

//check if the container is already on the path
boolean hasCustomContainer = false;

for (int i = 0; i < entries.length; i++) {
    if (entries[i].getEntryKind() == IClasspathEntry.CPE_CONTAINER
            && entries[i].getPath().equals(containerPath)) {
        hasCustomContainer = true;
    }
}
if (!hasCustomContainer) {
    IClasspathEntry[] newEntries = new IClasspathEntry[entries.length + 1];

    System.arraycopy(entries, 0, newEntries, 0, entries.length);

    // add a new entry using the path to the container
    newEntries[entries.length] = JavaCore
            .newContainerEntry(customContainer.getPath());

    javaProject.setRawClasspath(newEntries,
            new NullProgressMonitor());
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值