View Javadoc

1   package org.apache.maven.plugin.plugin.metadata;
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.artifact.repository.metadata.ArtifactRepositoryMetadata;
24  import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;
25  import org.apache.maven.artifact.repository.metadata.Versioning;
26  import org.apache.maven.plugin.AbstractMojo;
27  import org.apache.maven.plugin.MojoExecutionException;
28  import org.apache.maven.plugin.descriptor.PluginDescriptor;
29  import org.apache.maven.project.MavenProject;
30  
31  /**
32   * Inject any plugin-specific artifact metadata to the project's artifact, for subsequent installation
33   * and deployment. The first use-case for this is to add the LATEST metadata (which is plugin-specific)
34   * for shipping alongside the plugin's artifact.
35   *
36   * @version $Id: AddPluginArtifactMetadataMojo.java 1133737 2011-06-09 09:20:50Z stephenc $
37   * @since 2.0
38   * @phase package
39   * @goal addPluginArtifactMetadata
40   * @threadSafe
41   */
42  public class AddPluginArtifactMetadataMojo
43      extends AbstractMojo
44  {
45      /**
46       * The project artifact, which should have the LATEST metadata added to it.
47       *
48       * @parameter default-value="${project}"
49       * @required
50       * @readonly
51       */
52      private MavenProject project;
53  
54      /**
55       * The prefix for the plugin goal.
56       *
57       * @parameter
58       */
59      private String goalPrefix;
60  
61      /**
62       * Set this to "true" to skip invoking any goals or reports of the plugin.
63       *
64       * @parameter default-value="false" expression="${maven.plugin.skip}"
65       * @since 2.8
66       */
67      private boolean skip;
68  
69      /** {@inheritDoc} */
70      public void execute()
71          throws MojoExecutionException
72      {
73          if ( skip )
74          {
75              getLog().warn( "Execution skipped" );
76              return;
77          }
78          Artifact projectArtifact = project.getArtifact();
79  
80          Versioning versioning = new Versioning();
81          versioning.setLatest( projectArtifact.getVersion() );
82          versioning.updateTimestamp();
83          ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( projectArtifact, versioning );
84          projectArtifact.addMetadata( metadata );
85  
86          GroupRepositoryMetadata groupMetadata = new GroupRepositoryMetadata( project.getGroupId() );
87          groupMetadata.addPluginMapping( getGoalPrefix(), project.getArtifactId(), project.getName() );
88  
89          projectArtifact.addMetadata( groupMetadata );
90      }
91  
92      /**
93       * @return the goal prefix parameter or the goal prefix from the Plugin artifactId.
94       */
95      private String getGoalPrefix()
96      {
97          if ( goalPrefix == null )
98          {
99              goalPrefix = PluginDescriptor.getGoalPrefixFromArtifactId( project.getArtifactId() );
100         }
101 
102         return goalPrefix;
103     }
104 }