| /* | |
| * Licensed to the Apache Software Foundation (ASF) under one | |
| * or more contributor license agreements. See the NOTICE file | |
| * distributed with this work for additional information | |
| * regarding copyright ownership. The ASF licenses this file | |
| * to you 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 org.apache.maven.plugins.dependency; | |
| import javax.inject.Inject; | |
| import java.io.File; | |
| import java.util.List; | |
| import java.util.Set; | |
| import org.apache.maven.artifact.Artifact; | |
| import org.apache.maven.plugin.AbstractMojo; | |
| import org.apache.maven.plugin.MojoExecutionException; | |
| 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.plugins.dependency.utils.ParamArtifact; | |
| import org.apache.maven.plugins.dependency.utils.ResolverUtil; | |
| import org.apache.maven.project.MavenProject; | |
| import org.eclipse.aether.resolution.ArtifactDescriptorException; | |
| import org.eclipse.aether.resolution.ArtifactResolutionException; | |
| /** | |
| * Goal that sets a property pointing to the artifact file for each project dependency. For each dependency (direct and | |
| * transitive) a project property will be set which follows the <code>groupId:artifactId:type:[classifier]</code> form | |
| * and contains the path to the resolved artifact. When {@link #includeVersions} is enabled, a second property named | |
| * like the first one with a <code>.version</code> suffix holds the resolved version. | |
| * | |
| * @author Paul Gier | |
| * @since 2.2 | |
| */ | |
| @Mojo( | |
| name = "properties", | |
| requiresDependencyResolution = ResolutionScope.TEST, | |
| defaultPhase = LifecyclePhase.INITIALIZE, | |
| threadSafe = true) | |
| public class PropertiesMojo extends AbstractMojo { | |
| private static final String VERSION_SUFFIX = ".version"; | |
| /** | |
| * The current Maven project. | |
| */ | |
| private final MavenProject project; | |
| private final ResolverUtil resolverUtil; | |
| @Inject | |
| public PropertiesMojo(MavenProject project, ResolverUtil resolverUtil) { | |
| this.project = project; | |
| this.resolverUtil = resolverUtil; | |
| } | |
| /** | |
| * Skip plugin execution completely. | |
| * | |
| * @since 2.7 | |
| */ | |
| @Parameter(property = "mdep.skip", defaultValue = "false") | |
| private boolean skip; | |
| /** | |
| * Also set a property holding the version of the resolved artifact. The property is named like the path property | |
| * with a <code>.version</code> suffix, for example <code>org.slf4j:slf4j-api:jar.version</code>. For a snapshot | |
| * the base version (<code>1.0-SNAPSHOT</code>) is used, not the timestamped version of the resolved file. It is | |
| * set for the same artifacts as the path property, including the {@link #extraArtifacts}, and also for an | |
| * artifact that has no file. An extra artifact with the same key overrides the value of the project dependency. | |
| * | |
| * @since 3.12.0 | |
| */ | |
| @Parameter(property = "mdep.properties.includeVersions", defaultValue = "false") | |
| private boolean includeVersions; | |
| /** | |
| * Extra artifacts that can be provided to the plugin. For each artifact in this list a property will be set | |
| * similar to the project artifacts discovered. This allows callers to supply additional | |
| * resolved dependencies for which properties pointing to the artifact file shall be created. | |
| * | |
| * Example usage in plugin configuration: | |
| * <pre> | |
| * <extraArtifacts> | |
| * <extraArtifact> | |
| * <artifact>org.example:my-artifact:jar:1.0</artifact> | |
| * </extraArtifact> | |
| * <extraArtifact> | |
| * <groupId>org.example</groupId> | |
| * <artifactId>my-artifact</artifactId> | |
| * <version>1.0</version> | |
| * </extraArtifact> | |
| * </extraArtifacts> | |
| * </pre> | |
| * @since 3.11.0 | |
| */ | |
| @Parameter | |
| private List<ParamArtifact> extraArtifacts; | |
| /** | |
| * Main entry into mojo. Gets the list of dependencies and iterates through setting a property for each artifact. | |
| * Gets the list of declared plugin's extra artifacts and iterates through setting a property for each artifact. | |
| * @throws MojoExecutionException with a message if an error occurs | |
| */ | |
| @Override | |
| public void execute() throws MojoExecutionException { | |
| if (isSkip()) { | |
| getLog().info("Skipping plugin execution"); | |
| return; | |
| } | |
| Set<Artifact> artifacts = project.getArtifacts(); | |
| for (Artifact artifact : artifacts) { | |
| String conflictId = artifact.getDependencyConflictId(); | |
| File file = artifact.getFile(); | |
| if (file != null) { | |
| project.getProperties().setProperty(conflictId, file.getAbsolutePath()); | |
| } else { | |
| getLog().warn("Artifact " + conflictId + " has no associated file; no property will be set for it."); | |
| } | |
| if (includeVersions) { | |
| project.getProperties().setProperty(conflictId + VERSION_SUFFIX, artifact.getBaseVersion()); | |
| } | |
| } | |
| if (extraArtifacts != null) { | |
| try { | |
| for (ParamArtifact paramArtifact : extraArtifacts) { | |
| if (!paramArtifact.isDataSet()) { | |
| throw new MojoExecutionException("You must specify an artifact OR GAV separately"); | |
| } | |
| org.eclipse.aether.artifact.Artifact artifact = | |
| resolverUtil.createArtifactFromParams(paramArtifact); | |
| artifact = resolverUtil.resolveArtifact(artifact, project.getRemoteProjectRepositories()); | |
| String conflictId = toConflictId(artifact); | |
| File file = artifact.getFile(); | |
| if (file != null) { | |
| this.project.getProperties().setProperty(conflictId, file.getAbsolutePath()); | |
| } else { | |
| getLog().warn("Extra artifact " + conflictId | |
| + " has no associated file; no property will be set for it."); | |
| } | |
| if (includeVersions) { | |
| this.project | |
| .getProperties() | |
| .setProperty(conflictId + VERSION_SUFFIX, artifact.getBaseVersion()); | |
| } | |
| } | |
| } catch (ArtifactResolutionException | ArtifactDescriptorException e) { | |
| throw new MojoExecutionException("Couldn't download artifact: " + e.getMessage(), e); | |
| } | |
| } | |
| } | |
| /** | |
| * @return {@link #skip} | |
| */ | |
| public boolean isSkip() { | |
| return skip; | |
| } | |
| private String toConflictId(org.eclipse.aether.artifact.Artifact artifact) { | |
| // The conflict ID is the same as the one used by Maven's Artifact class, which is | |
| // groupId:artifactId:type[:classifier] | |
| StringBuilder sb = new StringBuilder(); | |
| sb.append(artifact.getGroupId()) | |
| .append(":") | |
| .append(artifact.getArtifactId()) | |
| .append(":") | |
| .append(artifact.getExtension()); | |
| if (artifact.getClassifier() != null && !artifact.getClassifier().isEmpty()) { | |
| sb.append(":").append(artifact.getClassifier()); | |
| } | |
| return sb.toString(); | |
| } | |
| } |