升级 AspectJ + Spring + Gradle

升级 AspectJ + Spring + Gradle

IT小君   2022-09-15T08:34:53

我正在尝试将 AspectJ jar 从 1.8.1 升级到 1.8.5,但我的构建一直失败并出现以下错误:

[ant:iajc] [error] javax.annotation.processing.FilerException: createResource. Resource already created

更新前的构建很好。我尝试从 1.8.1 升级到 1.8.2,但也失败了。这是我的 build.gradle 的片段

ext.aspectjCompiler = {
    ant.taskdef(
            resource: 'org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties',
            classpath: configurations.ajc.asPath
            )
    ant.iajc(
            source: sourceCompatibility,
            target: targetCompatibility,
            destDir: sourceSets.main.output.classesDir.absolutePath,
            maxmem: '512m',
            fork: 'true',
            aspectPath: configurations.aspects.asPath,
            sourceRootCopyFilter:'**/*.java',
            classpath: "${configurations.compile.asPath};${configurations.aspectCompile.asPath}",
            Xlint: 'ignore'
            ){
                sourceroots {
                    sourceSets.main.java.srcDirs.each { srcDir ->
                        pathelement(location: srcDir.absolutePath)
                    }
                }
            }
}

compileJava {
    doLast aspectjCompiler
}
服务器费用不足...
评论(1)
IT小君

AspectJ 1.8.2 开始,AspectJ 编译器ajc支持注释处理。AspectJ 维护者 Andy Clement在博客文章中也描述了这一点。可能这会以某种方式妨碍您项目中的其他 APT 内容。

遗憾的是,ajc的命令行界面并没有提及或解释新参数(仅在错误使用时显示错误消息),但基本上有三个:-proc-processor-processorpath在源代码中搜索,您甚至可以找到一些内联文档:

AjcTask.java

  /**
   * Controls whether annotation processing and/or compilation is done.
   * -proc:none means that compilation takes place without annotation processing.
   * -proc:only means that only annotation processing is done, without any subsequent compilation.
   */
  public void setProc(String proc) {
    if (proc.equals("none")) {
      cmd.addFlag("-proc:none", true);
    } else if (proc.equals("only")) {
      cmd.addFlag("-proc:only", true);
    }
  }

  /**
   * -processor class1[,class2,class3...]
   *  Names of the annotation processors to run. This bypasses the default discovery process.
   */
  public void setProcessor(String processors) {
    cmd.addFlagged("-processor", processors);
  }

  /**
   * -processorpath path
   * Specify where to find annotation processors; if this option is not used, the class path will be searched for processors.
   */
  public void setProcessorpath(String processorpath) {
    cmd.addFlagged("-processorpath", processorpath);
  }

来自AjcTaskTest.java的一些测试代码

  public void testAptProc() {
    AjcTask task = getTask(NOFILE);
    task.setProc("none");
    checkContains(task.makeCommand(), "-proc:none", true);
    task.setProc("only");
    checkContains(task.makeCommand(), "-proc:only", true);
  }

  public void testAptProcessor() {
    AjcTask task = getTask(NOFILE);
    task.setProcessor("some.SomeClass");
    checkContains(task.makeCommand(), "-processor", true);
    checkContains(task.makeCommand(), "some.SomeClass", true);
  }

  public void testAptProcessorpath() {
    AjcTask task = getTask(NOFILE);
    task.setProcessorpath("some/path");
    checkContains(task.makeCommand(), "-processorpath", true);
    checkContains(task.makeCommand(), "some/path", true);
  }

  public void testAptGeneratedDirectory() {
    AjcTask task = getTask(NOFILE);
    task.setS("some/path");
    checkContains(task.makeCommand(), "-s", true);
    checkContains(task.makeCommand(), "some/path", true);
  }

因此,也许您想在调用ajc-proc:none时在 Ant 作业中使用,以便在另一个构建步骤中避开您使用 APT 所做的事情。另一个选项是配置ajc以便它为您执行注释处理,而不是其他 APT 构建步骤。

2022-09-15T08:34:53   回复