use of org.apache.maven.model.converter.PomTranslationException in project archiva by apache.
the class LegacyToDefaultConverter method copyPom.
@SuppressWarnings("unchecked")
private boolean copyPom(Artifact artifact, ArtifactRepository targetRepository, FileTransaction transaction) throws ArtifactConversionException {
Artifact pom = artifactFactory.createProjectArtifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
pom.setBaseVersion(artifact.getBaseVersion());
ArtifactRepository repository = artifact.getRepository();
Path file = Paths.get(repository.getBasedir(), repository.pathOf(pom));
boolean result = true;
if (Files.exists(file)) {
Path targetFile = Paths.get(targetRepository.getBasedir(), targetRepository.pathOf(pom));
String contents = null;
boolean checksumsValid = false;
try {
if (testChecksums(artifact, file)) {
checksumsValid = true;
}
// Even if the checksums for the POM are invalid we should still convert the POM
contents = org.apache.archiva.common.utils.FileUtils.readFileToString(file, Charset.defaultCharset());
} catch (IOException e) {
throw new ArtifactConversionException(Messages.getString("unable.to.read.source.pom", e.getMessage()), // $NON-NLS-1$
e);
}
if (// $NON-NLS-1$
checksumsValid && contents.indexOf("modelVersion") >= 0) {
// v4 POM
boolean matching = false;
if (!force && Files.exists(targetFile)) {
String targetContents = org.apache.archiva.common.utils.FileUtils.readFileToString(targetFile, Charset.defaultCharset());
matching = targetContents.equals(contents);
}
if (force || !matching) {
transaction.createFile(contents, targetFile, digesters);
}
} else {
// v3 POM
try (StringReader stringReader = new StringReader(contents)) {
try (StringWriter writer = new StringWriter()) {
org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader v3Reader = new org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader();
org.apache.maven.model.v3_0_0.Model v3Model = v3Reader.read(stringReader);
if (doRelocation(artifact, v3Model, targetRepository, transaction)) {
Artifact relocatedPom = artifactFactory.createProjectArtifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
targetFile = Paths.get(targetRepository.getBasedir(), targetRepository.pathOf(relocatedPom));
}
Model v4Model = translator.translate(v3Model);
translator.validateV4Basics(v4Model, v3Model.getGroupId(), v3Model.getArtifactId(), v3Model.getVersion(), v3Model.getPackage());
MavenXpp3Writer xpp3Writer = new MavenXpp3Writer();
xpp3Writer.write(writer, v4Model);
transaction.createFile(writer.toString(), targetFile, digesters);
List<String> warnings = translator.getWarnings();
for (String message : warnings) {
addWarning(artifact, message);
}
} catch (XmlPullParserException e) {
addWarning(artifact, // $NON-NLS-1$
Messages.getString("invalid.source.pom", e.getMessage()));
result = false;
} catch (IOException e) {
throw new ArtifactConversionException(Messages.getString("unable.to.write.converted.pom"), // $NON-NLS-1$
e);
} catch (PomTranslationException e) {
addWarning(artifact, // $NON-NLS-1$
Messages.getString("invalid.source.pom", e.getMessage()));
result = false;
}
}
}
} else {
// $NON-NLS-1$
addWarning(artifact, Messages.getString("warning.missing.pom"));
}
return result;
}
Aggregations