|  | 
| 25 | 25 | 
 | 
| 26 | 26 | import java.io.File; | 
| 27 | 27 | import java.io.IOException; | 
| 28 |  | -import java.io.InputStream; | 
| 29 |  | -import java.io.OutputStream; | 
| 30 | 28 | import java.io.StringWriter; | 
| 31 | 29 | import java.nio.file.Files; | 
|  | 30 | +import java.nio.file.Path; | 
|  | 31 | +import java.nio.file.StandardOpenOption; | 
| 32 | 32 | import java.util.ArrayList; | 
| 33 | 33 | import java.util.Arrays; | 
| 34 | 34 | import java.util.Collection; | 
| @@ -438,7 +438,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { | 
| 438 | 438 |         inheritanceAssembler.flattenDependencyMode = this.flattenDependencyMode; | 
| 439 | 439 | 
 | 
| 440 | 440 |         File originalPomFile = this.project.getFile(); | 
| 441 |  | -        File flattenedPomFile = getFlattenedPomFile(); | 
|  | 441 | +        Path flattenedPomFile = getFlattenedPomFile(); | 
| 442 | 442 |         Model flattenedPom; | 
| 443 | 443 |         /* | 
| 444 | 444 |          * Non-destructive CI-friendly version flattening? | 
| @@ -485,7 +485,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { | 
| 485 | 485 |             writePom(flattenedPom, flattenedPomFile, headerComment, commentsOfOriginalPomFile); | 
| 486 | 486 |         } | 
| 487 | 487 |         if (isUpdatePomFile()) { | 
| 488 |  | -            this.project.setPomFile(flattenedPomFile); | 
|  | 488 | +            this.project.setPomFile(flattenedPomFile.toFile()); | 
| 489 | 489 |             this.project.setOriginalModel(flattenedPom); | 
| 490 | 490 |         } | 
| 491 | 491 |     } | 
| @@ -520,23 +520,16 @@ protected String extractHeaderComment(File xmlFile) throws MojoExecutionExceptio | 
| 520 | 520 |      * Writes the given POM {@link Model} to the given {@link File}. | 
| 521 | 521 |      * | 
| 522 | 522 |      * @param pom           the {@link Model} of the POM to write. | 
| 523 |  | -     * @param pomFile       the {@link File} where to write the given POM will be written to. | 
|  | 523 | +     * @param pomFile       the {@link Path} where to write the given POM will be written to. | 
| 524 | 524 |      *                      {@link File#getParentFile() | 
| 525 | 525 |      *                      Parent directories} are {@link File#mkdirs() created} automatically. | 
| 526 | 526 |      * @param headerComment is the content of a potential XML comment at the top of the XML (after XML declaration and | 
| 527 | 527 |      *                      before root tag). May be <code>null</code> if not present and to be omitted in target POM. | 
| 528 | 528 |      * @throws MojoExecutionException if the operation failed (e.g. due to an {@link IOException}). | 
| 529 | 529 |      */ | 
| 530 |  | -    protected void writePom(Model pom, File pomFile, String headerComment, KeepCommentsInPom anOriginalCommentsPath) | 
|  | 530 | +    protected void writePom(Model pom, Path pomFile, String headerComment, KeepCommentsInPom anOriginalCommentsPath) | 
| 531 | 531 |             throws MojoExecutionException { | 
| 532 | 532 | 
 | 
| 533 |  | -        File parentFile = pomFile.getParentFile(); | 
| 534 |  | -        if (!parentFile.exists()) { | 
| 535 |  | -            boolean success = parentFile.mkdirs(); | 
| 536 |  | -            if (!success) { | 
| 537 |  | -                throw new MojoExecutionException("Failed to create directory " + pomFile.getParent()); | 
| 538 |  | -            } | 
| 539 |  | -        } | 
| 540 | 533 |         // MavenXpp3Writer could internally add the comment but does not expose such feature to API! | 
| 541 | 534 |         // Instead we have to write POM XML to String and do post processing on that :( | 
| 542 | 535 |         MavenXpp3Writer pomWriter = new MavenXpp3Writer(); | 
| @@ -568,39 +561,41 @@ protected void writePom(Model pom, File pomFile, String headerComment, KeepComme | 
| 568 | 561 |      * Writes the given <code>data</code> to the given <code>file</code> using the specified <code>encoding</code>. | 
| 569 | 562 |      * | 
| 570 | 563 |      * @param data     is the {@link String} to write. | 
| 571 |  | -     * @param file     is the {@link File} to write to. | 
|  | 564 | +     * @param file     is the {@link Path} to write to. | 
| 572 | 565 |      * @param encoding is the encoding to use for writing the file. | 
| 573 | 566 |      * @throws MojoExecutionException if anything goes wrong. | 
| 574 | 567 |      */ | 
| 575 |  | -    protected void writeStringToFile(String data, File file, String encoding) throws MojoExecutionException { | 
|  | 568 | +    protected void writeStringToFile(String data, Path file, String encoding) throws MojoExecutionException { | 
| 576 | 569 |         data = NEW_LINE_PATTERN.matcher(data).replaceAll(System.lineSeparator()); | 
| 577 | 570 | 
 | 
| 578 |  | -        byte[] binaryData; | 
| 579 |  | - | 
| 580 | 571 |         try { | 
| 581 |  | -            binaryData = data.getBytes(encoding); | 
| 582 |  | -            if (file.isFile() && file.canRead() && file.length() == binaryData.length) { | 
| 583 |  | -                try (InputStream inputStream = Files.newInputStream(file.toPath())) { | 
| 584 |  | -                    byte[] buffer = new byte[binaryData.length]; | 
| 585 |  | -                    inputStream.read(buffer); | 
|  | 572 | +            byte[] binaryData = data.getBytes(encoding); | 
|  | 573 | +            if (Files.isReadable(file) && Files.size(file) == binaryData.length) { | 
|  | 574 | +                try { | 
|  | 575 | +                    byte[] buffer = Files.readAllBytes(file); | 
| 586 | 576 |                     if (Arrays.equals(buffer, binaryData)) { | 
| 587 | 577 |                         getLog().debug("Arrays.equals( buffer, binaryData ) "); | 
| 588 | 578 |                         return; | 
| 589 | 579 |                     } | 
| 590 | 580 |                     getLog().debug("Not Arrays.equals( buffer, binaryData ) "); | 
| 591 | 581 |                 } catch (IOException e) { | 
| 592 | 582 |                     // ignore those exceptions, we will overwrite the file | 
| 593 |  | -                    getLog().debug("Issue reading file: " + file.getPath(), e); | 
|  | 583 | +                    getLog().debug("Issue reading file: " + file, e); | 
| 594 | 584 |                 } | 
| 595 | 585 |             } else { | 
| 596 |  | -                getLog().debug("file: " + file + ",file.length(): " + file.length() + ", binaryData.length: " | 
|  | 586 | +                getLog().debug("file: " + file + ", not exist or has the same length as binaryData.length: " | 
| 597 | 587 |                         + binaryData.length); | 
| 598 | 588 |             } | 
| 599 |  | -        } catch (IOException e) { | 
| 600 |  | -            throw new MojoExecutionException("cannot read String as bytes", e); | 
| 601 |  | -        } | 
| 602 |  | -        try (OutputStream outStream = Files.newOutputStream(file.toPath())) { | 
| 603 |  | -            outStream.write(binaryData); | 
|  | 589 | +            Path parent = file.getParent(); | 
|  | 590 | +            if (parent != null) { | 
|  | 591 | +                Files.createDirectories(parent); | 
|  | 592 | +            } | 
|  | 593 | +            Files.write( | 
|  | 594 | +                    file, | 
|  | 595 | +                    binaryData, | 
|  | 596 | +                    StandardOpenOption.CREATE, | 
|  | 597 | +                    StandardOpenOption.TRUNCATE_EXISTING, | 
|  | 598 | +                    StandardOpenOption.WRITE); | 
| 604 | 599 |         } catch (IOException e) { | 
| 605 | 600 |             throw new MojoExecutionException("Failed to write to " + file, e); | 
| 606 | 601 |         } | 
| @@ -688,10 +683,10 @@ private Model createExtendedInterpolatedPom( | 
| 688 | 683 |         return extendedInterpolatedPom; | 
| 689 | 684 |     } | 
| 690 | 685 | 
 | 
| 691 |  | -    private Model createOriginalPom(File pomFile) throws MojoExecutionException { | 
|  | 686 | +    private Model createOriginalPom(Path pomFile) throws MojoExecutionException { | 
| 692 | 687 |         MavenXpp3Reader reader = new MavenXpp3Reader(); | 
| 693 | 688 |         try { | 
| 694 |  | -            return reader.read(Files.newInputStream(pomFile.toPath())); | 
|  | 689 | +            return reader.read(Files.newInputStream(pomFile)); | 
| 695 | 690 |         } catch (IOException | XmlPullParserException e) { | 
| 696 | 691 |             throw new MojoExecutionException("Error reading raw model.", e); | 
| 697 | 692 |         } | 
| @@ -1345,7 +1340,7 @@ public Model getEffectivePom() throws MojoExecutionException { | 
| 1345 | 1340 | 
 | 
| 1346 | 1341 |         public Model getOriginalPom() throws MojoExecutionException { | 
| 1347 | 1342 |             if (this.originalPom == null) { | 
| 1348 |  | -                this.originalPom = createOriginalPom(this.pomFile); | 
|  | 1343 | +                this.originalPom = createOriginalPom(this.pomFile.toPath()); | 
| 1349 | 1344 |             } | 
| 1350 | 1345 |             return this.originalPom; | 
| 1351 | 1346 |         } | 
|  | 
0 commit comments