Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: da30c18a4647665e85035a554cdd5372765a3d6b (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*******************************************************************************
 * Copyright (c) 2008-2010 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.core.project;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.osgi.util.NLS;

import org.apache.maven.model.Model;
import org.apache.maven.model.Profile;

import org.eclipse.m2e.core.embedder.MavenModelManager;
import org.eclipse.m2e.core.internal.IMavenConstants;
import org.eclipse.m2e.core.internal.Messages;


/**
 * @author Eugene Kuleshov
 */
public class LocalProjectScanner extends AbstractProjectScanner<MavenProjectInfo> {
  private final File workspaceRoot;

  private final List<String> folders;

  private final boolean basedirRemameRequired;

  private Set<File> scannedFolders = new HashSet<File>();

  private final MavenModelManager modelManager;

  public LocalProjectScanner(File workspaceRoot, String folder, boolean needsRename, MavenModelManager modelManager) {
    this(workspaceRoot, Collections.singletonList(folder), needsRename, modelManager);
  }

  public LocalProjectScanner(File workspaceRoot, List<String> folders, boolean basedirRemameRequired,
      MavenModelManager modelManager) {
    this.workspaceRoot = workspaceRoot;
    this.folders = folders;
    this.basedirRemameRequired = basedirRemameRequired;
    this.modelManager = modelManager;
  }

  public void run(IProgressMonitor monitor) throws InterruptedException {
    SubMonitor subMonitor = SubMonitor.convert(monitor, Messages.LocalProjectScanner_task_scanning, 1);

    subMonitor.beginTask(Messages.LocalProjectScanner_task_scanning, IProgressMonitor.UNKNOWN);
    try {
      for(String folderName : folders) {
        try {
          File folder = new File(folderName).getCanonicalFile();
          scanFolder(folder, "", new SubProgressMonitor(subMonitor, IProgressMonitor.UNKNOWN)); //$NON-NLS-1$
        } catch(IOException ex) {
          addError(ex);
        }
      }
    } finally {
      subMonitor.done();
    }
  }

  private void scanFolder(File baseDir, String rootRelPath, IProgressMonitor monitor) throws InterruptedException {
    if(monitor.isCanceled()) {
      throw new InterruptedException();
    }

    monitor.subTask(baseDir.toString());
    monitor.worked(1);

    // Don't scan the .metadata folder
    if(!baseDir.exists() || !baseDir.isDirectory() || IMavenConstants.METADATA_FOLDER.equals(baseDir.getName())) {
      return;
    }

    try {
      if(scannedFolders.contains(baseDir.getCanonicalFile())) {
        return;
      }
    } catch(IOException ex1) {
      addError(ex1);
      return;
    }

    MavenProjectInfo projectInfo = readMavenProjectInfo(baseDir, rootRelPath, null); //$NON-NLS-1$
    if(projectInfo != null) {
      addProject(projectInfo);
      return; // don't scan subfolders of the Maven project
    }

    File[] files = baseDir.listFiles();
    if(files == null) {
      addError(new Exception(NLS.bind(Messages.LocalProjectScanner_accessDeniedFromFolder, baseDir.getAbsolutePath())));
      return;
    }
    for(int i = 0; i < files.length; i++ ) {
      File file;
      try {
        file = files[i].getCanonicalFile();
        if(file.isDirectory()) {
          scanFolder(file, rootRelPath + "/" + file.getName(), monitor); //$NON-NLS-1$
        }
      } catch(IOException ex) {
        addError(ex);
      }
    }
  }

  private MavenProjectInfo readMavenProjectInfo(File baseDir, String modulePath, MavenProjectInfo parentInfo) {
    try {
      baseDir = baseDir.getCanonicalFile();

      File pomFile = new File(baseDir, IMavenConstants.POM_FILE_NAME);
      if(!pomFile.exists()) {
        return null;
      }

      if(!scannedFolders.add(baseDir)) {
        return null; // we already know this project
        //mkleint: well, if the project is first scanned standalone and later scanned via parent reference, the parent ref gets thrown away??
      }

      Model model = modelManager.readMavenModel(pomFile);

      String pomName = modulePath + "/" + IMavenConstants.POM_FILE_NAME; //$NON-NLS-1$

      MavenProjectInfo projectInfo = newMavenProjectInfo(pomName, pomFile, model, parentInfo);
      //We only want to optionally rename the base directory not any sub directory
      if(parentInfo == null) {
        projectInfo.setBasedirRename(getBasedirRename(projectInfo));
      }

      Map<String, Set<String>> modules = new LinkedHashMap<String, Set<String>>();
      for(String module : model.getModules()) {
        if(module.endsWith("/pom.xml")) { //$NON-NLS-1$
          module = module.substring(0, module.length() - "/pom.xml".length()); //$NON-NLS-1$
        }
        modules.put(module, new HashSet<String>());
      }

      for(Profile profile : model.getProfiles()) {
        for(String module : profile.getModules()) {
          if(module.endsWith("/pom.xml")) { //$NON-NLS-1$
            module = module.substring(0, module.length() - "/pom.xml".length()); //$NON-NLS-1$
          }
          Set<String> profiles = modules.get(module);
          if(profiles == null) {
            profiles = new HashSet<String>();
            modules.put(module, profiles);
          }
          profiles.add(profile.getId());
        }
      }

      for(Map.Entry<String, Set<String>> e : modules.entrySet()) {
        String module = e.getKey();
        Set<String> profiles = e.getValue();

        File moduleBaseDir = new File(baseDir, module);
        MavenProjectInfo moduleInfo = readMavenProjectInfo(moduleBaseDir, module, projectInfo);
        if(moduleInfo != null) {
          moduleInfo.addProfiles(profiles);
          projectInfo.add(moduleInfo);
        }
      }

      return projectInfo;

    } catch(CoreException ex) {
      addError(ex);
    } catch(IOException ex) {
      addError(ex);
    }

    return null;
  }

  protected MavenProjectInfo newMavenProjectInfo(String label, File pomFile, Model model, MavenProjectInfo parent) {
    return new MavenProjectInfo(label, pomFile, model, parent);
  }

  public String getDescription() {
    return folders.toString();
  }

  private int getBasedirRename(MavenProjectInfo mavenProjectInfo) throws IOException {

    if(basedirRemameRequired) {
      return MavenProjectInfo.RENAME_REQUIRED;
    }
    return MavenProjectInfo.RENAME_NO;
  }
}

Back to the top