-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3284 from c71n93/3236-download-jna-dependency
Implement mojo for downloading dependencies
- Loading branch information
Showing
3 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
128 changes: 128 additions & 0 deletions
128
eo-maven-plugin/src/main/java/org/eolang/maven/DownloadDepsMojo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
eo-maven-plugin/src/test/java/org/eolang/maven/DownloadDepsMojoTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters