View Javadoc

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          catch ( IOException e )
92          {
93              throw new BuildException( "Error writing temporary pom file: " + e.getMessage(), e );
94          }
95          finally
96          {
97              IOUtil.close( fw );
98          }
99      }
100 
101     public void setPomRefId( String pomRefId )
102     {
103         this.pomRefId = pomRefId;
104     }
105 
106     public String getPomRefId()
107     {
108         return pomRefId;
109     }
110 
111     public void setFile( File file )
112     {
113         this.file = file;
114     }
115 
116     public File getFile()
117     {
118         return file;
119     }
120 
121     public void setTrim( boolean trim )
122     {
123         this.trim = trim;
124     }
125 
126     public boolean isTrim()
127     {
128         return trim;
129     }
130 }