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 javax.inject.Inject;
22  
23  import java.util.Set;
24  
25  import org.apache.maven.artifact.Artifact;
26  import org.apache.maven.plugin.AbstractMojo;
27  import org.apache.maven.plugin.MojoExecutionException;
28  import org.apache.maven.plugins.annotations.LifecyclePhase;
29  import org.apache.maven.plugins.annotations.Mojo;
30  import org.apache.maven.plugins.annotations.Parameter;
31  import org.apache.maven.plugins.annotations.ResolutionScope;
32  import org.apache.maven.project.MavenProject;
33  
34  /**
35   * Goal that sets a property pointing to the artifact file for each project dependency. For each dependency (direct and
36   * transitive) a project property will be set which follows the <code>groupId:artifactId:type:[classifier]</code> form
37   * and contains the path to the resolved artifact.
38   *
39   * @author Paul Gier
40   * @since 2.2
41   */
42  // CHECKSTYLE_OFF: LineLength
43  @Mojo(
44          name = "properties",
45          requiresDependencyResolution = ResolutionScope.TEST,
46          defaultPhase = LifecyclePhase.INITIALIZE,
47          threadSafe = true)
48  // CHECKSTYLE_ON: LineLength
49  public class PropertiesMojo extends AbstractMojo {
50  
51      /**
52       * The current Maven project.
53       */
54      private final MavenProject project;
55  
56      @Inject
57      public PropertiesMojo(MavenProject project) {
58          this.project = project;
59      }
60  
61      /**
62       * Skip plugin execution completely.
63       *
64       * @since 2.7
65       */
66      @Parameter(property = "mdep.skip", defaultValue = "false")
67      private boolean skip;
68  
69      /**
70       * Main entry into mojo. Gets the list of dependencies and iterates through setting a property for each artifact.
71       *
72       * @throws MojoExecutionException with a message if an error occurs
73       */
74      @Override
75      public void execute() throws MojoExecutionException {
76          if (isSkip()) {
77              getLog().info("Skipping plugin execution");
78              return;
79          }
80  
81          Set<Artifact> artifacts = project.getArtifacts();
82  
83          for (Artifact artifact : artifacts) {
84              project.getProperties()
85                      .setProperty(
86                              artifact.getDependencyConflictId(),
87                              artifact.getFile().getAbsolutePath());
88          }
89      }
90  
91      /**
92       * @return {@link #skip}
93       */
94      public boolean isSkip() {
95          return skip;
96      }
97  }