Skip to content

Commit e02117c

Browse files
Create missing target directories, use NIO for file block operations
Fix #461
1 parent f9289bf commit e02117c

File tree

6 files changed

+43
-43
lines changed

6 files changed

+43
-43
lines changed

src/it/projects/resolve-properties-ci-do-not-interpolate-profile-activation-file/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<version>@project.version@</version>
1616
<configuration>
1717
<flattenMode>resolveCiFriendliesOnly</flattenMode>
18+
<outputDirectory>${project.build.directory}</outputDirectory>
1819
</configuration>
1920
</plugin>
2021
</plugins>

src/it/projects/resolve-properties-ci-do-not-interpolate-profile-activation-file/verify.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ assert '${basedir}/file.txt' == originalProject.profiles.profile[1].activation.f
2727
assert '${any.property}' == originalProject.profiles.profile[2].activation.file.exists.text()
2828
assert '${revision}' == originalProject.profiles.profile[3].activation.file.exists.text()
2929

30-
File flattenedPom = new File( basedir, '.flattened-pom.xml' )
30+
File flattenedPom = new File( basedir, 'target/.flattened-pom.xml' )
3131
assert flattenedPom.exists()
3232
def flattenedProject = new XmlSlurper().parse( flattenedPom )
3333

src/main/java/org/codehaus/mojo/flatten/AbstractFlattenMojo.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*/
2121

2222
import java.io.File;
23+
import java.nio.file.Path;
2324

2425
import org.apache.maven.plugin.AbstractMojo;
2526
import org.apache.maven.plugins.annotations.Parameter;
@@ -76,8 +77,8 @@ public File getOutputDirectory() {
7677
/**
7778
* @return a {@link File} instance pointing to the flattened POM.
7879
*/
79-
protected File getFlattenedPomFile() {
80-
return new File(getOutputDirectory(), getFlattenedPomFilename());
80+
protected Path getFlattenedPomFile() {
81+
return getOutputDirectory().toPath().resolve(getFlattenedPomFilename());
8182
}
8283

8384
protected boolean shouldSkip() {

src/main/java/org/codehaus/mojo/flatten/CleanMojo.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
* under the License.
2020
*/
2121

22-
import java.io.File;
22+
import java.io.IOException;
23+
import java.nio.file.Files;
24+
import java.nio.file.Path;
2325

2426
import org.apache.maven.plugin.MojoExecutionException;
2527
import org.apache.maven.plugin.MojoFailureException;
@@ -68,12 +70,13 @@ public void execute() throws MojoExecutionException, MojoFailureException {
6870
return;
6971
}
7072

71-
File flattenedPomFile = getFlattenedPomFile();
72-
if (flattenedPomFile.isFile()) {
73-
getLog().info("Deleting " + flattenedPomFile.getPath());
74-
boolean deleted = flattenedPomFile.delete();
75-
if (!deleted) {
76-
throw new MojoFailureException("Could not delete " + flattenedPomFile.getAbsolutePath());
73+
Path flattenedPomFile = getFlattenedPomFile();
74+
if (Files.isRegularFile(flattenedPomFile)) {
75+
getLog().info("Deleting " + flattenedPomFile);
76+
try {
77+
Files.delete(flattenedPomFile);
78+
} catch (IOException e) {
79+
throw new MojoFailureException("Could not delete " + flattenedPomFile, e);
7780
}
7881
}
7982
}

src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525

2626
import java.io.File;
2727
import java.io.IOException;
28-
import java.io.InputStream;
29-
import java.io.OutputStream;
3028
import java.io.StringWriter;
3129
import java.nio.file.Files;
30+
import java.nio.file.Path;
31+
import java.nio.file.StandardOpenOption;
3232
import java.util.ArrayList;
3333
import java.util.Arrays;
3434
import java.util.Collection;
@@ -438,7 +438,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
438438
inheritanceAssembler.flattenDependencyMode = this.flattenDependencyMode;
439439

440440
File originalPomFile = this.project.getFile();
441-
File flattenedPomFile = getFlattenedPomFile();
441+
Path flattenedPomFile = getFlattenedPomFile();
442442
Model flattenedPom;
443443
/*
444444
* Non-destructive CI-friendly version flattening?
@@ -485,7 +485,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
485485
writePom(flattenedPom, flattenedPomFile, headerComment, commentsOfOriginalPomFile);
486486
}
487487
if (isUpdatePomFile()) {
488-
this.project.setPomFile(flattenedPomFile);
488+
this.project.setPomFile(flattenedPomFile.toFile());
489489
this.project.setOriginalModel(flattenedPom);
490490
}
491491
}
@@ -520,23 +520,16 @@ protected String extractHeaderComment(File xmlFile) throws MojoExecutionExceptio
520520
* Writes the given POM {@link Model} to the given {@link File}.
521521
*
522522
* @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.
524524
* {@link File#getParentFile()
525525
* Parent directories} are {@link File#mkdirs() created} automatically.
526526
* @param headerComment is the content of a potential XML comment at the top of the XML (after XML declaration and
527527
* before root tag). May be <code>null</code> if not present and to be omitted in target POM.
528528
* @throws MojoExecutionException if the operation failed (e.g. due to an {@link IOException}).
529529
*/
530-
protected void writePom(Model pom, File pomFile, String headerComment, KeepCommentsInPom anOriginalCommentsPath)
530+
protected void writePom(Model pom, Path pomFile, String headerComment, KeepCommentsInPom anOriginalCommentsPath)
531531
throws MojoExecutionException {
532532

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-
}
540533
// MavenXpp3Writer could internally add the comment but does not expose such feature to API!
541534
// Instead we have to write POM XML to String and do post processing on that :(
542535
MavenXpp3Writer pomWriter = new MavenXpp3Writer();
@@ -568,39 +561,41 @@ protected void writePom(Model pom, File pomFile, String headerComment, KeepComme
568561
* Writes the given <code>data</code> to the given <code>file</code> using the specified <code>encoding</code>.
569562
*
570563
* @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.
572565
* @param encoding is the encoding to use for writing the file.
573566
* @throws MojoExecutionException if anything goes wrong.
574567
*/
575-
protected void writeStringToFile(String data, File file, String encoding) throws MojoExecutionException {
568+
protected void writeStringToFile(String data, Path file, String encoding) throws MojoExecutionException {
576569
data = NEW_LINE_PATTERN.matcher(data).replaceAll(System.lineSeparator());
577570

578-
byte[] binaryData;
579-
580571
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);
586576
if (Arrays.equals(buffer, binaryData)) {
587577
getLog().debug("Arrays.equals( buffer, binaryData ) ");
588578
return;
589579
}
590580
getLog().debug("Not Arrays.equals( buffer, binaryData ) ");
591581
} catch (IOException e) {
592582
// 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);
594584
}
595585
} 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: "
597587
+ binaryData.length);
598588
}
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);
604599
} catch (IOException e) {
605600
throw new MojoExecutionException("Failed to write to " + file, e);
606601
}
@@ -688,10 +683,10 @@ private Model createExtendedInterpolatedPom(
688683
return extendedInterpolatedPom;
689684
}
690685

691-
private Model createOriginalPom(File pomFile) throws MojoExecutionException {
686+
private Model createOriginalPom(Path pomFile) throws MojoExecutionException {
692687
MavenXpp3Reader reader = new MavenXpp3Reader();
693688
try {
694-
return reader.read(Files.newInputStream(pomFile.toPath()));
689+
return reader.read(Files.newInputStream(pomFile));
695690
} catch (IOException | XmlPullParserException e) {
696691
throw new MojoExecutionException("Error reading raw model.", e);
697692
}
@@ -1345,7 +1340,7 @@ public Model getEffectivePom() throws MojoExecutionException {
13451340

13461341
public Model getOriginalPom() throws MojoExecutionException {
13471342
if (this.originalPom == null) {
1348-
this.originalPom = createOriginalPom(this.pomFile);
1343+
this.originalPom = createOriginalPom(this.pomFile.toPath());
13491344
}
13501345
return this.originalPom;
13511346
}

src/test/java/org/codehaus/mojo/flatten/KeepCommentsInPomTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public void allNewLineSeparatorShouldBeNormalized() throws Exception {
9090
FlattenMojo flattenMojo = new FlattenMojo();
9191

9292
File tempFile = temporaryFolder.newFile();
93-
flattenMojo.writeStringToFile("line 1\n" + "line 2\r\n" + "line 3\r", tempFile, "UTF-8");
93+
flattenMojo.writeStringToFile("line 1\n" + "line 2\r\n" + "line 3\r", tempFile.toPath(), "UTF-8");
9494

9595
String lf = System.lineSeparator();
9696

0 commit comments

Comments
 (0)