Debian Codegraph @

gradle-helper-plugin/src/main/java/org/debian/gradle/tasks/MavenPom.java

raw
1 /**
2 * Copyright 2016 Kai-Chung Yan (殷啟聰)
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.tasks;
18
19 import org.codehaus.groovy.runtime.MethodClosure;
20 import org.codehaus.groovy.runtime.NullObject;
21 import org.gradle.api.DefaultTask;
22 import org.gradle.api.plugins.BasePluginConvention;
23 import org.gradle.api.plugins.MavenPluginConvention;
24 import org.gradle.api.tasks.OutputDirectory;
25 import org.gradle.api.tasks.OutputFile;
26 import org.gradle.api.tasks.TaskAction;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import java.io.File;
31 import java.util.Collections;
32 import java.util.Comparator;
33
34 public class MavenPom extends DefaultTask {
35
36 private final Logger log = LoggerFactory.getLogger(MavenPom.class);
37
38 /* Since the Groovy in Gradle scripts are dynamically typed and due to
39 Gradle's own class loading mechanism, we couldn't cast the type of
40 org.apache.maven.model.Dependency. So we have to use Reflections. */
41 private String getMavenDependencyProperty(Object dependency, String property) {
42 String result = null;
43 try {
44 result = ((String)dependency.getClass()
45 .getMethod("get" + property.substring(0, 1).toUpperCase() + property.substring(1))
46 .invoke(dependency));
47 } finally {
48 return (result == null) ? "" : result;
49 }
50 }
51
52 private void setMavenDependencyProperty(Object dependency, String property, String value) {
53 try {
54 dependency.getClass()
55 .getMethod("set" + property.substring(0, 1).toUpperCase() + property.substring(1), String.class)
56 .invoke(dependency, value);
57 } catch (Exception e) {
58 throw new RuntimeException("Unable to set the property '" + property + "' to '" + value + "' on " + dependency);
59 }
60 }
61
62 private String getMavenDependencyScope(Object dependency) {
63 return getMavenDependencyProperty(dependency, "scope");
64 }
65
66 private String getMavenDependencyGroupId(Object dependency) {
67 return getMavenDependencyProperty(dependency, "groupId");
68 }
69
70 private String getMavenDependencyArtifactId(Object dependency) {
71 return getMavenDependencyProperty(dependency, "artifactId");
72 }
73
74 private String getMavenDependencyVersion(Object dependency) {
75 return getMavenDependencyProperty(dependency, "version");
76 }
77
78 private void sortPomDependencies(org.gradle.api.artifacts.maven.MavenPom pom) {
79 Collections.sort(
80 pom.getDependencies(),
81 new Comparator<Object>() {
82 @Override
83 public int compare(Object o1, Object o2) {
84 if (getMavenDependencyScope(o1).compareTo(getMavenDependencyScope(o2)) != 0) {
85 return getMavenDependencyScope(o1).compareTo(getMavenDependencyScope(o2));
86 } else if (getMavenDependencyGroupId(o1).compareTo(getMavenDependencyGroupId(o2)) != 0) {
87 return getMavenDependencyGroupId(o1).compareTo(getMavenDependencyGroupId(o2));
88 } else if (getMavenDependencyArtifactId(o1).compareTo(getMavenDependencyArtifactId(o2)) != 0) {
89 return getMavenDependencyArtifactId(o1).compareTo(getMavenDependencyArtifactId(o2));
90 } else {
91 return getMavenDependencyVersion(o1).compareTo(getMavenDependencyVersion(o2));
92 }
93 }
94 @Override
95 public boolean equals(Object obj) { return false; }
96 }
97 );
98 }
99
100 /**
101 * Turns the dependencies with a scope set to 'optional' into dependencies
102 * with a 'compile' scope and the optional attribute set to true.
103 */
104 private void fixOptionalDependencies(org.gradle.api.artifacts.maven.MavenPom pom) {
105 for (Object dependency : pom.getDependencies()) {
106 if (dependency.getClass().getName().equals("org.apache.maven.model.Dependency")) {
107 if ("optional".equals(getMavenDependencyScope(dependency))) {
108 setMavenDependencyProperty(dependency, "optional", "true");
109 setMavenDependencyProperty(dependency, "scope", "compile");
110 }
111 }
112 }
113 }
114
115 @OutputDirectory
116 private File getDestinationPomDir() {
117 return new File(getProject().getBuildDir(), "debian");
118 }
119
120 @OutputFile
121 private File getDestinationPomFile() {
122 return new File(
123 getDestinationPomDir(),
124 getProject().getConvention()
125 .getPlugin(BasePluginConvention.class)
126 .getArchivesBaseName() + ".pom"
127 );
128 }
129
130 @TaskAction
131 private void generatePom() {
132 final org.gradle.api.artifacts.maven.MavenPom pom =
133 getProject().getConvention()
134 .getPlugin(MavenPluginConvention.class)
135 .pom();
136 pom.project(new MethodClosure(
137 new Object() { public void execute() {} },
138 "execute"
139 ));
140 pom.setArtifactId(getProject().getConvention()
141 .getPlugin(BasePluginConvention.class)
142 .getArchivesBaseName());
143 final org.gradle.api.artifacts.maven.MavenPom effectivePom =
144 pom.getEffectivePom();
145 fixOptionalDependencies(effectivePom);
146 sortPomDependencies(effectivePom);
147 getDestinationPomDir().mkdir();
148 log.info("\tGenerating pom file " + getDestinationPomFile());
149 effectivePom.writeTo(getDestinationPomFile());
150 }
151 }