Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 45d360eb3061190b6d05c2d2e2d0f69acba8aa51 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*******************************************************************************
 * Copyright (c) 2008-2017 Sonatype, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *      Sonatype, Inc. - initial API and implementation
 *******************************************************************************/

package org.eclipse.m2e.jdt.internal;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
import org.apache.maven.project.MavenProject;

import org.eclipse.m2e.core.MavenPlugin;
import org.eclipse.m2e.core.embedder.ArtifactKey;
import org.eclipse.m2e.core.project.IMavenProjectFacade;
import org.eclipse.m2e.core.project.IMavenProjectRegistry;
import org.eclipse.m2e.core.project.IProjectConfigurationManager;
import org.eclipse.m2e.core.project.configurator.AbstractProjectConfigurator;
import org.eclipse.m2e.core.project.configurator.ILifecycleMapping;
import org.eclipse.m2e.jdt.IClasspathDescriptor;
import org.eclipse.m2e.jdt.IClasspathEntryDescriptor;
import org.eclipse.m2e.jdt.IClasspathManager;
import org.eclipse.m2e.jdt.IClasspathManagerDelegate;
import org.eclipse.m2e.jdt.IJavaProjectConfigurator;


/**
 * DefaultClasspathManagerDelegate
 * 
 * @author igor
 */
public class DefaultClasspathManagerDelegate implements IClasspathManagerDelegate {
  private final IProjectConfigurationManager configurationManager;

  private final IMavenProjectRegistry projectManager;

  public DefaultClasspathManagerDelegate() {
    this.configurationManager = MavenPlugin.getProjectConfigurationManager();
    this.projectManager = MavenPlugin.getMavenProjectRegistry();
  }

  public void populateClasspath(final IClasspathDescriptor classpath, IMavenProjectFacade projectFacade, final int kind,
      final IProgressMonitor monitor) throws CoreException {

    addClasspathEntries(classpath, projectFacade, kind, monitor);

    for(IJavaProjectConfigurator configurator : getJavaProjectConfigurators(projectFacade, monitor)) {
      configurator.configureClasspath(projectFacade, classpath, monitor);
    }
  }

  private List<IJavaProjectConfigurator> getJavaProjectConfigurators(IMavenProjectFacade projectFacade,
      final IProgressMonitor monitor) throws CoreException {

    ArrayList<IJavaProjectConfigurator> configurators = new ArrayList<IJavaProjectConfigurator>();

    ILifecycleMapping lifecycleMapping = configurationManager.getLifecycleMapping(projectFacade);

    if(lifecycleMapping != null) {
      for(AbstractProjectConfigurator configurator : lifecycleMapping.getProjectConfigurators(projectFacade, monitor)) {
        if(configurator instanceof IJavaProjectConfigurator) {
          configurators.add((IJavaProjectConfigurator) configurator);
        }
      }
    }
    return configurators;
  }

  void addClasspathEntries(IClasspathDescriptor classpath, IMavenProjectFacade facade, int kind,
      IProgressMonitor monitor) throws CoreException {
    ArtifactFilter scopeFilter;

    if(BuildPathManager.CLASSPATH_RUNTIME == kind) {
      // ECLIPSE-33: runtime+provided scope
      // ECLIPSE-85: adding system scope
      scopeFilter = new ArtifactFilter() {
        public boolean include(Artifact artifact) {
          return BuildPathManager.SCOPE_FILTER_RUNTIME.include(artifact)
              || Artifact.SCOPE_PROVIDED.equals(artifact.getScope())
              || Artifact.SCOPE_SYSTEM.equals(artifact.getScope());
        }
      };
    } else {
      // ECLIPSE-33: test scope (already includes provided)
      scopeFilter = BuildPathManager.SCOPE_FILTER_TEST;
    }

    MavenProject mavenProject = facade.getMavenProject(monitor);
    Set<Artifact> artifacts = mavenProject.getArtifacts();

    Map<IPath, ProjectTestAttributes> projectTestAttributes = new HashMap<>(artifacts.size());

    for(Artifact a : artifacts) {
      if(!scopeFilter.include(a) || !a.getArtifactHandler().isAddedToClasspath()) {
        continue;
      }

      // project
      IMavenProjectFacade dependency = projectManager.getMavenProject(a.getGroupId(), a.getArtifactId(),
          a.getBaseVersion());
      if(dependency != null && dependency.getProject().equals(facade.getProject())) {
        continue;
      }

      IClasspathEntryDescriptor entry = null;

      if(dependency != null && dependency.getFullPath(a.getFile()) != null) {
        IPath projectPath = dependency.getFullPath();
        entry = classpath.addProjectEntry(projectPath);
        ProjectTestAttributes testAttributes = projectTestAttributes.get(projectPath);
        if(testAttributes == null) {
          testAttributes = new ProjectTestAttributes(isOnlyVisibleByTests(a), !isTestArtifact(a));
          projectTestAttributes.put(projectPath, testAttributes);
        } else {
          testAttributes.isTest &= isOnlyVisibleByTests(a);
          testAttributes.excludeTestSources &= !isTestArtifact(a);
        }

      } else {
        File artifactFile = a.getFile();
        if(artifactFile != null /*&& artifactFile.canRead()*/) {
          entry = classpath.addLibraryEntry(Path.fromOSString(artifactFile.getAbsolutePath()));
          entry.setClasspathAttribute(IClasspathManager.TEST_ATTRIBUTE, isOnlyVisibleByTests(a) ? "true" : null);
        }
      }

      if(entry != null) {
        entry.setArtifactKey(new ArtifactKey(a.getGroupId(), a.getArtifactId(), a.getBaseVersion(), a.getClassifier()));
        entry.setScope(a.getScope());
        entry.setOptionalDependency(a.isOptional());
      }
    }

    //2nd pass to set project test related attributes
    projectTestAttributes.forEach((entryPath, testAttributes) -> {
      //the classpath definitely has an entry matching the path
      IClasspathEntryDescriptor descriptor = findClasspathDescriptor(classpath, entryPath);
      descriptor.setClasspathAttribute(IClasspathManager.TEST_ATTRIBUTE, (testAttributes.isTest) ? "true" : null);
      descriptor.setClasspathAttribute(IClasspathManager.WITHOUT_TEST_CODE,
          (testAttributes.excludeTestSources) ? "true" : null);
    });
  }

  private boolean isOnlyVisibleByTests(Artifact a) {
    return Artifact.SCOPE_TEST.equals(a.getScope()) || Artifact.SCOPE_RUNTIME.equals(a.getScope());
  }

  private boolean isTestArtifact(Artifact a) {
    return BuildPathManager.CLASSIFIER_TESTS.equals(a.getClassifier()) || "test-jar".equals(a.getType());
  }

  private IClasspathEntryDescriptor findClasspathDescriptor(IClasspathDescriptor classpath, IPath p) {
    return classpath.getEntryDescriptors().stream().filter(e -> p.equals(e.getPath())).findFirst().orElse(null);
  }

  static class ProjectTestAttributes {
    ProjectTestAttributes(boolean isTest, boolean excludeTestSources) {
      this.isTest = isTest;
      this.excludeTestSources = excludeTestSources;
    }

    boolean isTest;

    boolean excludeTestSources;
  }

}

Back to the top