| 1 |
/* |
| 2 |
* Copyright 2002-2012 the original author or authors. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package org.springframework.build.gradle.propdep |
| 18 |
|
| 19 |
import org.gradle.api.* |
| 20 |
import org.gradle.api.artifacts.maven.Conf2ScopeMappingContainer |
| 21 |
import org.gradle.api.artifacts.maven.MavenPom |
| 22 |
import org.gradle.api.artifacts.maven.PomFilterContainer |
| 23 |
import org.gradle.api.plugins.MavenPlugin |
| 24 |
import org.gradle.api.tasks.* |
| 25 |
|
| 26 |
/** |
| 27 |
* Plugin to allow optional and provided dependency configurations to work with the |
| 28 |
* standard gradle 'maven' plugin |
| 29 |
* |
| 30 |
* @author Phillip Webb |
| 31 |
*/ |
| 32 |
class PropDepsMavenPlugin implements Plugin<Project> { |
| 33 |
|
| 34 |
public void apply(Project project) { |
| 35 |
project.plugins.apply(PropDepsPlugin) |
| 36 |
project.plugins.apply(MavenPlugin) |
| 37 |
|
| 38 |
Conf2ScopeMappingContainer scopeMappings = project.conf2ScopeMappings |
| 39 |
scopeMappings.addMapping(MavenPlugin.COMPILE_PRIORITY + 1, |
| 40 |
project.configurations.getByName("provided"), Conf2ScopeMappingContainer.PROVIDED) |
| 41 |
|
| 42 |
// Add a temporary new optional scope |
| 43 |
scopeMappings.addMapping(MavenPlugin.COMPILE_PRIORITY + 2, |
| 44 |
project.configurations.getByName("optional"), "optional") |
| 45 |
|
| 46 |
// Add a hook to replace the optional scope |
| 47 |
project.tasks.withType(Upload).each{ applyToUploadTask(project, it) } |
| 48 |
} |
| 49 |
|
| 50 |
private void applyToUploadTask(Project project, Upload upload) { |
| 51 |
upload.repositories.withType(PomFilterContainer).each{ applyToPom(project, it) } |
| 52 |
} |
| 53 |
|
| 54 |
private void applyToPom(Project project, PomFilterContainer pomContainer) { |
| 55 |
pomContainer.pom.whenConfigured{ MavenPom pom -> |
| 56 |
pom.dependencies.findAll{ it.scope == "optional" }.each { |
| 57 |
it.scope = "compile" |
| 58 |
it.optional = true |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
} |