ZmtJavaPlugin.groovy 2.21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
package de.zmt.gradle;

import org.gradle.api.*
import org.gradle.api.tasks.bundling.Jar

/**
 * Custom plugin for java projects in zmt.core
 * <p>
 * With this script a java project is set up with several custom tasks.
 */
class ZmtJavaPlugin implements Plugin<Project> {
    private static final def CONFIGURE = {
        apply plugin: 'java'
        sourceCompatibility = 1.8

        dependencies {
            testCompile group: 'junit', name: 'junit', version: '4.+'
            testCompile group: 'org.hamcrest', name: 'hamcrest-library', version: '1.3'
            testCompile 'org.mockito:mockito-core:2.+'
        }


        task('listJar') {
            group = 'Help'
            description = 'Lists the libraries this project depends on.'
            
            doLast {
                configurations.compile.each { File file -> println file.name }
            }
        }
31 32 33 34
        
        task('testJar', type: Jar) {
            group = 'Build'
            description = 'Assembles a jar archive containing the test classes'
35
            classifier 'test'
36 37
            from sourceSets.test.output
        }
38 39 40 41 42 43 44

        task('sourceJar', type: Jar) {
            group = 'Build'
            description = 'Assembles a jar archive containing sources.' 
            classifier = 'sources'
            from sourceSets.main.allSource
        }
45 46 47 48 49 50 51
        
        task('testSourceJar', type: Jar) {
            group = 'Build'
            description = 'Assembles a jar archive containing test sources.' 
            classifier = 'test-sources'
            from sourceSets.test.allSource
        }
52 53 54 55 56 57 58

        task('javadocJar', type: Jar, dependsOn: 'javadoc') {
            group = 'Build'
            description = 'Assembles a jar archive containing javadoc.'
            classifier = 'javadoc'
            from javadoc.destinationDir
        }
59 60
        
        // for referring to the test jar file from other projects
61
        configurations {
62
            create('test').extendsFrom(testCompile)
63
        }
64 65
        
        // the output of the 'testJar' task is added to the configuration
66
        artifacts {
67
            test testJar
68 69 70 71 72 73 74 75
        }
    }

    @Override
    void apply(Project project) {
        project.configure(project, CONFIGURE)
    }
}