Coverage Report - org.apache.maven.plugins.repository.BundleCreateMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
BundleCreateMojo
78%
36/46
69%
25/36
0
 
 1  
 package org.apache.maven.plugins.repository;
 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.project.MavenProject;
 25  
 import org.apache.maven.settings.Settings;
 26  
 import org.codehaus.plexus.archiver.jar.JarArchiver;
 27  
 import org.codehaus.plexus.components.interactivity.InputHandler;
 28  
 import org.codehaus.plexus.util.StringUtils;
 29  
 
 30  
 import java.io.File;
 31  
 import java.io.IOException;
 32  
 import java.util.List;
 33  
 
 34  
 /**
 35  
  * Goal which creates an upload bundle for a project built with Maven.
 36  
  *
 37  
  * @goal bundle-create
 38  
  * @execute phase="package"
 39  
  * @since 2.0
 40  
  */
 41  12
 public class BundleCreateMojo
 42  
     extends AbstractMojo
 43  
 {
 44  
     public static final String POM = "pom.xml";
 45  
 
 46  
     /**
 47  
      * Output directory.
 48  
      *
 49  
      * @parameter default-value="${project.build.directory}"
 50  
      * @readonly
 51  
      */
 52  
     private File outputDirectory;
 53  
 
 54  
     /**
 55  
      * The current Maven project.
 56  
      *
 57  
      * @parameter default-value="${project}"
 58  
      * @readonly
 59  
      */
 60  
     private MavenProject project;
 61  
     
 62  
     /**
 63  
      * Disable validations to make sure bundle supports project materialization.
 64  
      * <br/>
 65  
      * <b>WARNING: This means your project will be MUCH harder to use.</b>
 66  
      * @parameter expression="${bundle.disableMaterialization}" default-value="false"
 67  
      */
 68  
     private boolean disableMaterialization;
 69  
 
 70  
     /**
 71  
      * Jar archiver.
 72  
      *
 73  
      * @component role="org.codehaus.plexus.archiver.Archiver" roleHint="jar"
 74  
      */
 75  
     private JarArchiver jarArchiver;
 76  
 
 77  
     /**
 78  
      * @component
 79  
      */
 80  
     protected InputHandler inputHandler;
 81  
     
 82  
     /**
 83  
      * @parameter default-value="${settings}"
 84  
      * @readonly
 85  
      */
 86  
     protected Settings settings;
 87  
 
 88  
     public void execute()
 89  
         throws MojoExecutionException
 90  
     {
 91  
         // ----------------------------------------------------------------------
 92  
         // Check the mandatory elements of the POM
 93  
         //
 94  
         // modelVersion
 95  
         // groupId
 96  
         // artifactId
 97  
         // packaging
 98  
         // name
 99  
         // version
 100  
         // description
 101  
         // url
 102  
         // licenses
 103  
         // dependencies
 104  
         // ----------------------------------------------------------------------
 105  
 
 106  
         // We don't have to validate modelVersion, groupId, artifactId or version here,
 107  
         // it is done by DefaultMaven and maven-artifact
 108  
 
 109  10
         validate( project.getName(), "project.name" );
 110  
 
 111  10
         validate( project.getDescription(), "project.description" );
 112  
 
 113  10
         validate( project.getUrl(), "project.url" );
 114  
 
 115  10
         if ( project.getLicenses().isEmpty() )
 116  
         {
 117  0
             throw new MojoExecutionException( "At least one license must be defined." );
 118  
         }
 119  
         
 120  10
         if ( disableMaterialization )
 121  
         {
 122  0
             getLog().warn( "Validations to confirm support for project materialization have been DISABLED." +
 123  
                 "\n\nYour project may not provide the POM elements necessary to allow users to retrieve sources on-demand," +
 124  
                 "\nor to easily checkout your project in an IDE. THIS CAN SERIOUSLY INCONVENIENCE YOUR USERS." +
 125  
                 "\n\nContinue? [y/N]" );
 126  
             
 127  
             try
 128  
             {
 129  0
                 if ( 'y' != inputHandler.readLine().toLowerCase().charAt( 0 ) )
 130  
                 {
 131  0
                     disableMaterialization = false;
 132  
                 }
 133  
             }
 134  0
             catch ( IOException e )
 135  
             {
 136  0
                 getLog().debug( "Error reading confirmation: " + e.getMessage(), e );
 137  0
             }
 138  
             
 139  
         }
 140  
         
 141  10
         if ( !disableMaterialization )
 142  
         {
 143  10
             if ( project.getScm() == null )
 144  
             {
 145  1
                 throw new MojoExecutionException( "You must supply a valid <scm> section, with at least "
 146  
                     + "<url> (viewing URL) and <connection> (read-only tooling connection) specified." );
 147  
             }
 148  
             else
 149  
             {
 150  9
                 validate( project.getScm().getUrl(), "project.scm.url" );
 151  
                 
 152  9
                 validate( project.getScm().getConnection(), "project.scm.connection" );
 153  
             }
 154  
         }
 155  
 
 156  
         // ----------------------------------------------------------------------
 157  
         // Create the bundle archive
 158  
         // ----------------------------------------------------------------------
 159  
 
 160  9
         File pom = project.getFile();
 161  
 
 162  9
         final String finalName = project.getBuild().getFinalName();
 163  
 
 164  9
         boolean batchMode = settings == null ? false : !settings.isInteractiveMode();
 165  9
         List<File> files =
 166  
             BundleUtils.selectProjectFiles( outputDirectory, inputHandler, finalName, pom, getLog(), batchMode );
 167  
 
 168  9
         File bundle = new File( outputDirectory, finalName + "-bundle.jar" );
 169  
 
 170  
         try
 171  
         {
 172  9
             jarArchiver.addFile( pom, POM );
 173  
 
 174  9
             boolean artifactChecks = !"pom".equals( project.getPackaging() );
 175  9
             boolean sourcesFound = false;
 176  9
             boolean javadocsFound = false;
 177  
             
 178  9
             for ( File f : files )
 179  
             {
 180  13
                 if ( artifactChecks && f.getName().endsWith( finalName + "-sources.jar" ) )
 181  
                 {
 182  3
                     sourcesFound = true;
 183  
                 }
 184  10
                 else if ( artifactChecks && f.getName().equals( finalName + "-javadoc.jar" ) )
 185  
                 {
 186  2
                     javadocsFound = true;
 187  
                 }
 188  
                 
 189  13
                 jarArchiver.addFile( f, f.getName() );
 190  
             }
 191  
             
 192  9
             if ( artifactChecks && !sourcesFound )
 193  
             {
 194  5
                 getLog().warn( "Sources not included in upload bundle. In order to add sources please run"
 195  
                     + " \"mvn source:jar javadoc:jar repository:bundle-create\"" );
 196  
             }
 197  
 
 198  9
             if ( artifactChecks && !javadocsFound )
 199  
             {
 200  6
                 getLog().warn( "Javadoc not included in upload bundle. In order to add javadocs please run"
 201  
                     + " \"mvn source:jar javadoc:jar repository:bundle-create\"" );
 202  
             }
 203  
 
 204  9
             jarArchiver.setDestFile( bundle );
 205  
 
 206  9
             jarArchiver.createArchive();
 207  
         }
 208  0
         catch ( Exception e )
 209  
         {
 210  0
             throw new MojoExecutionException( "Error creating upload bundle archive.", e );
 211  9
         }
 212  9
     }
 213  
 
 214  
     private void validate( String data, String expression )
 215  
         throws MojoExecutionException
 216  
     {
 217  48
         if ( StringUtils.isEmpty( data ) )
 218  
         {
 219  0
             throw new MojoExecutionException( expression + " must be present." );
 220  
         }
 221  48
     }
 222  
 }