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
.project
;
18 import com
.intellij
.openapi
.application
.ApplicationManager
;
19 import com
.intellij
.openapi
.vfs
.LocalFileSystem
;
20 import gnu
.trove
.THashMap
;
21 import org
.apache
.maven
.artifact
.Artifact
;
22 import org
.jetbrains
.idea
.maven
.embedder
.MavenEmbedderWrapper
;
23 import org
.jetbrains
.idea
.maven
.utils
.MavenConstants
;
24 import org
.jetbrains
.idea
.maven
.utils
.MavenProgressIndicator
;
25 import org
.jetbrains
.idea
.maven
.utils
.MavenProcessCanceledException
;
30 public class MavenArtifactDownloader
{
31 private final MavenEmbedderWrapper myEmbedder
;
32 private final MavenProgressIndicator myProgress
;
33 private final MavenProjectsTree myProjectsTree
;
34 private final List
<MavenProject
> myMavenProjects
;
36 public static void download(MavenProjectsTree projectsTree
,
37 List
<MavenProject
> mavenProjects
,
39 MavenEmbedderWrapper embedder
,
40 MavenProgressIndicator p
) throws MavenProcessCanceledException
{
41 new MavenArtifactDownloader(projectsTree
, mavenProjects
, embedder
, p
).download(demand
);
44 private MavenArtifactDownloader(MavenProjectsTree projectsTree
,
45 List
<MavenProject
> mavenProjects
,
46 MavenEmbedderWrapper embedder
,
47 MavenProgressIndicator p
) {
48 myProjectsTree
= projectsTree
;
49 myMavenProjects
= mavenProjects
;
50 myEmbedder
= embedder
;
54 private void download(boolean demand
) throws MavenProcessCanceledException
{
55 List
<File
> downloadedFiles
= new ArrayList
<File
>();
57 Map
<MavenArtifact
, Set
<MavenRemoteRepository
>> artifacts
= collectArtifactsToDownload();
59 download(MavenConstants
.SOURCES_CLASSIFIER
, artifacts
, downloadedFiles
);
60 download(MavenConstants
.JAVADOC_CLASSIFIER
, artifacts
, downloadedFiles
);
63 scheduleFilesRefresh(downloadedFiles
);
67 private void scheduleFilesRefresh(final List
<File
> downloadedFiles
) {
68 Runnable refreshTask
= new Runnable() {
70 LocalFileSystem
.getInstance().refreshIoFiles(downloadedFiles
);
74 if (ApplicationManager
.getApplication().isUnitTestMode()
75 || ApplicationManager
.getApplication().isDispatchThread()) {
79 ApplicationManager
.getApplication().invokeLater(refreshTask
);
83 private Map
<MavenArtifact
, Set
<MavenRemoteRepository
>> collectArtifactsToDownload() {
84 Map
<MavenArtifact
, Set
<MavenRemoteRepository
>> result
= new THashMap
<MavenArtifact
, Set
<MavenRemoteRepository
>>();
86 for (MavenProject each
: myMavenProjects
) {
87 List
<MavenRemoteRepository
> repositories
= each
.getRemoteRepositories();
89 for (MavenArtifact eachDependency
: each
.getDependencies()) {
90 if (!each
.isSupportedDependency(eachDependency
)) continue;
91 if (Artifact
.SCOPE_SYSTEM
.equalsIgnoreCase(eachDependency
.getScope())) continue;
92 if (myProjectsTree
.findProject(eachDependency
.getMavenId()) != null) continue;
94 Set
<MavenRemoteRepository
> registeredRepositories
= result
.get(eachDependency
);
95 if (registeredRepositories
== null) {
96 registeredRepositories
= new LinkedHashSet
<MavenRemoteRepository
>();
97 result
.put(eachDependency
, registeredRepositories
);
99 registeredRepositories
.addAll(repositories
);
105 private void download(String classifier
,
106 Map
<MavenArtifact
, Set
<MavenRemoteRepository
>> libraryArtifacts
,
107 List
<File
> downloadedFiles
) throws MavenProcessCanceledException
{
108 myProgress
.setText(ProjectBundle
.message("maven.downloading.artifact", classifier
));
111 for (Map
.Entry
<MavenArtifact
, Set
<MavenRemoteRepository
>> eachEntry
: libraryArtifacts
.entrySet()) {
112 MavenArtifact eachArtifact
= eachEntry
.getKey();
114 myProgress
.checkCanceled();
115 myProgress
.setFraction(((double)step
++) / libraryArtifacts
.size());
116 myProgress
.setText2(eachArtifact
.toString());
118 Artifact a
= myEmbedder
.createArtifact(eachArtifact
.getGroupId(),
119 eachArtifact
.getArtifactId(),
120 eachArtifact
.getVersion(),
121 MavenConstants
.TYPE_JAR
,
123 myEmbedder
.resolve(a
, new ArrayList
<MavenRemoteRepository
>(eachEntry
.getValue()));
124 if (a
.isResolved()) downloadedFiles
.add(a
.getFile());