Beispiel #1
0
  private void maven(String[] goals) {
    if (goals == null) {
      return;
    }

    MavenEmbedder maven = new MavenEmbedder();
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    maven.setClassLoader(classLoader);
    maven.setLogger(new MavenEmbedderConsoleLogger());
    for (String goal : goals) {
      try {
        maven.start();
        File targetDirectory = new File(System.getProperty("user.dir"), "work");
        File pomFile = new File(targetDirectory, "pom.xml");
        MavenProject pom;
        pom = maven.readProjectWithDependencies(pomFile);
        EventMonitor eventMonitor =
            new DefaultEventMonitor(new PlexusLoggerAdapter(new MavenEmbedderConsoleLogger()));
        maven.execute(
            pom,
            Collections.singletonList(goal),
            eventMonitor,
            new ConsoleDownloadMonitor(),
            null,
            targetDirectory);
      } catch (MavenEmbedderException e) {
        e.printStackTrace();
      } catch (ArtifactResolutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (ArtifactNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (ProjectBuildingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (CycleDetectedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (LifecycleExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (BuildFailureException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (DuplicateProjectException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
  }
  /** @param xstreamObject not null */
  private void addAlias(XStream xstreamObject) {
    try {
      addAlias(xstreamObject, getMavenModelJarFile(), "org.apache.maven.model");
      addAlias(xstreamObject, getMavenSettingsJarFile(), "org.apache.maven.settings");
    } catch (MojoExecutionException e) {
      if (getLog().isDebugEnabled()) {
        getLog().debug("MojoExecutionException: " + e.getMessage(), e);
      }
    } catch (ArtifactResolutionException e) {
      if (getLog().isDebugEnabled()) {
        getLog().debug("ArtifactResolutionException: " + e.getMessage(), e);
      }
    } catch (ArtifactNotFoundException e) {
      if (getLog().isDebugEnabled()) {
        getLog().debug("ArtifactNotFoundException: " + e.getMessage(), e);
      }
    } catch (ProjectBuildingException e) {
      if (getLog().isDebugEnabled()) {
        getLog().debug("ProjectBuildingException: " + e.getMessage(), e);
      }
    }

    // TODO need to handle specific Maven objects like DefaultArtifact?
  }
Beispiel #3
0
  @NotNull
  protected List<ProjectBuildingResult> getProjectBuildingResults(
      @NotNull MavenExecutionRequest request, @NotNull Collection<File> files) {
    final ProjectBuilder builder = getComponent(ProjectBuilder.class);

    CustomMaven3ModelInterpolator2 modelInterpolator =
        (CustomMaven3ModelInterpolator2) getComponent(ModelInterpolator.class);

    String savedLocalRepository = modelInterpolator.getLocalRepository();
    modelInterpolator.setLocalRepository(request.getLocalRepositoryPath().getAbsolutePath());
    List<ProjectBuildingResult> buildingResults = new SmartList<ProjectBuildingResult>();

    final ProjectBuildingRequest projectBuildingRequest = request.getProjectBuildingRequest();
    projectBuildingRequest.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
    projectBuildingRequest.setResolveDependencies(false);
    try {
      buildingResults = builder.build(new ArrayList<File>(files), false, projectBuildingRequest);
    } catch (ProjectBuildingException e) {
      for (ProjectBuildingResult result : e.getResults()) {
        if (result.getProject() != null) {
          buildingResults.add(result);
        } else {
          try {
            ProjectBuildingResult build =
                builder.build(result.getPomFile(), projectBuildingRequest);
            buildingResults.add(build);
          } catch (ProjectBuildingException e2) {
            buildingResults.addAll(e2.getResults());
          }
        }
      }
    } finally {
      modelInterpolator.setLocalRepository(savedLocalRepository);
    }
    return buildingResults;
  }
  protected void addDependencySet(
      final DependencySet dependencySet,
      final Archiver archiver,
      final AssemblerConfigurationSource configSource)
      throws AssemblyFormattingException, ArchiveCreationException,
          InvalidAssemblerConfigurationException {
    logger.debug("Processing DependencySet (output=" + dependencySet.getOutputDirectory() + ")");

    if (!dependencySet.isUseTransitiveDependencies() && dependencySet.isUseTransitiveFiltering()) {
      logger.warn(
          "DependencySet has nonsensical configuration: useTransitiveDependencies == false "
              + "AND useTransitiveFiltering == true. Transitive filtering flag will be ignored.");
    }

    final Set<Artifact> dependencyArtifacts = resolveDependencyArtifacts(dependencySet);

    boolean filterContents = false;
    final UnpackOptions opts = dependencySet.getUnpackOptions();
    if (dependencySet.isUnpack()
        && opts != null
        && (opts.isFiltered() || opts.getLineEnding() != null)) {
      filterContents = true;
    } else if (dependencyArtifacts.size() > 1) {
      checkMultiArtifactOutputConfig(dependencySet);
    }

    logger.debug("Adding " + dependencyArtifacts.size() + " dependency artifacts.");

    for (final Iterator<Artifact> j = dependencyArtifacts.iterator(); j.hasNext(); ) {
      final Artifact depArtifact = j.next();

      MavenProject depProject;
      try {
        depProject =
            projectBuilder.buildFromRepository(
                depArtifact,
                configSource.getRemoteRepositories(),
                configSource.getLocalRepository());
      } catch (final ProjectBuildingException e) {
        logger.debug(
            "Error retrieving POM of module-dependency: "
                + depArtifact.getId()
                + "; Reason: "
                + e.getMessage()
                + "\n\nBuilding stub project instance.");

        depProject = buildProjectStub(depArtifact);
      }

      if (NON_ARCHIVE_DEPENDENCY_TYPES.contains(depArtifact.getType())) {
        addNonArchiveDependency(depArtifact, depProject, dependencySet, archiver, configSource);
      } else {
        if (filterContents) {
          addFilteredUnpackedArtifact(
              dependencySet, depArtifact, depProject, archiver, configSource);
        } else {
          addNormalArtifact(dependencySet, depArtifact, depProject, archiver, configSource);
        }
      }
    }
  }