@Override
  public void execute() throws MojoExecutionException, MojoFailureException {
    try {
      if (!project.isExecutionRoot()) {
        getLog().info("Not the execution root so ignoring this project");
        return;
      }
      buildDir.mkdirs();

      for (MavenProject reactorProject : reactorProjects) {
        // ignore the execution root which just aggregates stuff
        if (!reactorProject.isExecutionRoot()) {
          combineProfilesTo(reactorProject, buildDir);
        }
      }
      Zips.createZipFile(getLog(), buildDir, outputFile);

      projectHelper.attachArtifact(project, artifactType, artifactClassifier, outputFile);

      String relativePath = Files.getRelativePath(project.getBasedir(), outputFile);
      while (relativePath.startsWith("/")) {
        relativePath = relativePath.substring(1);
      }
      getLog().info("Created profile zip file: " + relativePath);
    } catch (MojoExecutionException e) {
      throw e;
    } catch (Exception e) {
      throw new MojoExecutionException("Error executing", e);
    }
  }
Example #2
0
 protected static String getChildProjectRelativePath(File projectBaseDir, MavenProject pomProject)
     throws IOException {
   // must include first dir as prefix
   String root = projectBaseDir.getName();
   String relativePath = Files.getRelativePath(projectBaseDir, pomProject.getBasedir());
   relativePath = root + File.separator + relativePath;
   return relativePath;
 }
  protected Iterable<PipelineDTO> getPipelines(UIContext context, boolean filterPipelines) {
    Set<String> builders = null;
    ProjectOverviewDTO projectOveriew = null;
    if (filterPipelines) {
      projectOveriew = getProjectOverview(context);
      builders = projectOveriew.getBuilders();
    }
    File dir = getJenkinsWorkflowFolder(context);
    Set<String> buildersFound = new HashSet<>();
    if (dir != null) {
      Filter<File> filter =
          new Filter<File>() {
            @Override
            public boolean matches(File file) {
              return file.isFile() && Objects.equal(JENKINSFILE, file.getName());
            }
          };
      Set<File> files = Files.findRecursive(dir, filter);
      List<PipelineDTO> pipelines = new ArrayList<>();
      for (File file : files) {
        try {
          String relativePath = Files.getRelativePath(dir, file);
          String value = Strings.stripPrefix(relativePath, "/");
          String label = value;
          String postfix = "/" + JENKINSFILE;
          if (label.endsWith(postfix)) {
            label = label.substring(0, label.length() - postfix.length());
          }
          if (label.startsWith(jenkinsFilePrefix)) {
            label = label.substring(jenkinsFilePrefix.length());
          }
          // Lets ignore the fabric8 specific pipelines
          if (label.startsWith("fabric8-release/")) {
            continue;
          }
          String builder = null;
          int idx = label.indexOf("/");
          if (idx > 0) {
            builder = label.substring(0, idx);
            if (filterPipelines && !builders.contains(builder)) {
              // ignore this builder
              continue;
            } else {
              buildersFound.add(builder);
            }
          }
          String descriptionMarkdown = null;
          File markdownFile = new File(file.getParentFile(), "ReadMe.md");
          if (Files.isFile(markdownFile)) {
            descriptionMarkdown = IOHelpers.readFully(markdownFile);
          }
          PipelineDTO pipeline = new PipelineDTO(value, label, builder, descriptionMarkdown);

          File yamlFile = new File(file.getParentFile(), "metadata.yml");
          if (Files.isFile(yamlFile)) {
            PipelineMetadata metadata = null;
            try {
              metadata = loadYaml(yamlFile, PipelineMetadata.class);
            } catch (IOException e) {
              LOG.warn("Failed to parse yaml file " + yamlFile + ". " + e, e);
            }
            if (metadata != null) {
              metadata.configurePipeline(pipeline);
            }
          }
          pipelines.add(pipeline);
        } catch (IOException e) {
          LOG.warn(
              "Failed to find relative path for folder " + dir + " and file " + file + ". " + e, e);
        }
      }
      if (buildersFound.size() == 1) {
        // lets trim the builder prefix from the labels
        for (String first : buildersFound) {
          String prefix = first + "/";
          for (PipelineDTO pipeline : pipelines) {
            String label = pipeline.getLabel();
            if (label.startsWith(prefix)) {
              label = label.substring(prefix.length());
              pipeline.setLabel(label);
            }
          }
          break;
        }
      }
      Collections.sort(pipelines);
      return pipelines;
    } else {
      LOG.warn("No jenkinsWorkflowFolder!");
      return new ArrayList<>();
    }
  }
Example #4
0
  protected void createAggregatedZip(
      File projectBaseDir,
      File projectBuildDir,
      String reactorProjectOutputPath,
      File projectOutputFile,
      boolean includeReadMe,
      Set<MavenProject> pomZipProjects)
      throws IOException {
    projectBuildDir.mkdirs();

    for (MavenProject reactorProject : pomZipProjects) {
      // ignoreProject the execution root which just aggregates stuff
      if (!reactorProject.isExecutionRoot()) {
        Log log = getLog();

        // TODO allow the project nesting to be defined via a property?
        String relativePath = getChildProjectRelativePath(projectBaseDir, reactorProject);
        File outDir = new File(projectBuildDir, relativePath);
        combineAppFilesToFolder(reactorProject, outDir, log, reactorProjectOutputPath);
      }
    }

    // we may want to include readme files for pom projects
    if (includeReadMe) {

      Map<String, File> pomNames = new HashMap<String, File>();

      for (MavenProject pomProject : pomZipProjects) {
        File src = pomProject.getFile().getParentFile();
        String relativePath = getChildProjectRelativePath(projectBaseDir, pomProject);
        File outDir = new File(projectBuildDir, relativePath);
        File copiedFile = copyReadMe(src, outDir);

        if (copiedFile != null) {
          String key = getReadMeFileKey(relativePath);
          pomNames.put(key, copiedFile);
        }
      }

      if (replaceReadmeLinksPrefix != null) {

        // now parse each readme file and replace github links
        for (Map.Entry<String, File> entry : pomNames.entrySet()) {
          File file = entry.getValue();
          String key = entry.getKey();

          boolean changed = false;
          List<String> lines = Files.readLines(file);
          for (int i = 0; i < lines.size(); i++) {
            String line = lines.get(i);
            String newLine = replaceGithubLinks(pomNames.keySet(), key, line);
            if (newLine != null) {
              lines.set(i, newLine);
              changed = true;
            }
          }
          if (changed) {
            Files.writeLines(file, lines);
            getLog().info("Replaced github links to fabric apps in reaadme file: " + file);
          }
        }
      }
    }

    Zips.createZipFile(getLog(), projectBuildDir, projectOutputFile, null);
    String relativePath = Files.getRelativePath(projectBaseDir, projectOutputFile);
    while (relativePath.startsWith("/")) {
      relativePath = relativePath.substring(1);
    }
    getLog().info("Created app zip file: " + relativePath);
  }