From 649bc8cfe696f64a817f16725ce1e734aa6184d5 Mon Sep 17 00:00:00 2001 From: Ivan Ponomarev Date: Fri, 19 Jan 2024 23:47:40 +0000 Subject: [PATCH] fix tests and README.md --- README.md | 6 ++--- pom.xml | 9 +++++++- src/main/java/org/atpfivt/ExportMojo.java | 13 +++++++++-- src/test/java/org/atpfivt/ExportMojoTest.java | 22 ++++++++++++++++--- 4 files changed, 41 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 9093601..c016f5e 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ # Export Maven Plugin [![Actions Status: build](https://github.com/atp-mipt/export-maven-plugin/workflows/build/badge.svg)](https://github.com/atp-mipt/homework-quickstart/actions?query=workflow%3A"build") -[![Maven Central](https://maven-badges.herokuapp.com/maven-central/org.atp-fivt/homework-quickstart/badge.svg)](https://maven-badges.herokuapp.com/maven-central/org.atp-fivt/export-maven-plugin) +[![Maven Central](https://maven-badges.herokuapp.com/maven-central/org.atp-fivt/export-maven-plugin/badge.svg)](https://maven-badges.herokuapp.com/maven-central/org.atp-fivt/export-maven-plugin) -The Export Maven Plugin is an Apache Maven plugin designed for exporting an entire Maven project into a .zip file. It respects the .gitignore settings and omits the build directory (target). +The Export Maven Plugin is an Apache Maven plugin designed for exporting an entire Maven project into a .zip file. It respects the `.gitignore` settings and, regardless of `.gitignore`, always omits the build directory (`target`). ## Why is it needed? @@ -44,4 +44,4 @@ The resulting `.zip` file will be located in the target directory. ## How does it work -This plugin leverages the [jGit](https://www.eclipse.org/jgit/) library to compile a list of all files in the project, adhering to the rules specified in `.gitignore` files. Importantly, the functionality of this plugin does not require Git to be installed on the target machine. This is particularly beneficial in educational settings or environments where Git installation cannot be assumed. By using jGit, the plugin operates independently of the local Git installation. \ No newline at end of file +This plugin leverages the [jGit](https://www.eclipse.org/jgit/) library to compile a list of all files in the project, adhering to the rules specified in `.gitignore` files. Importantly, the functionality of this plugin does not require Git to be installed on the target machine. This is particularly beneficial in educational settings or environments where Git installation cannot be assumed. By using jGit, the plugin operates independently of the local Git installation. diff --git a/pom.xml b/pom.xml index 4049046..28f241b 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ org.atp-fivt export-maven-plugin - 1.0-RC1 + 1.0-RC2 maven-plugin Maven project export plugin @@ -46,6 +46,7 @@ 11 3.9.5 3.3.0 + 3.1.0 10.12.0 @@ -120,6 +121,12 @@ + + org.apache.maven.plugins + maven-surefire-plugin + ${maven.surefire.plugin.version} + + org.apache.maven.plugins maven-plugin-plugin diff --git a/src/main/java/org/atpfivt/ExportMojo.java b/src/main/java/org/atpfivt/ExportMojo.java index 07a66ec..67ed68f 100644 --- a/src/main/java/org/atpfivt/ExportMojo.java +++ b/src/main/java/org/atpfivt/ExportMojo.java @@ -13,6 +13,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.List; +import java.util.stream.Collectors; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -43,15 +44,23 @@ void setZipFileName(String zipFileName) { this.zipFileName = zipFileName; } + private boolean isNotInBuildDirectory(Path path) { + return !path.startsWith(buildDirectory.toPath()); + } + public void execute() throws MojoExecutionException { + Path buildPath = buildDirectory.toPath(); File zipFile = new File(buildDirectory, zipFileName); try { - Files.createDirectories(buildDirectory.toPath()); + Files.createDirectories(buildPath); try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile))) { Path basePath = baseDirectory.toPath(); PathsCollector collector = new PathsCollector(basePath); - List pathList = collector.getFiles(); + List pathList = + collector.getFiles().stream().filter( + this::isNotInBuildDirectory + ).collect(Collectors.toList()); for (Path path : pathList) { Path relativePath = basePath.relativize(path); zos.putNextEntry(new ZipEntry(relativePath.toString())); diff --git a/src/test/java/org/atpfivt/ExportMojoTest.java b/src/test/java/org/atpfivt/ExportMojoTest.java index 85d4916..8727b84 100644 --- a/src/test/java/org/atpfivt/ExportMojoTest.java +++ b/src/test/java/org/atpfivt/ExportMojoTest.java @@ -33,15 +33,32 @@ void setup() throws IOException { exportMojo.setBaseDirectory(root.toFile()); exportMojo.setBuildDirectory(target.toFile()); exportMojo.setZipFileName("export.zip"); + } + + @Test + void exportSavesFilesRespectingTheGitignore() throws MojoExecutionException, IOException { Files.write(root.resolve(".gitignore"), List.of("#gitignore test", "*.ignoreme", "target/")); Files.writeString(root.resolve("a"), "test"); Files.writeString(root.resolve("b"), "test"); Files.writeString(root.resolve("a.ignoreme"), "test"); + exportMojo.execute(); + Set filenames = new HashSet<>(); + try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(target.resolve("export.zip").toFile()))) { + ZipEntry nextEntry; + while ((nextEntry = zipInputStream.getNextEntry()) != null) { + filenames.add(nextEntry.getName()); + } + } + assertEquals(Set.of("a", "b", ".gitignore"), filenames); } @Test - void exportSavesFilesRespectingTheGitignore() throws MojoExecutionException, IOException { + void exportOmitsTargetEvenIfNoGitignoreProvided() throws MojoExecutionException, IOException { + Files.writeString(root.resolve("a"), "test"); + Files.writeString(root.resolve("b"), "test"); + Files.writeString(root.resolve("a.ignoreme"), "test"); + Files.writeString(target.resolve("helloworld.class"), "cafebabe"); exportMojo.execute(); Set filenames = new HashSet<>(); try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(target.resolve("export.zip").toFile()))) { @@ -50,8 +67,7 @@ void exportSavesFilesRespectingTheGitignore() throws MojoExecutionException, IOE filenames.add(nextEntry.getName()); } } - assertEquals(Set.of("a", "b", ".gitignore"), filenames); - + assertEquals(Set.of("a", "b", "a.ignoreme"), filenames); } @AfterEach