Exemplo 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;
    }
  }
Exemplo n.º 2
0
 private <V> V executeBare(MavenProject project, ICallable<V> callable, IProgressMonitor monitor)
     throws CoreException {
   final MavenSession mavenSession = getSession();
   final FilterRepositorySystemSession repositorySession = getRepositorySession();
   final TransferListener origTransferListener =
       repositorySession.setTransferListener(maven.createArtifactTransferListener(monitor));
   final MavenProject origProject = mavenSession.getCurrentProject();
   final List<MavenProject> origProjects = mavenSession.getProjects();
   try {
     if (project != null) {
       mavenSession.setCurrentProject(project);
       mavenSession.setProjects(Collections.singletonList(project));
     }
     return callable.call(this, monitor);
   } finally {
     repositorySession.setTransferListener(origTransferListener);
     if (project != null) {
       mavenSession.setCurrentProject(origProject);
       mavenSession.setProjects(
           origProjects != null ? origProjects : Collections.<MavenProject>emptyList());
     }
   }
 }