in src/main/java/org/apache/maven/buildcache/DefaultRestoredArtifactHandler.java [61:138]
public Path adjustArchiveArtifactVersion(MavenProject project, String originalArtifactVersion, Path artifactFile)
throws IOException {
if (!adjustMetaInfVersion) {
// option is disabled in cache configuration, return file as is
return artifactFile;
}
File file = artifactFile.toFile();
if (project.getVersion().equals(originalArtifactVersion) || !CacheUtils.isArchive(file)) {
// versions of artifact and building project are the same or this is not an archive, return file as is
return artifactFile;
}
File tempDirName = Paths.get(project.getBuild().getDirectory())
.normalize()
.resolve(DIR_NAME)
.toFile();
if (tempDirName.mkdirs()) {
LOGGER.debug(
"Temporary directory to restore artifact was created [artifactFile={}, "
+ "originalVersion={}, tempDir={}]",
artifactFile,
originalArtifactVersion,
tempDirName);
}
String currentVersion = project.getVersion();
File tmpJarFile = File.createTempFile(
artifactFile.toFile().getName(), '.' + FilenameUtils.getExtension(file.getName()), tempDirName);
tmpJarFile.deleteOnExit();
String originalImplVersion = Attributes.Name.IMPLEMENTATION_VERSION + ": " + originalArtifactVersion;
String implVersion = Attributes.Name.IMPLEMENTATION_VERSION + ": " + currentVersion;
String commonXmlOriginalVersion = "<version>" + originalArtifactVersion + "</version>";
String commonXmlVersion = "<version>" + currentVersion + "</version>";
String originalPomPropsVersion = "version=" + originalArtifactVersion;
String pomPropsVersion = "version=" + currentVersion;
try (JarFile jarFile = new JarFile(artifactFile.toFile())) {
try (JarOutputStream jos =
new JarOutputStream(new BufferedOutputStream(Files.newOutputStream(tmpJarFile.toPath())))) {
// Copy original jar file to the temporary one.
Enumeration<JarEntry> jarEntries = jarFile.entries();
byte[] buffer = new byte[1024];
while (jarEntries.hasMoreElements()) {
JarEntry entry = jarEntries.nextElement();
String entryName = entry.getName();
if (entryName.startsWith("META-INF/maven")
&& (entryName.endsWith("plugin.xml") || entryName.endsWith("plugin-help.xml"))) {
replaceEntry(jarFile, entry, commonXmlOriginalVersion, commonXmlVersion, jos);
continue;
}
if (entryName.endsWith("pom.xml")) {
replaceEntry(jarFile, entry, commonXmlOriginalVersion, commonXmlVersion, jos);
continue;
}
if (entryName.endsWith("pom.properties")) {
replaceEntry(jarFile, entry, originalPomPropsVersion, pomPropsVersion, jos);
continue;
}
if (JarFile.MANIFEST_NAME.equals(entryName)) {
replaceEntry(jarFile, entry, originalImplVersion, implVersion, jos);
continue;
}
jos.putNextEntry(entry);
try (InputStream entryInputStream = jarFile.getInputStream(entry)) {
int bytesRead;
while ((bytesRead = entryInputStream.read(buffer)) != -1) {
jos.write(buffer, 0, bytesRead);
}
}
}
}
}
return tmpJarFile.toPath();
}