diff --git a/distribution-core/pom.xml b/distribution-core/pom.xml index 2f5f491b313..4c6894488e2 100644 --- a/distribution-core/pom.xml +++ b/distribution-core/pom.xml @@ -537,6 +537,12 @@ ${project.basedir}/../iidm/iidm-serde/src/main/resources/xsd/iidm_V1_0.xsd + + + ${project.basedir}/../LICENSE.txt + ${project.basedir}/../THIRD-PARTY.txt + + diff --git a/itools-packager/README.md b/itools-packager/README.md index b2b5f08831c..aa77c9e3119 100644 --- a/itools-packager/README.md +++ b/itools-packager/README.md @@ -18,6 +18,7 @@ This Maven plugin can be used to generate an itools package with the following l share java + ``` Here is how to configure itools package Maven plugin in your project @@ -61,6 +62,11 @@ Here is how to configure itools package Maven plugin in your project ... + + + ... + + @@ -71,5 +77,5 @@ Here is how to configure itools package Maven plugin in your project - 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 +- additional binaries, libraries, configurations, package files can be added to the package using optional copyToBin, copyToLib, copyToEtc, copyToPackageRoot tags. +- all the jars with compile and runtime scope will be included in the package diff --git a/itools-packager/pom.xml b/itools-packager/pom.xml index 93d247832b6..86cc8eed2e5 100644 --- a/itools-packager/pom.xml +++ b/itools-packager/pom.xml @@ -75,8 +75,8 @@ test - org.junit.vintage - junit-vintage-engine + org.junit.jupiter + junit-jupiter test diff --git a/itools-packager/src/main/java/com/powsybl/itools/ItoolsPackagerMojo.java b/itools-packager/src/main/java/com/powsybl/itools/ItoolsPackagerMojo.java index 2406af47658..d91799812ce 100644 --- a/itools-packager/src/main/java/com/powsybl/itools/ItoolsPackagerMojo.java +++ b/itools-packager/src/main/java/com/powsybl/itools/ItoolsPackagerMojo.java @@ -30,7 +30,6 @@ import java.nio.file.attribute.PosixFilePermission; import java.util.Arrays; import java.util.EnumSet; -import java.util.List; import java.util.Set; /** @@ -80,6 +79,9 @@ public void setFiles(File[] files) { @Parameter private CopyTo copyToEtc; + @Parameter + private CopyTo copyToPackageRoot; + private void zip(Path dir, Path baseDir, Path zipFilePath) throws IOException { getLog().info("Zip package"); try (ZipArchiveOutputStream zos = new ZipArchiveOutputStream(Files.newOutputStream(zipFilePath))) { @@ -169,32 +171,6 @@ private void writeItoolsConf(BufferedWriter writer) throws IOException { writer.newLine(); } - private void addLicenseFiles(Path packageDir) { - // List of the license files to copy - List licenseFiles = Arrays.asList("LICENSE.txt", "THIRD-PARTY.txt"); - - // Get the root directory of the project - File projectRoot = project.getBasedir(); - if (project.getParent() != null) { - projectRoot = projectRoot.getParentFile(); - } - - // Copy each license file to the package directory - for (String licenseFile : licenseFiles) { - Path sourcePath = Paths.get(projectRoot.getPath(), licenseFile); - if (Files.exists(sourcePath)) { - try { - getLog().info("Copy license file " + sourcePath + " to " + packageDir); - Files.copy(sourcePath, packageDir.resolve(licenseFile), StandardCopyOption.REPLACE_EXISTING); - } catch (IOException e) { - getLog().warn("Failed to copy license file " + sourcePath + ": " + e.getMessage()); - } - } else { - getLog().warn("License file " + sourcePath + " not found"); - } - } - } - @Override public void execute() { try { @@ -245,8 +221,8 @@ public void execute() { Files.createDirectories(libDir); copyFiles(copyToLib, libDir); - // Add licenses - addLicenseFiles(packageDir); + // Add misc files to package root (e.g. license, third party ...) + copyFiles(copyToPackageRoot, packageDir); String archiveNameNotNull = archiveName != null ? archiveName : packageNameNotNull; if (packageType.equalsIgnoreCase("zip")) { diff --git a/itools-packager/src/test/java/com/powsybl/itools/ItoolsPackagerMojoTest.java b/itools-packager/src/test/java/com/powsybl/itools/ItoolsPackagerMojoTest.java index 438cd1d6ef5..975d8922182 100644 --- a/itools-packager/src/test/java/com/powsybl/itools/ItoolsPackagerMojoTest.java +++ b/itools-packager/src/test/java/com/powsybl/itools/ItoolsPackagerMojoTest.java @@ -7,110 +7,97 @@ */ 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.api.plugin.testing.Basedir; +import org.apache.maven.api.plugin.testing.InjectMojo; +import org.apache.maven.api.plugin.testing.MojoParameter; +import org.apache.maven.api.plugin.testing.MojoTest; +import org.apache.maven.model.Build; 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 org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.mockito.Mockito; +import javax.inject.Inject; import java.io.File; +import java.nio.file.Path; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; /** * @author Damien Jeandemange {@literal } */ -public class ItoolsPackagerMojoTest extends AbstractMojoTestCase { +@MojoTest +class ItoolsPackagerMojoTest { - 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 - } + @Inject + MavenProject project; - 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; - } + @TempDir + Path tempDir; - protected MavenProject readMavenProject2() throws Exception { - MavenExecutionRequest request = new DefaultMavenExecutionRequest(); - File basedir2 = new File("src/test/resources/test-maven-project-2/"); - File pomXml2 = new File(basedir2, "pom.xml"); - request.setBaseDirectory(basedir2); - ProjectBuildingRequest projectBuildingRequest = request.getProjectBuildingRequest(); - projectBuildingRequest.setRepositorySession(new DefaultRepositorySystemSession()); - MavenProject project = lookup(ProjectBuilder.class).build(pomXml2, projectBuildingRequest).getProject(); - assertNotNull(project); - return project; + @BeforeEach + void setup() { + Build build = new Build(); + build.setFinalName(DEFAULT_PACKAGE_NAME); + String target = tempDir.resolve(UUID.randomUUID().toString()).toString(); + build.setDirectory(target); + Mockito.when(project.getBuild()).thenReturn(build); } - public void testDefaultConfiguration() throws Exception { - super.configureMojo(mojo, configuration); - assertNotNull(mojo); + @Test + @Basedir("src/test/resources/test-maven-project/") + @InjectMojo(goal = "package-zip") + void testDefaultConfiguration(ItoolsPackagerMojo mojo) { mojo.execute(); - assertTrue(new File(TARGET, DEFAULT_PACKAGE_NAME + ".zip").exists()); + String target = project.getBuild().getDirectory(); + assertTrue(new File(target, DEFAULT_PACKAGE_NAME + ".zip").exists()); + assertTrue(new File(target, DEFAULT_PACKAGE_NAME + "/LICENSE.txt").exists()); + assertTrue(new File(target, DEFAULT_PACKAGE_NAME + "/bin/itools").exists()); + assertTrue(new File(target, DEFAULT_PACKAGE_NAME + "/bin/itools.bat").exists()); + assertTrue(new File(target, DEFAULT_PACKAGE_NAME + "/bin/powsyblsh").exists()); + assertTrue(new File(target, DEFAULT_PACKAGE_NAME + "/etc/itools.conf").exists()); + assertTrue(new File(target, DEFAULT_PACKAGE_NAME + "/etc/logback-itools.xml").exists()); + assertTrue(new File(target, DEFAULT_PACKAGE_NAME + "/etc/logback-powsyblsh.xml").exists()); + assertTrue(new File(target, DEFAULT_PACKAGE_NAME + "/lib").exists()); + assertTrue(new File(target, DEFAULT_PACKAGE_NAME + "/lib").isDirectory()); + assertTrue(new File(target, DEFAULT_PACKAGE_NAME + "/share/java").exists()); + assertTrue(new File(target, DEFAULT_PACKAGE_NAME + "/share/java").isDirectory()); + assertTrue(new File(target, DEFAULT_PACKAGE_NAME + "/THIRD-PARTY.txt").exists()); + assertTrue(new File(target, DEFAULT_PACKAGE_NAME + "/file.txt").exists()); + assertFalse(new File(target, DEFAULT_PACKAGE_NAME + "/not_exists.txt").exists()); } - public void testPackageTypeTgz() throws Exception { - configuration.addChild("packageType", "tgz"); - super.configureMojo(mojo, configuration); - assertNotNull(mojo); + @Test + @Basedir("src/test/resources/test-maven-project/") + @InjectMojo(goal = "package-zip") + @MojoParameter(name = "packageType", value = "tgz") + void testTgzConfiguration(ItoolsPackagerMojo mojo) { mojo.execute(); - assertTrue(new File(TARGET, DEFAULT_PACKAGE_NAME + ".tgz").exists()); + String target = project.getBuild().getDirectory(); + assertTrue(new File(target, DEFAULT_PACKAGE_NAME + ".tgz").exists()); } - public void testLicenseFiles() throws Exception { - super.configureMojo(mojo, configuration); - assertNotNull(mojo); - mojo.execute(); - assertTrue(new File(TARGET, DEFAULT_PACKAGE_NAME + "/LICENSE.txt").exists()); - assertTrue(new File(TARGET, DEFAULT_PACKAGE_NAME + "/THIRD-PARTY.txt").exists()); + @Test + @Basedir("src/test/resources/test-maven-project/") + @InjectMojo(goal = "package-zip") + @MojoParameter(name = "packageType", value = "someUnsupportedValue") + void testUnknownPackageType(ItoolsPackagerMojo mojo) { + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, mojo::execute); + assertEquals("Unknown filetype 'someUnsupportedValue': should be either zip or tgz", e.getMessage()); } - public void testMissingLicenseFile() throws Exception { - MavenProject project = readMavenProject2(); - MavenSession session = newMavenSession(project); - MojoExecution execution = newMojoExecution("package-zip"); - mojo = (ItoolsPackagerMojo) lookupConfiguredMojo(session, execution); - super.configureMojo(mojo, configuration); - assertNotNull(mojo); + @Test + @Basedir("src/test/resources/test-maven-project/") + @InjectMojo(goal = "package-zip") + @MojoParameter(name = "packageName", value = "myOwnPackageName") + void testPackageNameConfiguration(ItoolsPackagerMojo mojo) { mojo.execute(); - File target2 = new File("src/test/resources/test-maven-project-2/target/"); - assertTrue(new File(target2, DEFAULT_PACKAGE_NAME + "/LICENSE.txt").exists()); - assertFalse(new File(target2, DEFAULT_PACKAGE_NAME + "/THIRD-PARTY.txt").exists()); - FileUtils.deleteDirectory(target2); // cleanup + String target = project.getBuild().getDirectory(); + assertTrue(new File(target, "myOwnPackageName" + ".zip").exists()); } + } diff --git a/itools-packager/src/test/resources/test-maven-project-2/LICENSE.txt b/itools-packager/src/test/resources/test-maven-project-2/LICENSE.txt deleted file mode 100644 index 7095cdc446b..00000000000 --- a/itools-packager/src/test/resources/test-maven-project-2/LICENSE.txt +++ /dev/null @@ -1 +0,0 @@ -License content \ No newline at end of file diff --git a/itools-packager/src/test/resources/test-maven-project-2/pom.xml b/itools-packager/src/test/resources/test-maven-project-2/pom.xml deleted file mode 100644 index d9284fc0eca..00000000000 --- a/itools-packager/src/test/resources/test-maven-project-2/pom.xml +++ /dev/null @@ -1,37 +0,0 @@ - - - - 4.0.0 - - pom - com.powsybl - itools-packager-test-project - 1.0.0-SNAPSHOT - - - - - powsybl-itools-packager-maven-plugin - - - package - - package-zip - - - - - - - - diff --git a/itools-packager/src/test/resources/test-maven-project/path/to/some/file.txt b/itools-packager/src/test/resources/test-maven-project/path/to/some/file.txt new file mode 100644 index 00000000000..b8a0db3a1d2 --- /dev/null +++ b/itools-packager/src/test/resources/test-maven-project/path/to/some/file.txt @@ -0,0 +1 @@ +hello itools packager! diff --git a/itools-packager/src/test/resources/test-maven-project/pom.xml b/itools-packager/src/test/resources/test-maven-project/pom.xml index f543d09b336..cf8a610ba2e 100644 --- a/itools-packager/src/test/resources/test-maven-project/pom.xml +++ b/itools-packager/src/test/resources/test-maven-project/pom.xml @@ -29,6 +29,16 @@ + + + + ${project.basedir}/LICENSE.txt + ${project.basedir}/THIRD-PARTY.txt + ${project.basedir}/path/to/some/file.txt + ${project.basedir}/does/not_exists.txt + + + diff --git a/pom.xml b/pom.xml index 3f444afaf50..5912a140ed6 100644 --- a/pom.xml +++ b/pom.xml @@ -127,7 +127,7 @@ 3.9.9 3.8.5 3.15.1 - 3.3.0 + 3.4.0 0.5.15 5.18.0 2.5.0