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  
28  import java.io.File;
29  
30  /**
31   * Sets the main artifact's file. This is the essence of the Maven JAR Plugin and all the other packaging plugins.
32   * Creating the actual file for the main artifact is a specific plugin job and not related to the Maven core.
33   *
34   * @goal set
35   * @phase package
36   *
37   * @author Benjamin Bentmann
38   *
39   */
40  public class SetMojo
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 file to set as the main artifact, relative to the project base directory. The plugin will not
55       * validate this path.
56       *
57       * @parameter property="artifact.mainFile"
58       * @required
59       */
60      private String mainFile;
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] Setting main artifact file: " + mainFile );
71  
72          if ( mainFile == null || mainFile.length() <= 0 )
73          {
74              throw new MojoFailureException( "Path name for main artifact 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 artifactFile = new File( mainFile );
81          if ( !artifactFile.isAbsolute() )
82          {
83              artifactFile = new File( project.getBasedir(), mainFile );
84          }
85  
86          if ( !artifactFile.exists() )
87          {
88              getLog().warn( "[MAVEN-CORE-IT-LOG] Main artifact file does not exist: " + artifactFile );
89          }
90  
91          Artifact artifact = project.getArtifact();
92          artifact.setFile( artifactFile );
93  
94          getLog().info( "[MAVEN-CORE-IT-LOG] Set main artifact file: " + artifactFile );
95      }
96  
97  }