Save This Page
Home » maven-ant-tasks-2.1.0-src » org.apache » maven » artifact » ant » [javadoc | source]
    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   public class WritePomTask
   39       extends Task
   40   {
   41       private String pomRefId;
   42   
   43       private File file;
   44       
   45       private boolean trim = true;
   46   
   47       public void execute()
   48       {
   49           // check valid configuration
   50           Pom pom = (Pom) getProject().getReference( pomRefId );
   51           Model model = pom.getModel();
   52           if ( trim )
   53           {
   54               trimModel ( model );            
   55           }
   56           writeModel ( model, file );
   57       }
   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           model.setBuild( null );
   66           model.setReporting( null );
   67           model.setProperties( null );
   68           model.setRepositories( null );
   69           model.setPluginRepositories( null );
   70           model.setProfiles( null );
   71           model.setDistributionManagement( null );
   72           model.setModules( null );
   73       }
   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           Writer fw = null;
   86           try
   87           {
   88               fw = WriterFactory.newXmlWriter( outputFile );
   89               new MavenXpp3Writer().write( fw, model );
   90   
   91           }
   92           catch ( IOException e )
   93           {
   94               throw new BuildException( "Error writing temporary pom file: " + e.getMessage(), e );
   95           }
   96           finally
   97           {
   98               IOUtil.close( fw );
   99           }
  100       }
  101   
  102       public void setPomRefId( String pomRefId )
  103       {
  104           this.pomRefId = pomRefId;
  105       }
  106   
  107       public String getPomRefId()
  108       {
  109           return pomRefId;
  110       }
  111   
  112       public void setFile( File file )
  113       {
  114           this.file = file;
  115       }
  116   
  117       public File getFile()
  118       {
  119           return file;
  120       }
  121   
  122       public void setTrim( boolean trim )
  123       {
  124           this.trim = trim;
  125       }
  126   
  127       public boolean isTrim()
  128       {
  129           return trim;
  130       }
  131   }

Save This Page
Home » maven-ant-tasks-2.1.0-src » org.apache » maven » artifact » ant » [javadoc | source]