我有一个Gradle项目,该项目利用了已导入Eclipse(STS)的AOP(来自Spring 3.x.x)。当我使用Gradle的上下文菜单刷新依赖项/重建源时,我必须转换为AspectJ项目,以使我能够正确运行测试(AspectJ Runtime库不在构建路径上,无法满足工厂中的方法bean定义)。我在任何地方都没有spring-aspects.jar,它可以毫无问题地部署到Tomcat(同样,在libs /文件夹中没有AspectJ)。
<bean id="fooBarAspect" class="foo.Bar" factory-method="aspectOf" >
此过程有效,但很痛苦,因为它使我每次需要刷新依赖关系并运行集成测试时都进行两次重建。
dependencies {
ajc 'org.aspectj:aspectjtools:1.7.3'
aspects 'org.springframework:spring-aspects:3.2.4.RELEASE'
compile (
'org.aspectj:aspectjrt:1.7.3'
)
}
有什么想法吗?
最佳答案
对于AspectJ:
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'eclipse'
apply plugin: 'eclipse-wtp'
ext.aspectjVersion = '1.7.4'
configurations {
ajc
aspects
aspectCompile
ajInpath
compile {
extendsFrom aspects
}
}
compileJava {
sourceCompatibility="1.7"
targetCompatibility="1.7"
doLast{
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",
inpath: configurations.ajInpath.asPath,
aspectPath:configurations.aspects.asPath,
sourceRootCopyFilter:"**/.svn/*,**/*.java",
classpath:"${configurations.compile.asPath};${configurations.aspectCompile.asPath}"){
sourceroots{
sourceSets.main.java.srcDirs.each{
pathelement(location:it.absolutePath)
}
}
}
}
}
dependencies {
ajc "org.aspectj:aspectjtools:1.7.3"
compile "org.aspectj:aspectjrt:1.7.3"
aspects group: 'org.springframework', name: 'spring-aspects', version: springVersion
aspectCompile group: 'org.hibernate.javax.persistence', name: 'hibernate-jpa-2.0-api', version: '1.0.0.Final'
aspectCompile group: 'org.springframework', name: 'spring-tx', version: springVersion
aspectCompile group: 'org.springframework', name: 'spring-orm', version: springVersion
}
对于Eclipse,我们必须做一点手动代码:
eclipseClasspath {
withXml { xmlProvider ->
def classpath = xmlProvider.asNode()
def parser = new XmlParser()
classpath.classpathentry.findAll{ entry ->
entry.@kind == 'var' && configurations.runtime.find {entry.@path.endsWith(it.name) && !entry.@path.contains('servlet-api')
}.each { entry ->
def attrs = entry.attributes ?: parser.createNode(entry, 'attributes', [:])
parser.createNode(attrs, 'attribute', [name: 'org.eclipse.jst.component.dependency', value: '../'])
}
}
}
关于eclipse - 使用AspectJ配置进行Eclipse Gradle导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23224300/