使用Gradle构建多项目中的AspectJ

时间:2018-03-02 14:33:01

标签: gradle aspectj

开发环境:Eclipse,Gradle插件,AspectJ插件,WiildFly 11
项目结构:核心项目和webapp项目,这两个单独的项目在Eclipse中添加了Gradle和AspectJ 问题:执行Gradle war任务失败

堆栈跟踪:

:core:compileAspect[ant:iajc] abort ABORT -- (RuntimeException) Problem 

processing attributes in C:\...\core\build\classes\...\AbstractClassInCore.class
Problem processing attributes in C:\...\core\build\classes\...\AbstractClassInCore.class
java.lang.RuntimeException: Problem processing attributes in C:\...\core\build\classes\...\AbstractClassInCore.class
    at org.aspectj.weaver.bcel.BcelObjectType.ensureAspectJAttributesUnpacked(BcelObjectType.java:387)
    at org.aspectj.weaver.bcel.BcelObjectType.<init>(BcelObjectType.java:162)
    at org.aspectj.weaver.bcel.BcelWorld.buildBcelDelegate(BcelWorld.java:410)
    at org.aspectj.weaver.bcel.BcelWorld.addSourceObjectType(BcelWorld.java:478)
    at org.aspectj.weaver.bcel.BcelWorld.addSourceObjectType(BcelWorld.java:453)
    at org.aspectj.weaver.bcel.BcelWeaver.addClassFile(BcelWeaver.java:456)
    at org.aspectj.weaver.bcel.BcelWeaver.addClassFile(BcelWeaver.java:480)
    at org.aspectj.ajdt.internal.core.builder.AjBuildManager.initBcelWorld(AjBuildManager.java:904)
    at 
    ... (shortened)
Caused by: java.lang.RuntimeException: bad WeaverState.Kind: -115.  File was :<Unknown>::0
    at org.aspectj.weaver.WeaverStateInfo.read(WeaverStateInfo.java:171)
    at org.aspectj.weaver.AjAttribute.read(AjAttribute.java:105)
    at org.aspectj.weaver.bcel.Utility.readAjAttributes(Utility.java:101)
    at org.aspectj.weaver.bcel.BcelObjectType.ensureAspectJAttributesUnpacked(BcelObjectType.java:383)
    ... 131 more
 FAILED

上述文件是一个抽象方面,它在webapp中实现。所以:

public abstract aspect AbstractClassInCore {
  @Before
  public void beforeMethod(ProceedingJoinPoint joinPoint) {}
}

public aspect ImplClassInWebApp extends AbstractClassInCore {}

构建脚本核心项目(缩短):

buildscript {
  repositories {
    maven {
      url "https://plugins.gradle.org/m2/"
    }
  }
  dependencies {
    classpath "gradle.plugin.aspectj:gradle-aspectj:0.1.6"
  }
  project.ext {
    aspectjVersion = '1.8.13'
  }
}

apply plugin: "eclipse"
apply plugin: "java"
apply plugin: 'eclipse-wtp'
apply plugin: "aspectj.gradle"

compileJava {  options.encoding = "UTF-8"  }

repositories {
  mavenCentral()
}

// to move some resources to correct location
jar {
  from ('./src/main/java') {
    include 'META-INF/resources/**/*.*'
  }
}

dependencies {
  // ... other dependencies
  // AspectJ
  compile group: 'org.aspectj', name: 'aspectjrt', version: '1.8.13'
}

构建脚本webapp项目(缩短):

buildscript {
  repositories {
    maven {
      url "https://plugins.gradle.org/m2/"
    }
  }
  dependencies {
    classpath "gradle.plugin.aspectj:gradle-aspectj:0.1.6"
  }
  project.ext {
    aspectjVersion = '1.8.13'
  }
}

apply plugin: "eclipse"
apply plugin: "java"
apply plugin: "war"
apply plugin: "aspectj.gradle"

compileJava {  options.encoding = "UTF-8"  }

repositories {
  mavenCentral()
}

dependencies {
  compile project(':core')
  compile project(':other-subproject') // has no aspects
}

我搜索了两个关键的例外情况。据我所知,当人们试图再次编织一个已编织的类时会发生这种情况。因此,我得出结论,原因在于多项目设置,应该在Gradle构建脚本中修复。我尝试了几次修改,但情况没有改变。例如,从任一脚本中删除插件并没有对war任务产生影响。但是,它会在最终的课程中产生影响,这将会错过这个方面。

刷新gradle操作和WildFly服务器上的webapp部署正常运行。这表明我想要的应该工作,但显然这两个过程的行为并不相同。 Eclipse仍然可以导出.war,但这对于持续集成不起作用,并且不太方便。

0 个答案:

没有答案