This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 194744
Collapse All | Expand All

(-)a/maven/nbproject/project.xml (-1 / +1 lines)
Lines 162-168 Link Here
162
                    <compile-dependency/>
162
                    <compile-dependency/>
163
                    <run-dependency>
163
                    <run-dependency>
164
                        <release-version>1</release-version>
164
                        <release-version>1</release-version>
165
                        <specification-version>1.25</specification-version>
165
                        <specification-version>1.35</specification-version>
166
                    </run-dependency>
166
                    </run-dependency>
167
                </dependency>
167
                </dependency>
168
                <dependency>
168
                <dependency>
(-)a/maven/src/org/netbeans/modules/maven/actions/CreateLibraryAction.java (-35 / +73 lines)
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 */
(-)a/maven/src/org/netbeans/modules/maven/actions/MavenLibraryDefiner.java (+83 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2011 Sun Microsystems, Inc.
41
 */
42
43
package org.netbeans.modules.maven.actions;
44
45
import java.util.Collections;
46
import java.util.concurrent.Callable;
47
import org.netbeans.api.project.libraries.Library;
48
import org.netbeans.api.project.libraries.LibraryManager;
49
import org.netbeans.modules.maven.api.NbMavenProject;
50
import org.netbeans.modules.maven.embedder.EmbedderFactory;
51
import org.netbeans.spi.java.project.support.ui.BrokenReferencesSupport.LibraryDefiner;
52
import org.openide.filesystems.FileObject;
53
import org.openide.filesystems.FileUtil;
54
import org.openide.util.lookup.ServiceProvider;
55
56
/**
57
 * Defines missing libraries using {@code org-netbeans-api-project-libraries/Maven/libname} files.
58
 * Prompts to create a library definition based on an artifact in the local repository
59
 * (and associated secondary artifacts - source and Javadoc),
60
 * first downloading that library if necessary.
61
 * Required attributes are {@code groupId}, {@code artifactId}, and {@code version}.
62
 * Currently does not support specifying a remote repository ID;
63
 * currently supports only {@code j2se}-type libraries based on {@code jar} packaging.
64
 */
65
@ServiceProvider(service=LibraryDefiner.class)
66
public class MavenLibraryDefiner implements LibraryDefiner {
67
68
    public @Override Callable<Library> missingLibrary(final String name) {
69
        final FileObject def = FileUtil.getConfigFile("org-netbeans-api-project-libraries/Maven/" + name);
70
        if (def == null) {
71
            return null;
72
        }
73
        return new Callable<Library>() {
74
            public @Override Library call() throws Exception {
75
                return CreateLibraryAction.createLibrary(LibraryManager.getDefault(), name, Collections.singletonList(
76
                        EmbedderFactory.getOnlineEmbedder().createArtifact(
77
                            (String) def.getAttribute("groupId"), (String) def.getAttribute("artifactId"), (String) def.getAttribute("version"), NbMavenProject.TYPE_JAR)
78
                    ), true, null, null);
79
            }
80
        };
81
    }
82
83
}

Return to bug 194744