Skip to content

Commit

Permalink
[MASSEMBLY-988] Use newer plexus-archiver methods, code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed May 8, 2023
1 parent bbfc895 commit 93ec5a9
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 220 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package org.apache.maven.plugins.assembly.archive;

import java.io.File;
import java.util.Date;
import java.nio.file.attribute.FileTime;

import org.apache.maven.plugins.assembly.AssemblerConfigurationSource;
import org.apache.maven.plugins.assembly.InvalidAssemblerConfigurationException;
Expand Down Expand Up @@ -65,6 +65,6 @@ File createArchive(
AssemblerConfigurationSource configSource,
boolean recompressZippedFiles,
String mergeManifestMode,
Date sourceDateEpoch)
FileTime sourceDateEpoch)
throws ArchiveCreationException, AssemblyFormattingException, InvalidAssemblerConfigurationException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
import java.io.StringReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.attribute.FileTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -128,7 +128,7 @@ public File createArchive(
final AssemblerConfigurationSource configSource,
boolean recompressZippedFiles,
String mergeManifestMode,
Date outputTimestamp)
FileTime outputTimestamp)
throws ArchiveCreationException, AssemblyFormattingException, InvalidAssemblerConfigurationException {
validate(assembly);

Expand Down Expand Up @@ -271,21 +271,25 @@ protected Archiver createArchiver(
final List<ContainerDescriptorHandler> containerHandlers,
boolean recompressZippedFiles,
String mergeManifestMode,
Date outputTimestamp)
FileTime outputTimestamp)
throws NoSuchArchiverException {
Archiver archiver;
if ("txz".equals(format)
|| "tgz".equals(format)
|| "tbz2".equals(format)
|| "tzst".equals(format)
|| format.startsWith("tar")) {
archiver = createTarArchiver(format, TarLongFileMode.valueOf(configSource.getTarLongFileMode()));
} else if ("war".equals(format)) {
archiver = createWarArchiver();

// one missing alias in plexus-archiver
if ("tzst".equals(format)) {
archiver = createTarZstArchiver();
} else {
archiver = archiverManager.getArchiver(format);
}

if (archiver instanceof TarArchiver) {
((TarArchiver) archiver).setLongfile(TarLongFileMode.valueOf(configSource.getTarLongFileMode()));
}

if (archiver instanceof WarArchiver) {
((WarArchiver) archiver).setExpectWebXml(false);
}

if (archiver instanceof AbstractZipArchiver) {
((AbstractZipArchiver) archiver).setRecompressAddedZips(recompressZippedFiles);
}
Expand Down Expand Up @@ -323,13 +327,12 @@ protected Archiver createArchiver(
archiver = new DryRunArchiver(archiver, LOGGER);
}

archiver.setUseJvmChmod(configSource.isUpdateOnly());
archiver.setIgnorePermissions(configSource.isIgnorePermissions());
archiver.setForced(!configSource.isUpdateOnly());

// configure for Reproducible Builds based on outputTimestamp value
if (outputTimestamp != null) {
archiver.configureReproducible(outputTimestamp);
archiver.configureReproducibleBuild(outputTimestamp);
}

if (configSource.getOverrideUid() != null) {
Expand Down Expand Up @@ -460,49 +463,9 @@ private Object[] getContainerRealm() {
}
}

protected Archiver createWarArchiver() throws NoSuchArchiverException {
final WarArchiver warArchiver = (WarArchiver) archiverManager.getArchiver("war");
warArchiver.setIgnoreWebxml(false); // See MNG-1274

return warArchiver;
}

protected Archiver createTarArchiver(final String format, final TarLongFileMode tarLongFileMode)
throws NoSuchArchiverException {
protected Archiver createTarZstArchiver() throws NoSuchArchiverException {
final TarArchiver tarArchiver = (TarArchiver) archiverManager.getArchiver("tar");
final int index = format.indexOf('.');
if (index >= 0) {
TarArchiver.TarCompressionMethod tarCompressionMethod;
// TODO: this should accept gz and bz2 as well so we can skip
// TODO: over the switch
final String compression = format.substring(index + 1);
if ("gz".equals(compression)) {
tarCompressionMethod = TarArchiver.TarCompressionMethod.gzip;
} else if ("bz2".equals(compression)) {
tarCompressionMethod = TarArchiver.TarCompressionMethod.bzip2;
} else if ("xz".equals(compression)) {
tarCompressionMethod = TarArchiver.TarCompressionMethod.xz;
} else if ("snappy".equals(compression)) {
tarCompressionMethod = TarArchiver.TarCompressionMethod.snappy;
} else if ("zst".equals(compression)) {
tarCompressionMethod = TarArchiver.TarCompressionMethod.zstd;
} else {
// TODO: better handling
throw new IllegalArgumentException("Unknown compression format: " + compression);
}
tarArchiver.setCompression(tarCompressionMethod);
} else if ("tgz".equals(format)) {
tarArchiver.setCompression(TarArchiver.TarCompressionMethod.gzip);
} else if ("tbz2".equals(format)) {
tarArchiver.setCompression(TarArchiver.TarCompressionMethod.bzip2);
} else if ("txz".equals(format)) {
tarArchiver.setCompression(TarArchiver.TarCompressionMethod.xz);
} else if ("tzst".equals(format)) {
tarArchiver.setCompression(TarArchiver.TarCompressionMethod.zstd);
}

tarArchiver.setLongfile(tarLongFileMode);

tarArchiver.setCompression(TarArchiver.TarCompressionMethod.zstd);
return tarArchiver;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
package org.apache.maven.plugins.assembly.mojos;

import java.io.File;
import java.nio.file.attribute.FileTime;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Properties;

Expand Down Expand Up @@ -475,19 +475,20 @@ public void execute() throws MojoExecutionException, MojoFailureException {
// TODO: include dependencies marked for distribution under certain formats
// TODO: how, might we plug this into an installer, such as NSIS?

MavenArchiver mavenArchiver = new MavenArchiver();
Date outputDate = mavenArchiver.parseOutputTimestamp(outputTimestamp);
FileTime outputDate = MavenArchiver.parseBuildOutputTimestamp(outputTimestamp)
.map(FileTime::from)
.orElse(null);

boolean warnedAboutMainProjectArtifact = false;
for (final Assembly assembly : assemblies) {
try {
final String fullName = AssemblyFormatUtils.getDistributionName(assembly, this);

List<String> effectiveFormats = formats;
if (effectiveFormats == null || effectiveFormats.size() == 0) {
if (effectiveFormats == null || effectiveFormats.isEmpty()) {
effectiveFormats = assembly.getFormats();
}
if (effectiveFormats == null || effectiveFormats.size() == 0) {
if (effectiveFormats == null || effectiveFormats.isEmpty()) {
throw new MojoFailureException(
"No formats specified in the execution parameters or the assembly descriptor.");
}
Expand Down
Loading

0 comments on commit 93ec5a9

Please sign in to comment.