Skip to content

Commit

Permalink
Add tgz option to iTools packager Maven plugin (#3170)
Browse files Browse the repository at this point in the history
* Add tgz option to packager
* Tgz packaging handles large ids correctly

Signed-off-by: Guillaume Verger <[email protected]>

Co-authored-by: jeandemanged <[email protected]>

Co-authored-by: Olivier Perrin <[email protected]>
  • Loading branch information
gverger and olperr1 authored Nov 13, 2024
1 parent b49d67d commit 6dd9ad6
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 3 deletions.
8 changes: 8 additions & 0 deletions distribution-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,14 @@
<artifactId>powsybl-ucte-util</artifactId>
<version>${project.version}</version>
</dependency>

<!-- In provided scope, so that coverage is correctly reported but the jar is not packaged for distribution -->
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>powsybl-itools-packager-maven-plugin</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
Expand Down
2 changes: 2 additions & 0 deletions itools-packager/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Here is how to configure itools package Maven plugin in your project
<configuration>
<packageName>powsybl</packageName>
<archiveName>powsybl-x-y-x</archiveName>
<packageType>zip</packageType>
<javaXmx>8G</javaXmx>
<mpiTasks>2</mpiTasks>
<mpiHosts>
Expand Down Expand Up @@ -68,6 +69,7 @@ Here is how to configure itools package Maven plugin in your project

- packageName is optional, project final name is used as default value.
- archiveName is optional, packageName is used as default value.
- packageType is optional, "zip" is the default value. It can be either zip or tgz.
- javaXmx, mpiTasks and mpiHosts are used to generate itools.conf, are all optional and default values are respectively 8G, 2 and localhost.
- additional binaries, libraries and configurations file can be added to the package using optional copyToBin, copyToLib and copyToEtc tags.
- all of the jars with compile and runtime scope will be included in the package
28 changes: 28 additions & 0 deletions itools-packager/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,34 @@
<version>${maven.plugin.annotations.version}</version>
<scope>provided</scope>
</dependency>
<!-- Test dependencies -->
<dependency>
<groupId>org.apache.maven.plugin-testing</groupId>
<artifactId>maven-plugin-testing-harness</artifactId>
<version>${maven.plugin.testing.harness.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-compat</artifactId>
<version>${maven.compat}</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
*/
package com.powsybl.itools;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugins.annotations.Mojo;
Expand Down Expand Up @@ -64,6 +67,9 @@ public void setFiles(File[] files) {
}
}

@Parameter(defaultValue = "zip")
private String packageType;

@Parameter
private CopyTo copyToBin;

Expand All @@ -73,7 +79,8 @@ public void setFiles(File[] files) {
@Parameter
private CopyTo copyToEtc;

private static void zip(Path dir, Path baseDir, Path zipFilePath) throws IOException {
private void zip(Path dir, Path baseDir, Path zipFilePath) throws IOException {
getLog().info("Zip package");
try (ZipArchiveOutputStream zos = new ZipArchiveOutputStream(Files.newOutputStream(zipFilePath))) {
Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
@Override
Expand All @@ -100,6 +107,38 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) th
}
}

private void targz(Path dir, Path baseDir, Path zipFilePath) throws IOException {
getLog().info("Targz package");
try (TarArchiveOutputStream zos =
new TarArchiveOutputStream(new GzipCompressorOutputStream(Files.newOutputStream(zipFilePath)))) {
zos.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX); // allow long file paths
zos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX); // allow large numbers (for instance a big GID)
Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
TarArchiveEntry entry = new TarArchiveEntry(file.toFile(), baseDir.relativize(file).toString());
if (Files.isExecutable(file)) {
entry.setMode(0100770);
} else {
entry.setMode(0100660);
}
zos.putArchiveEntry(entry);
Files.copy(file, zos);
zos.closeArchiveEntry();
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
zos.putArchiveEntry(new TarArchiveEntry(baseDir.relativize(dir).toString() + "/"));
zos.closeArchiveEntry();
return FileVisitResult.CONTINUE;
}
});
}

}

private void copyFiles(CopyTo copyTo, Path destDir) {
if (copyTo != null) {
for (File file : copyTo.getFiles()) {
Expand Down Expand Up @@ -179,9 +218,14 @@ public void execute() {
Files.createDirectories(libDir);
copyFiles(copyToLib, libDir);

getLog().info("Zip package");
String archiveNameNotNull = archiveName != null ? archiveName : packageNameNotNull;
zip(packageDir, targetDir, targetDir.resolve(archiveNameNotNull + ".zip"));
if (packageType.equalsIgnoreCase("zip")) {
zip(packageDir, targetDir, targetDir.resolve(archiveNameNotNull + ".zip"));
} else if (packageType.equalsIgnoreCase("tgz")) {
targz(packageDir, targetDir, targetDir.resolve(archiveNameNotNull + ".tgz"));
} else {
throw new IllegalArgumentException("Unknown filetype '" + packageType + "': should be either zip or tgz");
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* Copyright (c) 2024, Coreso SA (https://www.coreso.eu/) and TSCNET Services GmbH (https://www.tscnet.eu/)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.itools;

import org.apache.commons.io.FileUtils;
import org.apache.maven.execution.DefaultMavenExecutionRequest;
import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.project.ProjectBuildingRequest;
import org.codehaus.plexus.configuration.DefaultPlexusConfiguration;
import org.codehaus.plexus.configuration.PlexusConfiguration;
import org.eclipse.aether.DefaultRepositorySystemSession;

import java.io.File;

/**
* @author Damien Jeandemange {@literal <damien.jeandemange at artelys.com>}
*/
public class ItoolsPackagerMojoTest extends AbstractMojoTestCase {

private static final File BASEDIR = new File("src/test/resources/test-maven-project/");
private static final File POM_XML = new File(BASEDIR, "pom.xml");
private static final File TARGET = new File(BASEDIR, "target");
public static final String DEFAULT_PACKAGE_NAME = "itools-packager-test-project-1.0.0-SNAPSHOT";

ItoolsPackagerMojo mojo;
PlexusConfiguration configuration;

@Override
protected void setUp()
throws Exception {
// required
super.setUp();
MavenProject project = readMavenProject();
MavenSession session = newMavenSession(project);
MojoExecution execution = newMojoExecution("package-zip");
mojo = (ItoolsPackagerMojo) lookupConfiguredMojo(session, execution);
configuration = new DefaultPlexusConfiguration("configuration");
}

@Override
protected void tearDown()
throws Exception {
// required
super.tearDown();
FileUtils.deleteDirectory(TARGET); // cleanup
}

protected MavenProject readMavenProject() throws Exception {
MavenExecutionRequest request = new DefaultMavenExecutionRequest();
request.setBaseDirectory(BASEDIR);
ProjectBuildingRequest projectBuildingRequest = request.getProjectBuildingRequest();
projectBuildingRequest.setRepositorySession(new DefaultRepositorySystemSession());
MavenProject project = lookup(ProjectBuilder.class).build(POM_XML, projectBuildingRequest).getProject();
assertNotNull(project);
return project;
}

public void testDefaultConfiguration() throws Exception {
super.configureMojo(mojo, configuration);
assertNotNull(mojo);
mojo.execute();
assertTrue(new File(TARGET, DEFAULT_PACKAGE_NAME + ".zip").exists());
}

public void testPackageTypeTgz() throws Exception {
configuration.addChild("packageType", "tgz");
super.configureMojo(mojo, configuration);
assertNotNull(mojo);
mojo.execute();
assertTrue(new File(TARGET, DEFAULT_PACKAGE_NAME + ".tgz").exists());
}
}
36 changes: 36 additions & 0 deletions itools-packager/src/test/resources/test-maven-project/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2024, Coreso SA (https://www.coreso.eu/) and TSCNET Services GmbH (https://www.tscnet.eu/)
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
SPDX-License-Identifier: MPL-2.0
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<packaging>pom</packaging>
<groupId>com.powsybl</groupId>
<artifactId>itools-packager-test-project</artifactId>
<version>1.0.0-SNAPSHOT</version>

<build>
<plugins>
<plugin>
<artifactId>powsybl-itools-packager-maven-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>package-zip</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,10 @@
<jimfs.version>1.3.0</jimfs.version>
<junit-jupiter.version>5.10.2</junit-jupiter.version>
<logback.version>1.5.6</logback.version>
<maven.compat>3.9.9</maven.compat>
<maven.core.version>3.8.5</maven.core.version>
<maven.plugin.annotations.version>3.13.0</maven.plugin.annotations.version>
<maven.plugin.testing.harness.version>3.3.0</maven.plugin.testing.harness.version>
<mfl.version>0.5.15</mfl.version>
<mockito.version>5.12.0</mockito.version>
<nativelibloader.version>2.5.0</nativelibloader.version>
Expand Down Expand Up @@ -926,6 +928,12 @@
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand Down

0 comments on commit 6dd9ad6

Please sign in to comment.