/**
  * Writes a StringBuilder into a file.
  *
  * @param outFile The file to read.
  * @param input The contents of the file.
  * @throws IOException when things go wrong.
  */
 protected final void writeFile(File outFile, StringBuilder input) throws IOException {
   Writer writer = WriterFactory.newXmlWriter(outFile);
   try {
     IOUtil.copy(input.toString(), writer);
   } finally {
     IOUtil.close(writer);
   }
 }
  /**
   * Write a DOM-Tree to a target file.
   *
   * @param target File to write to.
   * @param dom dom to write to file.
   * @throws IOException in case of unexpected writer exceptions.
   */
  private void writeDomToTarget(final File target, final Xpp3Dom dom) throws IOException {
    if (getLog().isDebugEnabled())
      getLog().debug("writing: " + dom.getName() + " to: " + target.getAbsoluteFile());

    final XmlStreamWriter writer = WriterFactory.newXmlWriter(target);
    final PrettyPrintXMLWriter pretty = new PrettyPrintXMLWriter(writer);
    Xpp3DomWriter.write(pretty, dom);
    writer.close();
    if (getLog().isDebugEnabled())
      getLog().debug(dom.getName() + " written to: " + target.getAbsoluteFile());
  }
  /**
   * Saves the {@link WebappStructure} to the specified file.
   *
   * @param webappStructure the structure to save
   * @param targetFile the file to use to save the structure
   * @throws IOException if an error occurred while saving the webapp structure
   */
  public void toXml(WebappStructure webappStructure, File targetFile) throws IOException {
    // CHECKSTYLE_OFF: LineLength
    Writer writer = null;
    try {
      if (!targetFile.getParentFile().exists() && !targetFile.getParentFile().mkdirs()) {
        throw new IOException(
            "Could not create parent [" + targetFile.getParentFile().getAbsolutePath() + "]");
      }

      if (!targetFile.exists() && !targetFile.createNewFile()) {
        throw new IOException("Could not create file [" + targetFile.getAbsolutePath() + "]");
      }
      writer = WriterFactory.newXmlWriter(targetFile);
      XSTREAM.toXML(webappStructure, writer);
    } finally {
      IOUtil.close(writer);
    }
    // CHECKSTYLE_ON: LineLength
  }
Ejemplo n.º 4
0
  /**
   * Create an eclipse launch configuration file for the specified test
   *
   * @param test the GWTTestCase
   * @param testSrc the source directory where the test lives
   * @throws MojoExecutionException some error occured
   */
  private void createLaunchConfigurationForGwtTestCase(File testSrc, String test)
      throws MojoExecutionException {
    File testFile = new File(testSrc, test);

    String fqcn = test.replace(File.separatorChar, '.').substring(0, test.lastIndexOf('.'));
    File launchFile = new File(getProject().getBasedir(), fqcn + ".launch");
    if (launchFile.exists() && launchFile.lastModified() > testFile.lastModified()) {
      return;
    }

    Configuration cfg = new Configuration();
    cfg.setClassForTemplateLoading(EclipseTestMojo.class, "");

    Map<String, Object> context = new HashMap<String, Object>();
    List<String> sources = new LinkedList<String>();
    sources.addAll(executedProject.getTestCompileSourceRoots());
    sources.addAll(executedProject.getCompileSourceRoots());
    context.put("sources", sources);
    context.put("test", fqcn);
    int basedir = getProject().getBasedir().getAbsolutePath().length();
    context.put("out", testOutputDirectory.getAbsolutePath().substring(basedir + 1));
    context.put("extraJvmArgs", getExtraJvmArgs());
    context.put("project", eclipseUtil.getProjectName(getProject()));

    try {
      //            context.put( "gwtDevJarPath", getGwtDevJar().getAbsolutePath() );
      Writer configWriter = WriterFactory.newXmlWriter(launchFile);
      Template template = cfg.getTemplate("test-launch.fm", "UTF-8");
      template.process(context, configWriter);
      configWriter.flush();
      configWriter.close();
      getLog().info("Write launch configuration for GWT test : " + launchFile.getAbsolutePath());
    } catch (IOException ioe) {
      throw new MojoExecutionException("Unable to write launch configuration", ioe);
    } catch (TemplateException te) {
      throw new MojoExecutionException("Unable to merge freemarker template", te);
    }
  }
  /**
   * Write the POM file corresponding to a given maven file
   *
   * @param mvnFile the maven file
   * @param file the pom file to be written to
   * @throws MojoExecutionException when any issue occurs
   */
  private void writePomFile(MavenFile mvnFile, File file) throws MojoExecutionException {
    Model model = mvnFile.getModel();

    model.setDescription(
        "POM file generated by maven-mavenizer-plugin for "
            + mvnFile.getFile().getName()
            + " from archive "
            + getArchiveFile().getName());
    if (getArchiveURL() != null) {
      model.setUrl(getArchiveURL().toString());
    }

    Writer writer = null;
    try {
      writer = WriterFactory.newXmlWriter(file);
      new MavenXpp3Writer().write(writer, model);
    } catch (IOException e) {
      throw new MojoExecutionException(
          "Error writing " + file.getAbsolutePath() + " : " + e.getMessage(), e);
    } finally {
      IOUtil.close(writer);
    }
  }