Lines 42-47
Link Here
|
42 |
|
42 |
|
43 |
package org.netbeans.modules.maven.actions; |
43 |
package org.netbeans.modules.maven.actions; |
44 |
|
44 |
|
|
|
45 |
import java.util.Set; |
46 |
import org.apache.maven.model.building.ModelBuildingRequest; |
47 |
import org.apache.maven.model.License; |
48 |
import org.netbeans.api.annotations.common.NonNull; |
45 |
import org.codehaus.plexus.util.FileUtils; |
49 |
import org.codehaus.plexus.util.FileUtils; |
46 |
import java.awt.event.ActionEvent; |
50 |
import java.awt.event.ActionEvent; |
47 |
import java.io.File; |
51 |
import java.io.File; |
Lines 51-69
Link Here
|
51 |
import java.net.URL; |
55 |
import java.net.URL; |
52 |
import java.util.ArrayList; |
56 |
import java.util.ArrayList; |
53 |
import java.util.HashMap; |
57 |
import java.util.HashMap; |
|
|
58 |
import java.util.HashSet; |
54 |
import java.util.Iterator; |
59 |
import java.util.Iterator; |
55 |
import java.util.List; |
60 |
import java.util.List; |
56 |
import java.util.Map; |
61 |
import java.util.Map; |
57 |
import java.util.logging.Level; |
62 |
import java.util.logging.Level; |
58 |
import java.util.logging.Logger; |
63 |
import java.util.logging.Logger; |
59 |
import javax.swing.AbstractAction; |
64 |
import javax.swing.AbstractAction; |
|
|
65 |
import javax.swing.JEditorPane; |
66 |
import javax.swing.JScrollPane; |
60 |
import javax.swing.SwingUtilities; |
67 |
import javax.swing.SwingUtilities; |
61 |
import org.apache.maven.artifact.Artifact; |
68 |
import org.apache.maven.artifact.Artifact; |
|
|
69 |
import org.apache.maven.artifact.repository.ArtifactRepository; |
70 |
import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy; |
71 |
import org.apache.maven.artifact.repository.MavenArtifactRepository; |
72 |
import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout; |
73 |
import org.apache.maven.project.DefaultProjectBuildingRequest; |
62 |
import org.netbeans.api.annotations.common.SuppressWarnings; |
74 |
import org.netbeans.api.annotations.common.SuppressWarnings; |
63 |
import org.netbeans.modules.maven.embedder.MavenEmbedder; |
75 |
import org.netbeans.modules.maven.embedder.MavenEmbedder; |
64 |
import org.apache.maven.project.MavenProject; |
76 |
import org.apache.maven.project.MavenProject; |
65 |
import org.apache.maven.shared.dependency.tree.DependencyNode; |
77 |
import org.apache.maven.shared.dependency.tree.DependencyNode; |
66 |
import org.netbeans.api.annotations.common.CheckForNull; |
|
|
67 |
import org.netbeans.api.progress.ProgressHandle; |
78 |
import org.netbeans.api.progress.ProgressHandle; |
68 |
import org.netbeans.api.progress.ProgressHandleFactory; |
79 |
import org.netbeans.api.progress.ProgressHandleFactory; |
69 |
import org.netbeans.api.project.libraries.LibrariesCustomizer; |
80 |
import org.netbeans.api.project.libraries.LibrariesCustomizer; |
Lines 72-84
Link Here
|
72 |
import org.netbeans.modules.maven.api.FileUtilities; |
83 |
import org.netbeans.modules.maven.api.FileUtilities; |
73 |
import org.netbeans.modules.maven.embedder.EmbedderFactory; |
84 |
import org.netbeans.modules.maven.embedder.EmbedderFactory; |
74 |
import org.netbeans.modules.maven.embedder.exec.ProgressTransferListener; |
85 |
import org.netbeans.modules.maven.embedder.exec.ProgressTransferListener; |
|
|
86 |
import org.netbeans.modules.maven.indexer.api.RepositoryInfo; |
87 |
import org.netbeans.modules.maven.indexer.api.RepositoryPreferences; |
75 |
import org.netbeans.spi.java.project.support.JavadocAndSourceRootDetection; |
88 |
import org.netbeans.spi.java.project.support.JavadocAndSourceRootDetection; |
76 |
import org.netbeans.spi.project.libraries.support.LibrariesSupport; |
89 |
import org.netbeans.spi.project.libraries.support.LibrariesSupport; |
77 |
import org.openide.DialogDescriptor; |
90 |
import org.openide.DialogDescriptor; |
78 |
import org.openide.DialogDisplayer; |
91 |
import org.openide.DialogDisplayer; |
|
|
92 |
import org.openide.NotifyDescriptor; |
79 |
import org.openide.filesystems.FileObject; |
93 |
import org.openide.filesystems.FileObject; |
80 |
import org.openide.filesystems.FileUtil; |
94 |
import org.openide.filesystems.FileUtil; |
81 |
import org.openide.util.Exceptions; |
|
|
82 |
import org.openide.util.ImageUtilities; |
95 |
import org.openide.util.ImageUtilities; |
83 |
import org.openide.util.Lookup; |
96 |
import org.openide.util.Lookup; |
84 |
import org.openide.util.LookupEvent; |
97 |
import org.openide.util.LookupEvent; |
Lines 123-131
Link Here
|
123 |
if (DialogDisplayer.getDefault().notify(dd) == DialogDescriptor.OK_OPTION) { |
136 |
if (DialogDisplayer.getDefault().notify(dd) == DialogDescriptor.OK_OPTION) { |
124 |
RequestProcessor.getDefault().post(new Runnable() { |
137 |
RequestProcessor.getDefault().post(new Runnable() { |
125 |
public @Override void run() { |
138 |
public @Override void run() { |
|
|
139 |
try { |
126 |
Library lib = createLibrary(pnl.getLibraryManager(), pnl.getLibraryName(), pnl.getIncludeArtifacts(), pnl.isAllSourceAndJavadoc(), project, pnl.getCopyDirectory()); |
140 |
Library lib = createLibrary(pnl.getLibraryManager(), pnl.getLibraryName(), pnl.getIncludeArtifacts(), pnl.isAllSourceAndJavadoc(), project, pnl.getCopyDirectory()); |
127 |
if (lib != null) { |
|
|
128 |
LibrariesCustomizer.showCustomizer(lib, pnl.getLibraryManager()); |
141 |
LibrariesCustomizer.showCustomizer(lib, pnl.getLibraryManager()); |
|
|
142 |
} catch (ThreadDeath d) { |
143 |
// download canceled; ignore |
144 |
} catch (Exception x) { |
145 |
Logger.getLogger(CreateLibraryAction.class.getName()).log(Level.INFO, "could not download " + pnl.getLibraryName(), x); |
129 |
} |
146 |
} |
130 |
} |
147 |
} |
131 |
}); |
148 |
}); |
Lines 140-161
Link Here
|
140 |
}); |
157 |
}); |
141 |
} |
158 |
} |
142 |
|
159 |
|
|
|
160 |
private static final Set<String> acceptedLicenses = new HashSet<String>(); |
161 |
|
143 |
@Messages({ |
162 |
@Messages({ |
144 |
"MSG_Create_Library=Create Library", |
163 |
"MSG_Create_Library=Create Library", |
|
|
164 |
"MSG_Downloading_pom=Maven: downloading POM for {0}", |
165 |
"MSG_Downloading_license=Maven: downloading license for {0}", |
166 |
"TITLE_accept_license=Accept License for {0}", |
145 |
"MSG_Downloading=Maven: downloading {0}", |
167 |
"MSG_Downloading=Maven: downloading {0}", |
146 |
"MSG_Downloading_javadoc=Maven: downloading Javadoc {0}", |
168 |
"MSG_Downloading_javadoc=Maven: downloading Javadoc {0}", |
147 |
"MSG_Downloading_sources=Maven: downloading sources {0}" |
169 |
"MSG_Downloading_sources=Maven: downloading sources {0}" |
148 |
}) |
170 |
}) |
149 |
@SuppressWarnings("RV_RETURN_VALUE_IGNORED_BAD_PRACTICE") // baseFolder.mkdirs; will throw IOE later from getJarUri |
171 |
@SuppressWarnings("RV_RETURN_VALUE_IGNORED_BAD_PRACTICE") // baseFolder.mkdirs; will throw IOE later from getJarUri |
150 |
private static @CheckForNull Library createLibrary(LibraryManager libraryManager, String libraryName, List<Artifact> includeArtifacts, boolean allSourceAndJavadoc, MavenProject project, String copyTo) { |
172 |
static @NonNull Library createLibrary(LibraryManager libraryManager, String libraryName, List<Artifact> includeArtifacts, boolean allSourceAndJavadoc, MavenProject project, String copyTo) throws Exception { |
151 |
ProgressHandle handle = ProgressHandleFactory.createHandle(MSG_Create_Library(), |
173 |
ProgressHandle handle = ProgressHandleFactory.createHandle(MSG_Create_Library(), |
152 |
ProgressTransferListener.cancellable()); |
174 |
ProgressTransferListener.cancellable()); |
153 |
int count = includeArtifacts.size() * (allSourceAndJavadoc ? 3 : 1) + 5; |
175 |
int count = includeArtifacts.size() * (allSourceAndJavadoc ? 4 : 2) + 1; |
154 |
handle.start(count); |
176 |
handle.start(count); |
155 |
try { |
177 |
try { |
156 |
MavenEmbedder online = EmbedderFactory.getOnlineEmbedder(); |
|
|
157 |
int index = 1; |
178 |
int index = 1; |
158 |
List<Artifact> failed = new ArrayList<Artifact>(); |
|
|
159 |
List<URI> classpathVolume = new ArrayList<URI>(); |
179 |
List<URI> classpathVolume = new ArrayList<URI>(); |
160 |
List<URI> javadocVolume = new ArrayList<URI>(); |
180 |
List<URI> javadocVolume = new ArrayList<URI>(); |
161 |
List<URI> sourceVolume = new ArrayList<URI>(); |
181 |
List<URI> sourceVolume = new ArrayList<URI>(); |
Lines 167-181
Link Here
|
167 |
URL libRoot = libraryManager.getLocation(); |
187 |
URL libRoot = libraryManager.getLocation(); |
168 |
File base = null; |
188 |
File base = null; |
169 |
if (libRoot != null) { |
189 |
if (libRoot != null) { |
170 |
try { |
|
|
171 |
base = new File(libRoot.toURI()); |
190 |
base = new File(libRoot.toURI()); |
172 |
//getLocation() points to a file |
191 |
//getLocation() points to a file |
173 |
base = base.getParentFile(); |
192 |
base = base.getParentFile(); |
174 |
nonDefaultLibBase = base; |
193 |
nonDefaultLibBase = base; |
175 |
} catch (URISyntaxException ex) { |
|
|
176 |
Exceptions.printStackTrace(ex); |
177 |
base = new File(System.getProperty("netbeans.user"), "libraries"); |
178 |
} |
179 |
} else { |
194 |
} else { |
180 |
base = new File(System.getProperty("netbeans.user"), "libraries"); |
195 |
base = new File(System.getProperty("netbeans.user"), "libraries"); |
181 |
} |
196 |
} |
Lines 188-246
Link Here
|
188 |
volumes.put("javadoc", javadocVolume); //NOI18N |
203 |
volumes.put("javadoc", javadocVolume); //NOI18N |
189 |
volumes.put("src", sourceVolume); //NOI18N |
204 |
volumes.put("src", sourceVolume); //NOI18N |
190 |
} |
205 |
} |
|
|
206 |
List<ArtifactRepository> remoteArtifactRepositories; |
207 |
if (project != null) { |
208 |
remoteArtifactRepositories = project.getRemoteArtifactRepositories(); |
209 |
} else { |
210 |
remoteArtifactRepositories = new ArrayList<ArtifactRepository>(); |
211 |
for (RepositoryInfo ri : RepositoryPreferences.getInstance().getRepositoryInfos()) { |
212 |
if (!ri.isLocal()) { |
213 |
remoteArtifactRepositories.add(new MavenArtifactRepository(ri.getId(), ri.getRepositoryUrl(), new DefaultRepositoryLayout(), new ArtifactRepositoryPolicy(), new ArtifactRepositoryPolicy())); |
214 |
} |
215 |
} |
216 |
} |
217 |
MavenEmbedder online = EmbedderFactory.getOnlineEmbedder(); |
218 |
ArtifactRepository localRepository = online.getLocalRepository(); |
191 |
for (Artifact a : includeArtifacts) { |
219 |
for (Artifact a : includeArtifacts) { |
192 |
handle.progress(MSG_Downloading(a.getId()), index); |
220 |
Artifact pom = online.createArtifact(a.getGroupId(), a.getArtifactId(), a.getVersion(), "pom"); // NOI18N |
193 |
try { |
221 |
handle.progress(MSG_Downloading_pom(a.getId()), index++); |
194 |
online.resolve(a, project.getRemoteArtifactRepositories(), online.getLocalRepository()); |
222 |
online.resolve(pom, remoteArtifactRepositories, localRepository); |
|
|
223 |
if (pom.getFile().exists()) { |
224 |
DefaultProjectBuildingRequest dpbr = new DefaultProjectBuildingRequest(); |
225 |
dpbr.setLocalRepository(localRepository); |
226 |
dpbr.setRemoteRepositories(remoteArtifactRepositories); |
227 |
dpbr.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL); |
228 |
dpbr.setSystemProperties(online.getSystemProperties()); |
229 |
MavenProject pomPrj = online.buildProject(pom, dpbr).getProject(); |
230 |
List<License> licenses = pomPrj.getLicenses(); |
231 |
if (!licenses.isEmpty()) { |
232 |
String licenseURL = licenses.get(0).getUrl(); |
233 |
if (licenseURL != null && acceptedLicenses.add(licenseURL)) { |
234 |
handle.progress(MSG_Downloading_license(a.getId()), index); |
235 |
// XXX looks terrible for http://opensource.org/*.txt licenses; have contacted webmaster |
236 |
JEditorPane licensePane = new JEditorPane(licenseURL); |
237 |
licensePane.setEditable(false); |
238 |
if (DialogDisplayer.getDefault().notify(new NotifyDescriptor.Confirmation(new JScrollPane(licensePane), TITLE_accept_license(pomPrj.getName()), NotifyDescriptor.OK_CANCEL_OPTION)) != NotifyDescriptor.OK_OPTION) { |
239 |
throw new Exception("license " + licenseURL + " rejected"); |
240 |
} |
241 |
} |
242 |
} |
243 |
} |
244 |
index++; // whether or not we got a license |
245 |
handle.progress(MSG_Downloading(a.getId()), index++); |
246 |
online.resolve(a, remoteArtifactRepositories, localRepository); |
195 |
classpathVolume.add(getJarUri(a, baseFolder, nonDefaultLibBase, ClassifierType.BINARY)); |
247 |
classpathVolume.add(getJarUri(a, baseFolder, nonDefaultLibBase, ClassifierType.BINARY)); |
196 |
try { |
|
|
197 |
if (allSourceAndJavadoc) { |
248 |
if (allSourceAndJavadoc) { |
198 |
handle.progress(MSG_Downloading_javadoc(a.getId()), index + 1); |
249 |
handle.progress(MSG_Downloading_javadoc(a.getId()), index++); |
199 |
Artifact javadoc = online.createArtifactWithClassifier( |
250 |
Artifact javadoc = online.createArtifactWithClassifier( |
200 |
a.getGroupId(), |
251 |
a.getGroupId(), |
201 |
a.getArtifactId(), |
252 |
a.getArtifactId(), |
202 |
a.getVersion(), |
253 |
a.getVersion(), |
203 |
a.getType(), |
254 |
a.getType(), |
204 |
"javadoc"); //NOI18N |
255 |
"javadoc"); //NOI18N |
205 |
online.resolve(javadoc, project.getRemoteArtifactRepositories(), online.getLocalRepository()); |
256 |
online.resolve(javadoc, remoteArtifactRepositories, localRepository); |
206 |
if (javadoc.getFile().exists()) { |
257 |
if (javadoc.getFile().exists()) { |
207 |
URI javadocUri = getJarUri(javadoc, baseFolder, nonDefaultLibBase, ClassifierType.JAVADOC); |
258 |
URI javadocUri = getJarUri(javadoc, baseFolder, nonDefaultLibBase, ClassifierType.JAVADOC); |
208 |
javadocVolume.add(javadocUri); |
259 |
javadocVolume.add(javadocUri); |
209 |
} |
260 |
} |
210 |
|
261 |
|
211 |
handle.progress(MSG_Downloading_sources(a.getId()), index + 2); |
262 |
handle.progress(MSG_Downloading_sources(a.getId()), index++); |
212 |
Artifact sources = online.createArtifactWithClassifier( |
263 |
Artifact sources = online.createArtifactWithClassifier( |
213 |
a.getGroupId(), |
264 |
a.getGroupId(), |
214 |
a.getArtifactId(), |
265 |
a.getArtifactId(), |
215 |
a.getVersion(), |
266 |
a.getVersion(), |
216 |
a.getType(), |
267 |
a.getType(), |
217 |
"sources"); //NOI18N |
268 |
"sources"); //NOI18N |
218 |
online.resolve(sources, project.getRemoteArtifactRepositories(), online.getLocalRepository()); |
269 |
online.resolve(sources, remoteArtifactRepositories, localRepository); |
219 |
if (sources.getFile().exists()) { |
270 |
if (sources.getFile().exists()) { |
220 |
sourceVolume.add(getJarUri(sources, baseFolder, nonDefaultLibBase, ClassifierType.SOURCES)); |
271 |
sourceVolume.add(getJarUri(sources, baseFolder, nonDefaultLibBase, ClassifierType.SOURCES)); |
221 |
} |
272 |
} |
222 |
} |
273 |
} |
223 |
} catch (Exception ex) { |
|
|
224 |
Logger.getLogger(CreateLibraryAction.class.getName()).log(Level.FINE, "Failed to download artifact", ex); |
225 |
} |
226 |
|
274 |
|
227 |
} catch (Exception ex) { |
|
|
228 |
failed.add(a); |
229 |
Logger.getLogger(CreateLibraryAction.class.getName()).log(Level.FINE, "Failed to download artifact", ex); |
230 |
} |
231 |
index = index + (allSourceAndJavadoc ? 3 : 1); |
232 |
} |
275 |
} |
233 |
try { |
276 |
handle.progress("Adding library", index++); |
234 |
handle.progress("Adding library", index + 4); |
277 |
// XXX #182388 would allow us to set a display name here (e.g. based on pomPrj.getName(), maybe plus pomPrj.getVersion()) |
235 |
return libraryManager.createURILibrary("j2se", libraryName, volumes); //NOI18N |
278 |
return libraryManager.createURILibrary("j2se", libraryName, volumes); //NOI18N |
236 |
} catch (IOException ex) { |
|
|
237 |
Exceptions.printStackTrace(ex); |
238 |
} |
239 |
} catch (ThreadDeath d) { // download interrupted |
240 |
} finally { |
279 |
} finally { |
241 |
handle.finish(); |
280 |
handle.finish(); |
242 |
} |
281 |
} |
243 |
return null; |
|
|
244 |
} |
282 |
} |
245 |
|
283 |
|
246 |
/** append path to given jar root uri */ |
284 |
/** append path to given jar root uri */ |