-
Notifications
You must be signed in to change notification settings - Fork 1
/
PomFileIO.java
55 lines (48 loc) · 1.9 KB
/
PomFileIO.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.exasol.projectkeeper.validators.pom;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import com.exasol.errorreporting.ExaError;
/**
* This class reads and writes POM files using the Maven POM {@link Model} class.
* <p>
* Use {@link com.exasol.projectkeeper.validators.pom.io.PomFileReader} or
* {@link com.exasol.projectkeeper.validators.pom.io.PomFileWriter} if you want to read/write POM files as XML.
*/
public class PomFileIO {
private static final MavenXpp3Writer WRITER = new MavenXpp3Writer();
private static final MavenXpp3Reader READER = new MavenXpp3Reader();
/**
* Read the given file and parse it as a POM file.
*
* @param path path to read
* @return parsed POM model
*/
public Model readPom(final Path path) {
try {
return READER.read(Files.newBufferedReader(path));
} catch (IOException | XmlPullParserException exception) {
throw new IllegalStateException(ExaError.messageBuilder("E-PK-CORE-172")
.message("Failed to read pom {{pom file path}}", path).toString(), exception);
}
}
/**
* Write a POM model to a file
*
* @param model the POM model to write
* @param path the path to write
*/
public void writePom(final Model model, final Path path) {
try {
WRITER.write(Files.newOutputStream(path), model);
} catch (final IOException exception) {
throw new UncheckedIOException(ExaError.messageBuilder("E-PK-CORE-173")
.message("Failed to write pom {{pom file path}}", path).toString(), exception);
}
}
}