Integration test + aspectJ + gradle

2019-09-03 11:57发布

I was working with maven and aspectj plugin and was fine, recently I move to gradle, I already have the configuration to build/compile with aspects and that is working fine, but for integration tests are not working fine. I have the following configuration

sourceSets {
    main {
        java {
            srcDir 'src/main/java'
        }
        resources {
            srcDir "src/main/resources/"
        }
    }

    integrationTest {
        java {
            compileClasspath += main.output + test.output
            runtimeClasspath += main.output + test.output
            srcDir file('src/test/it')
        }
        resources.srcDir file('src/test/resources')
    }

    generated {
        java {
            srcDirs = [
                "src/main/generated"
            ]
        }
        compileClasspath += sourceSets.main.output
    }
}

but the aspects are not compiled/working correctly in the integration test. what I tried to do is the following configuration

compileIntegrationTestJava{

    sourceCompatibility="1.7"
    targetCompatibility="1.7"
    dependsOn generateQueryDSL
    source generateQueryDSL.destinationDir

    dependsOn configurations.ajc.getTaskDependencyFromProjectDependency(true, "compileJava")

    doLast{
        ant.taskdef( resource:"org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties", classpath: configurations.ajc.asPath)
        ant.iajc(source:"1.7", target:"1.7",
                destDir:sourceSets.integrationTest.output.classesDir.absolutePath, maxmem:"512m", fork:"true",
                aspectPath:configurations.aspects.asPath,
                sourceRootCopyFilter:"**/.svn/*,**/*.java",classpath:configurations.compile.asPath){
            sourceroots{
                sourceSets.main.java.srcDirs.each{
                    pathelement(location:it.absolutePath)
                }
                sourceSets.integrationTest.java.srcDirs.each{
                    pathelement(location:it.absolutePath)
                }
            }
        }
    }
}

And I get this error.

[ant:iajc] [error] build config error: bad sourceroot: \src\integrationTest\java

If I change the line sourceSets.integrationTest.java.srcDirs.each for sourceSets.test.java.srcDirs.each, the IT under /src/test/it are not being compiled and are not moved to the correct package, BUT if I put my IT test under /src/test/java that compile correctly.

I would like to know how to put the correct sourceSets to point src/test/it and how to add to the classpath some classes instead of add

sourceroots{
                sourceSets.main.java.srcDirs.each

As this makes the whole classes be added to the clasess/test/ directory

If you have any link or guide to do this, will be truly appreciate

1条回答
来,给爷笑一个
2楼-- · 2019-09-03 12:13

I tackled this and got it working I believe.

When you say:

I would like to know how to put the correct sourceSets to point src/test/it and how to add to the classpath some classes instead of add sourceroots{...

Do you mean 'why do you need to write all that code to add a srcDir'? I'm with you!

What I believe got this working for me is:

configurations {
    querydslapt
    integrationTestCompile.extendsFrom testCompile
    integrationTestRuntime.extendsFrom testRuntime

    ajc
    aspectpath
}

dependencies {
    ...
    compile group: 'org.aspectj', name: 'aspectjrt', version: '1.8.9'
    compile group: 'org.aspectj', name: 'aspectjweaver', version: '1.8.9'
    aspectpath group: 'org.springframework', name: 'spring-aspects', version: '4.3.0.RELEASE'
    ajc 'org.aspectj:aspectjtools:1.8.9'
}

Where I'm only weaving with Spring-aspects such as for transactions.

compileIntegrationTestJava {
    dependsOn generateQueryDSL
    classpath += sourceSets.main.runtimeClasspath
    sourceCompatibility = "1.8"
    targetCompatibility = "1.8"

    doLast {
        ant.taskdef(resource: "org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties", classpath: configurations.ajc.asPath)
        ant.iajc(source: "1.8", target: "1.8",
                maxmem: "512m", fork: "true",
                destDir: sourceSets.integrationTest.output.classesDir.absolutePath,
                aspectPath: configurations.aspectpath.asPath,
                classpath: classpath.asPath) {
            sourceroots {
                sourceSets.integrationTest.java.srcDirs.each { sd ->
                    if (file(sd).exists()) {
                        pathelement(location: sd.absolutePath)
                    }
                }
            }
        }
    }

The tricky part was the need to add:

                    if (file(sd).exists()) {

That was to eliminate the extra non-existant path. That is:

sourceSets.integrationTest.java.srcDirs contains (absolute paths to):

  • src/integrationTest/java and
  • src/integration-test/java

And before I put in this guard ajc was failing with [ant:iajc] [error] build config error: bad sourceroot: \src\integrationTest\java just like you. I determined this by adding this debug task (very helpful):

task printprojectDebug {
    doLast {
        sourceSets.integrationTest.java.srcDirs.each { println it }
        println '---- configurations.integrationTestCompile.asPath: '
        println configurations.integrationTestCompile.asPath
        println '---- configurations.aspectpath.asPath:'
        println configurations.aspectpath.asPath
        println 'end'
    }
}

and run gradle printprojectDebug.

I'm still testing to see if this is working as I'm getting errors that I haven't fully determined if is from aspect weaving problems. But it should get you working again.

Hope my feedback helps (anyone!).

查看更多
登录 后发表回答