Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3783cb475fcc217a69b21120a975e73af95d4504 (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
/*******************************************************************************
 * Copyright (c) 2014 Takari, 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:
 *      Takari, Inc. - initial API and implementation
 *******************************************************************************/

package org.eclipse.m2e.core.internal.launch;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.aether.util.StringUtils;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.osgi.util.NLS;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.project.MavenProject;

import org.eclipse.m2e.core.MavenPlugin;
import org.eclipse.m2e.core.embedder.IMavenLauncherConfiguration;
import org.eclipse.m2e.core.embedder.MavenRuntime;
import org.eclipse.m2e.core.internal.IMavenConstants;
import org.eclipse.m2e.core.internal.Messages;
import org.eclipse.m2e.core.project.IMavenProjectFacade;
import org.eclipse.m2e.core.project.IMavenProjectRegistry;


/**
 * @since 1.5
 */
@SuppressWarnings("deprecation")
public abstract class AbstractMavenRuntime implements MavenRuntime {

  private static final VersionRange SUPPORTED_VERSION;

  static {
    VersionRange supportedVersion;
    try {
      supportedVersion = VersionRange.createFromVersionSpec("[3.0,)");
    } catch(InvalidVersionSpecificationException ex) {
      supportedVersion = null;
    }
    SUPPORTED_VERSION = supportedVersion;
  }

  private static final IWorkspaceRoot workspace = ResourcesPlugin.getWorkspace().getRoot();

  private static final IMavenProjectRegistry registry = MavenPlugin.getMavenProjectRegistry();

  private final String name;

  private List<ClasspathEntry> extensions;

  @Deprecated
  protected AbstractMavenRuntime() {
    this.name = null;
  }

  protected AbstractMavenRuntime(String name) {
    this.name = name;
  }

  public String getName() {
    return name != null ? name : getLocation();
  }

  public List<ClasspathEntry> getExtensions() {
    return extensions;
  }

  public void setExtensions(List<ClasspathEntry> extensions) {
    this.extensions = extensions != null && !extensions.isEmpty() ? new ArrayList<ClasspathEntry>(extensions) : null;
  }

  public boolean isLegacy() {
    return name == null;
  }

  protected void collectExtensions(IMavenLauncherConfiguration collector, IProgressMonitor monitor)
      throws CoreException {
    if(extensions != null) {
      for(ClasspathEntry entry : extensions) {
        if(entry instanceof ProjectClasspathEntry) {
          collectProject(collector, (ProjectClasspathEntry) entry, monitor);
        }
      }
    }
  }

  private void collectProject(IMavenLauncherConfiguration collector, ProjectClasspathEntry entry,
      IProgressMonitor monitor) throws CoreException {
    IProject project = workspace.getProject(entry.getProject());
    IMavenProjectFacade facade = registry.create(project, monitor);
    if(facade == null) {
      throw new CoreException(new Status(IStatus.ERROR, IMavenConstants.PLUGIN_ID, NLS.bind(
          Messages.AbstractMavenRuntime_unknownProject, entry.getProject())));
    }
    collector.addProjectEntry(facade);
    MavenProject mavenProject = facade.getMavenProject(monitor);
    for(Artifact dependency : mavenProject.getArtifacts()) {
      if(Artifact.SCOPE_COMPILE.equals(dependency.getScope()) || Artifact.SCOPE_RUNTIME.equals(dependency.getScope())) {
        collector.addArchiveEntry(dependency.getFile().getAbsolutePath());
      }
    }
  }

  public boolean equals(Object o) {
    if(o != null && getClass().equals(o.getClass())) {
      return getName().equals(((AbstractMavenRuntime) o).getName());
    }
    return false;
  }

  public int hashCode() {
    return getName().hashCode();
  }

  protected boolean isSupportedVersion() {
    return SUPPORTED_VERSION != null && SUPPORTED_VERSION.containsVersion(new DefaultArtifactVersion(getVersion()));
  }

  public abstract void createLauncherConfiguration(IMavenLauncherConfiguration collector, IProgressMonitor monitor)
      throws CoreException;

  public abstract String getLocation();

  public abstract boolean isAvailable();

  public abstract boolean isEditable();

  public String getSettings() {
    String settings = MavenPlugin.getMavenConfiguration().getGlobalSettingsFile();
    if(!StringUtils.isEmpty(settings)) {
      try {
        settings = new File(settings).getCanonicalPath();
      } catch(IOException ex) {
      }
    }
    return settings;
  }

  public abstract String getVersion();
}

Back to the top