无法在注释处理器中加载资源(不在类路径上) [英] Cannot load resources in Annotation Processor (Not on classpath)

查看:184
本文介绍了无法在注释处理器中加载资源(不在类路径上)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个注释处理器,它将使用接口的getter方法定义的键来生成枚举。



该接口位于


MyProject / src / main / java / my.package.MyInterfaces.java。


如果它们包含定义的键,我想验证驻留在 MyProject / src / main / resources / META-INF / resource-bundle / 中的属性文件在生成的枚举中。



我的问题是属性文件无法通过处理器的类加载器或Filer.getResource(...)。 >


  • 如何使此项目的源或类路径可用于处理器,以便我可以加载属性文件?


  • 现在我只有处理器所在的jar中的资源可用。
    我尝试通过eclipse Project / Properties / Java编译器/注释处理/处理器选项来定义-classpath和/或-sourcepath,但没有解决。




有没有人面临这样的问题,任何人都可以给我一个线索,说明如何使资源可用于处理器?



我确实有maven结构,但不要使用maven,因为应用程序中的旧依赖关系。所以maven现在不是我的选择。


Eclipse 3.6 Helios
似乎 StandardLocation.SOURCE_PATH在Filer#getResource()中没有实现 StandardLocation.CLASS_PATH ,因此将生成的源文件或类文件写入 SOURCE_PATH CLASS_PATH 似乎不可能,还可以访问 SOURCE_PATH CLASS_PATH *


谢谢。

解决方案


我的问题是属性文件不能通过处理器的类加载器或通过 Filer.getResource(...)


我不知道我理解你的问题。但是也许这里有一些帮助。


如何使该项目的源或类路径可用于处理器,以便我可以加载属性文件?


您需要添加 src / main / resources 作为Eclipse中的源文件夹。首先在Java项目中的Eclipse中选择配置构建路径。然后选择选项卡,然后单击添加文件夹。您应该可以选择 src / main / resources 文件夹,然后单击 Ok 。您现在应该在源文件夹列表中看到 src / main / resources



如果您查看 target / classes 目录,你应该看到资源目录中的所有文件,让你知道他们被正确地复制到类路径。

  src中的#文件主要资源
> ls src / main / resources / x / y / z
jgroups_udp.xml
#应该编译成目标/类
> ls target / classes / x / y / z
jgroups_udp.xml org
#,应该显示在jar
> -tvf target / project.jar
0 Thu Nov 03 18:50:00 EDT 2016 META-INF /
135 Thu Nov 03 18:49:58 EDT 2016 META-INF / MANIFEST.MF
...
3036 Thu Nov 03 18:49:36 EDT 2016 x / y / z / jgroups_udp.xml

然后在您的代码中,您可以通过执行以下操作来引用该文件。这将从类路径的顶部加载文件。如果它在一个子目录中,那么你会导致:

  InputStream stream = 
getClass()。getClassLoader()。的getResourceAsStream( X / Y / Z / jgroups_udp.xml);

如果您使用maven ,您可以添加类似跟随你 pom.xml

 < build> 
< resources>
< resource>
< directory> src / main / resources< / directory>
< / resource>
< / resources>
< / build>


I have an annotation processor which shall generate a enumeration with the keys defined by getter methods of an interface.

The interface resides in

MyProject/src/main/java/my.package.MyInterfaces.java.

I want to validate the properties files which reside in MyProject/src/main/resources/META-INF/resource-bundle/ if they do contain the keys defined in the generated enum.

My problem is that the properties files are not available via the classloader of the processor or via Filer.getResource(...).

  • How can I make the source or classpath of this project available to the processor, so that I can load the properties files ?

  • Now I have only the resources within the jar where the processor resides available. I did try to define -classpath and/or -sourcepath via eclipse Project/Properties/Java compiler/Annotation processing/Processor options but it did not work out.

Has anyone faced an issue like this, and can anyone give me a clue as to how I can make the resources available for the processor?

I do have the maven structure but do not use maven, because of old dependencies within the application. So maven is now not an option for me.

Eclipse 3.6 Helios Seems that StandardLocation.SOURCE_PATH and StandardLocation.CLASS_PATH are not implemented in Filer#getResource(), so writing generated source or class files to SOURCE_PATH or CLASS_PATH seems not be possible, also accessing any files on SOURCE_PATH and CLASS_PATH*

Thanks.

解决方案

My problem is that the properties files are not available via the classloader of the processor or via Filer.getResource(...).

I'm not sure I'm understanding your problem. But maybe something here will help.

How can I make the source or classpath of this project available to the processor, so that I can load the properties files ?

You need to add the src/main/resources as a "source folder" in Eclipse. First select Configure Build Path in Eclipse on your Java project. Then choose the Source tab and click on Add Folder. You should be able to select the src/main/resources folder and click Ok. You should now see the src/main/resources in the Source Folders list.

If you look into your target/classes directory, you should see all of the files from the resources directory in there which lets you know that they were copied into the classpath correctly.

# files in the src main resources
> ls src/main/resources/x/y/z
jgroups_udp.xml
# should compile into target/classes
> ls target/classes/x/y/z
jgroups_udp.xml org
# and should show up in the jar
> -tvf target/project.jar 
   0 Thu Nov 03 18:50:00 EDT 2016 META-INF/
 135 Thu Nov 03 18:49:58 EDT 2016 META-INF/MANIFEST.MF
 ...
3036 Thu Nov 03 18:49:36 EDT 2016 x/y/z/jgroups_udp.xml

Then in your code you can reference the file by doing the following. This will load the file from the top of the classpath. If it is in a subdir then you lead with:

InputStream stream =
    getClass().getClassLoader().getResourceAsStream("x/y/z/jgroups_udp.xml");

Btw, if you were using maven, you would add something like the following to you pom.xml:

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
    </resources>
</build>

这篇关于无法在注释处理器中加载资源(不在类路径上)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆