| 1 |
/** |
| 2 |
* Copyright 2018 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.plugin; |
| 18 |
|
| 19 |
import java.util.HashMap; |
| 20 |
import java.util.Map; |
| 21 |
|
| 22 |
import org.debian.maven.cliargs.ArgumentsIterable; |
| 23 |
import org.debian.maven.cliargs.ArgumentsMap; |
| 24 |
import org.debian.maven.repo.Dependency; |
| 25 |
import org.debian.maven.repo.DependencyRule; |
| 26 |
import org.debian.maven.repo.DependencyRuleSetFiles; |
| 27 |
|
| 28 |
/** |
| 29 |
* Hook for Gradle's MavenResolver class. |
| 30 |
*/ |
| 31 |
public class MavenResolverHook { |
| 32 |
|
| 33 |
private static final MavenResolverHook INSTANCE = new MavenResolverHook(); |
| 34 |
|
| 35 |
/** The substitution rules for the Maven dependencies */ |
| 36 |
private DependencyRuleSetFiles rulesets; |
| 37 |
|
| 38 |
/** The placeholder used for ignored dependencies */ |
| 39 |
private static final Dependency IGNORED_DEPENDENCY_PLACEHOLDER = new Dependency("org.debian.gradle", "gradle-dependency-placeholder", "jar", "1.0", "compile", false, null, null); |
| 40 |
|
| 41 |
private MavenResolverHook() { |
| 42 |
} |
| 43 |
|
| 44 |
public static MavenResolverHook getInstance() { |
| 45 |
return INSTANCE; |
| 46 |
} |
| 47 |
|
| 48 |
private void initRules() { |
| 49 |
System.out.println("\tLoading the Maven rules..."); |
| 50 |
ArgumentsMap args = new ArgumentsMap(new ArgumentsIterable(new String[] { "--rules=debian/maven.rules", "--ignore-rules=debian/maven.ignoreRules"})); |
| 51 |
rulesets = DependencyRuleSetFiles.fromCLIArguments(args, false); |
| 52 |
rulesets.addDefaultRules(); |
| 53 |
} |
| 54 |
|
| 55 |
public Map<String, String> rewrite(String groupId, String artifactId, String version, String type, String classifier) throws Exception { |
| 56 |
if (rulesets == null) { |
| 57 |
initRules(); |
| 58 |
} |
| 59 |
Dependency dependency = new Dependency(groupId, artifactId, type, version, "compile", false, classifier, null); |
| 60 |
Dependency resolved = resolve(dependency); |
| 61 |
|
| 62 |
if (resolved == null) { |
| 63 |
System.out.println("\tIgnoring " + format(dependency)); |
| 64 |
resolved = IGNORED_DEPENDENCY_PLACEHOLDER; |
| 65 |
|
| 66 |
} else if (dependency == resolved) { |
| 67 |
System.out.println("\tPassing through " + format(dependency)); |
| 68 |
} else { |
| 69 |
System.out.println("\tReplacing " + format(dependency) + " -> " + format(resolved)); |
| 70 |
} |
| 71 |
|
| 72 |
Map<String, String> result = new HashMap<String, String>(); |
| 73 |
result.put("groupId", resolved.getGroupId()); |
| 74 |
result.put("artifactId", resolved.getArtifactId()); |
| 75 |
result.put("version", resolved.getVersion()); |
| 76 |
result.put("type", resolved.getType()); |
| 77 |
result.put("classifier", resolved.getClassifier()); |
| 78 |
|
| 79 |
return result; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Apply the Maven rules to the specified dependency. |
| 84 |
* |
| 85 |
* @param dependency the resolved dependency, or null if the dependency is ignored. |
| 86 |
*/ |
| 87 |
private Dependency resolve(Dependency dependency) { |
| 88 |
// check if the dependency is ignored |
| 89 |
for (DependencyRule rule : rulesets.get(DependencyRuleSetFiles.RulesType.IGNORE).getRules()) { |
| 90 |
if (rule.matches(dependency)) { |
| 91 |
return null; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* The transitive dependencies are also resolved but unfortunately there is no way to detect them as such. |
| 97 |
* This means that a transitive dependency on asm:4.x for example would be transformed into asm:debian unless |
| 98 |
* its rule is copied into debian/maven.rules in order to preserve the generic '4.x' version. To mitigate this |
| 99 |
* issue the '.x' generic versions are detected and passed through. Artifacts with no generic version are still |
| 100 |
* affected though, and their rules have to be added to debian/maven.rules. |
| 101 |
*/ |
| 102 |
if (!dependency.getVersion().endsWith(".x") && !dependency.getVersion().equals("debian")) { |
| 103 |
// apply the first rule that matches |
| 104 |
for (DependencyRule rule : rulesets.get(DependencyRuleSetFiles.RulesType.RULES).getRules()) { |
| 105 |
if (rule.matches(dependency)) { |
| 106 |
return rule.apply(dependency); |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
return dependency; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Format a dependency for display (slightly more compact than dependency.toString()) |
| 116 |
*/ |
| 117 |
private String format(Dependency dependency) { |
| 118 |
StringBuilder builder = new StringBuilder(); |
| 119 |
builder.append(dependency.getGroupId()); |
| 120 |
builder.append(":"); |
| 121 |
builder.append(dependency.getArtifactId()); |
| 122 |
builder.append(":"); |
| 123 |
builder.append(dependency.getType()); |
| 124 |
builder.append(":"); |
| 125 |
builder.append(dependency.getVersion()); |
| 126 |
if (dependency.getClassifier() != null && dependency.getClassifier().trim().length() > 0) { |
| 127 |
builder.append(":"); |
| 128 |
builder.append(dependency.getClassifier()); |
| 129 |
} |
| 130 |
|
| 131 |
return builder.toString(); |
| 132 |
} |
| 133 |
} |