void backGroundBuild(MavenProject project) throws MojoExecutionException {
    MavenExecutionRequest executionRequest = session.getRequest();

    InvocationRequest request = new DefaultInvocationRequest();
    request.setBaseDirectory(project.getBasedir());
    request.setPomFile(project.getFile());
    request.setGoals(executionRequest.getGoals());
    request.setRecursive(false);
    request.setInteractive(false);

    request.setProfiles(executionRequest.getActiveProfiles());
    request.setProperties(executionRequest.getUserProperties());
    Invoker invoker = new DefaultInvoker();
    try {
      InvocationResult result = invoker.execute(request);
      if (result.getExitCode() != 0) {
        throw new IllegalStateException(
            "Error invoking Maven goals:["
                + StringUtils.join(executionRequest.getGoals(), ", ")
                + "]",
            result.getExecutionException());
      }
    } catch (MavenInvocationException e) {
      throw new IllegalStateException(
          "Error invoking Maven goals:["
              + StringUtils.join(executionRequest.getGoals(), ", ")
              + "]",
          e);
    }
  }
Beispiel #2
0
  public void mavenInstall(IMavenVO iMavenVO) {

    try {

      InvocationRequest request = new DefaultInvocationRequest();

      request.setPomFile(new File(iMavenVO.getMavenProjectPath() + "/pom.xml"));
      request.setGoals(Collections.singletonList("package"));
      PrintStream out =
          new PrintStream(iMavenVO.getMavenLogPath() + "/" + iMavenVO.getLogFileName());
      InvocationOutputHandler outputHandler = new PrintStreamHandler(out, true);
      request.setOutputHandler(outputHandler);

      Invoker invoker = new DefaultInvoker();
      File mvnhome = new File(iMavenVO.getMavenPath());
      invoker.setMavenHome(mvnhome);

      invoker.setOutputHandler(outputHandler);
      InvokerLogger log = invoker.getLogger();
      log.setThreshold(4);
      invoker.setLogger(log);

      InvocationResult result = invoker.execute(request);

      log.info(result.getExitCode() + "");

      moveToFolder(iMavenVO);

    } catch (Exception e) {
      log.info(e.getMessage(), e);
      throw new IMavenException(e.getMessage(), e);
    } finally {

    }
  }
Beispiel #3
0
 @Test
 public void failOnOwaspFailure() throws OwaspDiffRunnerException {
   expectedEx.expect(OwaspDiffRunnerException.class);
   expectedEx.expectMessage(OwaspDiffRunnerException.FOUND_VULNERABILITIES);
   when(mavenBuildResult.getExitCode()).thenReturn(1);
   owaspDiffRunner.main(null);
 }
Beispiel #4
0
  @Before
  public void before() throws Exception {
    fakeRepo = new TemporaryFolder();
    fakeRepo.create();
    File fakeChangedPom = fakeRepo.newFile("pom.xml");
    System.setProperty("maven.repo.local", fakeChangedPom.getParent());
    // Set command line returns
    when(mavenVersionCommandProcess.getInputStream())
        .thenReturn(
            new ByteArrayInputStream(
                ("Maven home: " + fakeRepo.getRoot().getPath()).getBytes(StandardCharsets.UTF_8)));

    when(mavenSettingCommandProcess.getInputStream())
        .thenReturn(
            new ByteArrayInputStream(
                ("<localRepository>" + fakeRepo.getRoot().getPath() + "</localRepository>")
                    .getBytes(StandardCharsets.UTF_8)));

    when(gitShortBranchNameCommandProcess.getInputStream())
        .thenReturn(new ByteArrayInputStream("test-branch".getBytes(StandardCharsets.UTF_8)));

    when(gitDiffNameCommandProcess.getInputStream())
        .thenReturn(
            new ByteArrayInputStream(fakeChangedPom.getPath().getBytes(StandardCharsets.UTF_8)));

    // Set runtime when executing commands
    when(runtime.exec(MAVEN_VERSION_COMMAND)).thenReturn(mavenVersionCommandProcess);

    when(runtime.exec(MAVEN_SETTINGS_COMMAND)).thenReturn(mavenSettingCommandProcess);

    when(runtime.exec(GIT_SHORT_BRANCH_NAME_COMMAND)).thenReturn(gitShortBranchNameCommandProcess);

    when(runtime.exec(matches(GIT_DIFF_NAME_COMMAND + ".*"))).thenReturn(gitDiffNameCommandProcess);

    // Set maven executor
    when(mavenBuildResult.getExitCode()).thenReturn(0);

    when(invoker.execute(any())).thenReturn(mavenBuildResult);

    owaspDiffRunner = new OwaspDiffRunner(runtime, invoker);
  }
Beispiel #5
0
  protected void generateAggregatedZip(
      MavenProject rootProject,
      List<MavenProject> reactorProjects,
      Set<MavenProject> pomZipProjects)
      throws IOException, MojoExecutionException {
    File projectBaseDir = rootProject.getBasedir();
    String rootProjectGroupId = rootProject.getGroupId();
    String rootProjectArtifactId = rootProject.getArtifactId();
    String rootProjectVersion = rootProject.getVersion();

    String aggregatedZipFileName =
        "target/" + rootProjectArtifactId + "-" + rootProjectVersion + "-app.zip";
    File projectOutputFile = new File(projectBaseDir, aggregatedZipFileName);
    getLog()
        .info(
            "Generating "
                + projectOutputFile.getAbsolutePath()
                + " from root project "
                + rootProjectArtifactId);
    File projectBuildDir = new File(projectBaseDir, reactorProjectOutputPath);

    if (projectOutputFile.exists()) {
      projectOutputFile.delete();
    }
    createAggregatedZip(
        projectBaseDir,
        projectBuildDir,
        reactorProjectOutputPath,
        projectOutputFile,
        includeReadMe,
        pomZipProjects);
    if (rootProject.getAttachedArtifacts() != null) {
      // need to remove existing as otherwise we get a WARN
      Artifact found = null;
      for (Artifact artifact : rootProject.getAttachedArtifacts()) {
        if (artifactClassifier != null
            && artifact.hasClassifier()
            && artifact.getClassifier().equals(artifactClassifier)) {
          found = artifact;
          break;
        }
      }
      if (found != null) {
        rootProject.getAttachedArtifacts().remove(found);
      }
    }

    getLog()
        .info(
            "Attaching aggregated zip "
                + projectOutputFile
                + " to root project "
                + rootProject.getArtifactId());
    projectHelper.attachArtifact(rootProject, artifactType, artifactClassifier, projectOutputFile);

    // if we are doing an install goal, then also install the aggregated zip manually
    // as maven will install the root project first, and then build the reactor projects, and at
    // this point
    // it does not help to attach artifact to root project, as those artifacts will not be installed
    // so we need to install manually
    List<String> activeProfileIds = new ArrayList<>();
    List<Profile> activeProfiles = rootProject.getActiveProfiles();
    if (activeProfiles != null) {
      for (Profile profile : activeProfiles) {
        String id = profile.getId();
        if (Strings.isNotBlank(id)) {
          activeProfileIds.add(id);
        }
      }
    }
    if (rootProject.hasLifecyclePhase("install")) {
      getLog().info("Installing aggregated zip " + projectOutputFile);
      InvocationRequest request = new DefaultInvocationRequest();
      request.setBaseDirectory(rootProject.getBasedir());
      request.setPomFile(new File("./pom.xml"));
      request.setGoals(Collections.singletonList("install:install-file"));
      request.setRecursive(false);
      request.setInteractive(false);
      request.setProfiles(activeProfileIds);

      Properties props = new Properties();
      props.setProperty("file", aggregatedZipFileName);
      props.setProperty("groupId", rootProjectGroupId);
      props.setProperty("artifactId", rootProjectArtifactId);
      props.setProperty("version", rootProjectVersion);
      props.setProperty("classifier", "app");
      props.setProperty("packaging", "zip");
      props.setProperty("generatePom", "false");
      request.setProperties(props);

      getLog()
          .info(
              "Installing aggregated zip using: mvn install:install-file"
                  + serializeMvnProperties(props));
      Invoker invoker = new DefaultInvoker();
      try {
        InvocationResult result = invoker.execute(request);
        if (result.getExitCode() != 0) {
          throw new IllegalStateException("Error invoking Maven goal install:install-file");
        }
      } catch (MavenInvocationException e) {
        throw new MojoExecutionException("Error invoking Maven goal install:install-file", e);
      }
    }

    if (rootProject.hasLifecyclePhase("deploy")) {

      if (deploymentRepository == null && Strings.isNullOrBlank(altDeploymentRepository)) {
        String msg =
            "Cannot run deploy phase as Maven project has no <distributionManagement> with the maven url to use for deploying the aggregated zip file, neither an altDeploymentRepository property.";
        getLog().warn(msg);
        throw new MojoExecutionException(msg);
      }

      getLog()
          .info(
              "Deploying aggregated zip "
                  + projectOutputFile
                  + " to root project "
                  + rootProject.getArtifactId());
      getLog()
          .info(
              "Using deploy goal: "
                  + deployFileGoal
                  + " with active profiles: "
                  + activeProfileIds);

      InvocationRequest request = new DefaultInvocationRequest();
      request.setBaseDirectory(rootProject.getBasedir());
      request.setPomFile(new File("./pom.xml"));
      request.setGoals(Collections.singletonList(deployFileGoal));
      request.setRecursive(false);
      request.setInteractive(false);
      request.setProfiles(activeProfileIds);
      request.setProperties(getProject().getProperties());

      Properties props = new Properties();
      props.setProperty("file", aggregatedZipFileName);
      props.setProperty("groupId", rootProjectGroupId);
      props.setProperty("artifactId", rootProjectArtifactId);
      props.setProperty("version", rootProjectVersion);
      props.setProperty("classifier", "app");
      props.setProperty("packaging", "zip");
      String deployUrl = null;

      if (!Strings.isNullOrBlank(deployFileUrl)) {
        deployUrl = deployFileUrl;
      } else if (altDeploymentRepository != null && altDeploymentRepository.contains("::")) {
        deployUrl =
            altDeploymentRepository.substring(altDeploymentRepository.lastIndexOf("::") + 2);
      } else {
        deployUrl = deploymentRepository.getUrl();
      }

      props.setProperty("url", deployUrl);
      props.setProperty("repositoryId", deploymentRepository.getId());
      props.setProperty("generatePom", "false");
      request.setProperties(props);

      getLog()
          .info(
              "Deploying aggregated zip using: mvn deploy:deploy-file"
                  + serializeMvnProperties(props));
      Invoker invoker = new DefaultInvoker();
      try {
        InvocationResult result = invoker.execute(request);
        if (result.getExitCode() != 0) {
          throw new IllegalStateException("Error invoking Maven goal deploy:deploy-file");
        }
      } catch (MavenInvocationException e) {
        throw new MojoExecutionException("Error invoking Maven goal deploy:deploy-file", e);
      }
    }
  }