Maven: classpath fix (IDEA-52167)
[fedora-idea.git] / plugins / maven / src / main / java / org / jetbrains / idea / maven / execution / MavenJavaClasspathPolicyExtender.java
blob6af86b1a0e12d78dbf4eb15c8c4e912edf48bc2d
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.jetbrains.idea.maven.execution;
18 import com.intellij.execution.configurations.JavaClasspathPolicyExtender;
19 import com.intellij.openapi.module.Module;
20 import com.intellij.openapi.project.Project;
21 import com.intellij.openapi.roots.*;
22 import org.jetbrains.annotations.NotNull;
23 import org.jetbrains.idea.maven.project.MavenArtifact;
24 import org.jetbrains.idea.maven.project.MavenProject;
25 import org.jetbrains.idea.maven.project.MavenProjectsManager;
26 import org.jetbrains.idea.maven.utils.MavenConstants;
28 import java.util.Collections;
30 public class MavenJavaClasspathPolicyExtender implements JavaClasspathPolicyExtender {
31 @NotNull
32 public ProjectRootsTraversing.RootTraversePolicy extend(Project project, @NotNull ProjectRootsTraversing.RootTraversePolicy policy) {
33 return policy;
36 @NotNull
37 public ProjectRootsTraversing.RootTraversePolicy extend(final Module module, @NotNull ProjectRootsTraversing.RootTraversePolicy policy) {
38 Project project = module.getProject();
39 final MavenProjectsManager manager = MavenProjectsManager.getInstance(project);
40 if (!manager.isMavenizedProject() || !manager.isMavenizedModule(module)) return policy;
42 return new ProjectRootsTraversing.RootTraversePolicy(extend(module, manager, policy.getVisitSource(), false),
43 policy.getVisitJdk(),
44 extend(module, manager, policy.getVisitLibrary(), true),
45 extend(module, manager, policy.getVisitModule(), true));
48 private <T extends OrderEntry> ProjectRootsTraversing.RootTraversePolicy.Visit<T> extend(
49 final Module originalModule,
50 final MavenProjectsManager manager,
51 final ProjectRootsTraversing.RootTraversePolicy.Visit<T> original,
52 final boolean skipDependencyModules) {
54 return new ProjectRootsTraversing.RootTraversePolicy.Visit<T>() {
55 public void visit(T entry, ProjectRootsTraversing.TraverseState state, RootPolicy<ProjectRootsTraversing.TraverseState> policy) {
56 Module ownerModule = entry.getOwnerModule();
57 if (skipDependencyModules && originalModule != ownerModule) return;
59 if (originalModule != ownerModule && entry instanceof ModuleSourceOrderEntry) {
60 MavenProject project = manager.findProject(originalModule);
61 MavenProject depProject = manager.findProject(ownerModule);
63 if (project == null || depProject == null) {
64 original.visit(entry, state, policy);
65 return;
68 for (MavenArtifact each : project.findDependencies(depProject)) {
69 boolean isTestClasspath = original == ProjectClasspathTraversing.ALL_OUTPUTS;
71 if (!isTestClasspath && MavenConstants.SCOPE_PROVIDEED.equals(each.getScope())) continue;
72 if (isTestClasspath || !MavenConstants.SCOPE_TEST.equals(each.getScope())) {
73 addOutput(ownerModule, MavenConstants.TYPE_TEST_JAR.equals(each.getType()), state);
77 else {
78 // should be in some generic place
79 if (entry instanceof ExportableOrderEntry) {
80 boolean isTestClasspath = original == ProjectRootsTraversing.RootTraversePolicy.ADD_CLASSES
81 || original == ProjectRootsTraversing.RootTraversePolicy.RECURSIVE;
82 if (!isTestClasspath && ((ExportableOrderEntry)entry).getScope() == DependencyScope.PROVIDED) return;
84 original.visit(entry, state, policy);
90 public void addOutput(Module module, boolean tests, ProjectRootsTraversing.TraverseState state) {
91 CompilerModuleExtension ex = CompilerModuleExtension.getInstance(module);
92 if (ex == null) return;
94 String output = tests ? ex.getCompilerOutputUrlForTests() : ex.getCompilerOutputUrl();
95 if (output != null) state.addAllUrls(Collections.singletonList(output));