问题
I have set up weaving successfully in Eclipse via the AJDT plugin and in my gradle build via the gradle-aspectj plugin (which took quite some time...).
In Eclipse this works both for production and test code, i.e. all tests pass. When I run the gradle build, the resulting app also works fine and I can see that aspects are working as expected.
The gradle "test" task however fails as many tests fail. I can track most of the failures back to some aspect (here: for spring transactions) not working or some ajc-compiler option for encoding not being active (see here for details). The same tests do run fine when triggered from Eclipse.
Is some additional configuration required in order to get the weaving to work for tests as well?
I found some related question, however this didn't resolve the issue for me. It still looks like neither aspects are picked up, nor compiler options are active (I see encoding errors in tests only).
My (abbreviated) build.gradle (Note: I had a hard time getting weaving to work at all so this probably contains some unnecessary configuration):
buildscript
{
dependencies
{
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.6.RELEASE")
classpath("nl.eveoh:gradle-aspectj:1.6")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'
apply plugin: 'aspectj'
configurations {
runtime
testCompile {
extendsFrom testAspectpath
}
aspectpath
testAspectpath
ajInpath
compile {
extendsFrom aspectpath
}
}
dependencies
{
// Non aspect dependencies
// ...
// Dependencies that require weaving - works for compile but not for test task
aspectpath("org.springframework:spring-context:4.2.1.RELEASE")
compile("org.springframework:spring-context:4.2.1.RELEASE")
aspectpath("org.springframework:spring-context-support:4.2.1.RELEASE")
compile("org.springframework:spring-context-support:4.2.1.RELEASE")
compile 'com.vaadin:vaadin-spring-boot-starter:1.0.0'
testCompile("org.springframework.boot:spring-boot-starter-test:${springVersion}")
// Spring Data Neo4j
compile "org.springframework.data:spring-data-neo4j:${springDataGraphVersion}"
// Additional aspects - also need to be configured in ADJT
aspectpath "org.aspectj:aspectjtools:${aspectjVersion}"
compile "org.aspectj:aspectjrt:${aspectjVersion}"
testCompile "org.aspectj:aspectjrt:${aspectjVersion}"
compile "org.springframework:spring-aspects:4.2.1.RELEASE"
aspectpath "org.springframework:spring-aspects:4.2.1.RELEASE"
compile "org.springframework:spring-instrument:3.2.1.RELEASE"
aspectpath "org.springframework:spring-instrument:3.2.1.RELEASE"
compile "org.springframework.data:spring-data-neo4j-aspects:${springDataGraphVersion}"
aspectpath "org.springframework.data:spring-data-neo4j-aspects:${springDataGraphVersion}"
// Required by spring aspects
compile "org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.1.Final"
aspectpath "org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.1.Final"
compile "javax.persistence:persistence-api:1.0"
aspectpath "javax.persistence:persistence-api:1.0"
testAspectpath sourceSets.main.output // from related question [4]
}
compileAspect {
// Works in compile but not in test task!
additionalAjcArgs = ['encoding' : 'UTF-8']
}
Edit: Encoding problem has been solved with snippet from kriegaex. The problem of aspects that are not present in tests when run from gradle still remains. Most tests fail because of a
org.neo4j.graphdb.NotInTransactionException
which indicates that the method based annotation
@Transactional
is not effective. Any thoughts?
回答1:
I have not tried but according to the plugin description I think you should add this to your Gradle config file:
compileTestAspect {
additionalAjcArgs = ['encoding' : 'UTF-8']
}
来源:https://stackoverflow.com/questions/34474273/gradle-aspectj-weaving-ajc-compiler-options-working-in-compile-but-not-in-tes