Esempio n. 1
0
  protected void filter(File sourceFile, File targetFile) throws MojoExecutionException {
    try {

      if (StringUtils.isEmpty(encoding)) {
        getLog()
            .warn(
                "File encoding has not been set, using platform encoding "
                    + ReaderFactory.FILE_ENCODING
                    + ", i.e. build is platform dependent!");
      }
      targetFile.getParentFile().mkdirs();

      final MavenResourcesExecution mre = new MavenResourcesExecution();
      mre.setMavenProject(project);
      mre.setMavenSession(mavenSession);
      mre.setFilters(null);
      mre.setEscapedBackslashesInFilePath(true);
      final LinkedHashSet<String> delimiters = new LinkedHashSet<>();
      delimiters.add("${*}");
      mre.setDelimiters(delimiters);

      @SuppressWarnings("rawtypes")
      List filters = mavenFileFilter.getDefaultFilterWrappers(mre);
      mavenFileFilter.copyFile(sourceFile, targetFile, true, filters, encoding, true);
    } catch (MavenFilteringException e) {
      throw new MojoExecutionException(e.getMessage(), e);
    }
  }
Esempio n. 2
0
  private void copyAndInterpolate(File sourceDir, File destinationDir)
      throws MojoExecutionException {
    destinationDir.mkdirs();

    getLog().debug("Copying and interpolating dir '" + sourceDir + "'");
    try {
      DirectoryScanner scanner = new DirectoryScanner();

      scanner.setBasedir(sourceDir);
      scanner.addDefaultExcludes();
      scanner.scan();

      String[] files = scanner.getIncludedFiles();
      for (String file : files) {
        String extension = FileUtils.getExtension(file);

        File source = new File(sourceDir, file);

        File dest = new File(destinationDir, file);
        dest.getParentFile().mkdirs();

        if (Arrays.asList("zip", "jar", "tar.gz").contains(extension)) {
          // just copy know binaries
          FileUtils.copyFile(source, dest);
        } else {
          mavenFileFilter.copyFile(source, dest, true, project, null, false, "UTF-8", session);
        }
      }
    } catch (MavenFilteringException e) {
      throw new MojoExecutionException("Failed to copy : " + sourceDir, e);
    } catch (IOException e) {
      throw new MojoExecutionException("Failed to copy : " + sourceDir, e);
    }
  }
Esempio n. 3
0
  private void merge(File originalFile, File extraContentFile, String type)
      throws MojoFailureException, MojoExecutionException {
    InputStream originalReader = null;
    InputStream extraContentReader = null;
    OutputStream originalWriter = null;
    try {
      String name = FileUtils.removeExtension(extraContentFile.getName());
      String extension = FileUtils.getExtension(extraContentFile.getName());

      if ("properties".equals(type)) {
        File tempFile = File.createTempFile(name, extension);
        mavenFileFilter.copyFile(
            extraContentFile, tempFile, true, project, null, true, "UTF-8", session);

        originalReader = new FileInputStream(originalFile);
        extraContentReader = new FileInputStream(tempFile);

        Properties original = new Properties();
        original.load(originalReader);
        IOUtil.close(originalReader);

        originalWriter = new FileOutputStream(originalFile);

        Properties extra = new Properties();
        extra.load(extraContentReader);
        IOUtil.close(extraContentReader);

        for (Object key : extra.keySet()) {
          original.put(key, extra.get(key));
        }

        original.store(originalWriter, "Updated by EnvironmentMojo");
      } else {
        throw new MojoFailureException("Invalid file type: " + type);
      }
    } catch (Exception e) {
      throw new MojoExecutionException(
          "Error merging files: Original '"
              + originalFile.getAbsolutePath()
              + "', extraContent '"
              + extraContentFile.getAbsolutePath()
              + "'.",
          e);
    } finally {
      IOUtil.close(originalReader);
      IOUtil.close(extraContentReader);
      IOUtil.close(originalWriter);
    }
  }
Esempio n. 4
0
  private void copyUrl(String sourceUrl, File destinationFile) throws MojoExecutionException {
    getLog().debug("Copying url '" + sourceUrl + "'");

    String name = FileUtils.removeExtension(FileUtils.removePath(sourceUrl, '/'));
    String extension = FileUtils.getExtension(sourceUrl);

    try {
      destinationFile.getParentFile().mkdirs();
      destinationFile.createNewFile();

      File tempFile = File.createTempFile(name, extension);
      FileUtils.copyStreamToFile(
          new RawInputStreamFacade(getClass().getResourceAsStream(sourceUrl)), tempFile);
      mavenFileFilter.copyFile(
          tempFile, destinationFile, true, project, null, true, "UTF-8", session);
      tempFile.delete();
    } catch (Exception e) {
      throw new MojoExecutionException(
          "Unable to copy resouce " + sourceUrl + " to " + name + "." + extension, e);
    }
  }