1 16 package org.outerj.daisy.tools.artifacter; 17 18 import org.apache.commons.cli.*; 19 import org.w3c.dom.Document ; 20 import org.w3c.dom.Element ; 21 import org.w3c.dom.NodeList ; 22 import org.jaxen.dom.DOMXPath; 23 24 import javax.xml.parsers.DocumentBuilderFactory ; 25 import javax.xml.parsers.DocumentBuilder ; 26 import javax.xml.transform.TransformerFactory ; 27 import javax.xml.transform.Transformer ; 28 import javax.xml.transform.stream.StreamResult ; 29 import javax.xml.transform.dom.DOMSource ; 30 import java.io.*; 31 import java.util.ArrayList ; 32 import java.util.Collection ; 33 import java.util.Iterator ; 34 import java.util.List ; 35 import java.util.regex.Pattern ; 36 import java.util.regex.Matcher ; 37 import java.nio.channels.FileChannel ; 38 39 42 public class Artifacter { 43 public static void main(String [] args) throws Exception { 44 new Artifacter().run(args); 45 } 46 47 private Collection mavenProjectXmls; 48 private Collection merlinBlockXmls; 49 50 public void run(String [] args) throws Exception { 51 52 Options options = new Options(); 53 options.addOption(new Option("f", "find-usages", true, "Find usages of an artifact <groupId:artifactId>")); 54 options.addOption(new Option("r", "rename", false, "Renames artifact usages, use options o and n to specify original and new artifact name")); 55 options.addOption(new Option("o", "original", true, "original artifact name in rename operation as <groupId:artifactId>")); 56 options.addOption(new Option("n", "new", true, "new artifact name in rename operation as <groupId:artifactId>")); 57 options.addOption(new Option("u", "update", true, "Update artifact version, specify <groupId:artifactId:versionId>")); 58 options.addOption(new Option("g", "global-update", true, "Update version of all artifacts of a certain group, specify <groupId:versionId>")); 59 options.addOption(new Option("c", "create-local-repo", false, "Creates a repository directory structure containing all jars contained in the classloader description of all block.xml files.")); 60 options.addOption(new Option("s", "source-repo", true, "Source repository for -c and -l.")); 61 options.addOption(new Option("d", "dest-repo", true, "Destination repository for -c and -l.")); 62 options.addOption(new Option("l", "copy-deps-into-local-repo", true, "Copies the dependencies from the project.xml given as argument into another maven repository. Needs -s and -d arguments.")); 63 options.addOption(new Option("a", "copy-artifact-into-local-repo", true, "Copies the artifact build by the project.xml given as argument into another maven repository. Needs -s and -d arguments.")); 64 65 CommandLineParser parser = new PosixParser(); 66 CommandLine cmd = parser.parse(options, args); 67 68 if (cmd.hasOption('f')) { 69 String artifactSpec = cmd.getOptionValue('f'); 70 findUsages(artifactSpec); 71 } else if (cmd.hasOption('r')) { 72 if (!cmd.hasOption('o') || !cmd.hasOption('n')) { 73 System.out.println("Missing arguments for rename operation."); 74 return; 75 } 76 String originalArtifactSpec = cmd.getOptionValue('o'); 77 String newArtifactSpec = cmd.getOptionValue('n'); 78 renameArtifact(originalArtifactSpec, newArtifactSpec); 79 } else if (cmd.hasOption('u')) { 80 String artifactSpec = cmd.getOptionValue('u'); 81 updateArtifactVersion(artifactSpec); 82 } else if (cmd.hasOption('g')) { 83 String spec = cmd.getOptionValue('g'); 84 globalVersionUpdate(spec); 85 } else if (cmd.hasOption('c') || cmd.hasOption('l') || cmd.hasOption('a')) { 86 File sourceRepo = new File(cmd.getOptionValue('s')); 87 if (!sourceRepo.exists()) { 88 System.err.println("Source repository " + sourceRepo.getAbsolutePath() + " does not exist."); 89 System.exit(1); 90 } 91 File destinationRepo = new File(cmd.getOptionValue('d')); 92 if (!destinationRepo.exists()) { 93 System.err.println("Destination repository " + destinationRepo.getAbsolutePath() + " does not exist."); 94 System.exit(1); 95 } 96 if (cmd.hasOption('c')) { 97 createBlockDepsRepository(sourceRepo, destinationRepo); 98 } else if (cmd.hasOption('l')) { 99 File projectXml = new File(cmd.getOptionValue('l')); 100 if (!projectXml.exists()) { 101 System.err.println("Maven project.xml " + destinationRepo.getAbsolutePath() + " does not exist."); 102 System.exit(1); 103 } 104 copyDepsToRepository(sourceRepo, destinationRepo, projectXml); 105 } else if (cmd.hasOption('a')) { 106 File projectXml = new File(cmd.getOptionValue('a')); 107 if (!projectXml.exists()) { 108 System.err.println("Maven project.xml " + destinationRepo.getAbsolutePath() + " does not exist."); 109 System.exit(1); 110 } 111 copyArtifactToRepository(sourceRepo, destinationRepo, projectXml); 112 } 113 } else { 114 printHelp(); 115 } 116 117 } 118 119 private void printHelp() { 120 System.out.println("[help instructions]"); 121 } 122 123 private void updateArtifactVersion(String artifact) throws Exception { 124 findFiles(); 125 126 Pattern artifactPattern = Pattern.compile("^(.*):(.*):(.*)$"); 127 Matcher matcher = artifactPattern.matcher(artifact); 128 if (!matcher.matches()) { 129 System.out.println("Invalid artifact spec: " + artifact); 130 System.exit(1); 131 } 132 133 String groupId = matcher.group(1); 134 String artifactId = matcher.group(2); 135 String version = matcher.group(3); 136 137 updateInProjectXmls(groupId, artifactId, version); 138 updateInBlockXmls(groupId, artifactId, version); 139 saveFiles(mavenProjectXmls); 140 saveFiles(merlinBlockXmls); 141 } 142 143 private void updateInProjectXmls(String groupId, String artifactId, String version) throws Exception { 144 System.out.println("Searching and updating in project.xml files..."); 145 146 DOMXPath projectXPath = new DOMXPath("/project[groupId='" + groupId + "' and id='" + artifactId + "']"); 147 DOMXPath projectVersionXPath = new DOMXPath("/project/currentVersion"); 148 149 DOMXPath xpath = new DOMXPath("/project/dependencies/dependency[groupId='" + groupId + "' and artifactId='" + artifactId + "']"); 150 DOMXPath versionXPath = new DOMXPath("version"); 151 Iterator mavenProjectXmlsIt = mavenProjectXmls.iterator(); 152 while (mavenProjectXmlsIt.hasNext()) { 153 XmlFile xmlFile = (XmlFile)mavenProjectXmlsIt.next(); 154 if (projectXPath.booleanValueOf(xmlFile.document)) { 156 Element versionEl = (Element )projectVersionXPath.selectSingleNode(xmlFile.document); 157 setElementValue(versionEl, version); 158 xmlFile.changed = true; 159 } 160 161 List nodes = xpath.selectNodes(xmlFile.document); 163 Iterator nodesIt = nodes.iterator(); 164 while (nodesIt.hasNext()) { 165 Element element = (Element )nodesIt.next(); 166 Element versionEl = (Element )versionXPath.selectSingleNode(element); 167 setElementValue(versionEl, version); 168 xmlFile.changed = true; 169 } 170 } 171 } 172 173 private void updateInBlockXmls(String groupId, String artifactId, String version) throws Exception { 174 System.out.println("Searching and updating in block.xml files..."); 175 String artifactSpec = groupId + ":" + artifactId; 176 DOMXPath xpath = new DOMXPath("//classloader/classpath/repository/resource[@id='" + artifactSpec + "']"); 177 Iterator merlinBlockXmlsIt = merlinBlockXmls.iterator(); 178 while (merlinBlockXmlsIt.hasNext()) { 179 XmlFile xmlFile = (XmlFile)merlinBlockXmlsIt.next(); 180 List nodes = xpath.selectNodes(xmlFile.document); 181 Iterator nodesIt = nodes.iterator(); 182 while (nodesIt.hasNext()) { 183 Element element = (Element )nodesIt.next(); 184 element.setAttribute("version", version); 185 xmlFile.changed = true; 186 } 187 } 188 } 189 190 private void globalVersionUpdate(String spec) throws Exception { 191 findFiles(); 192 193 Pattern artifactPattern = Pattern.compile("^(.*):(.*)$"); 194 Matcher matcher = artifactPattern.matcher(spec); 195 if (!matcher.matches()) { 196 System.out.println("Invalid spec: " + spec); 197 System.exit(1); 198 } 199 200 String groupId = matcher.group(1); 201 String version = matcher.group(2); 202 203 globalUpdateInProjectXmls(groupId, version); 204 globalUpdateInBlockXmls(groupId, version); 205 saveFiles(mavenProjectXmls); 206 saveFiles(merlinBlockXmls); 207 } 208 209 private void globalUpdateInProjectXmls(String groupId, String version) throws Exception { 210 System.out.println("Searching and updating in project.xml files..."); 211 212 DOMXPath projectXPath = new DOMXPath("/project[groupId='" + groupId + "']"); 213 DOMXPath projectVersionXPath = new DOMXPath("/project/currentVersion"); 214 215 DOMXPath xpath = new DOMXPath("/project/dependencies/dependency[groupId='" + groupId + "']"); 216 DOMXPath versionXPath = new DOMXPath("version"); 217 Iterator mavenProjectXmlsIt = mavenProjectXmls.iterator(); 218 while (mavenProjectXmlsIt.hasNext()) { 219 XmlFile xmlFile = (XmlFile)mavenProjectXmlsIt.next(); 220 if (projectXPath.booleanValueOf(xmlFile.document)) { 222 Element versionEl = (Element )projectVersionXPath.selectSingleNode(xmlFile.document); 223 setElementValue(versionEl, version); 224 xmlFile.changed = true; 225 } 226 227 List nodes = xpath.selectNodes(xmlFile.document); 229 Iterator nodesIt = nodes.iterator(); 230 while (nodesIt.hasNext()) { 231 Element element = (Element )nodesIt.next(); 232 Element versionEl = (Element )versionXPath.selectSingleNode(element); 233 setElementValue(versionEl, version); 234 xmlFile.changed = true; 235 } 236 } 237 } 238 239 private void globalUpdateInBlockXmls(String groupId, String version) throws Exception { 240 System.out.println("Searching and updating in block.xml files..."); 241 String artifactSpec = groupId + ":"; 242 DOMXPath xpath = new DOMXPath("//classloader/classpath/repository/resource[starts-with(@id,'" + artifactSpec + "')] | //include[starts-with(@id,'" + artifactSpec + "')]"); 243 Iterator merlinBlockXmlsIt = merlinBlockXmls.iterator(); 244 while (merlinBlockXmlsIt.hasNext()) { 245 XmlFile xmlFile = (XmlFile)merlinBlockXmlsIt.next(); 246 List nodes = xpath.selectNodes(xmlFile.document); 247 Iterator nodesIt = nodes.iterator(); 248 while (nodesIt.hasNext()) { 249 Element element = (Element )nodesIt.next(); 250 element.setAttribute("version", version); 251 xmlFile.changed = true; 252 } 253 } 254 } 255 256 private void renameArtifact(String originalArtifactSpec, String newArtifactSpec) throws Exception { 257 Artifact originalArtifact = parseArtifactSpec(originalArtifactSpec); 258 Artifact newArtifact = parseArtifactSpec(newArtifactSpec); 259 260 findFiles(); 261 checkIfNewAlreadyExists(newArtifact); 262 renameInProjectXmls(originalArtifact, newArtifact); 263 renameUsagesInBlockXmls(originalArtifact, newArtifact); 264 saveFiles(mavenProjectXmls); 265 saveFiles(merlinBlockXmls); 266 } 267 268 private void checkIfNewAlreadyExists(Artifact newArtifact) throws Exception { 269 DOMXPath projectXPath = new DOMXPath("/project[groupId='" + newArtifact.groupId + "' and id='" + newArtifact.artifactId + "']"); 270 271 Iterator mavenProjectXmlsIt = mavenProjectXmls.iterator(); 272 while (mavenProjectXmlsIt.hasNext()) { 273 XmlFile xmlFile = (XmlFile)mavenProjectXmlsIt.next(); 274 if (projectXPath.booleanValueOf(xmlFile.document)) { 275 System.out.println("Warning: the following file:"); 276 System.out.println(xmlFile.file.getAbsolutePath()); 277 System.out.println("already defines the artifact " + newArtifact.groupId + ":" + newArtifact.artifactId); 278 if (!promptYesNo("Are you sure you want to continue? [yes/no, default = no]", false)) { 279 System.exit(1); 280 } 281 } 282 } 283 } 284 285 private void renameInProjectXmls(Artifact originalArtifact, Artifact newArtifact) throws Exception { 286 System.out.println("Searching and renaming in project.xml files..."); 287 288 DOMXPath projectXPath = new DOMXPath("/project[groupId='" + originalArtifact.groupId + "' and id='" + originalArtifact.artifactId + "']"); 289 DOMXPath projectGroupIdXPath = new DOMXPath("/project/groupId"); 290 DOMXPath projectIdXPath = new DOMXPath("/project/id"); 291 292 DOMXPath xpath = new DOMXPath("/project/dependencies/dependency[groupId='" + originalArtifact.groupId + "' and artifactId='" + originalArtifact.artifactId + "']"); 293 DOMXPath groupIdXPath = new DOMXPath("groupId"); 294 DOMXPath artifactIdXPath = new DOMXPath("artifactId"); 295 Iterator mavenProjectXmlsIt = mavenProjectXmls.iterator(); 296 while (mavenProjectXmlsIt.hasNext()) { 297 XmlFile xmlFile = (XmlFile)mavenProjectXmlsIt.next(); 298 if (projectXPath.booleanValueOf(xmlFile.document)) { 300 Element groupIdEl = (Element )projectGroupIdXPath.selectSingleNode(xmlFile.document); 301 Element idEl = (Element )projectIdXPath.selectSingleNode(xmlFile.document); 302 setElementValue(groupIdEl, newArtifact.groupId); 303 setElementValue(idEl, newArtifact.artifactId); 304 xmlFile.changed = true; 305 } 306 307 List nodes = xpath.selectNodes(xmlFile.document); 309 Iterator nodesIt = nodes.iterator(); 310 while (nodesIt.hasNext()) { 311 Element element = (Element )nodesIt.next(); 312 Element groupIdEl = (Element )groupIdXPath.selectSingleNode(element); 313 Element artifactIdEl = (Element )artifactIdXPath.selectSingleNode(element); 314 setElementValue(groupIdEl, newArtifact.groupId); 315 setElementValue(artifactIdEl, newArtifact.artifactId); 316 xmlFile.changed = true; 317 } 318 } 319 } 320 321 private void setElementValue(Element element, String value) { 322 NodeList nodes = element.getChildNodes(); 323 for (int i = 0; i < nodes.getLength(); i++) 324 element.removeChild(nodes.item(i)); 325 326 element.appendChild(element.getOwnerDocument().createTextNode(value)); 327 } 328 329 private void renameUsagesInBlockXmls(Artifact originalArtifact, Artifact newArtifact) throws Exception { 330 System.out.println("Searching and renaming in block.xml files..."); 331 String originalArtifactSpec = originalArtifact.groupId + ":" + originalArtifact.artifactId; 332 String newArtifactSpec = newArtifact.groupId + ":" + newArtifact.artifactId; 333 DOMXPath xpath = new DOMXPath("//classloader/classpath/repository/resource[@id='" + originalArtifactSpec + "']"); 334 Iterator merlinBlockXmlsIt = merlinBlockXmls.iterator(); 335 while (merlinBlockXmlsIt.hasNext()) { 336 XmlFile xmlFile = (XmlFile)merlinBlockXmlsIt.next(); 337 List nodes = xpath.selectNodes(xmlFile.document); 338 Iterator nodesIt = nodes.iterator(); 339 while (nodesIt.hasNext()) { 340 Element element = (Element )nodesIt.next(); 341 element.setAttribute("id", newArtifactSpec); 342 xmlFile.changed = true; 343 } 344 } 345 } 346 347 private void findUsages(String artifactSpec) throws Exception { 348 Artifact artifact = parseArtifactSpec(artifactSpec); 349 350 System.out.println("Will search for groupId '" + artifact.groupId + "' and artifactId '" + artifact.artifactId + "'."); 351 352 findFiles(); 353 printUsagesInProjectXmls(artifact); 354 printUsagesInBlockXmls(artifact); 355 } 356 357 private void printUsagesInProjectXmls(Artifact artifact) throws Exception { 358 System.out.println("Searching in project.xml files..."); 359 DOMXPath xpath = new DOMXPath("/project/dependencies/dependency[groupId='" + artifact.groupId + "' and artifactId='" + artifact.artifactId + "']"); 360 DOMXPath versionXPath = new DOMXPath("string(version)"); 361 Iterator mavenProjectXmlsIt = mavenProjectXmls.iterator(); 362 while (mavenProjectXmlsIt.hasNext()) { 363 XmlFile xmlFile = (XmlFile)mavenProjectXmlsIt.next(); 364 List nodes = xpath.selectNodes(xmlFile.document); 365 Iterator nodesIt = nodes.iterator(); 366 while (nodesIt.hasNext()) { 367 Element element = (Element )nodesIt.next(); 368 String version = versionXPath.stringValueOf(element); 369 System.out.println(xmlFile.file.getAbsolutePath() + " uses version " + version); 370 } 371 } 372 } 373 374 private void printUsagesInBlockXmls(Artifact artifact) throws Exception { 375 System.out.println("Searching in block.xml files..."); 376 String artifactSpec = artifact.groupId + ":" + artifact.artifactId; 377 DOMXPath xpath = new DOMXPath("//classloader/classpath/repository/resource[@id='" + artifactSpec + "']"); 378 DOMXPath versionXPath = new DOMXPath("string(@version)"); 379 Iterator merlinBlockXmlsIt = merlinBlockXmls.iterator(); 380 while (merlinBlockXmlsIt.hasNext()) { 381 XmlFile xmlFile = (XmlFile)merlinBlockXmlsIt.next(); 382 List nodes = xpath.selectNodes(xmlFile.document); 383 Iterator nodesIt = nodes.iterator(); 384 while (nodesIt.hasNext()) { 385 Element element = (Element )nodesIt.next(); 386 String version = versionXPath.stringValueOf(element); 387 System.out.println(xmlFile.file.getAbsolutePath() + " uses version " + version); 388 } 389 } 390 } 391 392 private Document parseFile(File file) throws Exception { 393 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); 394 documentBuilderFactory.setNamespaceAware(true); 395 DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); 396 return documentBuilder.parse(file); 397 } 398 399 private void saveFiles(Collection xmlFiles) throws Exception { 400 Iterator it = xmlFiles.iterator(); 401 while (it.hasNext()) { 402 XmlFile xmlFile = (XmlFile)it.next(); 403 if (xmlFile.changed) { 404 save(xmlFile.file, xmlFile.document); 405 System.out.println("Saved updated " + xmlFile.file.getAbsolutePath()); 406 } 407 } 408 } 409 410 private void save(File file, Document document) throws Exception { 411 TransformerFactory transformerFactory = TransformerFactory.newInstance(); 412 Transformer transformer = transformerFactory.newTransformer(); 413 DOMSource source = new DOMSource (document); 414 StreamResult result = new StreamResult (file); 415 transformer.setOutputProperty("encoding", "UTF-8"); 416 transformer.transform(source, result); 417 } 418 419 private void findFiles() throws Exception { 420 File workingDir = new File(System.getProperty("user.dir")); 421 System.out.println("Working directory is: " + workingDir); 422 423 findMavenProjectXmls(workingDir); 424 findMerlinBlockXmls(workingDir); 425 } 426 427 private void findMavenProjectXmls(File workingDir) throws Exception { 428 System.out.println("Searching for Maven project.xml files..."); 429 mavenProjectXmls = findFiles(workingDir, "project.xml"); 430 System.out.println("Found " + mavenProjectXmls.size() + " project.xml files."); 431 } 432 433 private void findMerlinBlockXmls(File workingDir) throws Exception { 434 System.out.println("Searching for Merlin block.xml files..."); 435 merlinBlockXmls = findFiles(workingDir, "block.xml"); 436 System.out.println("Found " + merlinBlockXmls.size() + " block.xml files."); 437 } 438 439 private Collection findFiles(File startDir, String name) throws Exception { 440 ArrayList result = new ArrayList (); 441 File[] files = startDir.listFiles(); 442 for (int i = 0; i < files.length; i++) { 443 if (files[i].isDirectory() && !files[i].getName().equals("target")) { 444 Collection subDirResult = findFiles(files[i], name); 445 result.addAll(subDirResult); 446 } else if (files[i].getName().equals(name)) { 447 XmlFile xmlFile = new XmlFile(); 448 xmlFile.file = files[i]; 449 xmlFile.document = parseFile(xmlFile.file); 450 result.add(xmlFile); 451 } 452 } 453 return result; 454 } 455 456 public Artifact parseArtifactSpec(String artifactSpec) throws Exception { 457 int colonPos = artifactSpec.indexOf(':'); 458 if (colonPos == -1) 459 throw new Exception ("artifact specification does not contain a colon"); 460 461 Artifact artifact = new Artifact(); 462 artifact.groupId = artifactSpec.substring(0, colonPos); 463 artifact.artifactId = artifactSpec.substring(colonPos + 1); 464 return artifact; 465 } 466 467 private boolean promptYesNo(String message, boolean defaultInput) throws Exception { 468 String input = ""; 469 while (!input.equals("yes") && !input.equals("no")) { 470 input = prompt(message, defaultInput ? "yes" : "no"); 471 input = input.toLowerCase(); 472 } 473 return input.equals("yes"); 474 } 475 476 private String prompt(String message, String defaultInput) throws Exception { 477 System.err.println(message); 478 System.err.flush(); 479 BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 480 String input; 481 try { 482 input = in.readLine(); 483 } catch (IOException e) { 484 throw new Exception ("Error reading input from console.", e); 485 } 486 if (input == null || input.trim().equals("")) 487 input = defaultInput; 488 return input; 489 } 490 491 static class Artifact { 492 public String groupId; 493 public String artifactId; 494 } 495 496 static class XmlFile { 497 public File file; 498 public Document document; 499 public boolean changed = false; 500 } 501 502 505 private void createBlockDepsRepository(File sourceRepo, File destinationRepo) throws Exception { 506 findFiles(); 507 508 DOMXPath xpath = new DOMXPath("//classloader/classpath/repository/resource | //include"); 509 Iterator merlinBlockXmlsIt = merlinBlockXmls.iterator(); 510 while (merlinBlockXmlsIt.hasNext()) { 511 XmlFile xmlFile = (XmlFile)merlinBlockXmlsIt.next(); 512 List nodes = xpath.selectNodes(xmlFile.document); 513 Iterator nodesIt = nodes.iterator(); 514 while (nodesIt.hasNext()) { 515 Element element = (Element )nodesIt.next(); 516 String id = element.getAttribute("id"); 517 String version = element.getAttribute("version"); 518 Artifact artifact = parseArtifactSpec(id); 519 520 copyArtifact(sourceRepo, destinationRepo, artifact.groupId, artifact.artifactId, version); 521 } 522 } 523 } 524 525 private void copyArtifact(File sourceRepo, File destinationRepo, String groupId, String artifactId, String version) throws Exception { 526 String jarName = artifactId + "-" + version + ".jar"; 527 528 File groupDir = new File(sourceRepo, groupId); 529 File jarDir = new File(groupDir, "jars"); 530 File artifactFile = new File(jarDir, jarName); 531 532 File destinationGroupDir = new File(destinationRepo, groupId); 533 File destinationJarDir = new File(destinationGroupDir, "jars"); 534 if (!destinationJarDir.exists()) 535 destinationJarDir.mkdirs(); 536 File destinationArtifactFile = new File(destinationJarDir, jarName); 537 538 copyFile(artifactFile, destinationArtifactFile); 539 System.out.println("Copying from: " + artifactFile.getAbsolutePath()); 540 System.out.println(" to: " + destinationArtifactFile.getAbsolutePath()); 541 } 542 543 private void copyArtifactToRepository(File sourceRepo, File destinationRepo, File projectXml) throws Exception { 544 Document project = parseFile(projectXml); 545 546 DOMXPath xpath = new DOMXPath("/project/groupId"); 547 String groupId = xpath.stringValueOf(project); 548 xpath = new DOMXPath("/project/id"); 549 String artifactId = xpath.stringValueOf(project); 550 xpath = new DOMXPath("/project/currentVersion"); 551 String version = xpath.stringValueOf(project); 552 copyArtifact(sourceRepo, destinationRepo, groupId, artifactId, version); 553 } 554 555 private void copyDepsToRepository(File sourceRepo, File destinationRepo, File projectXml) throws Exception { 556 Document project = parseFile(projectXml); 557 558 DOMXPath xpath = new DOMXPath("/project/dependencies/dependency"); 559 List nodes = xpath.selectNodes(project); 560 561 DOMXPath groupXPath = new DOMXPath("groupId"); 562 DOMXPath artifactXPath = new DOMXPath("artifactId"); 563 DOMXPath versionXPath = new DOMXPath("version"); 564 565 Iterator nodesIt = nodes.iterator(); 566 while (nodesIt.hasNext()) { 567 Element element = (Element )nodesIt.next(); 568 String groupId = groupXPath.stringValueOf(element); 569 String artifactId = artifactXPath.stringValueOf(element); 570 String version = versionXPath.stringValueOf(element); 571 copyArtifact(sourceRepo, destinationRepo, groupId, artifactId, version); 572 } 573 } 574 575 private void copyFile(File source, File destination) throws Exception { 576 FileChannel srcChannel = new FileInputStream(source).getChannel(); 577 FileChannel dstChannel = new FileOutputStream(destination).getChannel(); 578 dstChannel.transferFrom(srcChannel, 0, srcChannel.size()); 579 srcChannel.close(); 580 dstChannel.close(); 581 } 582 } 583 | Popular Tags |