public UpdateResult updateVersion() {
    List<File> changedPoms = new ArrayList<File>();
    List<String> errors = new ArrayList<String>();
    for (ReleasableModule module : reactor.getModulesInBuildOrder()) {
      try {
        MavenProject project = module.getProject();
        if (module.willBeReleased()) {
          log.info("Going to release " + module.getArtifactId() + " " + module.getNewVersion());
        }

        List<String> errorsForCurrentPom = alterModel(project, module.getNewVersion());
        errors.addAll(errorsForCurrentPom);

        File pom = project.getFile().getCanonicalFile();
        changedPoms.add(pom);
        Writer fileWriter = new FileWriter(pom);

        Model originalModel = project.getOriginalModel();
        try {
          MavenXpp3Writer pomWriter = new MavenXpp3Writer();
          pomWriter.write(fileWriter, originalModel);
        } finally {
          fileWriter.close();
        }
      } catch (Exception e) {
        return new UpdateResult(changedPoms, errors, e);
      }
    }
    return new UpdateResult(changedPoms, errors, null);
  }
Пример #2
0
 private String createDefaultPOM() {
   MavenXpp3Writer writer = new MavenXpp3Writer();
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   org.apache.maven.project.MavenProject mavenProject =
       new org.apache.maven.project.MavenProject();
   mavenProject.setModelVersion("4.0.0");
   try {
     writer.write(baos, mavenProject.getModel());
     return baos.toString();
   } catch (IOException e) {
     // Should not happen
     throw new RuntimeException("Failed to create default pom.xml", e);
   }
 }
Пример #3
0
 @Override
 public void setModel(final Model pom) {
   MavenXpp3Writer writer = new MavenXpp3Writer();
   ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
   MavenModelResource modelResource = getModelResource();
   try (Writer fw = new OutputStreamWriter(outputStream)) {
     writer.write(fw, pom);
     modelResource.setContents(outputStream.toString());
   } catch (IOException e) {
     throw new RuntimeException(
         "Could not write POM file: " + modelResource.getFullyQualifiedName(), e);
   } finally {
     buildManager.evictFromCache(modelResource);
   }
 }
Пример #4
0
  /**
   * @param expression the user expression asked.
   * @throws MojoExecutionException if any
   * @throws MojoFailureException if any reflection exceptions occur or missing components.
   */
  private void handleResponse(String expression)
      throws MojoExecutionException, MojoFailureException {
    StringBuilder response = new StringBuilder();

    Object obj;
    try {
      obj = getEvaluator().evaluate(expression);
    } catch (ExpressionEvaluationException e) {
      throw new MojoExecutionException("Error when evaluating the Maven expression", e);
    }

    if (obj != null && expression.equals(obj.toString())) {
      getLog().warn("The Maven expression was invalid. Please use a valid expression.");
      return;
    }

    // handle null
    if (obj == null) {
      response.append("null object or invalid expression");
    }
    // handle primitives objects
    else if (obj instanceof String) {
      response.append(obj.toString());
    } else if (obj instanceof Boolean) {
      response.append(obj.toString());
    } else if (obj instanceof Byte) {
      response.append(obj.toString());
    } else if (obj instanceof Character) {
      response.append(obj.toString());
    } else if (obj instanceof Double) {
      response.append(obj.toString());
    } else if (obj instanceof Float) {
      response.append(obj.toString());
    } else if (obj instanceof Integer) {
      response.append(obj.toString());
    } else if (obj instanceof Long) {
      response.append(obj.toString());
    } else if (obj instanceof Short) {
      response.append(obj.toString());
    }
    // handle specific objects
    else if (obj instanceof File) {
      File f = (File) obj;
      response.append(f.getAbsolutePath());
    }
    // handle Maven pom object
    else if (obj instanceof MavenProject) {
      MavenProject projectAsked = (MavenProject) obj;
      StringWriter sWriter = new StringWriter();
      MavenXpp3Writer pomWriter = new MavenXpp3Writer();
      try {
        pomWriter.write(sWriter, projectAsked.getModel());
      } catch (IOException e) {
        throw new MojoExecutionException("Error when writing pom", e);
      }

      response.append(sWriter.toString());
    }
    // handle Maven Settings object
    else if (obj instanceof Settings) {
      Settings settingsAsked = (Settings) obj;
      StringWriter sWriter = new StringWriter();
      SettingsXpp3Writer settingsWriter = new SettingsXpp3Writer();
      try {
        settingsWriter.write(sWriter, settingsAsked);
      } catch (IOException e) {
        throw new MojoExecutionException("Error when writing settings", e);
      }

      response.append(sWriter.toString());
    } else {
      // others Maven objects
      response.append(toXML(expression, obj));
    }

    getLog().info("\n" + response.toString());
  }