Coverage Report - org.apache.maven.artifact.ant.WritePomTask
 
Classes in this File Line Coverage Branch Coverage Complexity
WritePomTask
0%
0/34
0%
0/2
1.333
 
 1  
 package org.apache.maven.artifact.ant;
 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 java.io.File;
 23  
 import java.io.IOException;
 24  
 import java.io.Writer;
 25  
 
 26  
 import org.apache.maven.model.Model;
 27  
 import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
 28  
 import org.apache.tools.ant.BuildException;
 29  
 import org.apache.tools.ant.Task;
 30  
 import org.codehaus.plexus.util.IOUtil;
 31  
 import org.codehaus.plexus.util.WriterFactory;
 32  
 
 33  
 /**
 34  
  * Write a POM to a file.
 35  
  *
 36  
  * @since 2.1.0
 37  
  */
 38  0
 public class WritePomTask
 39  
     extends Task
 40  
 {
 41  
     private String pomRefId;
 42  
 
 43  
     private File file;
 44  
 
 45  0
     private boolean trim = true;
 46  
 
 47  
     public void execute()
 48  
     {
 49  
         // check valid configuration
 50  0
         Pom pom = (Pom) getProject().getReference( pomRefId );
 51  0
         Model model = pom.getModel();
 52  0
         if ( trim )
 53  
         {
 54  0
             trimModel ( model );
 55  
         }
 56  0
         writeModel( model, file );
 57  0
     }
 58  
 
 59  
     /**
 60  
      * Removes a lot of unnecessary information from the POM.
 61  
      * This includes the build section, reporting, repositories, etc.
 62  
      */
 63  
     public void trimModel( Model model )
 64  
     {
 65  0
         model.setBuild( null );
 66  0
         model.setReporting( null );
 67  0
         model.setProperties( null );
 68  0
         model.setRepositories( null );
 69  0
         model.setPluginRepositories( null );
 70  0
         model.setProfiles( null );
 71  0
         model.setDistributionManagement( null );
 72  0
         model.setModules( null );
 73  0
     }
 74  
 
 75  
     /**
 76  
      * Write a POM model to a file
 77  
      *
 78  
      * @param model
 79  
      * @return
 80  
      * @throws MojoExecutionException
 81  
      */
 82  
     public void writeModel( Model model, File outputFile )
 83  
         throws BuildException
 84  
     {
 85  0
         Writer fw = null;
 86  
         try
 87  
         {
 88  0
             fw = WriterFactory.newXmlWriter( outputFile );
 89  0
             new MavenXpp3Writer().write( fw, model );
 90  
         }
 91  0
         catch ( IOException e )
 92  
         {
 93  0
             throw new BuildException( "Error writing temporary pom file: " + e.getMessage(), e );
 94  
         }
 95  
         finally
 96  
         {
 97  0
             IOUtil.close( fw );
 98  0
         }
 99  0
     }
 100  
 
 101  
     public void setPomRefId( String pomRefId )
 102  
     {
 103  0
         this.pomRefId = pomRefId;
 104  0
     }
 105  
 
 106  
     public String getPomRefId()
 107  
     {
 108  0
         return pomRefId;
 109  
     }
 110  
 
 111  
     public void setFile( File file )
 112  
     {
 113  0
         this.file = file;
 114  0
     }
 115  
 
 116  
     public File getFile()
 117  
     {
 118  0
         return file;
 119  
     }
 120  
 
 121  
     public void setTrim( boolean trim )
 122  
     {
 123  0
         this.trim = trim;
 124  0
     }
 125  
 
 126  
     public boolean isTrim()
 127  
     {
 128  0
         return trim;
 129  
     }
 130  
 }