View Javadoc
1   package org.apache.maven.its.plugins;
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.FileOutputStream;
24  import java.io.IOException;
25  import java.io.OutputStreamWriter;
26  import java.io.Writer;
27  
28  import org.apache.maven.plugin.AbstractMojo;
29  import org.apache.maven.plugin.MojoExecutionException;
30  import org.apache.maven.plugin.MojoFailureException;
31  import org.codehaus.plexus.util.IOUtil;
32  import org.codehaus.plexus.util.xml.Xpp3Dom;
33  import org.codehaus.plexus.util.xml.pull.MXSerializer;
34  import org.codehaus.plexus.util.xml.pull.XmlSerializer;
35  
36  /**
37   * @goal serialize
38   * @phase validate
39   */
40  public class SerializeMojo
41      extends AbstractMojo
42  {
43  
44      /**
45       * @parameter default-value="${project.build.directory}/serialized.xml"
46       */
47      private File file;
48  
49      public void execute()
50          throws MojoExecutionException, MojoFailureException
51      {
52          Writer writer = null;
53          XmlSerializer s = new MXSerializer();
54          try
55          {
56              file.getParentFile().mkdirs();
57              writer = new OutputStreamWriter( new FileOutputStream( file ), "UTF-8" );
58              s.setOutput( writer );
59  
60              Xpp3Dom dom = new Xpp3Dom( "root" );
61  
62              dom.writeToSerializer( "", s );
63          }
64          catch ( IOException e )
65          {
66              throw new MojoExecutionException( e.getMessage(), e );
67          }
68          finally
69          {
70              IOUtil.close( writer );
71          }
72      }
73  }