Pierre De Rop | 3a00a21 | 2015-03-01 09:27:46 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 26 | import aQute.bnd.build.Workspace |
Pierre De Rop | 09d1ff4 | 2015-09-24 21:26:25 +0000 | [diff] [blame] | 27 | import aQute.bnd.osgi.Constants |
Pierre De Rop | 3a00a21 | 2015-03-01 09:27:46 +0000 | [diff] [blame] | 28 | |
Pierre De Rop | 09d1ff4 | 2015-09-24 21:26:25 +0000 | [diff] [blame] | 29 | /* Add bnd gradle plugin as a script dependency */ |
Pierre De Rop | 3a00a21 | 2015-03-01 09:27:46 +0000 | [diff] [blame] | 30 | buildscript { |
Pierre De Rop | 09d1ff4 | 2015-09-24 21:26:25 +0000 | [diff] [blame] | 31 | repositories { |
| 32 | jcenter() |
| 33 | } |
Pierre De Rop | 3a00a21 | 2015-03-01 09:27:46 +0000 | [diff] [blame] | 34 | dependencies { |
Pierre De Rop | 09d1ff4 | 2015-09-24 21:26:25 +0000 | [diff] [blame] | 35 | 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 Rop | 3a00a21 | 2015-03-01 09:27:46 +0000 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | |
| 44 | /* Initialize the bnd workspace */ |
Pierre De Rop | 09d1ff4 | 2015-09-24 21:26:25 +0000 | [diff] [blame] | 45 | Workspace.setDriver(Constants.BNDDRIVER_GRADLE) |
| 46 | Workspace.addGestalt(Constants.GESTALT_BATCH, null) |
| 47 | def workspace = new Workspace(rootDir, bnd_cnf) |
Pierre De Rop | 3a00a21 | 2015-03-01 09:27:46 +0000 | [diff] [blame] | 48 | if (workspace == null) { |
| 49 | throw new GradleException("Unable to load workspace ${rootDir}/${bnd_cnf}") |
| 50 | } |
| 51 | |
| 52 | /* Add cnf project to the graph */ |
| 53 | include bnd_cnf |
| 54 | |
| 55 | /* Start with the declared build project name */ |
| 56 | def defaultProjectName = bnd_build |
| 57 | |
| 58 | /* If in a subproject, use the subproject name */ |
| 59 | for (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 */ |
| 64 | def 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 */ |
| 77 | if ((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 */ |
| 82 | if (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 */ |
| 92 | projectNames.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 Rop | 09d1ff4 | 2015-09-24 21:26:25 +0000 | [diff] [blame] | 97 | project?.getDependson()*.getName().each { |
| 98 | include it |
Pierre De Rop | 3a00a21 | 2015-03-01 09:27:46 +0000 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /* Get the bnd project for the specified project name */ |
| 104 | def 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 Rop | 09d1ff4 | 2015-09-24 21:26:25 +0000 | [diff] [blame] | 116 | project.getWarnings().each { |
Pierre De Rop | 3a00a21 | 2015-03-01 09:27:46 +0000 | [diff] [blame] | 117 | println "Warning: ${it}" |
| 118 | } |
Pierre De Rop | 09d1ff4 | 2015-09-24 21:26:25 +0000 | [diff] [blame] | 119 | project.getErrors().each { |
Pierre De Rop | 3a00a21 | 2015-03-01 09:27:46 +0000 | [diff] [blame] | 120 | 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 | } |