public void testConfigureRequestProject() throws Exception {
    Properties props = new Properties();
    InvokerProperties facade = new InvokerProperties(props);

    InvocationRequest request = new DefaultInvocationRequest();

    File tempPom = File.createTempFile("maven-invoker-plugin-test", ".pom");
    File tempDir = tempPom.getParentFile();

    request.setBaseDirectory(tempDir);
    facade.configureInvocation(request, 0);
    assertEquals(tempDir, request.getBaseDirectory());
    assertEquals(null, request.getPomFile());

    props.setProperty("invoker.project", tempPom.getName());
    request.setBaseDirectory(tempDir);
    facade.configureInvocation(request, 0);
    assertEquals(tempDir, request.getBaseDirectory());
    assertEquals(tempPom, request.getPomFile());

    props.setProperty("invoker.project", "");
    request.setBaseDirectory(tempDir);
    facade.configureInvocation(request, 0);
    assertEquals(tempDir, request.getBaseDirectory());
    assertEquals(null, request.getPomFile());

    tempPom.delete();
  }
示例#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 {

    }
  }
  public void testConfigureRequestFailureBehavior() throws Exception {
    Properties props = new Properties();
    InvokerProperties facade = new InvokerProperties(props);

    InvocationRequest request = new DefaultInvocationRequest();

    request.setFailureBehavior("fail-at-end");
    facade.configureInvocation(request, 0);
    assertEquals("fail-at-end", request.getFailureBehavior());

    props.setProperty("invoker.failureBehavior", "fail-never");
    facade.configureInvocation(request, 0);
    assertEquals("fail-never", request.getFailureBehavior());
  }
  public void testConfigureRequestMavenOpts() throws Exception {
    Properties props = new Properties();
    InvokerProperties facade = new InvokerProperties(props);

    InvocationRequest request = new DefaultInvocationRequest();

    request.setMavenOpts("default");
    facade.configureInvocation(request, 0);
    assertEquals("default", request.getMavenOpts());

    props.setProperty("invoker.mavenOpts", "-Xmx512m");
    facade.configureInvocation(request, 0);
    assertEquals("-Xmx512m", request.getMavenOpts());
  }
示例#5
0
  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);
    }
  }
  public void testConfigureRequestGoals() throws Exception {
    Properties props = new Properties();
    InvokerProperties facade = new InvokerProperties(props);

    InvocationRequest request = new DefaultInvocationRequest();

    request.setGoals(Collections.singletonList("test"));
    facade.configureInvocation(request, 0);
    assertEquals(Collections.singletonList("test"), request.getGoals());

    props.setProperty("invoker.goals", "verify");
    facade.configureInvocation(request, 0);
    assertEquals(Collections.singletonList("verify"), request.getGoals());

    props.setProperty("invoker.goals", "   ");
    facade.configureInvocation(request, 0);
    assertEquals(Arrays.asList(new String[0]), request.getGoals());

    props.setProperty("invoker.goals", "  clean , test   verify  ");
    facade.configureInvocation(request, 0);
    assertEquals(Arrays.asList(new String[] {"clean", "test", "verify"}), request.getGoals());

    props.setProperty("invoker.goals", "");
    facade.configureInvocation(request, 0);
    assertEquals(Arrays.asList(new String[0]), request.getGoals());
  }
  public void testConfigureRequestOffline() throws Exception {
    Properties props = new Properties();
    InvokerProperties facade = new InvokerProperties(props);

    InvocationRequest request = new DefaultInvocationRequest();

    request.setOffline(true);
    facade.configureInvocation(request, 0);
    assertTrue(request.isOffline());
    request.setOffline(false);
    facade.configureInvocation(request, 0);
    assertFalse(request.isOffline());

    props.setProperty("invoker.offline", "true");
    facade.configureInvocation(request, 0);
    assertTrue(request.isOffline());

    props.setProperty("invoker.offline", "false");
    facade.configureInvocation(request, 0);
    assertFalse(request.isOffline());
  }
示例#8
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);
      }
    }
  }