无法在 apt 处理器类中初始化 Velocity 引擎

发布于 2024-11-27 09:04:52 字数 8754 浏览 2 评论 0原文

我正在尝试在 apt 注释处理类(使用 Maven)中使用 Velocity。 初始化 Velocity

Properties vProps = new Properties();
vProps.load(this.getClass().getResourceAsStream("velocity.properties"));
mEngine = new VelocityEngine(vProps);

使用以下 velocity.properties

runtime.log.logsystem.class=org.apache.velocity.runtime.log.NullLogChute

resource.loader=class
class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
class.resource.loader.path=.

这是 apt 调试跟踪,

[INFO] Processing 35 source files
[DEBUG] Invoking apt with arguments:
[DEBUG]   -classpath
[DEBUG]   D:\personal\depot\maven2\com\mycompany\portal\portal-annotations\1.0\portal-annotations-1.0.jar;D:\personal\depot\maven2\org\apache\velocity\velocity\1.7\velocity-1.7.jar;D:\personal\depot\maven2\commons-collections\commons-collections\3.2.1\commons-collections-3.2.1.jar;D:\personal\depot\maven2\commons-lang\commons-lang\2.4\commons-lang-2.4.jar;D:\personal\depot\maven2\org\codehaus\plexus\plexus-utils\1.4.6\plexus-utils-1.4.6.jar;D:\personal\depot\maven2\org\codehaus\plexus\plexus-compiler-api\1.5.2\plexus-compiler-api-1.5.2.jar;D:\personal\depot\maven2\asm\asm\3.2\asm-3.2.jar;D:\personal\depot\maven2\asm\asm-commons\3.2\asm-commons-3.2.jar;D:\personal\depot\maven2\asm\asm-tree\3.2\asm-tree-3.2.jar;D:\personal\workspaces\portal-pm\emc-disk-arrays\target\classes;D:\personal\depot\maven2\com\mycompany\library\wbem-client\1.0\wbem-client-1.0.jar;D:\personal\app\java\bmc_jdk1.5.0_10\jre\..\lib\tools.jar
[DEBUG]   -sourcepath
[DEBUG]   D:\personal\workspaces\portal-pm\emc-disk-arrays\src\main\java
[DEBUG]   -d
[DEBUG]   D:\personal\workspaces\portal-pm\emc-disk-arrays\target\generated-resources\apt
[DEBUG]   -nowarn
[DEBUG]   -encoding
[DEBUG]   ISO-8859-1
[DEBUG]   -verbose
[DEBUG]   -s
[DEBUG]   D:\personal\workspaces\portal-pm\emc-disk-arrays\target\generated-sources\apt
[DEBUG]   -nocompile
[DEBUG]   -factory
[DEBUG]   com.mycompany.portal.annotation.processing.MyAnnotationProcessorFactory

我收到以下错误

[WARNING] Problem encountered during annotation processing; 
[WARNING] see stacktrace below for more information.
[WARNING] org.apache.velocity.exception.VelocityException: Error initializing log: Failed to initialize an instance of org.apache.velocity.runtime.log.NullLogChute with the current runtime configuration.
[WARNING]   at org.apache.velocity.runtime.RuntimeInstance.initializeLog(RuntimeInstance.java:875)
[WARNING]   at org.apache.velocity.runtime.RuntimeInstance.init(RuntimeInstance.java:262)
[WARNING]   at org.apache.velocity.app.VelocityEngine.init(VelocityEngine.java:93)
[WARNING]   at com.mycompany.portal.annotation.template.engine.Engine.<init>(Engine.java:26)
[WARNING]   at com.mycompany.portal.annotation.processing.MyAnnotationProcessor.createGeneratedFile(MyAnnotationProcessor.java:106)
[WARNING]   at com.mycompany.portal.annotation.processing.MyAnnotationProcessor.createGeneratedContent(MyAnnotationProcessor.java:95)
[WARNING]   at com.mycompany.portal.annotation.processing.MyAnnotationProcessor.process(MyAnnotationProcessor.java:75)
[WARNING]   at com.sun.mirror.apt.AnnotationProcessors$CompositeAnnotationProcessor.process(AnnotationProcessors.java:60)
[WARNING]   at com.sun.tools.apt.comp.Apt.main(Unknown Source)
[WARNING]   at com.sun.tools.apt.main.JavaCompiler.compile(Unknown Source)
[WARNING]   at com.sun.tools.apt.main.Main.compile(Unknown Source)
[WARNING]   at com.sun.tools.apt.main.Main.compile(Unknown Source)
[WARNING]   at com.sun.tools.apt.Main.processing(Unknown Source)
[WARNING]   at com.sun.tools.apt.Main.process(Unknown Source)
[WARNING]   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[WARNING]   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[WARNING]   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
[WARNING]   at java.lang.reflect.Method.invoke(Method.java:585)
[WARNING]   at org.codehaus.mojo.apt.AptUtils.invoke(AptUtils.java:114)
[WARNING]   at org.codehaus.mojo.apt.AbstractAptMojo.executeApt(AbstractAptMojo.java:389)
[WARNING]   at org.codehaus.mojo.apt.AbstractAptMojo.executeImpl(AbstractAptMojo.java:263)
[WARNING]   at org.codehaus.mojo.apt.AbstractAptMojo.execute(AbstractAptMojo.java:240)
[WARNING]   at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
[WARNING]   at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
[WARNING]   at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
[WARNING]   at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
[WARNING]   at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
[WARNING]   at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
[WARNING]   at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
[WARNING]   at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
[WARNING]   at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:319)
[WARNING]   at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
[WARNING]   at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
[WARNING]   at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
[WARNING]   at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
[WARNING]   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[WARNING]   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[WARNING]   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
[WARNING]   at java.lang.reflect.Method.invoke(Method.java:585)
[WARNING]   at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
[WARNING]   at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
[WARNING]   at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
[WARNING]   at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
[WARNING] Caused by: org.apache.velocity.exception.VelocityException: Failed to initialize an instance of org.apache.velocity.runtime.log.NullLogChute with the current runtime configuration.
[WARNING]   at org.apache.velocity.runtime.log.LogManager.createLogChute(LogManager.java:220)
[WARNING]   at org.apache.velocity.runtime.log.LogManager.updateLog(LogManager.java:269)
[WARNING]   at org.apache.velocity.runtime.RuntimeInstance.initializeLog(RuntimeInstance.java:871)
[WARNING]   ... 42 more
[WARNING] Caused by: org.apache.velocity.exception.VelocityException: The specified logger class org.apache.velocity.runtime.log.NullLogChute does not implement the org.apache.velocity.runtime.log.LogChute interface.
[WARNING]   at org.apache.velocity.runtime.log.LogManager.createLogChute(LogManager.java:181)
[WARNING]   ... 44 more
[DEBUG] Apt returned 4

但是当我在测试类上创建一个简单的 main 方法并使用 eclipse 启动它时,引擎初始化良好我没有有任何错误。

我添加了一些附加信息,

maven pom.xml 构建部分

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>apt-maven-plugin</artifactId>
  <version>1.0-alpha-4</version>
  <executions>
    <execution>
      <goals>
        <goal>process</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <verbose>true</verbose>
    <factory>com.mycompany.portal.annotation.processing.MyAnnotationProcessorFactory</factory>
  </configuration>
  <dependencies>
    <dependency>
      <!-- The maven artifact containing the Engine class -->
      <groupId>com.mycompany.portal</groupId>
      <artifactId>portal-annotations</artifactId>
      <version>1.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.velocity</groupId>
      <artifactId>velocity</artifactId>
      <version>1.7</version>
    </dependency>
  </dependencies>
</plugin>

我也尝试在 Eclipse 调试模式下执行,看来该

private static LogChute createLogChute(RuntimeServices rsvc) throws Exception

方法不考虑 org.apache.velocity.runtime .log.NullLogChute 作为 LogChute 接口的实例(LogManager.class 第 155 行测试为 false)并抛出 VelocityException

Velocity 是否与 maven 兼容并且apt-maven-插件?

I am trying to use Velocity in an apt annotation processing class (with Maven). On initializing Velocity

Properties vProps = new Properties();
vProps.load(this.getClass().getResourceAsStream("velocity.properties"));
mEngine = new VelocityEngine(vProps);

with the following velocity.properties

runtime.log.logsystem.class=org.apache.velocity.runtime.log.NullLogChute

resource.loader=class
class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
class.resource.loader.path=.

Here is the apt debug trace

[INFO] Processing 35 source files
[DEBUG] Invoking apt with arguments:
[DEBUG]   -classpath
[DEBUG]   D:\personal\depot\maven2\com\mycompany\portal\portal-annotations\1.0\portal-annotations-1.0.jar;D:\personal\depot\maven2\org\apache\velocity\velocity\1.7\velocity-1.7.jar;D:\personal\depot\maven2\commons-collections\commons-collections\3.2.1\commons-collections-3.2.1.jar;D:\personal\depot\maven2\commons-lang\commons-lang\2.4\commons-lang-2.4.jar;D:\personal\depot\maven2\org\codehaus\plexus\plexus-utils\1.4.6\plexus-utils-1.4.6.jar;D:\personal\depot\maven2\org\codehaus\plexus\plexus-compiler-api\1.5.2\plexus-compiler-api-1.5.2.jar;D:\personal\depot\maven2\asm\asm\3.2\asm-3.2.jar;D:\personal\depot\maven2\asm\asm-commons\3.2\asm-commons-3.2.jar;D:\personal\depot\maven2\asm\asm-tree\3.2\asm-tree-3.2.jar;D:\personal\workspaces\portal-pm\emc-disk-arrays\target\classes;D:\personal\depot\maven2\com\mycompany\library\wbem-client\1.0\wbem-client-1.0.jar;D:\personal\app\java\bmc_jdk1.5.0_10\jre\..\lib\tools.jar
[DEBUG]   -sourcepath
[DEBUG]   D:\personal\workspaces\portal-pm\emc-disk-arrays\src\main\java
[DEBUG]   -d
[DEBUG]   D:\personal\workspaces\portal-pm\emc-disk-arrays\target\generated-resources\apt
[DEBUG]   -nowarn
[DEBUG]   -encoding
[DEBUG]   ISO-8859-1
[DEBUG]   -verbose
[DEBUG]   -s
[DEBUG]   D:\personal\workspaces\portal-pm\emc-disk-arrays\target\generated-sources\apt
[DEBUG]   -nocompile
[DEBUG]   -factory
[DEBUG]   com.mycompany.portal.annotation.processing.MyAnnotationProcessorFactory

I have got the following error

[WARNING] Problem encountered during annotation processing; 
[WARNING] see stacktrace below for more information.
[WARNING] org.apache.velocity.exception.VelocityException: Error initializing log: Failed to initialize an instance of org.apache.velocity.runtime.log.NullLogChute with the current runtime configuration.
[WARNING]   at org.apache.velocity.runtime.RuntimeInstance.initializeLog(RuntimeInstance.java:875)
[WARNING]   at org.apache.velocity.runtime.RuntimeInstance.init(RuntimeInstance.java:262)
[WARNING]   at org.apache.velocity.app.VelocityEngine.init(VelocityEngine.java:93)
[WARNING]   at com.mycompany.portal.annotation.template.engine.Engine.<init>(Engine.java:26)
[WARNING]   at com.mycompany.portal.annotation.processing.MyAnnotationProcessor.createGeneratedFile(MyAnnotationProcessor.java:106)
[WARNING]   at com.mycompany.portal.annotation.processing.MyAnnotationProcessor.createGeneratedContent(MyAnnotationProcessor.java:95)
[WARNING]   at com.mycompany.portal.annotation.processing.MyAnnotationProcessor.process(MyAnnotationProcessor.java:75)
[WARNING]   at com.sun.mirror.apt.AnnotationProcessors$CompositeAnnotationProcessor.process(AnnotationProcessors.java:60)
[WARNING]   at com.sun.tools.apt.comp.Apt.main(Unknown Source)
[WARNING]   at com.sun.tools.apt.main.JavaCompiler.compile(Unknown Source)
[WARNING]   at com.sun.tools.apt.main.Main.compile(Unknown Source)
[WARNING]   at com.sun.tools.apt.main.Main.compile(Unknown Source)
[WARNING]   at com.sun.tools.apt.Main.processing(Unknown Source)
[WARNING]   at com.sun.tools.apt.Main.process(Unknown Source)
[WARNING]   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[WARNING]   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[WARNING]   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
[WARNING]   at java.lang.reflect.Method.invoke(Method.java:585)
[WARNING]   at org.codehaus.mojo.apt.AptUtils.invoke(AptUtils.java:114)
[WARNING]   at org.codehaus.mojo.apt.AbstractAptMojo.executeApt(AbstractAptMojo.java:389)
[WARNING]   at org.codehaus.mojo.apt.AbstractAptMojo.executeImpl(AbstractAptMojo.java:263)
[WARNING]   at org.codehaus.mojo.apt.AbstractAptMojo.execute(AbstractAptMojo.java:240)
[WARNING]   at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
[WARNING]   at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
[WARNING]   at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
[WARNING]   at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
[WARNING]   at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
[WARNING]   at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
[WARNING]   at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
[WARNING]   at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
[WARNING]   at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:319)
[WARNING]   at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
[WARNING]   at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
[WARNING]   at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
[WARNING]   at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
[WARNING]   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[WARNING]   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[WARNING]   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
[WARNING]   at java.lang.reflect.Method.invoke(Method.java:585)
[WARNING]   at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
[WARNING]   at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
[WARNING]   at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
[WARNING]   at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
[WARNING] Caused by: org.apache.velocity.exception.VelocityException: Failed to initialize an instance of org.apache.velocity.runtime.log.NullLogChute with the current runtime configuration.
[WARNING]   at org.apache.velocity.runtime.log.LogManager.createLogChute(LogManager.java:220)
[WARNING]   at org.apache.velocity.runtime.log.LogManager.updateLog(LogManager.java:269)
[WARNING]   at org.apache.velocity.runtime.RuntimeInstance.initializeLog(RuntimeInstance.java:871)
[WARNING]   ... 42 more
[WARNING] Caused by: org.apache.velocity.exception.VelocityException: The specified logger class org.apache.velocity.runtime.log.NullLogChute does not implement the org.apache.velocity.runtime.log.LogChute interface.
[WARNING]   at org.apache.velocity.runtime.log.LogManager.createLogChute(LogManager.java:181)
[WARNING]   ... 44 more
[DEBUG] Apt returned 4

But when I create a simple main method on a test class and launch it with eclipse, the engine initialize well and I don't have any error.

I put some additional information,

the maven pom.xml build section

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>apt-maven-plugin</artifactId>
  <version>1.0-alpha-4</version>
  <executions>
    <execution>
      <goals>
        <goal>process</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <verbose>true</verbose>
    <factory>com.mycompany.portal.annotation.processing.MyAnnotationProcessorFactory</factory>
  </configuration>
  <dependencies>
    <dependency>
      <!-- The maven artifact containing the Engine class -->
      <groupId>com.mycompany.portal</groupId>
      <artifactId>portal-annotations</artifactId>
      <version>1.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.velocity</groupId>
      <artifactId>velocity</artifactId>
      <version>1.7</version>
    </dependency>
  </dependencies>
</plugin>

I also try to follow in eclipse debug mode the execution and it appears that the

private static LogChute createLogChute(RuntimeServices rsvc) throws Exception

method do not consider org.apache.velocity.runtime.log.NullLogChute as an instance of LogChute interface (LogManager.class line 155 the test is false) and throw the VelocityException

Is Velocity compatible with maven and apt-maven-plugin ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

给不了的爱 2024-12-04 09:04:52

你的问题是 Maven 本身使用速度并且你会遇到版本冲突。

我刚刚为 maven-jxr-plugin 解决了这个问题。查看源代码中的 pom.xml,了解要使用哪些组件的哪个版本。

Your problem is that maven itself uses velocity and you get version conflicts.

I just resolved this for the maven-jxr-plugin. Have a look at the pom.xml in the source of that to see what versions of which components to use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文