Ejemplo n.º 1
0
  public <V> V execute(MavenProject project, ICallable<V> callable, IProgressMonitor monitor)
      throws CoreException {
    Deque<MavenExecutionContext> stack = threadLocal.get();
    if (stack == null) {
      stack = new ArrayDeque<MavenExecutionContext>();
      threadLocal.set(stack);
    }
    final MavenExecutionContext parent = stack.peek();

    if (this == parent) {
      // shortcut the setup logic, this is nested invocation of the same context
      return executeBare(project, callable, monitor);
    }

    // remember original configuration to "pop" the session stack properly
    final MavenExecutionRequest origRequest = request;
    final Map<String, Object> origContext = context;

    if (request == null && parent != null) {
      this.request = parent.request;
      this.context = new HashMap<String, Object>(parent.context);
    } else {
      this.context = new HashMap<String, Object>();
      if (request == null) {
        request = newExecutionRequest();
      }
      maven.populateDefaults(request);
      populateSystemProperties(request);
      setValue(CTX_LOCALREPOSITORY, request.getLocalRepository());
      final FilterRepositorySystemSession repositorySession =
          maven.createRepositorySession(request);
      setValue(CTX_REPOSITORYSESSION, repositorySession);
      if (parent != null) {
        repositorySession.setData(parent.getRepositorySession().getData());
      }
      final MavenExecutionResult result = new DefaultMavenExecutionResult();
      setValue(
          CTX_MAVENSESSION,
          new MavenSession(maven.getPlexusContainer(), repositorySession, request, result));
    }

    final LegacySupport legacySupport = maven.lookup(LegacySupport.class);
    final MavenSession origLegacySession =
        legacySupport.getSession(); // TODO validate == origSession

    stack.push(this);
    legacySupport.setSession(getSession());
    try {
      return executeBare(project, callable, monitor);
    } finally {
      stack.pop();
      if (stack.isEmpty()) {
        threadLocal.set(null); // TODO decide if this is useful
      }
      legacySupport.setSession(origLegacySession);
      request = origRequest;
      context = origContext;
    }
  }
  public void executeWithMavenSession(MavenExecutionRequest request, Runnable runnable) {
    DefaultMaven maven = (DefaultMaven) getComponent(Maven.class);
    RepositorySystemSession repositorySession = maven.newRepositorySession(request);

    request.getProjectBuildingRequest().setRepositorySession(repositorySession);

    MavenSession mavenSession =
        new MavenSession(
            myContainer, repositorySession, request, new DefaultMavenExecutionResult());
    LegacySupport legacySupport = getComponent(LegacySupport.class);

    MavenSession oldSession = legacySupport.getSession();

    legacySupport.setSession(mavenSession);
    try {
      runnable.run();
    } finally {
      legacySupport.setSession(oldSession);
    }
  }