4

我正在将一个依赖于@Configurable Spring 注释的项目的 Maven 构建迁移到 Gradle,但是当我的(Web)应用程序运行时,没有一个 @Configurable 类被注入到 Gradle 构建下(它们在构建我的 Maven 时工作良好)。

在 Maven 中,我使用了以下插件:

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.4</version>

        <executions>
            <execution>
                <goals>
                    <goal>compile</goal>
                    <goal>test-compile</goal>
                </goals>
            </execution>
        </executions>

        <configuration>
            <aspectLibraries>
                <aspectLibrary>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-aspects</artifactId>
                </aspectLibrary>
            </aspectLibraries>
            <source>${java.version}</source>
            <target>${java.version}</target>
        </configuration>
    </plugin>

对于 Gradle 1.0,我调整了 0.9 ajc 插件(下面的 url),但无法弄清楚如何将 configuration/aspectLibraries/spring-aspects 添加到此:

    apply plugin: 'war'
apply plugin: 'jetty'

sourceCompatibility = 1.6
version = 1.0
// Based on: http://github.com/breskeby/gradleplugins/raw/0.9-upgrade/aspectjPlugin/aspectJ.gradle    
configurations {
    ajc
    aspects
    ajInpath
}

task compileJava(dependsOn: JavaPlugin.PROCESS_RESOURCES_TASK_NAME, overwrite: true)  {
    dependsOn configurations.ajc.getTaskDependencyFromProjectDependency(true, "compileJava")

    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",
                aspectPath:configurations.aspects.asPath, inpath:configurations.ajInpath.asPath, sourceRootCopyFilter:"**/.svn/*,**/*.java",classpath:configurations.compile.asPath){
            sourceroots{
                sourceSets.main.java.srcDirs.each{
                    pathelement(location:it.absolutePath)
                }
            }
        }
    }
}

dependencies {
    ajc     group: 'org.aspectj',                     name: 'aspectjtools',                   version: '1.6.12'
    compile group: 'org.aspectj',                     name: 'aspectjrt',                      version: '1.6.12'
    compile group: 'org.aspectj',                     name: 'aspectjweaver',                  version: '1.6.12'

    compile group: 'org.springframework',             name: 'spring-jdbc',                    version: '3.1.1.RELEASE'
    compile group: 'org.springframework',             name: 'spring-orm',                     version: '3.1.1.RELEASE'
    compile group: 'org.springframework',             name: 'spring-aop',                     version: '3.1.1.RELEASE'
    compile group: 'org.springframework',             name: 'spring-aspects',                 version: '3.1.1.RELEASE'
    compile group: 'org.springframework',             name: 'spring-web',                     version: '3.1.1.RELEASE'
    compile group: 'org.springframework',             name: 'spring-webmvc',                  version: '3.1.1.RELEASE'
    compile group: 'org.springframework',             name: 'spring-expression',              version: '3.1.1.RELEASE'
}

我需要做什么才能让 spring-aspects 在 Gradle 构建的 Web 应用程序中工作?

谢谢

4

1 回答 1

1

我知道这篇文章已有 5 年的历史,但我有答案,而且互联网上似乎没有其他人知道。不过,我正在使用 Gradle 3.3(让我休息一下,现在是 2017 年)。这是我的 Gradle 构建文件,用于使 AWS SWF 工作流和 @Asynchronous 标签一起工作。

buildscript {
    repositories {
        maven {
            url "https://maven.eveoh.nl/content/repositories/releases"
        }
    }

    dependencies {
        classpath "nl.eveoh:gradle-aspectj:1.6"
    }
}

project.ext {
    aspectjVersion = '1.8.9'
}

apply plugin: 'aspectj'
apply plugin: 'java'

dependencies {
    compile group: 'org.aspectj', name: 'aspectjrt', version:'1.8.9'
    compile group: 'org.aspectj', name: 'aspectjtools', version:'1.8.9'
    compile group: 'org.freemarker', name: 'freemarker', version:'2.3.25-incubating'
    compile group: 'com.amazonaws', name: 'aws-java-sdk-swf-libraries', version:'1.11.22'
    compile group: 'com.amazonaws', name: 'aws-swf-build-tools', version:'1.1'
    compile(group: 'org.springframework.boot', name: 'spring-boot-starter', version:'1.4.0.RELEASE') {
        exclude(module: 'commons-logging')
    }
    compile group: 'org.springframework.boot', name: 'spring-boot-configuration-processor', version:'1.4.0.RELEASE'
    testCompile(group: 'org.springframework.boot', name: 'spring-boot-starter-test', version:'1.4.0.RELEASE') {
        exclude(module: 'commons-logging')
    }
    aspectpath group: 'com.amazonaws', name: 'aws-java-sdk-swf-libraries', version:'1.11.22'
}

吸引我的关键是将方面路径添加到依赖项中。我花了很长时间才弄清楚这一点。

于 2017-01-20T03:03:31.377 回答