ZmtLaunchPlugin.groovy 4.1 KB
Newer Older
1 2 3 4 5 6
package de.zmt.gradle;

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

/*
7
 * Launch plugin for simulations based on zmt-core.
8 9 10
 * <p>
 * With this plugin the simulation's jar file, source, dependencies
 * and the runtime dependencies are added to the distribution's lib
11
 * folder. It also adds start scripts for the launcher.
12 13
 */
public class ZmtLaunchPlugin implements Plugin<Project> {
14 15
    public static final def LAUNCHER_DEPENDENCY_PROPERTY_NAME = 'launcherDependency'

16
    private static final def MAIN_CLASS_NAME = 'de.zmt.Main'
17
    
18 19 20
    private static final def CONFIGURE = {
        apply plugin: 'distribution'
        
21 22 23 24 25 26
        distributions.main.contents {
            into ('lib') {
                // project's own library and dependencies     
                from (project.jar)
                from (project.sourceJar)
                from (project.configurations.runtime)
27
            }
28 29 30 31 32 33 34
            
            
            
            // prevent adding duplicates 
            // e.g. when several projects depend on the same library
            // otherwise they are added several times to archives
            duplicatesStrategy = DuplicatesStrategy.EXCLUDE
35 36
        }
        
37
        // properties map will return null if launcher project is not set
38
        def launcherProject = rootProject.ext.properties.get('launcherProject')
39 40 41 42
        // if this project is the launcher
        if (project.name == 'zmt-launcher') {
            // make it an application
            configure(project, MAKE_APPLICATION)
43
        }
44 45 46
        // else if the launcher project is present
        else if (launcherProject != null) {
            // include distribution contents from that project
47 48 49 50 51 52 53 54
            launcherProject.afterEvaluate {
                project.distributions.main.contents {
                    // include launcher and dependencies without sources
                    with (copySpec {
                        with (launcherProject.distributions.main.contents)
                        exclude('*sources*.jar')
                    })
                }
55 56 57 58 59 60
                
                /* add runtime dependency for this project in launcher 
                 * (if available) to make it executable */
                launcherProject.dependencies {
                    runtime(project)
                }
61 62
            }
        }
63
        // the launcher project is not present
64
        else {
65
            // add launcher dependency and make it an application
66 67 68 69 70 71 72 73 74
            afterEvaluate {
                def launcherDependency = project.properties.get(LAUNCHER_DEPENDENCY_PROPERTY_NAME)
                // if not set by project, use latest
                if (launcherDependency == null) {
                    launcherDependency = 'de.zmt:zmt-launcher:latest.release'
                }
                project.dependencies {
                    runtime launcherDependency
                }
75
            }
76
            configure(project, MAKE_APPLICATION)
77 78
        }
        
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
        // 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'
        }
102 103 104
        
        run {
            description = description + ". Arguments can be supplied via 'args' project property (-Pargs='<args>')."
105
            if (project.hasProperty('args')) {
106 107
                args(project.args.split())
            }
108
            if (project.hasProperty('workingDir')) {
109 110
                workingDir = project.workingDir
            }
111
        }
112 113 114 115 116 117 118 119 120 121
    }
    
    @Override
    void apply(Project project) {
        project.configure(project, CONFIGURE)
    }
}