-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding simple-dependencies mojo to output dependencies in JSON
- Loading branch information
1 parent
443673e
commit 0d7bcb3
Showing
5 changed files
with
261 additions
and
25 deletions.
There are no files selected for viewing
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
142 changes: 142 additions & 0 deletions
142
yupiik-tools-maven-plugin/src/main/java/io/yupiik/maven/mojo/SimpleDependenciesMojo.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,142 @@ | ||
/* | ||
* Copyright (c) 2020 - 2023 - Yupiik SAS - https://www.yupiik.com | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package io.yupiik.maven.mojo; | ||
|
||
import io.yupiik.maven.service.artifact.Filters; | ||
import jakarta.json.bind.JsonbBuilder; | ||
import jakarta.json.bind.JsonbConfig; | ||
import org.apache.maven.plugin.AbstractMojo; | ||
import org.apache.maven.plugin.MojoExecutionException; | ||
import org.apache.maven.plugin.MojoFailureException; | ||
import org.apache.maven.plugins.annotations.LifecyclePhase; | ||
import org.apache.maven.plugins.annotations.Mojo; | ||
import org.apache.maven.plugins.annotations.Parameter; | ||
import org.apache.maven.plugins.annotations.ResolutionScope; | ||
import org.apache.maven.project.MavenProject; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
import static io.yupiik.maven.mojo.SimpleDependenciesMojo.Format.JSON_PRETTY; | ||
import static jakarta.json.bind.config.PropertyOrderStrategy.LEXICOGRAPHICAL; | ||
import static java.util.stream.Collectors.toList; | ||
|
||
@Mojo( | ||
name = "simple-dependencies", requiresDependencyResolution = ResolutionScope.TEST, | ||
defaultPhase = LifecyclePhase.GENERATE_SOURCES, threadSafe = true) | ||
public class SimpleDependenciesMojo extends AbstractMojo { | ||
/** | ||
* Where to dump the dependencies, {@code log} to log them, else a file path. | ||
*/ | ||
@Parameter(property = "yupiik.simple-dependencies.source", defaultValue = "${project.build.directory/dependencies.adoc") | ||
protected String output; | ||
|
||
/** | ||
* Which format to use to dump them. | ||
*/ | ||
@Parameter(property = "yupiik.simple-dependencies.format", defaultValue = "JSON") | ||
protected Format format; | ||
|
||
/** | ||
* Which scope to include, use {@code all} to include them all. | ||
*/ | ||
@Parameter(property = "yupiik.simple-dependencies.scope", defaultValue = "all") | ||
protected String scope; | ||
|
||
@Parameter(defaultValue = "${project}", readonly = true, required = true) | ||
protected MavenProject project; | ||
|
||
@Override | ||
public void execute() throws MojoExecutionException, MojoFailureException { | ||
final var dependencies = findDependencies(); | ||
final var formatted = format(dependencies); | ||
if (output.equals("log")) { | ||
getLog().info(formatted); | ||
} else { | ||
final var file = Path.of(output); | ||
try { | ||
if (file.getParent() != null) { | ||
Files.createDirectories(file.getParent()); | ||
} | ||
try (final var out = Files.newBufferedWriter(file)) { | ||
out.write(formatted); | ||
} | ||
} catch (final IOException ioe) { | ||
throw new MojoFailureException(ioe.getMessage(), ioe); | ||
} | ||
} | ||
} | ||
|
||
private String format(final List<DepArtifact> dependencies) throws MojoFailureException { | ||
switch (format) { | ||
// todo: ADOC? | ||
case JSON: | ||
case JSON_PRETTY: | ||
default: | ||
try (final var jsonb = JsonbBuilder.create(new JsonbConfig() | ||
.withPropertyOrderStrategy(LEXICOGRAPHICAL) | ||
.withFormatting(JSON_PRETTY.equals(format)))) { | ||
return jsonb.toJson(new JsonWrapper(dependencies)); | ||
} catch (final Exception e) { | ||
throw new MojoFailureException(e.getMessage(), e); | ||
} | ||
} | ||
} | ||
|
||
private List<DepArtifact> findDependencies() { | ||
final var filter = Filters.createScopeFilter(scope); | ||
return project.getArtifacts().stream() | ||
.filter(filter::include) | ||
.map(a -> new DepArtifact(a.getGroupId(), a.getArtifactId(), a.getBaseVersion(), a.getType(), a.getClassifier(), a.getScope())) | ||
.collect(toList()); | ||
} | ||
|
||
public static class JsonWrapper { | ||
public Collection<DepArtifact> items; | ||
|
||
public JsonWrapper(final Collection<DepArtifact> items) { | ||
this.items = items; | ||
} | ||
} | ||
|
||
public static class DepArtifact { | ||
public String groupId; | ||
public String artifactId; | ||
public String version; | ||
public String type; | ||
public String classifier; | ||
public String scope; | ||
|
||
public DepArtifact(final String groupId, final String artifactId, | ||
final String version, final String type, | ||
final String classifier, final String scope) { | ||
this.groupId = groupId; | ||
this.artifactId = artifactId; | ||
this.version = version; | ||
this.type = type; | ||
this.classifier = classifier; | ||
this.scope = scope; | ||
} | ||
} | ||
|
||
public enum Format { | ||
JSON, | ||
JSON_PRETTY, | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
yupiik-tools-maven-plugin/src/main/java/io/yupiik/maven/service/artifact/Filters.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,50 @@ | ||
/* | ||
* Copyright (c) 2020 - 2023 - Yupiik SAS - https://www.yupiik.com | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package io.yupiik.maven.service.artifact; | ||
|
||
import org.apache.maven.artifact.resolver.filter.ArtifactFilter; | ||
import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter; | ||
|
||
public final class Filters { | ||
private Filters() { | ||
// no-op | ||
} | ||
|
||
public static ArtifactFilter createScopeFilter(final String scope) { | ||
switch (scope) { | ||
case "all": | ||
return artifact -> true; | ||
case "compile": | ||
case "runtime": | ||
case "compile+runtime": | ||
case "runtime+system": | ||
case "test": | ||
return new ScopeArtifactFilter(scope); | ||
case "test_only": | ||
return artifact -> "test".equals(artifact.getScope()); | ||
case "compile_only": | ||
return artifact -> "compile".equals(artifact.getScope()); | ||
case "runtime_only": | ||
return artifact -> "runtime".equals(artifact.getScope()); | ||
case "system_only": | ||
return artifact -> "system".equals(artifact.getScope()); | ||
case "provided_only": | ||
return artifact -> "provided".equals(artifact.getScope()); | ||
default: | ||
throw new IllegalArgumentException("Unsupported scope: '" + scope + "'"); | ||
} | ||
} | ||
} |
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
61 changes: 61 additions & 0 deletions
61
yupiik-tools-maven-plugin/src/test/java/io/yupiik/maven/mojo/SimpleDependenciesMojoTest.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,61 @@ | ||
/* | ||
* Copyright (c) 2020 - 2023 - Yupiik SAS - https://www.yupiik.com | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package io.yupiik.maven.mojo; | ||
|
||
import org.apache.maven.artifact.DefaultArtifact; | ||
import org.apache.maven.plugin.MojoExecutionException; | ||
import org.apache.maven.plugin.MojoFailureException; | ||
import org.apache.maven.project.MavenProject; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Set; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class SimpleDependenciesMojoTest { | ||
@Test | ||
void run(@TempDir final Path work) throws MojoExecutionException, MojoFailureException, IOException { | ||
final var file = work.resolve("deps.json"); | ||
final var mojo = new SimpleDependenciesMojo() {{ | ||
output = file.toString(); | ||
format = Format.JSON_PRETTY; | ||
scope = "all"; | ||
project = new MavenProject(); | ||
project.setArtifacts(Set.of(new DefaultArtifact( | ||
"the.group", "the.art", "the.version", | ||
"the.scope", "the.type", "the.classifier", null))); | ||
}}; | ||
mojo.execute(); | ||
assertEquals("" + | ||
"{\n" + | ||
" \"items\":[\n" + | ||
" {\n" + | ||
" \"artifactId\":\"the.art\",\n" + | ||
" \"classifier\":\"the.classifier\",\n" + | ||
" \"groupId\":\"the.group\",\n" + | ||
" \"scope\":\"the.scope\",\n" + | ||
" \"type\":\"the.type\",\n" + | ||
" \"version\":\"the.version\"\n" + | ||
" }\n" + | ||
" ]\n" + | ||
"}" + | ||
"", Files.readString(file)); | ||
} | ||
} |