ZmtLaunchPlugin.groovy 3.08 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 31 32 33 34 35 36 37
package de.zmt.gradle;

import org.gradle.api.*
import org.gradle.api.file.DuplicatesStrategy

/*
 * Distribution plugin for simulations based on zmt.core
 * <p>
 * With this plugin the simulation's jar file, source, dependencies
 * and the runtime dependencies are added to the distribution's lib
 * folder.
 */
public class ZmtLaunchPlugin implements Plugin<Project> {
    private static final def MAIN_CLASS_NAME = 'de.zmt.Main'

    private static final def CONFIGURE = {
        apply plugin: 'distribution'
        
        def launcherProject = rootProject.ext.properties.get('launcherProject')
        
        distributions {
            main {
                contents {
                    into ('lib') {
                        // project's own library and dependencies     
                        from (project.jar)
                        from (project.sourceJar)
                        from (project.configurations.runtime)
                    }
                    // prevent adding duplicates 
                    // e.g. when several projects depend on the same library
                    // otherwise they are added several times to archives
                    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
                }
            }
        }
        
38 39 40 41
        // if this project is the launcher
        if (project.name == 'zmt-launcher') {
            // make it an application
            configure(project, MAKE_APPLICATION)
42
        }
43 44 45
        // else if the launcher project is present
        else if (launcherProject != null) {
            // include distribution contents from that project
46 47 48 49 50 51 52 53 54 55
            launcherProject.afterEvaluate {
                project.distributions.main.contents {
                    // include launcher and dependencies without sources
                    with (copySpec {
                        with (launcherProject.distributions.main.contents)
                        exclude('*sources*.jar')
                    })
                }
            }
        }
56
        // the launcher project is not present
57
        else {
58 59 60
            // add launcher dependency and make it an application
            project.dependencies {
                runtime 'de.zmt:zmt-launcher:latest.release'
61
            }
62
            configure(project, MAKE_APPLICATION)
63 64
        }
        
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
        // make jar executable
        jar {
            manifest {
                attributes 'Main-Class': MAIN_CLASS_NAME
            }
        }
    }
    
    /** 
     * Applies the application plugin, makes the main class point to launcher
     * and adds start scripts.
     */
    private static final def MAKE_APPLICATION = {
        apply plugin: 'application'
            
        mainClassName = MAIN_CLASS_NAME
    
        startScripts {
            /* Although launcher does not depend on other simulations, those
             * are needed to be included in the class path to be launched. */
            classpath += files('$APP_HOME/lib/*')
            applicationName = 'launcher'
        }
88 89 90 91 92 93 94 95 96 97
    }
    
    @Override
    void apply(Project project) {
        project.configure(project, CONFIGURE)
    }
}