View Javadoc

1   package org.apache.maven.plugin.dependency;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.artifact.Artifact;
23  import org.apache.maven.plugin.AbstractMojo;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugins.annotations.Component;
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  import java.util.Set;
33  
34  /**
35   * Goal that sets a property pointing to the artifact file for each project dependency.
36   * For each dependency (direct and transitive) a project property will be set which follows the
37   * <code>groupId:artifactId:type:[classifier]</code> form and contains the path to the resolved artifact.
38   *
39   * @author Paul Gier
40   * @version $Id: PropertiesMojo.java 1451330 2013-02-28 20:41:08Z rfscholte $
41   * @since 2.2
42   */
43  @Mojo( name = "properties", requiresDependencyResolution = ResolutionScope.TEST,
44         defaultPhase = LifecyclePhase.INITIALIZE, threadSafe = true )
45  public class PropertiesMojo
46      extends AbstractMojo
47  {
48  
49      /**
50       * The current Maven project
51       */
52      @Component
53      protected 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      public void execute()
69          throws MojoExecutionException
70      {
71          if ( isSkip() )
72          {
73              getLog().info( "Skipping plugin execution" );
74              return;
75          }
76  
77          @SuppressWarnings( "unchecked" ) Set<Artifact> artifacts = getProject().getArtifacts();
78  
79          for ( Artifact artifact : artifacts )
80          {
81              project.getProperties().setProperty( artifact.getDependencyConflictId(),
82                                                   artifact.getFile().getAbsolutePath() );
83          }
84      }
85  
86      public MavenProject getProject()
87      {
88          return project;
89      }
90  
91      public boolean isSkip()
92      {
93          return skip;
94  }
95  
96      public void setSkip( boolean skip )
97      {
98          this.skip = skip;
99      }
100 }