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.plugin.AbstractMojo;
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.apache.maven.plugin.MojoFailureException;
25  import org.apache.maven.project.MavenProject;
26  import org.apache.maven.project.MavenProjectHelper;
27  
28  import java.io.File;
29  
30  /**
31   * Attaches a secondary artifact to the current project. This mimics source/javadoc attachments or other assemblies.
32   *
33   * @goal attach
34   * @phase package
35   *
36   * @author Benjamin Bentmann
37   *
38   */
39  public class AttachMojo
40      extends AbstractMojo
41  {
42  
43      /**
44       * The current Maven project.
45       *
46       * @parameter default-value="${project}"
47       * @readonly
48       * @required
49       */
50      private MavenProject project;
51  
52      /**
53       * The Maven project helper.
54       *
55       * @component
56       */
57      private MavenProjectHelper helper;
58  
59      /**
60       * The path to the file to attach, relative to the project base directory. The plugin will not validate this path.
61       *
62       * @parameter property="artifact.attachedFile"
63       * @required
64       */
65      private String attachedFile;
66  
67      /**
68       * The type of the artifact to attach.
69       *
70       * @parameter property="artifact.artifactType"
71       */
72      private String artifactType;
73  
74      /**
75       * The classifier for the attached artifact. If unspecified, the default classifier for the specified artifact type
76       * is used.
77       *
78       * @parameter property="artifact.artifactClassifier"
79       */
80      private String artifactClassifier;
81  
82      /**
83       * Runs this mojo.
84       *
85       * @throws MojoFailureException If the attached file has not been set.
86       */
87      public void execute()
88          throws MojoExecutionException, MojoFailureException
89      {
90          getLog().info( "[MAVEN-CORE-IT-LOG] Attaching artifact file: " + attachedFile );
91          getLog().info( "[MAVEN-CORE-IT-LOG] type=" + artifactType + ", classifier=" + artifactClassifier );
92  
93          if ( attachedFile == null || attachedFile.length() <= 0 )
94          {
95              throw new MojoFailureException( "Path name for attached artifact file has not been specified" );
96          }
97  
98          /*
99           * NOTE: We do not want to test path translation here, so resolve relative paths manually.
100          */
101         File artifactFile = new File( attachedFile );
102         if ( !artifactFile.isAbsolute() )
103         {
104             artifactFile = new File( project.getBasedir(), attachedFile );
105         }
106 
107         if ( !artifactFile.exists() )
108         {
109             getLog().warn( "[MAVEN-CORE-IT-LOG] Attached artifact file does not exist: " + artifactFile );
110         }
111 
112         helper.attachArtifact( project, artifactType, artifactClassifier, artifactFile );
113 
114         getLog().info( "[MAVEN-CORE-IT-LOG] Attached artifact file: " + artifactFile );
115     }
116 
117 }