ZmtLaunchPlugin.groovy 2.8 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
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
                }
            }
        }
        
        // if launcher is not available
        if (launcherProject == null) {
            // ... add dependency
            project.dependencies {
                runtime 'de.zmt:zmt-launcher:latest.release'
            }
        }
        
        // if launcher project is available and it is not this project
        if (launcherProject != null && launcherProject != project) {
            // ... include its distribution
            launcherProject.afterEvaluate {
                project.distributions.main.contents {
                    // include launcher and dependencies without sources
                    with (copySpec {
                        with (launcherProject.distributions.main.contents)
                        exclude('*sources*.jar')
                    })
                }
            }
        }
        // ... otherwise generate start scripts pointing to launcher
        else {
            apply plugin: 'application'
            
            mainClassName = MAIN_CLASS_NAME
        
            jar {
                manifest {
                    attributes 'Main-Class': mainClassName
                }
            }
            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'
            }
        }
        
    }
    
    @Override
    void apply(Project project) {
        project.configure(project, CONFIGURE)
    }
}