View Javadoc
1   package org.apache.maven.plugin.coreit;
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.plugin.MojoFailureException;
26  import org.apache.maven.project.MavenProject;
27  import org.apache.maven.project.artifact.ProjectArtifactMetadata;
28  
29  import java.io.File;
30  
31  /**
32   * Attaches a POM to the main artifact.
33   *
34   * @goal attach-pom
35   * @phase package
36   *
37   * @author Benjamin Bentmann
38   *
39   */
40  public class AttachPomMojo
41      extends AbstractMojo
42  {
43  
44      /**
45       * The current Maven project.
46       *
47       * @parameter default-value="${project}"
48       * @readonly
49       * @required
50       */
51      private MavenProject project;
52  
53      /**
54       * The path to the POM file to attach to the main artifact, relative to the project base directory. The plugin will
55       * not validate this path.
56       *
57       * @parameter property="artifact.pomFile" default-value="${project.file.path}"
58       * @required
59       */
60      private String pomFile;
61  
62      /**
63       * Runs this mojo.
64       *
65       * @throws MojoFailureException If the artifact file has not been set.
66       */
67      public void execute()
68          throws MojoExecutionException, MojoFailureException
69      {
70          getLog().info( "[MAVEN-CORE-IT-LOG] Attaching POM to main artifact: " + pomFile );
71  
72          if ( pomFile == null || pomFile.length() <= 0 )
73          {
74              throw new MojoFailureException( "Path name for POM file has not been specified" );
75          }
76  
77          /*
78           * NOTE: We do not want to test path translation here, so resolve relative paths manually.
79           */
80          File metadataFile = new File( pomFile );
81          if ( !metadataFile.isAbsolute() )
82          {
83              metadataFile = new File( project.getBasedir(), pomFile );
84          }
85  
86          Artifact artifact = project.getArtifact();
87          artifact.addMetadata( new ProjectArtifactMetadata( artifact, metadataFile ) );
88  
89          getLog().info( "[MAVEN-CORE-IT-LOG] Attached POM to main artifact: " + metadataFile );
90      }
91  
92  }