| 1 |
/* |
| 2 |
* Copyright 2009 Ludovic Claude. |
| 3 |
* Copyright 2011 Damien Raude-Morvan. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*/ |
| 17 |
|
| 18 |
package org.debian.maven.packager; |
| 19 |
|
| 20 |
import java.io.BufferedReader; |
| 21 |
import java.io.File; |
| 22 |
import java.io.IOException; |
| 23 |
import java.io.InputStream; |
| 24 |
import java.io.InputStreamReader; |
| 25 |
import java.util.*; |
| 26 |
import java.util.logging.Level; |
| 27 |
import java.util.logging.Logger; |
| 28 |
import java.util.regex.Matcher; |
| 29 |
import java.util.regex.Pattern; |
| 30 |
|
| 31 |
import javax.xml.stream.XMLStreamException; |
| 32 |
|
| 33 |
import org.debian.maven.packager.interaction.ChoiceQuestion; |
| 34 |
import org.debian.maven.packager.interaction.SimpleQuestion; |
| 35 |
import org.debian.maven.packager.interaction.YesNoQuestion; |
| 36 |
import org.debian.maven.packager.util.*; |
| 37 |
import org.debian.maven.repo.Dependency; |
| 38 |
import org.debian.maven.repo.DependencyNotFoundException; |
| 39 |
import org.debian.maven.repo.DependencyRule; |
| 40 |
import org.debian.maven.repo.DependencyRuleSet; |
| 41 |
import org.debian.maven.repo.DependencyRuleSetFiles; |
| 42 |
import org.debian.maven.repo.ListOfPOMs; |
| 43 |
import org.debian.maven.repo.POMHandler; |
| 44 |
import org.debian.maven.repo.POMInfo; |
| 45 |
import org.debian.maven.repo.POMOptions; |
| 46 |
import org.debian.maven.repo.Substvars; |
| 47 |
import org.debian.maven.repo.DependencyRuleSetFiles.RulesType; |
| 48 |
import org.debian.maven.repo.POMInfo.DependencyType; |
| 49 |
import org.debian.maven.repo.POMTransformer; |
| 50 |
import org.debian.maven.repo.Repository; |
| 51 |
import org.debian.maven.repo.Rule; |
| 52 |
|
| 53 |
import static org.debian.maven.packager.DebianDependencies.Type.*; |
| 54 |
import static org.debian.maven.repo.DependencyRuleSetFiles.RulesType.*; |
| 55 |
|
| 56 |
/** |
| 57 |
* Analyze the Maven dependencies and extract the Maven rules to use |
| 58 |
* as well as the list of dependent packages. |
| 59 |
* |
| 60 |
* @author Ludovic Claude |
| 61 |
*/ |
| 62 |
public class DependenciesSolver { |
| 63 |
|
| 64 |
private static final Logger log = Logger.getLogger(DependenciesSolver.class.getName()); |
| 65 |
private final IgnoreDependencyQuestions ignoreDependencyQuestion; |
| 66 |
|
| 67 |
private File baseDir; |
| 68 |
final POMTransformer pomTransformer = new POMTransformer(); |
| 69 |
private final File outputDirectory; |
| 70 |
String packageName; |
| 71 |
private String packageVersion; |
| 72 |
File mavenRepo = new File("/usr/share/maven-repo"); |
| 73 |
// explore (search) for additional pom files or look only for those defined in debian/*.poms? |
| 74 |
boolean exploreProjects; |
| 75 |
private Repository repository; |
| 76 |
List<String> issues = new ArrayList<String>(); |
| 77 |
private List<Dependency> projectPoms = new ArrayList<Dependency>(); |
| 78 |
private List<ToResolve> toResolve = new ArrayList<ToResolve>(); |
| 79 |
private Set<Dependency> knownProjectDependencies = new TreeSet<Dependency>(); |
| 80 |
private Set<Dependency> ignoredDependencies = new TreeSet<Dependency>(); |
| 81 |
|
| 82 |
private DebianDependencies debianDeps = new DebianDependencies(); |
| 83 |
boolean runTests; |
| 84 |
boolean generateJavadoc; |
| 85 |
final boolean interactive; |
| 86 |
private boolean askedToFilterModules = false; |
| 87 |
private boolean filterModules = false; |
| 88 |
boolean verbose = false; |
| 89 |
private Map<String, POMInfo> pomInfoCache = new HashMap<String, POMInfo>(); |
| 90 |
|
| 91 |
/** The original POMs for reference */ |
| 92 |
private Map<String, POMInfo> originalPomInfoCache = new HashMap<String, POMInfo>(); |
| 93 |
|
| 94 |
/** Map of the previously selected rule for a given version */ |
| 95 |
private Map<String, Rule> versionToRules = new HashMap<String, Rule>(); |
| 96 |
|
| 97 |
/** List of packages and dependencies */ |
| 98 |
private Map<DebianDependency, Dependency> versionedPackagesAndDependencies = new HashMap<DebianDependency, Dependency>(); |
| 99 |
|
| 100 |
private List<Rule> defaultRules = new ArrayList<Rule>(); |
| 101 |
private PackageScanner scanner; |
| 102 |
|
| 103 |
public DependenciesSolver(File outputDirectory, PackageScanner scanner, boolean interactive) { |
| 104 |
this.outputDirectory = outputDirectory; |
| 105 |
this.scanner = scanner; |
| 106 |
this.interactive = interactive; |
| 107 |
this.ignoreDependencyQuestion = new IgnoreDependencyQuestions(interactive); |
| 108 |
pomTransformer.setVerbose(true); |
| 109 |
pomTransformer.setFixVersions(false); |
| 110 |
pomTransformer.setRulesFiles(initDependencyRuleSetFiles(outputDirectory, verbose)); |
| 111 |
|
| 112 |
defaultRules.add(new Rule("s/.*/debian/", "Change the version to the symbolic 'debian' version")); |
| 113 |
defaultRules.add(new Rule("*", "Keep the version")); |
| 114 |
defaultRules.add(new Rule("CUSTOM", "Custom rule")); |
| 115 |
} |
| 116 |
|
| 117 |
public static DependencyRuleSetFiles initDependencyRuleSetFiles(File outputDirectory, boolean verbose) { |
| 118 |
DependencyRuleSetFiles depFiles = new DependencyRuleSetFiles(); |
| 119 |
for (RulesType type : RulesType.values()) { |
| 120 |
if (type.filename != null) { |
| 121 |
File rulesFile = new File(outputDirectory, type.filename); |
| 122 |
String description = readResource(type.descriptionResource); |
| 123 |
DependencyRuleSet ruleSet = DependencyRuleSet.readRules(rulesFile, description, verbose, false); |
| 124 |
depFiles.get(type).addAll(ruleSet); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
return depFiles; |
| 129 |
} |
| 130 |
|
| 131 |
// TODO move to another class for reuse |
| 132 |
private static String readResource(String resource) { |
| 133 |
StringBuilder sb = new StringBuilder(); |
| 134 |
try { |
| 135 |
InputStream is = DependenciesSolver.class.getResourceAsStream("/" + resource); |
| 136 |
BufferedReader r = new BufferedReader(new InputStreamReader(is)); |
| 137 |
String line; |
| 138 |
while ((line = r.readLine()) != null) { |
| 139 |
sb.append(line); |
| 140 |
sb.append("\n"); |
| 141 |
} |
| 142 |
r.close(); |
| 143 |
} catch (IOException e) { |
| 144 |
log.log(Level.SEVERE, "Cannot read resource " + resource, e); |
| 145 |
} |
| 146 |
return sb.toString(); |
| 147 |
} |
| 148 |
|
| 149 |
private boolean isDefaultMavenPlugin(Dependency dependency) { |
| 150 |
if (getRepository() != null && getRepository().getSuperPOM() != null) { |
| 151 |
for (Dependency defaultPlugin : getRepository().getSuperPOM().getDependencies().get(DependencyType.PLUGIN_MANAGEMENT)) { |
| 152 |
if (defaultPlugin.equalsIgnoreVersion(dependency)) { |
| 153 |
return true; |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
return false; |
| 158 |
} |
| 159 |
|
| 160 |
private class ToResolve { |
| 161 |
|
| 162 |
private final File sourcePom; |
| 163 |
private final DependencyType listType; |
| 164 |
private final boolean buildTime; |
| 165 |
private final boolean mavenExtension; |
| 166 |
private final boolean management; |
| 167 |
|
| 168 |
private ToResolve(File sourcePom, DependencyType listType, boolean buildTime, boolean mavenExtension, boolean management) { |
| 169 |
this.sourcePom = sourcePom; |
| 170 |
this.listType = listType; |
| 171 |
this.buildTime = buildTime; |
| 172 |
this.mavenExtension = mavenExtension; |
| 173 |
this.management = management; |
| 174 |
} |
| 175 |
|
| 176 |
public void resolve() { |
| 177 |
try { |
| 178 |
POMInfo pom = getPOM(sourcePom); |
| 179 |
List<Dependency> dependenciesByType = pom.getDependencies().get(listType); |
| 180 |
|
| 181 |
for (Dependency dependency : dependenciesByType) { |
| 182 |
resolveDependency(dependency, sourcePom, buildTime, mavenExtension, management, false); |
| 183 |
} |
| 184 |
} catch (DependencyNotFoundException e) { |
| 185 |
log.log(Level.SEVERE, "Cannot resolve dependencies in " + sourcePom + ": " + e.getMessage()); |
| 186 |
} catch (Exception e) { |
| 187 |
log.log(Level.SEVERE, "Cannot resolve dependencies in " + sourcePom + ": " + e.getMessage(), e); |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
public void saveSubstvars() { |
| 193 |
Properties depVars = Substvars.loadSubstvars(outputDirectory, packageName); |
| 194 |
|
| 195 |
if (generateJavadoc) { |
| 196 |
System.out.println("Checking dependencies for documentation packages..."); |
| 197 |
|
| 198 |
debianDeps.add(DOC_RUNTIME, new DebianDependency("default-jdk-doc")); |
| 199 |
debianDeps.add(DOC_RUNTIME, scanner.addDocDependencies(debianDeps.get(RUNTIME), versionedPackagesAndDependencies)); |
| 200 |
debianDeps.add(DOC_OPTIONAL, scanner.addDocDependencies(debianDeps.get(OPTIONAL), versionedPackagesAndDependencies)); |
| 201 |
} |
| 202 |
|
| 203 |
debianDeps.putInProperties(depVars); |
| 204 |
|
| 205 |
if (packageVersion != null) { |
| 206 |
depVars.put("maven.UpstreamPackageVersion", packageVersion); |
| 207 |
} |
| 208 |
// Write everything to debian/substvars |
| 209 |
Substvars.write(outputDirectory, packageName, depVars); |
| 210 |
} |
| 211 |
|
| 212 |
public void setBaseDir(File baseDir) { |
| 213 |
this.baseDir = baseDir; |
| 214 |
if (pomTransformer.getListOfPOMs() != null) { |
| 215 |
pomTransformer.getListOfPOMs().setBaseDir(baseDir); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
public void setListOfPoms(File listOfPoms) { |
| 220 |
if (pomTransformer.getListOfPOMs() == null) { |
| 221 |
pomTransformer.setListOfPOMs(new ListOfPOMs(listOfPoms)); |
| 222 |
} else { |
| 223 |
pomTransformer.getListOfPOMs().setListOfPOMsFile(listOfPoms); |
| 224 |
} |
| 225 |
pomTransformer.getListOfPOMs().setBaseDir(baseDir); |
| 226 |
} |
| 227 |
|
| 228 |
private Repository getRepository() { |
| 229 |
if (repository == null && mavenRepo != null) { |
| 230 |
repository = new Repository(mavenRepo); |
| 231 |
repository.scan(); |
| 232 |
} |
| 233 |
return repository; |
| 234 |
} |
| 235 |
|
| 236 |
public void solveDependencies() { |
| 237 |
pomTransformer.setRepository(getRepository()); |
| 238 |
pomTransformer.usePluginVersionsFromRepository(); |
| 239 |
|
| 240 |
IOUtil.mkDirIfNotExists(outputDirectory); |
| 241 |
|
| 242 |
if (exploreProjects) { |
| 243 |
File pom; |
| 244 |
if (pomTransformer.getListOfPOMs().getPomOptions().isEmpty()) { |
| 245 |
pom = new File(baseDir, "pom.xml"); |
| 246 |
if (pom.exists()) { |
| 247 |
pomTransformer.getListOfPOMs().addPOM("pom.xml"); |
| 248 |
} else { |
| 249 |
pom = new File(baseDir, "debian/pom.xml"); |
| 250 |
if (pom.exists()) { |
| 251 |
pomTransformer.getListOfPOMs().addPOM("debian/pom.xml"); |
| 252 |
} else { |
| 253 |
System.err.println("Cannot find the POM file"); |
| 254 |
return; |
| 255 |
} |
| 256 |
} |
| 257 |
} else { |
| 258 |
pom = new File(baseDir, pomTransformer.getListOfPOMs().getFirstPOM()); |
| 259 |
} |
| 260 |
resolveDependencies(pom); |
| 261 |
} else { |
| 262 |
pomTransformer.getListOfPOMs().foreachPoms(new POMHandler() { |
| 263 |
|
| 264 |
public void handlePOM(File pomFile, boolean noParent, boolean hasPackageVersion) throws Exception { |
| 265 |
resolveDependencies(pomFile); |
| 266 |
} |
| 267 |
|
| 268 |
public void ignorePOM(File pomFile) throws Exception { |
| 269 |
} |
| 270 |
}); |
| 271 |
} |
| 272 |
|
| 273 |
for (ToResolve tr : toResolve) { |
| 274 |
tr.resolve(); |
| 275 |
} |
| 276 |
|
| 277 |
if (!issues.isEmpty()) { |
| 278 |
System.err.println("ERROR:"); |
| 279 |
for (String issue : issues) { |
| 280 |
System.err.println(issue); |
| 281 |
} |
| 282 |
System.err.println("--------"); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
private void resolveDependencies(File projectPom) { |
| 287 |
|
| 288 |
if (pomTransformer.getListOfPOMs().getOrCreatePOMOptions(projectPom) != null && pomTransformer.getListOfPOMs().getOrCreatePOMOptions(projectPom).isIgnore()) { |
| 289 |
return; |
| 290 |
} |
| 291 |
|
| 292 |
System.out.println("Analysing " + IOUtil.relativePath(baseDir, projectPom) + "..."); |
| 293 |
|
| 294 |
try { |
| 295 |
POMInfo pom = getPOM(projectPom); |
| 296 |
pom.setProperties(new HashMap<String, String>()); |
| 297 |
pom.getProperties().put("debian.package", packageName); |
| 298 |
|
| 299 |
if (pomTransformer.getListOfPOMs().getOrCreatePOMOptions(projectPom).isNoParent()) { |
| 300 |
pom.setParent(null); |
| 301 |
} else if (pom.getParent() != null && !pom.getParent().isSuperPom()) { |
| 302 |
boolean oldNoParent = pomTransformer.getListOfPOMs().getOrCreatePOMOptions(projectPom).isNoParent(); |
| 303 |
// Don't mark the parent dependency as 'build time' dependency because once installed, |
| 304 |
// the POM for this project will always need the parent POM to be available |
| 305 |
Dependency parent = resolveDependency(pom.getParent(), projectPom, false, false, false, true); |
| 306 |
// The user may have set or unset the --no-parent option, if so we update the POM to include or not the |
| 307 |
// parent according to the user's choice |
| 308 |
if (pomTransformer.getListOfPOMs().getOrCreatePOMOptions(projectPom).isNoParent() != oldNoParent) { |
| 309 |
pomInfoCache.remove(projectPom.getAbsolutePath()); |
| 310 |
pom = getPOM(projectPom); |
| 311 |
} |
| 312 |
pom.setParent(parent); |
| 313 |
// If the parent is found, search the parent POM and update current POM |
| 314 |
if (parent != null) { |
| 315 |
POMInfo parentPOM = getRepository().searchMatchingPOM(parent); |
| 316 |
pom.setParentPOM(parentPOM); |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
getRepository().registerPom(projectPom, pom); |
| 321 |
// Also register automatically the test jar which may accompany the current jar and be |
| 322 |
// used in another module of the same project |
| 323 |
if (pom.getThisPom().isJar()) { |
| 324 |
POMInfo testPom = (POMInfo) pom.clone(); |
| 325 |
testPom.getThisPom().setType("test-jar"); |
| 326 |
getRepository().registerPom(projectPom, testPom); |
| 327 |
} |
| 328 |
|
| 329 |
knownProjectDependencies.add(pom.getThisPom()); |
| 330 |
|
| 331 |
if (interactive && packageVersion == null) { |
| 332 |
String question = "Enter the upstream version for the package."; |
| 333 |
String v = new SimpleQuestion(question, pom.getOriginalVersion()).ask(); |
| 334 |
if (v.isEmpty()) { |
| 335 |
v = pom.getOriginalVersion(); |
| 336 |
} |
| 337 |
packageVersion = v; |
| 338 |
} |
| 339 |
|
| 340 |
if (pom.getOriginalVersion().equals(packageVersion)) { |
| 341 |
pom.getProperties().put("debian.hasPackageVersion", "true"); |
| 342 |
pomTransformer.getListOfPOMs().getOrCreatePOMOptions(projectPom).setHasPackageVersion(true); |
| 343 |
} |
| 344 |
|
| 345 |
if (filterModules) { |
| 346 |
boolean includeModule = new YesNoQuestion("Include the module " + IOUtil.relativePath(baseDir, projectPom) + " ?", true).ask(); |
| 347 |
if (!includeModule) { |
| 348 |
pomTransformer.getListOfPOMs().getOrCreatePOMOptions(projectPom).setIgnore(true); |
| 349 |
pomTransformer.getRulesFiles().get(IGNORE).add(DependencyRule.newToMatch(pom.getThisPom())); |
| 350 |
return; |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
projectPoms.add(pom.getThisPom()); |
| 355 |
|
| 356 |
// Previous rule from another run |
| 357 |
boolean explicitlyMentionedInRules = false; |
| 358 |
for (DependencyRule previousRule : pomTransformer.getRulesFiles().get(RULES).findMatchingRules(pom.getThisPom())) { |
| 359 |
if (!previousRule.explicitlyMentions(pom.getThisPom())) { |
| 360 |
explicitlyMentionedInRules = true; |
| 361 |
break; |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
if (interactive && !explicitlyMentionedInRules && !pom.getThisPom().isPlugin()) { |
| 366 |
Rule selectedRule = askForVersionRule(pom.getThisPom()); |
| 367 |
versionToRules.put(pom.getThisPom().getVersion(), selectedRule); |
| 368 |
if (selectedRule.getPattern().equals("CUSTOM")) { |
| 369 |
String rule = new SimpleQuestion("Enter the pattern for your custom rule (in the form s/regex/replace/)").ask().toLowerCase(); |
| 370 |
selectedRule = new Rule(rule, "My custom rule " + rule); |
| 371 |
defaultRules.add(selectedRule); |
| 372 |
} |
| 373 |
|
| 374 |
pomTransformer.getRulesFiles().get(RULES).add(new DependencyRule(pom.getThisPom().getGroupId(), |
| 375 |
pom.getThisPom().getArtifactId(), pom.getThisPom().getType(), selectedRule.toString())); |
| 376 |
POMInfo transformedPom = pom.newPOMFromRules(pomTransformer.getRulesFiles().get(RULES).getRules(), getRepository()); |
| 377 |
getRepository().registerPom(projectPom, transformedPom); |
| 378 |
projectPoms.add(transformedPom.getThisPom()); |
| 379 |
|
| 380 |
if (pom.getThisPom().isBundle()) { |
| 381 |
String question2 = pom.getThisPom().getGroupId() + ":" + pom.getThisPom().getArtifactId() + |
| 382 |
" is a bundle.\n" |
| 383 |
+ "Inform mh_make that dependencies of type jar which may match this library should be transformed into bundles automatically?"; |
| 384 |
|
| 385 |
boolean transformJarsIntoBundle = new YesNoQuestion(question2, true).ask(); |
| 386 |
|
| 387 |
if (transformJarsIntoBundle) { |
| 388 |
String transformBundleRule = pom.getThisPom().getGroupId() + " " + pom.getThisPom().getArtifactId() |
| 389 |
+ " s/jar/bundle/ " + selectedRule.toString(); |
| 390 |
pomTransformer.getRulesFiles().get(PUBLISHED).add(new DependencyRule(transformBundleRule)); |
| 391 |
} |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
if (pom.getParent() != null && !pom.getParent().isSuperPom()) { |
| 396 |
POMInfo parentPom = getRepository().searchMatchingPOM(pom.getParent()); |
| 397 |
if (parentPom == null || parentPom.equals(getRepository().getSuperPOM())) { |
| 398 |
pomTransformer.getListOfPOMs().getOrCreatePOMOptions(projectPom).setNoParent(true); |
| 399 |
} |
| 400 |
if (!baseDir.equals(projectPom.getParentFile())) { |
| 401 |
System.out.println("Checking the parent dependency in the sub project " + IOUtil.relativePath(baseDir, projectPom)); |
| 402 |
resolveDependency(pom.getParent(), projectPom, false, false, false, true); |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
toResolve.add(new ToResolve(projectPom, DependencyType.DEPENDENCIES, false, false, false)); |
| 407 |
toResolve.add(new ToResolve(projectPom, DependencyType.DEPENDENCY_MANAGEMENT_LIST, false, false, true)); |
| 408 |
toResolve.add(new ToResolve(projectPom, DependencyType.PLUGINS, true, true, false)); |
| 409 |
toResolve.add(new ToResolve(projectPom, DependencyType.PLUGIN_DEPENDENCIES, true, true, false)); |
| 410 |
toResolve.add(new ToResolve(projectPom, DependencyType.PLUGIN_MANAGEMENT, true, true, true)); |
| 411 |
toResolve.add(new ToResolve(projectPom, DependencyType.REPORTING_PLUGINS, true, true, false)); |
| 412 |
toResolve.add(new ToResolve(projectPom, DependencyType.EXTENSIONS, true, true, false)); |
| 413 |
|
| 414 |
if (exploreProjects && !pom.getModules().isEmpty()) { |
| 415 |
if (interactive && !askedToFilterModules) { |
| 416 |
filterModules = !new YesNoQuestion("This project contains modules. Include all modules? (no to select them individually)", true).ask(); |
| 417 |
askedToFilterModules = true; |
| 418 |
} |
| 419 |
for (String module : pom.getModules()) { |
| 420 |
resolveDependencies(new File(projectPom.getParent(), module + "/pom.xml")); |
| 421 |
} |
| 422 |
} |
| 423 |
} catch (Exception ex) { |
| 424 |
log.log(Level.SEVERE, "Error while resolving " + projectPom + ": " + ex.getMessage()); |
| 425 |
log.log(Level.SEVERE, "", ex); |
| 426 |
System.exit(1); |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
/** |
| 431 |
* Asks the user to specify the substitution rule for the version. |
| 432 |
* |
| 433 |
* @param dependency |
| 434 |
* @return the version rule selected |
| 435 |
*/ |
| 436 |
public Rule askForVersionRule(Dependency dependency) { |
| 437 |
String question = "\n" |
| 438 |
+ "Version of " + dependency.getGroupId() + ":" |
| 439 |
+ dependency.getArtifactId() + " is " + dependency.getVersion() |
| 440 |
+ "\nChoose how the version will be transformed:"; |
| 441 |
|
| 442 |
List<Rule> choices = getVersionRules(dependency.getVersion()); |
| 443 |
|
| 444 |
// select the default choice (either the previously selected rule or the 'debian' version rule) |
| 445 |
int defaultChoice = 1; // the 'debian' version rule is the first one of the default rules |
| 446 |
if (versionToRules.containsKey(dependency.getVersion())) { |
| 447 |
Rule previouslySelectedRule = versionToRules.get(dependency.getVersion()); |
| 448 |
if (choices.contains(previouslySelectedRule)) { |
| 449 |
defaultChoice = choices.indexOf(previouslySelectedRule); |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
List<String> choicesDescriptions = new ArrayList<String>(); |
| 454 |
for (Rule choice : choices) { |
| 455 |
choicesDescriptions.add(choice.getDescription()); |
| 456 |
} |
| 457 |
|
| 458 |
int choice = new ChoiceQuestion(question, defaultChoice, choicesDescriptions).ask(); |
| 459 |
return choices.get(choice); |
| 460 |
} |
| 461 |
|
| 462 |
/** |
| 463 |
* Returns the substitution rules for the specified version. |
| 464 |
*/ |
| 465 |
private List<Rule> getVersionRules(String version) { |
| 466 |
List<Rule> rules = new ArrayList<Rule>(); |
| 467 |
|
| 468 |
// add the 1.0 -> 1.x rule |
| 469 |
Pattern p = Pattern.compile("(\\d+)(\\..*)"); |
| 470 |
Matcher matcher = p.matcher(version); |
| 471 |
if (matcher.matches()) { |
| 472 |
String mainVersion = matcher.group(1); |
| 473 |
Rule mainVersionRule = new Rule("s/" + mainVersion + "\\..*/" + mainVersion + ".x/", |
| 474 |
"Replace all versions starting by " + mainVersion + ". with " + mainVersion + ".x"); |
| 475 |
rules.add(mainVersionRule); |
| 476 |
} |
| 477 |
|
| 478 |
// add the default rules |
| 479 |
rules.addAll(defaultRules); |
| 480 |
|
| 481 |
return rules; |
| 482 |
} |
| 483 |
|
| 484 |
private POMInfo getPOM(File projectPom) throws XMLStreamException, IOException { |
| 485 |
POMInfo info = pomInfoCache.get(projectPom.getAbsolutePath()); |
| 486 |
if (info != null) { |
| 487 |
return info; |
| 488 |
} |
| 489 |
File tmpDest = File.createTempFile("pom", ".tmp", baseDir); |
| 490 |
tmpDest.deleteOnExit(); |
| 491 |
POMOptions options = pomTransformer.getListOfPOMs().getOrCreatePOMOptions(projectPom); |
| 492 |
boolean noParent = false; |
| 493 |
boolean hasPackageVersion = false; |
| 494 |
if (options != null) { |
| 495 |
noParent = options.isNoParent(); |
| 496 |
hasPackageVersion = options.getHasPackageVersion(); |
| 497 |
} |
| 498 |
|
| 499 |
info = pomTransformer.transformPom(projectPom, tmpDest, noParent, hasPackageVersion, false, false, null, null, true); |
| 500 |
pomInfoCache.put(projectPom.getAbsolutePath(), info); |
| 501 |
return info; |
| 502 |
} |
| 503 |
|
| 504 |
private POMInfo getOriginalPOM(File projectPom) throws XMLStreamException, IOException { |
| 505 |
POMInfo info = originalPomInfoCache.get(projectPom.getAbsolutePath()); |
| 506 |
if (info != null) { |
| 507 |
return info; |
| 508 |
} |
| 509 |
|
| 510 |
info = pomTransformer.readPom(projectPom); |
| 511 |
originalPomInfoCache.put(projectPom.getAbsolutePath(), info); |
| 512 |
return info; |
| 513 |
} |
| 514 |
|
| 515 |
private Dependency resolveDependency(Dependency dependency, File sourcePom, boolean buildTime, boolean mavenExtension, boolean management, boolean resolvingParent) throws DependencyNotFoundException { |
| 516 |
|
| 517 |
if (containsDependencyIgnoreVersion(knownProjectDependencies, dependency)) { |
| 518 |
return dependency; |
| 519 |
} |
| 520 |
|
| 521 |
if (containsDependencyIgnoreVersion(ignoredDependencies, dependency) || |
| 522 |
(management && isDefaultMavenPlugin(dependency))) { |
| 523 |
return null; |
| 524 |
} |
| 525 |
|
| 526 |
if (resolvingParent && dependency.isSuperPom()) { |
| 527 |
return dependency; |
| 528 |
} |
| 529 |
|
| 530 |
String sourcePomLoc = sourcePom.getAbsolutePath(); |
| 531 |
String baseDirPath = baseDir.getAbsolutePath(); |
| 532 |
sourcePomLoc = sourcePomLoc.substring(baseDirPath.length() + 1, sourcePomLoc.length()); |
| 533 |
if (verbose) { |
| 534 |
System.out.println("Resolving " + dependency + (dependency.getScope() == null ? "" : " of scope " + dependency.getScope()) + "..."); |
| 535 |
} |
| 536 |
|
| 537 |
// First let the packager mark as ignored those dependencies which should be ignored |
| 538 |
if (ignoreDependencyQuestion.askIgnoreUnnecessaryDependency(dependency, sourcePomLoc, runTests, generateJavadoc)) { |
| 539 |
ignoredDependencies.add(dependency); |
| 540 |
pomTransformer.getRulesFiles().get(IGNORE).add(new DependencyRule(dependency.getGroupId(), dependency.getArtifactId(), "*", "*")); |
| 541 |
if (verbose) { |
| 542 |
System.out.println("[ignored]"); |
| 543 |
} |
| 544 |
return null; |
| 545 |
} |
| 546 |
|
| 547 |
POMInfo pom = getRepository().searchMatchingPOM(dependency); |
| 548 |
try { |
| 549 |
if (pom == null && dependency.getVersion() == null) { |
| 550 |
POMInfo containerPom = getPOM(sourcePom); |
| 551 |
String version = containerPom.getVersionFromManagementDependency(dependency); |
| 552 |
dependency.setVersion(version); |
| 553 |
if (version != null) { |
| 554 |
pom = getRepository().searchMatchingPOM(dependency); |
| 555 |
} else { |
| 556 |
System.out.println("In " + sourcePomLoc + ", cannot find the version for dependency " + dependency + " from this POM or its parent POMs"); |
| 557 |
if (pomTransformer.getListOfPOMs().getOrCreatePOMOptions(sourcePom).isNoParent()) { |
| 558 |
Dependency originalParent = getOriginalPOM(sourcePom).getParent(); |
| 559 |
System.out.println("[warning] Option --no-parent has been set for POM file " + sourcePomLoc + ", maybe it was not a good idea and you should first package the parent POM " + originalParent); |
| 560 |
} |
| 561 |
} |
| 562 |
} |
| 563 |
} catch (Exception e) { |
| 564 |
e.printStackTrace(); |
| 565 |
} |
| 566 |
if (pom == null && dependency.getVersion() != null) { |
| 567 |
List<POMInfo> poms = getRepository().searchMatchingPOMsIgnoreVersion(dependency); |
| 568 |
for (POMInfo potentialPom : poms) { |
| 569 |
for(DependencyRule rule : potentialPom.getPublishedRules()) { |
| 570 |
if (rule.matches(dependency) && rule.apply(dependency).equals(potentialPom.getThisPom())) { |
| 571 |
pom = potentialPom; |
| 572 |
pomTransformer.getRulesFiles().get(RULES).add(rule); |
| 573 |
} |
| 574 |
} |
| 575 |
} |
| 576 |
} |
| 577 |
if (pom == null && dependency.getVersion() == null) { |
| 578 |
// Set a dummy version and try again |
| 579 |
for (int version = 0; version < 10; version++) { |
| 580 |
dependency.setVersion(version + ".0"); |
| 581 |
pom = getRepository().searchMatchingPOM(dependency); |
| 582 |
if (pom != null) { |
| 583 |
System.out.println("Use best guess version: " + dependency.getVersion() + " for " |
| 584 |
+ dependency.getGroupId() + ":" + dependency.getArtifactId()); |
| 585 |
break; |
| 586 |
} |
| 587 |
dependency.setVersion(null); |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
if (pom == null && dependency.isPlugin()) { |
| 592 |
List<POMInfo> matchingPoms = getRepository().searchMatchingPOMsIgnoreVersion(dependency); |
| 593 |
if (matchingPoms.size() > 1) { |
| 594 |
issues.add(sourcePomLoc + ": More than one version matches the plugin " + dependency.getGroupId() + ":" |
| 595 |
+ dependency.getArtifactId() + ":" + dependency.getVersion()); |
| 596 |
} |
| 597 |
if (!matchingPoms.isEmpty()) { |
| 598 |
pom = matchingPoms.get(0); |
| 599 |
// Don't add a rule to force the version of a Maven plugin, it's now done |
| 600 |
// automatically at build time |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
// Ignore fast cases |
| 605 |
if (pom == null) { |
| 606 |
if (management) { |
| 607 |
if (verbose) System.out.println("[skipped dependency or plugin management]"); |
| 608 |
return null; |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
// In case we didn't find anything for "jar" packaging type, just check for a "bundle" type inside repository. |
| 613 |
// Some jars have been upgraded to OSGi bundles as OSGi metadata has been added to them. |
| 614 |
// |
| 615 |
// drazzib: I'm not sure this is really the right way to fix that (ie. maybe we should install "bundle" artifacts |
| 616 |
// directly with "jar" type inside Debian ?). |
| 617 |
// |
| 618 |
// ludovicc: a complex issue, I believe that libraries which evolve from a jar type to a bundle type should |
| 619 |
// inform packagers with a rule of the form |
| 620 |
// '<groupId> <artifactId> s/jar/bundle/ <versionRule>' |
| 621 |
// in other words, the packager of the library should add a published rule which will transform matching |
| 622 |
// libraries from jar type into bundle types, and apply as well the version substitution (for example to 2.x) |
| 623 |
// for Debian. |
| 624 |
// |
| 625 |
if (pom == null && dependency.isJar()) { |
| 626 |
if (verbose) System.out.println("[check dependency with bundle type]"); |
| 627 |
|
| 628 |
Dependency bundleDependency = dependency.builder().setType("bundle").build(); |
| 629 |
pom = getRepository().searchMatchingPOM(bundleDependency); |
| 630 |
if (pom != null) { |
| 631 |
dependency = bundleDependency; |
| 632 |
for (DependencyRule rule: pom.getPublishedRules()) { |
| 633 |
if (rule.matchesAndPreservesGroupArtifactAndType(dependency)) { |
| 634 |
pomTransformer.getRulesFiles().get(RULES).add(new DependencyRule( |
| 635 |
pom.getThisPom().getGroupId(), |
| 636 |
pom.getThisPom().getArtifactId(), |
| 637 |
"s/jar/bundle/", |
| 638 |
rule.getVersionRule().toString())); |
| 639 |
} |
| 640 |
} |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
if (pom == null) { |
| 645 |
if (resolvingParent && ignoreDependencyQuestion.askIgnoreDependency(sourcePomLoc, dependency, |
| 646 |
"The parent POM cannot be found in the Maven repository for Debian. Ignore it?")) { |
| 647 |
pomTransformer.getListOfPOMs().getOrCreatePOMOptions(sourcePom).setNoParent(true); |
| 648 |
if (verbose) System.out.println("[no-parent]"); |
| 649 |
return null; |
| 650 |
} |
| 651 |
boolean ignoreDependency = ignoreDependencyQuestion.askIgnoreDocOrReportPlugin(sourcePomLoc, dependency); |
| 652 |
if(!ignoreDependency) { |
| 653 |
String issue = ignoreDependencyQuestion.askIgnoreNeededDependency(sourcePomLoc, dependency); |
| 654 |
if(issue.isEmpty()) { |
| 655 |
ignoreDependency = true; |
| 656 |
} else { |
| 657 |
issues.add(issue); |
| 658 |
} |
| 659 |
} |
| 660 |
if (ignoreDependency) { |
| 661 |
ignoredDependencies.add(dependency); |
| 662 |
pomTransformer.getRulesFiles().get(IGNORE).add(new DependencyRule(dependency.getGroupId(), dependency.getArtifactId(), "*", "*")); |
| 663 |
if (verbose) System.out.println("[ignored]"); |
| 664 |
return null; |
| 665 |
} |
| 666 |
|
| 667 |
// We're not ignoring the dependency |
| 668 |
DebianDependency pkg = scanner.searchPkgContainingPom(dependency); |
| 669 |
if (pkg != null) { |
| 670 |
String installedVersion = scanner.getPackageVersion(pkg, true); |
| 671 |
if (installedVersion != null) { |
| 672 |
System.out.println("[error] Package " + pkg + " (" + installedVersion + ") is already installed and contains a possible match," ); |
| 673 |
System.out.println("but I cannot resolve library " + dependency + " in it."); |
| 674 |
System.out.println("[error] Please check manually that the library is up to date, otherwise it may be necessary to package version " |
| 675 |
+ dependency.getVersion() + " in Debian."); |
| 676 |
} else { |
| 677 |
System.out.println("[warning] Please install the missing dependency. Run the following command in another terminal:"); |
| 678 |
System.out.println(" sudo apt-get install " + pkg); |
| 679 |
} |
| 680 |
} |
| 681 |
|
| 682 |
if (interactive && pkg == null) { |
| 683 |
pkg = scanner.searchPkgContainingJar(dependency); |
| 684 |
if (pkg != null) { |
| 685 |
String question = "[error] Package " + pkg + " does not contain Maven dependency " + dependency + " but there seem to be a match\n" |
| 686 |
+ "If the package contains already Maven artifacts but the names don't match, try to enter a substitution rule\n" |
| 687 |
+ "of the form s/groupId/newGroupId/ s/artifactId/newArtifactId/ jar s/version/newVersion/ here:"; |
| 688 |
String newRule = new SimpleQuestion(question).ask(); |
| 689 |
if (!newRule.isEmpty()) { |
| 690 |
DependencyRule userRule = new DependencyRule(newRule); |
| 691 |
pomTransformer.getRulesFiles().get(RULES).add(userRule); |
| 692 |
System.out.println("Please suggest the maintainer of package " + pkg + " to add this rule to debian/maven.publishedRules"); |
| 693 |
return resolveDependency(dependency.applyRules(Arrays.asList(userRule)), sourcePom, buildTime, mavenExtension, management, false); |
| 694 |
} |
| 695 |
} else { |
| 696 |
String newRule = new SimpleQuestion( |
| 697 |
"[error] Cannot resolve Maven dependency " + dependency + ". If you know a package that contains a compatible dependency,\n" |
| 698 |
+ "try to enter a substitution rule of the form s/groupId/newGroupId/ s/artifactId/newArtifactId/ jar s/version/newVersion/ here:\n").ask(); |
| 699 |
while (!newRule.isEmpty()) { |
| 700 |
DependencyRule userRule = new DependencyRule(newRule); |
| 701 |
Dependency newDependency = dependency.applyRules(Arrays.asList(userRule)); |
| 702 |
if (newDependency.equals(dependency)) { |
| 703 |
newRule = new SimpleQuestion("Your rule doesn't seem to apply on " + dependency |
| 704 |
+ "Please enter a substitution rule of the form s/groupId/newGroupId/ s/artifactId/newArtifactId/ jar s/version/newVersion/ here," |
| 705 |
+ "or press <Enter> to give up").ask(); |
| 706 |
} else { |
| 707 |
pomTransformer.getRulesFiles().get(RULES).add(userRule); |
| 708 |
System.out.println("Rescanning /usr/share/maven-repo..."); |
| 709 |
pomTransformer.getRepository().scan(); |
| 710 |
return resolveDependency(dependency.applyRules(Arrays.asList(userRule)), sourcePom, buildTime, mavenExtension, management, false); |
| 711 |
} |
| 712 |
} |
| 713 |
} |
| 714 |
} |
| 715 |
|
| 716 |
if (interactive && new YesNoQuestion("Try again to resolve the dependency?", true).ask()) { |
| 717 |
System.out.println("Rescanning /usr/share/maven-repo..."); |
| 718 |
pomTransformer.getRepository().scan(); |
| 719 |
// Clear caches |
| 720 |
scanner = scanner.newInstanceWithFreshCaches(); |
| 721 |
return resolveDependency(dependency, sourcePom, buildTime, mavenExtension, management, false); |
| 722 |
} |
| 723 |
|
| 724 |
if (verbose) System.out.println("[error]"); |
| 725 |
|
| 726 |
throw new DependencyNotFoundException(dependency); |
| 727 |
} |
| 728 |
|
| 729 |
// Handle the case of Maven plugins built and used in a multi-module build: |
| 730 |
// they need to be added to maven.cleanIgnoreRules to avoid errors during |
| 731 |
// a mvn clean |
| 732 |
if (dependency.isPlugin() && containsDependencyIgnoreVersion(projectPoms, dependency)) { |
| 733 |
String ruleDef = dependency.getGroupId() + " " + dependency.getArtifactId() + " maven-plugin *"; |
| 734 |
pomTransformer.getRulesFiles().get(CLEAN).add(new DependencyRule(ruleDef)); |
| 735 |
} |
| 736 |
|
| 737 |
// Discover the library to import for the dependency |
| 738 |
DebianDependency pkg = getPackage(pom, sourcePomLoc); |
| 739 |
|
| 740 |
if (pkg != null && !pkg.equals(packageName)) { |
| 741 |
DebianDependency libraryWithVersionConstraint; |
| 742 |
if (pom.getOriginalVersion() != null && (pom.getProperties().containsKey("debian.hasPackageVersion"))) { |
| 743 |
String version = dependency.getVersion(); |
| 744 |
if (version == null || (pom.getOriginalVersion() != null && version.compareTo(pom.getOriginalVersion()) > 0)) { |
| 745 |
version = pom.getOriginalVersion(); |
| 746 |
} |
| 747 |
libraryWithVersionConstraint = new DebianDependency(pkg.getPackageName(), version); |
| 748 |
} else { |
| 749 |
libraryWithVersionConstraint = pkg; |
| 750 |
} |
| 751 |
if (!management) { |
| 752 |
if (buildTime) { |
| 753 |
if ("test".equals(dependency.getScope())) { |
| 754 |
debianDeps.add(TEST, libraryWithVersionConstraint); |
| 755 |
} else if (dependency.isPlugin()) { |
| 756 |
debianDeps.add(COMPILE, libraryWithVersionConstraint); |
| 757 |
} else if (mavenExtension) { |
| 758 |
debianDeps.add(COMPILE, libraryWithVersionConstraint); |
| 759 |
} else { |
| 760 |
debianDeps.add(COMPILE, libraryWithVersionConstraint); |
| 761 |
} |
| 762 |
} else { |
| 763 |
if ("test".equals(dependency.getScope())) { |
| 764 |
debianDeps.add(TEST, libraryWithVersionConstraint); |
| 765 |
} else if (dependency.isOptional()) { |
| 766 |
debianDeps.add(OPTIONAL, libraryWithVersionConstraint); |
| 767 |
} else if (!"provided".equals(dependency.getScope())) { |
| 768 |
debianDeps.add(RUNTIME, libraryWithVersionConstraint); |
| 769 |
} |
| 770 |
} |
| 771 |
} |
| 772 |
versionedPackagesAndDependencies.put(libraryWithVersionConstraint, dependency); |
| 773 |
} |
| 774 |
|
| 775 |
pomTransformer.getRulesFiles().get(RULES).addAll(pom.getPublishedRules()); |
| 776 |
|
| 777 |
if (verbose) { |
| 778 |
System.out.println("Dependency " + dependency + " found in package " + pkg); |
| 779 |
System.out.println("[ok]"); |
| 780 |
System.out.println(); |
| 781 |
} |
| 782 |
|
| 783 |
if (resolvingParent) { |
| 784 |
try { |
| 785 |
POMInfo containerPom = getPOM(sourcePom); |
| 786 |
containerPom.setParentPOM(pom); |
| 787 |
|
| 788 |
} catch (Exception e) { |
| 789 |
e.printStackTrace(); |
| 790 |
} |
| 791 |
} |
| 792 |
return pom.getThisPom(); |
| 793 |
} |
| 794 |
|
| 795 |
private DebianDependency getPackage(POMInfo pom, String sourcePomLoc) { |
| 796 |
DebianDependency pkg = null; |
| 797 |
if (pom.getProperties() != null) { |
| 798 |
pkg = new DebianDependency(pom.getProperties().get("debian.package")); |
| 799 |
} |
| 800 |
if (pkg == null) { |
| 801 |
Dependency dependency = pom.getThisPom(); |
| 802 |
issues.add(sourcePomLoc + ": Dependency is missing the Debian properties in its POM: " + dependency.getGroupId() + ":" |
| 803 |
+ dependency.getArtifactId() + ":" + dependency.getVersion()); |
| 804 |
File pomFile = new File(mavenRepo, dependency.getGroupId().replace(".", "/") + "/" + dependency.getArtifactId() + "/" + dependency.getVersion() + "/" + dependency.getArtifactId() + "-" + dependency.getVersion() + ".pom"); |
| 805 |
pkg = scanner.searchPkg(pomFile); |
| 806 |
} |
| 807 |
return pkg; |
| 808 |
} |
| 809 |
|
| 810 |
private boolean containsDependencyIgnoreVersion(Collection<Dependency> dependencies, Dependency dependency) { |
| 811 |
for (Dependency ignoredDependency : dependencies) { |
| 812 |
if (ignoredDependency.equalsIgnoreVersion(dependency)) { |
| 813 |
return true; |
| 814 |
} |
| 815 |
} |
| 816 |
return false; |
| 817 |
} |
| 818 |
|
| 819 |
public static void main(String[] args) { |
| 820 |
if (args.length == 0 || "-h".equals(args[0]) || "--help".equals(args[0])) { |
| 821 |
System.out.println("Purpose: Solve the dependencies in the POM(s)."); |
| 822 |
System.out.println("Usage: [option]"); |
| 823 |
System.out.println(""); |
| 824 |
System.out.println("Options:"); |
| 825 |
System.out.println(" -v, --verbose: be extra verbose"); |
| 826 |
System.out.println(" -p<package>, --package=<package>: name of the Debian package containing"); |
| 827 |
System.out.println(" this library"); |
| 828 |
// System.out.println(" -r<rules>, --rules=<rules>: path to the file containing the"); |
| 829 |
// System.out.println(" extra rules to apply when cleaning the POM"); |
| 830 |
// System.out.println(" -i<rules>, --published-rules=<rules>: path to the file containing the"); |
| 831 |
// System.out.println(" extra rules to publish in the property debian.mavenRules in the cleaned POM"); |
| 832 |
System.out.println(" --run-tests: run the unit tests"); |
| 833 |
System.out.println(" --generate-javadoc: generate Javadoc"); |
| 834 |
System.out.println(" --non-interactive: non interactive session"); |
| 835 |
System.out.println(" --offline: offline mode for Debian build compatibility"); |
| 836 |
System.out.println(" -m<repo root>--maven-repo=<repo root>: location of the Maven repository,"); |
| 837 |
System.out.println(" used to force the versions of the Maven plugins used in the current"); |
| 838 |
System.out.println(" POM file with the versions found in the repository"); |
| 839 |
System.out.println(" --base-directory: path to root directory of package"); |
| 840 |
System.out.println(" --non-explore: doesn't explore directories for pom.xml"); |
| 841 |
System.out.println(" --build: build mode (don't write the .poms and rules files)"); |
| 842 |
return; |
| 843 |
} |
| 844 |
|
| 845 |
// Default values |
| 846 |
boolean verbose = false; |
| 847 |
String debianPackage = ""; |
| 848 |
File mavenRepo = null; |
| 849 |
File baseDirectory = new File("."); |
| 850 |
boolean exploreProjects = true; |
| 851 |
boolean runTests = false; |
| 852 |
boolean generateJavadoc = false; |
| 853 |
boolean interactive = true; |
| 854 |
boolean offline = false; |
| 855 |
boolean build = false; |
| 856 |
|
| 857 |
// Parse parameters |
| 858 |
int i = inc(-1, args); |
| 859 |
while (i < args.length && (args[i].trim().startsWith("-") || args[i].trim().isEmpty())) { |
| 860 |
String arg = args[i].trim(); |
| 861 |
if ("--verbose".equals(arg) || "-v".equals(arg)) { |
| 862 |
verbose = true; |
| 863 |
} else if ("--debug".equals(arg)) { |
| 864 |
log.setLevel(Level.FINEST); |
| 865 |
} else if (arg.startsWith("-p")) { |
| 866 |
debianPackage = arg.substring(2); |
| 867 |
} else if (arg.startsWith("--package=")) { |
| 868 |
debianPackage = arg.substring("--package=".length()); |
| 869 |
} else if (arg.equals("--run-tests")) { |
| 870 |
runTests = true; |
| 871 |
} else if (arg.equals("--generate-javadoc")) { |
| 872 |
generateJavadoc = true; |
| 873 |
} else if (arg.equals("--non-interactive")) { |
| 874 |
interactive = false; |
| 875 |
} else if (arg.equals("--offline")) { |
| 876 |
offline = true; |
| 877 |
} else if (arg.startsWith("-m")) { |
| 878 |
mavenRepo = new File(arg.substring(2)); |
| 879 |
} else if (arg.startsWith("--maven-repo=")) { |
| 880 |
mavenRepo = new File(arg.substring("--maven-repo=".length())); |
| 881 |
} else if (arg.startsWith("-b")) { |
| 882 |
baseDirectory = new File(arg.substring(2)); |
| 883 |
} else if (arg.startsWith("--base-directory=")) { |
| 884 |
baseDirectory = new File(arg.substring("--base-directory=".length())); |
| 885 |
} else if (arg.equals("--non-explore")) { |
| 886 |
exploreProjects = false; |
| 887 |
} else if (arg.equals("--build")) { |
| 888 |
build = true; |
| 889 |
} |
| 890 |
|
| 891 |
i = inc(i, args); |
| 892 |
} |
| 893 |
|
| 894 |
File outputDirectory = new File(baseDirectory, "debian"); |
| 895 |
DependenciesSolver solver = new DependenciesSolver(outputDirectory, new PackageScanner(offline), interactive); |
| 896 |
solver.generateJavadoc = generateJavadoc; |
| 897 |
solver.runTests = runTests; |
| 898 |
solver.exploreProjects = exploreProjects; |
| 899 |
solver.setBaseDir(baseDirectory); |
| 900 |
solver.packageName = debianPackage; |
| 901 |
File poms = new File(solver.outputDirectory, debianPackage + ".poms"); |
| 902 |
solver.setListOfPoms(poms); |
| 903 |
|
| 904 |
if (mavenRepo != null) { |
| 905 |
Repository repository = new Repository(mavenRepo); |
| 906 |
solver.pomTransformer.setRepository(repository); |
| 907 |
solver.pomTransformer.usePluginVersionsFromRepository(); |
| 908 |
} |
| 909 |
|
| 910 |
if (verbose) { |
| 911 |
String msg = "Solving dependencies for package " + debianPackage; |
| 912 |
if (solver.runTests) { |
| 913 |
msg += " (tests are included)"; |
| 914 |
} |
| 915 |
if (solver.generateJavadoc) { |
| 916 |
msg += " (documentation is included)"; |
| 917 |
} |
| 918 |
System.out.println(msg); |
| 919 |
solver.verbose = true; |
| 920 |
} |
| 921 |
|
| 922 |
solver.solveDependencies(); |
| 923 |
|
| 924 |
// Don't overwrite the poms or rules files during a build |
| 925 |
if (!build) { |
| 926 |
solver.pomTransformer.getListOfPOMs().save(); |
| 927 |
solver.pomTransformer.getRulesFiles().save(outputDirectory); |
| 928 |
} |
| 929 |
// Do generate the substvars though |
| 930 |
solver.saveSubstvars(); |
| 931 |
|
| 932 |
if (!solver.issues.isEmpty()) { |
| 933 |
System.err.println("Some problems were found in this project, exiting..."); |
| 934 |
System.exit(1); |
| 935 |
} |
| 936 |
} |
| 937 |
|
| 938 |
private static int inc(int i, String[] args) { |
| 939 |
do { |
| 940 |
i++; |
| 941 |
} while (i < args.length && args[i].isEmpty()); |
| 942 |
return i; |
| 943 |
} |
| 944 |
} |