|
| 1 | +/* |
| 2 | + * Copyright (c) 2023-2024 Maveniverse Org. |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v2.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * https://www.eclipse.org/legal/epl-v20.html |
| 7 | + */ |
| 8 | +package eu.maveniverse.maven.toolbox.plugin; |
| 9 | + |
| 10 | +import eu.maveniverse.maven.mima.context.Context; |
| 11 | +import eu.maveniverse.maven.mima.context.ContextOverrides; |
| 12 | +import eu.maveniverse.maven.mima.context.Runtime; |
| 13 | +import eu.maveniverse.maven.mima.context.Runtimes; |
| 14 | +import eu.maveniverse.maven.toolbox.shared.Output; |
| 15 | +import eu.maveniverse.maven.toolbox.shared.ResolutionRoot; |
| 16 | +import eu.maveniverse.maven.toolbox.shared.Slf4jOutput; |
| 17 | +import eu.maveniverse.maven.toolbox.shared.ToolboxCommando; |
| 18 | +import java.util.stream.Collectors; |
| 19 | +import org.apache.maven.RepositoryUtils; |
| 20 | +import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager; |
| 21 | +import org.apache.maven.execution.MavenSession; |
| 22 | +import org.apache.maven.plugin.AbstractMojo; |
| 23 | +import org.apache.maven.plugin.MojoExecutionException; |
| 24 | +import org.apache.maven.plugin.MojoFailureException; |
| 25 | +import org.apache.maven.plugins.annotations.Component; |
| 26 | +import org.apache.maven.plugins.annotations.Parameter; |
| 27 | +import org.apache.maven.project.MavenProject; |
| 28 | +import org.eclipse.aether.artifact.ArtifactTypeRegistry; |
| 29 | +import org.eclipse.aether.artifact.DefaultArtifact; |
| 30 | +import org.slf4j.Logger; |
| 31 | +import org.slf4j.LoggerFactory; |
| 32 | + |
| 33 | +/** |
| 34 | + * Support class for "project aware" Mojos. |
| 35 | + */ |
| 36 | +public abstract class ProjectMojoSupport extends AbstractMojo { |
| 37 | + protected final Logger logger = LoggerFactory.getLogger(getClass()); |
| 38 | + |
| 39 | + protected final Output output = new Slf4jOutput(logger); |
| 40 | + |
| 41 | + @Parameter(property = "verbose", defaultValue = "false", required = true) |
| 42 | + protected boolean verbose; |
| 43 | + |
| 44 | + @Component |
| 45 | + private MavenProject mavenProject; |
| 46 | + |
| 47 | + @Component |
| 48 | + private MavenSession mavenSession; |
| 49 | + |
| 50 | + @Component |
| 51 | + private ArtifactHandlerManager artifactHandlerManager; |
| 52 | + |
| 53 | + protected ResolutionRoot projectAsResolutionRoot() { |
| 54 | + ArtifactTypeRegistry artifactTypeRegistry = |
| 55 | + mavenSession.getRepositorySession().getArtifactTypeRegistry(); |
| 56 | + return ResolutionRoot.ofNotLoaded(new DefaultArtifact( |
| 57 | + mavenProject.getGroupId(), |
| 58 | + mavenProject.getArtifactId(), |
| 59 | + artifactHandlerManager |
| 60 | + .getArtifactHandler(mavenProject.getPackaging()) |
| 61 | + .getExtension(), |
| 62 | + mavenProject.getVersion())) |
| 63 | + .withDependencies(mavenProject.getDependencies().stream() |
| 64 | + .map(d -> RepositoryUtils.toDependency(d, artifactTypeRegistry)) |
| 65 | + .collect(Collectors.toList())) |
| 66 | + .withManagedDependencies(mavenProject.getDependencyManagement().getDependencies().stream() |
| 67 | + .map(d -> RepositoryUtils.toDependency(d, artifactTypeRegistry)) |
| 68 | + .collect(Collectors.toList())) |
| 69 | + .build(); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public final void execute() throws MojoExecutionException, MojoFailureException { |
| 74 | + Runtime runtime = Runtimes.INSTANCE.getRuntime(); |
| 75 | + try (Context context = runtime.create(ContextOverrides.create().build())) { |
| 76 | + doExecute(ToolboxCommando.getOrCreate(runtime, context)); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + protected abstract void doExecute(ToolboxCommando toolboxCommando) |
| 81 | + throws MojoExecutionException, MojoFailureException; |
| 82 | +} |
0 commit comments