ZmtLaunchPlugin.groovy 3.69 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
            launcherProject.afterEvaluate {
                project.distributions.main.contents {
                    // include launcher and dependencies without sources
                    with (copySpec {
                        with (launcherProject.distributions.main.contents)
                        exclude('*sources*.jar')
                    })
                }
54
55
56
57
58
59
                
                /* add runtime dependency for this project in launcher 
                 * (if available) to make it executable */
                launcherProject.dependencies {
                    runtime(project)
                }
60
61
            }
        }
62
        // the launcher project is not present
63
        else {
64
65
            // add launcher dependency and make it an application
            project.dependencies {
66
                runtime 'de.zmt:zmt-launcher:latest.release'
67
            }
68
            configure(project, MAKE_APPLICATION)
69
70
        }
        
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
        // 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'
        }
94
95
96
        
        run {
            description = description + ". Arguments can be supplied via 'args' project property (-Pargs='<args>')."
97
            if (project.hasProperty('args')) {
98
99
                args(project.args.split())
            }
100
            if (project.hasProperty('workingDir')) {
101
102
                workingDir = project.workingDir
            }
103
        }
104
105
106
107
108
109
110
111
112
113
    }
    
    @Override
    void apply(Project project) {
        project.configure(project, CONFIGURE)
    }
}