View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.dependency;
20  
21  import java.util.Set;
22  
23  import org.apache.maven.artifact.Artifact;
24  import org.apache.maven.plugin.AbstractMojo;
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.plugins.annotations.LifecyclePhase;
27  import org.apache.maven.plugins.annotations.Mojo;
28  import org.apache.maven.plugins.annotations.Parameter;
29  import org.apache.maven.plugins.annotations.ResolutionScope;
30  import org.apache.maven.project.MavenProject;
31  
32  /**
33   * Goal that sets a property pointing to the artifact file for each project dependency. For each dependency (direct and
34   * transitive) a project property will be set which follows the <code>groupId:artifactId:type:[classifier]</code> form
35   * and contains the path to the resolved artifact.
36   *
37   * @author Paul Gier
38   * @since 2.2
39   */
40  // CHECKSTYLE_OFF: LineLength
41  @Mojo(
42          name = "properties",
43          requiresDependencyResolution = ResolutionScope.TEST,
44          defaultPhase = LifecyclePhase.INITIALIZE,
45          threadSafe = true)
46  // CHECKSTYLE_ON: LineLength
47  public class PropertiesMojo extends AbstractMojo {
48  
49      /**
50       * The current Maven project
51       */
52      @Parameter(defaultValue = "${project}", readonly = true, required = true)
53      private MavenProject project;
54  
55      /**
56       * Skip plugin execution completely.
57       *
58       * @since 2.7
59       */
60      @Parameter(property = "mdep.skip", defaultValue = "false")
61      private boolean skip;
62  
63      /**
64       * Main entry into mojo. Gets the list of dependencies and iterates through setting a property for each artifact.
65       *
66       * @throws MojoExecutionException with a message if an error occurs.
67       */
68      @Override
69      public void execute() throws MojoExecutionException {
70          if (isSkip()) {
71              getLog().info("Skipping plugin execution");
72              return;
73          }
74  
75          Set<Artifact> artifacts = project.getArtifacts();
76  
77          for (Artifact artifact : artifacts) {
78              project.getProperties()
79                      .setProperty(
80                              artifact.getDependencyConflictId(),
81                              artifact.getFile().getAbsolutePath());
82          }
83      }
84  
85      /**
86       * @return {@link #skip}
87       */
88      public boolean isSkip() {
89          return skip;
90      }
91  }