|
| 1 | +/* |
| 2 | + * NodeJS Maven Plugin |
| 3 | + * Copyright (C) 2025-2025 SonarSource SA |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | + * See the Sonar Source-Available License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the Sonar Source-Available License |
| 15 | + * along with this program; if not, see https://sonarsource.com/license/ssal/ |
| 16 | + */ |
| 17 | +import java.io.FileNotFoundException; |
| 18 | +import java.io.IOException; |
| 19 | +import java.nio.file.Files; |
| 20 | +import java.nio.file.Path; |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.List; |
| 23 | +import org.apache.maven.plugin.AbstractMojo; |
| 24 | +import org.apache.maven.plugins.annotations.Mojo; |
| 25 | +import org.apache.maven.plugins.annotations.Parameter; |
| 26 | +import org.tukaani.xz.LZMA2Options; |
| 27 | +import org.tukaani.xz.XZOutputStream; |
| 28 | + |
| 29 | +@Mojo(name = "compress") |
| 30 | +public class CompressMojo extends AbstractMojo { |
| 31 | + |
| 32 | + @Parameter(required = true) |
| 33 | + private String baseDirectory; |
| 34 | + |
| 35 | + @Parameter(required = true) |
| 36 | + private List<String> filenames; |
| 37 | + |
| 38 | + @Parameter(required = true) |
| 39 | + private String targetDirectory; |
| 40 | + |
| 41 | + @Parameter(defaultValue = "9") |
| 42 | + private int compressionLevel; |
| 43 | + |
| 44 | + @Override |
| 45 | + public void execute() { |
| 46 | + try { |
| 47 | + this.compress( |
| 48 | + this.filenames.stream() |
| 49 | + .map(filename -> Path.of(this.baseDirectory, filename).toAbsolutePath()) |
| 50 | + .toList() |
| 51 | + ); |
| 52 | + } catch (IOException e) { |
| 53 | + throw new IllegalStateException( |
| 54 | + "Error while compressing " + Arrays.toString(filenames.toArray()), |
| 55 | + e |
| 56 | + ); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + protected void compress(List<Path> filenames) throws IOException { |
| 61 | + for (var file : filenames) { |
| 62 | + var outputFile = Path.of( |
| 63 | + this.targetDirectory, |
| 64 | + Path.of(this.baseDirectory).toAbsolutePath().relativize(file) + ".xz" |
| 65 | + ); |
| 66 | + |
| 67 | + this.getLog().info("Compressing " + file + " to " + outputFile); |
| 68 | + |
| 69 | + if (!Files.exists(file)) { |
| 70 | + throw new FileNotFoundException(String.format("File %s does not exist.", file)); |
| 71 | + } |
| 72 | + |
| 73 | + if (Files.exists(outputFile)) { |
| 74 | + this.getLog() |
| 75 | + .info(String.format("Skipping compression, file %s already exists.", outputFile)); |
| 76 | + continue; |
| 77 | + } |
| 78 | + |
| 79 | + outputFile.toFile().getParentFile().mkdirs(); |
| 80 | + |
| 81 | + try ( |
| 82 | + var is = Files.newInputStream(file); |
| 83 | + var outfile = Files.newOutputStream(outputFile); |
| 84 | + var outxz = new XZOutputStream(outfile, new LZMA2Options(this.compressionLevel)) |
| 85 | + ) { |
| 86 | + is.transferTo(outxz); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments