| 1 |
/** |
| 2 |
* Copyright 2015 Emmanuel Bourg |
| 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.debian.gradle; |
| 18 |
|
| 19 |
import java.util.HashMap; |
| 20 |
import java.util.Map; |
| 21 |
|
| 22 |
import org.debian.gradle.tasks.MavenPom; |
| 23 |
import org.gradle.api.Action; |
| 24 |
import org.gradle.api.Plugin; |
| 25 |
import org.gradle.api.Project; |
| 26 |
import org.gradle.api.Task; |
| 27 |
import org.gradle.api.artifacts.repositories.MavenArtifactRepository; |
| 28 |
import org.gradle.api.invocation.Gradle; |
| 29 |
import org.gradle.api.tasks.javadoc.Javadoc; |
| 30 |
import org.gradle.external.javadoc.StandardJavadocDocletOptions; |
| 31 |
import org.slf4j.Logger; |
| 32 |
import org.slf4j.LoggerFactory; |
| 33 |
|
| 34 |
/** |
| 35 |
* Gradle plugin installing the Debian repository before those defined in the build script. |
| 36 |
*/ |
| 37 |
public class DebianHelperPlugin implements Plugin<Gradle> { |
| 38 |
|
| 39 |
private final Logger log = LoggerFactory.getLogger(DebianHelperPlugin.class); |
| 40 |
|
| 41 |
@Override |
| 42 |
public void apply(Gradle gradle) { |
| 43 |
// add the Debian repository to all projects |
| 44 |
gradle.allprojects(new Action<Project>() { |
| 45 |
@Override |
| 46 |
public void execute(final Project project) { |
| 47 |
log.info("\tAdding Debian repository to project '" + project.getName() + "'"); |
| 48 |
Map<String, String> repositoryParams = new HashMap<>(); |
| 49 |
repositoryParams.put("name", "Debian Maven Repository"); |
| 50 |
repositoryParams.put("url", "file:/usr/share/maven-repo/"); |
| 51 |
|
| 52 |
MavenArtifactRepository repository = project.getRepositories().mavenCentral(repositoryParams); |
| 53 |
project.getRepositories().remove(repository); |
| 54 |
project.getRepositories().addFirst(repository); |
| 55 |
project.getBuildscript().getRepositories().addFirst(repository); |
| 56 |
} |
| 57 |
}); |
| 58 |
|
| 59 |
// generate the Maven pom after building a project |
| 60 |
gradle.allprojects(new Action<Project>() { |
| 61 |
@Override |
| 62 |
public void execute(final Project project) { |
| 63 |
project.afterEvaluate(new Action<Project>() { |
| 64 |
@Override |
| 65 |
public void execute(Project project) { |
| 66 |
if (project.getPluginManager().hasPlugin("java")) { |
| 67 |
log.info("\tAdding Maven pom generation to project '" + project.getName() + "'"); |
| 68 |
project.getPluginManager().apply("maven"); |
| 69 |
Map<String, Class> debianMavenPomTaskOptions = new HashMap<String, Class>(); |
| 70 |
debianMavenPomTaskOptions.put("type", MavenPom.class); |
| 71 |
project.task(debianMavenPomTaskOptions, "debianMavenPom"); |
| 72 |
((Task) project.getTasksByName("jar", false).toArray()[0]).dependsOn("debianMavenPom"); |
| 73 |
} |
| 74 |
} |
| 75 |
}); |
| 76 |
} |
| 77 |
}); |
| 78 |
|
| 79 |
// link the javadoc with the system documentation |
| 80 |
gradle.allprojects(new Action<Project>() { |
| 81 |
@Override |
| 82 |
public void execute(final Project project) { |
| 83 |
project.afterEvaluate(new Action<Project>() { |
| 84 |
@Override |
| 85 |
public void execute(Project project) { |
| 86 |
if (project.getPluginManager().hasPlugin("java")) { |
| 87 |
Javadoc javadocTask = ((Javadoc) (project.getTasksByName("javadoc", false).toArray()[0])); |
| 88 |
if (javadocTask.getOptions() instanceof StandardJavadocDocletOptions) { |
| 89 |
log.info("\tLinking the generated javadoc to the system JDK API documentation"); |
| 90 |
((StandardJavadocDocletOptions) javadocTask.getOptions()).getLinks().add(0, "file:///usr/share/doc/default-jdk/api"); |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
}); |
| 95 |
} |
| 96 |
}); |
| 97 |
|
| 98 |
// start a timer displaying a message periodically to prevent the builders from killing Gradle on slow architectures |
| 99 |
gradle.addBuildListener(new KeepAliveTimer()); |
| 100 |
|
| 101 |
// initialize the project name if necessary |
| 102 |
gradle.addBuildListener(new ProjectNameInitializer()); |
| 103 |
} |
| 104 |
} |
| 105 |
|