Java – split main and test in gradle’s eclipse build

Today, I tried to switch integration testing from Maven to gradle Everything is fine except that I have a serious problem with TestNG

The project uses hibernate / JPA2 for database access, and some rely on test / resources / meta inf / persistence Test of persistence unit in XML When I run the test suite using gradle, everything is fine But when I run XML (or any test class) from eclipse, it seems to try to use main / resources / meta - inf / persistence. XML xml.

Because I do most of the work with TDD, I really need to run / debug tests from eclipse When I add a persistence unit to production persistence XML, it works (it can even get some other resources from the "test" directory) This will be a solution, but I really don't like the idea of adding test resources to the "main / resources"

When I use the old POM in Maven When XML is imported into it, the same project works normally

Build gradle

apply plugin: 'java'
apply plugin: 'eclipse'

sourceCompatibility = 1.7
version = '0.1'
jar {
    manifest {
        attributes 'Implementation-Title': 'md','Implementation-Version': version
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.slf4j:slf4j-api:1.7.5'
    compile 'com.google.guava:guava:14.0.1'
    compile 'org.hibernate:hibernate-entitymanager:4.2.2.Final'
    compile 'org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.1.Final'

    testCompile 'ch.qos.logback:logback-classic:1.0.13'
    testCompile 'org.testng:testng:6.8.5'
    testCompile 'org.dbunit:dbunit:2.4.9'
    testCompile 'org.mockito:mockito-all:1.9.5'
    testCompile 'org.easytesting:fest-assert-core:2.0M10'
    testCompile 'org.hsqldb:hsqldb:2.2.9'

}

test {
    useTestNG(){    
        suites 'src/test/resources/testng.xml' 
    } 
}

to update:

The generated classpath file is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src/main/java"/>
    <classpathentry kind="src" path="src/main/resources"/>
    <classpathentry kind="src" path="src/test/java"/>
    <classpathentry kind="src" path="src/test/resources"/>
    <classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
    <classpathentry exported="true" kind="con" path="org.springsource.ide.eclipse.gradle.classpathcontainer"/>
    <classpathentry kind="output" path="bin"/>
</classpath>

It seems to merge everything into the bin folder and doesn't distinguish between main and test, just like Maven generated The classpath file is the same

Solution

The problem is that gradle's Eclipse Plug-in merges tests and home folders by default So main's persistence XML does cover the beta version

Adding the following code will solve this problem by changing the output directory of the generated classpath entries and deleting entries using the default output

import org.gradle.plugins.ide.eclipse.model.sourceFolder 
eclipse.classpath.file {
    beforeMerged { classpath -> 
        classpath.entries.clear()
    }
    whenMerged {  cp -> 
        cp.entries.findAll { it instanceof SourceFolder && it.path.startsWith("src/main/") }*.output = "bin/main" 
        cp.entries.findAll { it instanceof SourceFolder && it.path.startsWith("src/test/") }*.output = "bin/test" 
        cp.entries.removeAll { it.kind == "output" }
    }
}

Update: changed related classpath entries

<classpathentry output="bin/main" kind="src" path="src/main/java"/>
<classpathentry output="bin/main" kind="src" path="src/main/resources"/>
<classpathentry output="bin/test" kind="src" path="src/test/java"/>
<classpathentry output="bin/test" kind="src" path="src/test/resources"/>
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>