Skip to content

Commit

Permalink
Merge pull request #3284 from c71n93/3236-download-jna-dependency
Browse files Browse the repository at this point in the history
Implement mojo for downloading dependencies
  • Loading branch information
yegor256 authored Jul 29, 2024
2 parents 5b1a1cf + 0adbeb3 commit 2fc4446
Show file tree
Hide file tree
Showing 3 changed files with 215 additions and 0 deletions.
128 changes: 128 additions & 0 deletions eo-maven-plugin/src/main/java/org/eolang/maven/DownloadDepsMojo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2024 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.eolang.maven;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Collection;
import java.util.function.BiConsumer;
import java.util.function.Supplier;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.cactoos.list.ListOf;

/**
* Downloads dependencies.
*
* @since 0.39
*/
@Mojo(
name = "deps",
defaultPhase = LifecyclePhase.PROCESS_SOURCES,
threadSafe = true
)
public final class DownloadDepsMojo extends SafeMojo {

/**
* Dependencies to download.
*/
private static final Collection<Supplier<Dependency>> DEPS = new ListOf<>(
new DepFunc("net.java.dev.jna", "jna", "5.14.0")
);

/**
* Directory where classes are stored in target.
* @checkstyle MemberNameCheck (8 lines)
*/
@Parameter(
defaultValue = "${project.build.directory}/classes",
readonly = true,
required = true
)
@SuppressWarnings("PMD.UnusedPrivateField")
private File classesDir;

/**
* The central.
*/
@SuppressWarnings("PMD.ImmutableField")
private BiConsumer<Dependency, Path> central;

@Override
void exec() throws IOException {
if (this.central == null) {
this.central = new Central(this.project, this.session, this.manager);
}
for (final Supplier<Dependency> dep : DownloadDepsMojo.DEPS) {
this.central.accept(dep.get(), this.classesDir.toPath());
}
}

/**
* Object for lazy building {@link Dependency} inplace.
*
* @since 0.39
*/
private static class DepFunc implements Supplier<Dependency> {
/**
* The groupId.
*/
private final String group;

/**
* The artifactId.
*/
private final String artifact;

/**
* The version.
*/
private final String version;

/**
* Ctor.
*
* @param group The groupId.
* @param artifact The artifactId.
* @param version The version.
*/
DepFunc(final String group, final String artifact, final String version) {
this.group = group;
this.artifact = artifact;
this.version = version;
}

@Override
public Dependency get() {
final Dependency dep = new Dependency();
dep.setGroupId(this.group);
dep.setArtifactId(this.artifact);
dep.setVersion(this.version);
return dep;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2024 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.eolang.maven;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import org.cactoos.list.ListOf;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

/**
* Test case for {@link DownloadDepsMojo}.
*
* @since 0.39
*/
final class DownloadDepsMojoTest {

/**
* Path to result output directory.
*/
private static final Path OUT = Paths.get("target/classes");

/**
* Names of all necessary dependencies.
*/
private static final Collection<String> DEPS_NAMES = new ListOf<>("jna-5.14.0.jar");

@Test
void executesWithoutErrors(@TempDir final Path temp) {
Assertions.assertDoesNotThrow(
() -> new FakeMaven(temp).execute(DownloadDepsMojo.class),
"Exception shouldn't been thrown"
);
}

@Test
void createsOutDir(@TempDir final Path temp) throws IOException {
new FakeMaven(temp).execute(DownloadDepsMojo.class);
Assertions.assertTrue(
Files.exists(temp.resolve(DownloadDepsMojoTest.OUT)),
String.format("Expected that \"%s\" target directory exists", DownloadDepsMojoTest.OUT)
);
}

@Test
void downloadsCorrectly(@TempDir final Path temp) throws IOException {
new FakeMaven(temp).execute(DownloadDepsMojo.class);
for (final String dep : DownloadDepsMojoTest.DEPS_NAMES) {
Assertions.assertTrue(
Files.exists(temp.resolve(DownloadDepsMojoTest.OUT).resolve(dep)),
String.format(
"Expected that \"%s\" dependency was downloaded correctly",
temp.resolve(DownloadDepsMojoTest.OUT).resolve(dep)
)
);
}
}
}
4 changes: 4 additions & 0 deletions eo-maven-plugin/src/test/java/org/eolang/maven/FakeMaven.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,10 @@ public <T extends AbstractMojo> FakeMaven execute(final Class<T> mojo) throws IO
Paths.get(String.format("target/%s", ParseMojo.DIR))
).toFile()
);
this.params.putIfAbsent(
"classesDir",
this.workspace.absolute(Paths.get("target/classes")).toFile()
);
}
final Moja<T> moja = new Moja<>(mojo);
for (final Map.Entry<String, ?> entry : this.allowedParams(mojo).entrySet()) {
Expand Down

0 comments on commit 2fc4446

Please sign in to comment.