blob: 90f6d5dbdf92cb56acdc13d4b217f7fccd9ff72d [file] [log] [blame]
Pierre De Rop3a00a212015-03-01 09:27:46 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20/*
21 * Master Gradle initialization script
22 *
23 * Depends on bnd_* values from gradle.properties.
24 */
25
26import aQute.bnd.build.Workspace
Pierre De Rop09d1ff42015-09-24 21:26:25 +000027import aQute.bnd.osgi.Constants
Pierre De Rop3a00a212015-03-01 09:27:46 +000028
Pierre De Rop09d1ff42015-09-24 21:26:25 +000029/* Add bnd gradle plugin as a script dependency */
Pierre De Rop3a00a212015-03-01 09:27:46 +000030buildscript {
Pierre De Rop09d1ff42015-09-24 21:26:25 +000031 repositories {
32 jcenter()
33 }
Pierre De Rop3a00a212015-03-01 09:27:46 +000034 dependencies {
Pierre De Rop09d1ff42015-09-24 21:26:25 +000035 classpath bnd_plugin
36 }
37 /* Pass bnd gradle plugin classpath to rootProject once created */
38 def bndPlugin = files(configurations.classpath.files)
39 gradle.rootProject { rootProject ->
40 rootProject.ext.bndPlugin = bndPlugin
Pierre De Rop3a00a212015-03-01 09:27:46 +000041 }
42}
43
44/* Initialize the bnd workspace */
Pierre De Rop09d1ff42015-09-24 21:26:25 +000045Workspace.setDriver(Constants.BNDDRIVER_GRADLE)
46Workspace.addGestalt(Constants.GESTALT_BATCH, null)
47def workspace = new Workspace(rootDir, bnd_cnf)
Pierre De Rop3a00a212015-03-01 09:27:46 +000048if (workspace == null) {
49 throw new GradleException("Unable to load workspace ${rootDir}/${bnd_cnf}")
50}
51
52/* Add cnf project to the graph */
53include bnd_cnf
54
55/* Start with the declared build project name */
56def defaultProjectName = bnd_build
57
58/* If in a subproject, use the subproject name */
59for (def currentDir = startParameter.currentDir; currentDir != rootDir; currentDir = currentDir.parentFile) {
60 defaultProjectName = currentDir.name
61}
62
63/* Build a set of project names we need to include from the specified tasks */
64def projectNames = startParameter.taskNames.collect { taskName ->
65 def elements = taskName.split(':')
66 switch (elements.length) {
67 case 1:
68 return defaultProjectName
69 case 2:
70 return elements[0].empty ? bnd_build : elements[0]
71 default:
72 return elements[0].empty ? elements[1] : elements[0]
73 }
74}.toSet()
75
76/* Include the default project name if in a subproject or no tasks specified */
77if ((startParameter.currentDir != rootDir) || projectNames.empty) {
78 projectNames += defaultProjectName
79}
80
81/* If bnd_build used but declared empty, add all non-private folders of rootDir */
82if (projectNames.remove('')) {
83 rootDir.eachDir {
84 def projectName = it.name
85 if (!projectName.startsWith('.')) {
86 projectNames += projectName
87 }
88 }
89}
90
91/* Add each project and its dependencies to the graph */
92projectNames.each { projectName ->
93 // Don't build the org.apache.felix.dependencymanager.benchmark, which requires java8 (build the benchmark bundle only makes sense within eclipse
94 if (! projectName.equals("org.apache.felix.dependencymanager.benchmark")) {
95 include projectName
96 def project = getBndProject(workspace, projectName)
Pierre De Rop09d1ff42015-09-24 21:26:25 +000097 project?.getDependson()*.getName().each {
98 include it
Pierre De Rop3a00a212015-03-01 09:27:46 +000099 }
100 }
101}
102
103/* Get the bnd project for the specified project name */
104def getBndProject(Workspace workspace, String projectName) {
105 def project = workspace.getProject(projectName)
106 if (project == null) {
107 return null
108 }
109 project.prepare()
110 if (project.isValid()) {
111 return project
112 }
113
114 project.getInfo(workspace, "${rootDir} :")
115 def errorCount = 0
Pierre De Rop09d1ff42015-09-24 21:26:25 +0000116 project.getWarnings().each {
Pierre De Rop3a00a212015-03-01 09:27:46 +0000117 println "Warning: ${it}"
118 }
Pierre De Rop09d1ff42015-09-24 21:26:25 +0000119 project.getErrors().each {
Pierre De Rop3a00a212015-03-01 09:27:46 +0000120 println "Error : ${it}"
121 errorCount++
122 }
123 if (!project.isOk()) {
124 def str = 'even though no errors were reported'
125 if (errorCount == 1) {
126 str = 'one error was reported'
127 } else if (errorCount > 1) {
128 str = "${errorCount} errors were reported"
129 }
130 throw new GradleException("Project ${rootDir}/${projectName} is invalid, ${str}")
131 }
132 throw new GradleException("Project ${rootDir}/${projectName} is not a valid bnd project")
133}