Skip to content

Commit

Permalink
fix tests and README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
inponomarev committed Jan 19, 2024
1 parent 80145a5 commit 649bc8c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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?

Expand Down Expand Up @@ -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.
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.
9 changes: 8 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>org.atp-fivt</groupId>
<artifactId>export-maven-plugin</artifactId>
<version>1.0-RC1</version>
<version>1.0-RC2</version>
<packaging>maven-plugin</packaging>

<name>Maven project export plugin</name>
Expand Down Expand Up @@ -46,6 +46,7 @@
<maven.compiler.target>11</maven.compiler.target>
<maven.version>3.9.5</maven.version>
<checkstyle.maven.plugin.version>3.3.0</checkstyle.maven.plugin.version>
<maven.surefire.plugin.version>3.1.0</maven.surefire.plugin.version>
<checkstyle.version>10.12.0</checkstyle.version>
</properties>

Expand Down Expand Up @@ -120,6 +121,12 @@
</dependencies>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.plugin.version}</version>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/org/atpfivt/ExportMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Path> pathList = collector.getFiles();
List<Path> 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()));
Expand Down
22 changes: 19 additions & 3 deletions src/test/java/org/atpfivt/ExportMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<String> filenames = new HashSet<>();
try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(target.resolve("export.zip").toFile()))) {
Expand All @@ -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
Expand Down

0 comments on commit 649bc8c

Please sign in to comment.