Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
AddPluginArtifactMetadataMojo |
|
| 2.5;2.5 |
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.plugins.annotations.Component; | |
30 | import org.apache.maven.plugins.annotations.LifecyclePhase; | |
31 | import org.apache.maven.plugins.annotations.Mojo; | |
32 | import org.apache.maven.plugins.annotations.Parameter; | |
33 | import org.apache.maven.project.MavenProject; | |
34 | ||
35 | /** | |
36 | * Inject any plugin-specific | |
37 | * <a href="/ref/current/maven-repository-metadata/repository-metadata.html">artifact metadata</a> to the project's | |
38 | * artifact, for subsequent installation and deployment. | |
39 | * It is used: | |
40 | * <ol> | |
41 | * <li>to add the <code>latest</code> metadata (which is plugin-specific) for shipping alongside the plugin's artifact</li> | |
42 | * <li>to define plugin mapping in the group</li> | |
43 | * </ol> | |
44 | * | |
45 | * @see ArtifactRepositoryMetadata | |
46 | * @see GroupRepositoryMetadata | |
47 | * @version $Id: AddPluginArtifactMetadataMojo.java 1345787 2012-06-03 21:58:22Z hboutemy $ | |
48 | * @since 2.0 | |
49 | */ | |
50 | @Mojo( name = "addPluginArtifactMetadata", defaultPhase = LifecyclePhase.PACKAGE, threadSafe = true ) | |
51 | 0 | public class AddPluginArtifactMetadataMojo |
52 | extends AbstractMojo | |
53 | { | |
54 | /** | |
55 | * The project artifact, which should have the <code>latest</code> metadata added to it. | |
56 | */ | |
57 | @Component | |
58 | private MavenProject project; | |
59 | ||
60 | /** | |
61 | * The prefix for the plugin goal. | |
62 | */ | |
63 | @Parameter | |
64 | private String goalPrefix; | |
65 | ||
66 | /** | |
67 | * Set this to "true" to skip invoking any goals or reports of the plugin. | |
68 | * | |
69 | * @since 2.8 | |
70 | */ | |
71 | @Parameter( defaultValue = "false", property = "maven.plugin.skip" ) | |
72 | private boolean skip; | |
73 | ||
74 | /** {@inheritDoc} */ | |
75 | public void execute() | |
76 | throws MojoExecutionException | |
77 | { | |
78 | 0 | if ( skip ) |
79 | { | |
80 | 0 | getLog().warn( "Execution skipped" ); |
81 | 0 | return; |
82 | } | |
83 | 0 | Artifact projectArtifact = project.getArtifact(); |
84 | ||
85 | 0 | Versioning versioning = new Versioning(); |
86 | 0 | versioning.setLatest( projectArtifact.getVersion() ); |
87 | 0 | versioning.updateTimestamp(); |
88 | 0 | ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( projectArtifact, versioning ); |
89 | 0 | projectArtifact.addMetadata( metadata ); |
90 | ||
91 | 0 | GroupRepositoryMetadata groupMetadata = new GroupRepositoryMetadata( project.getGroupId() ); |
92 | 0 | groupMetadata.addPluginMapping( getGoalPrefix(), project.getArtifactId(), project.getName() ); |
93 | ||
94 | 0 | projectArtifact.addMetadata( groupMetadata ); |
95 | 0 | } |
96 | ||
97 | /** | |
98 | * @return the goal prefix parameter or the goal prefix from the Plugin artifactId. | |
99 | */ | |
100 | private String getGoalPrefix() | |
101 | { | |
102 | 0 | if ( goalPrefix == null ) |
103 | { | |
104 | 0 | goalPrefix = PluginDescriptor.getGoalPrefixFromArtifactId( project.getArtifactId() ); |
105 | } | |
106 | ||
107 | 0 | return goalPrefix; |
108 | } | |
109 | } |