--- maven.archetype/apichanges.xml +++ maven.archetype/apichanges.xml @@ -0,0 +1,167 @@ + + + + + + + + + + + + Maven Archetype + + + + + + + + + + Created the public Maven Archetype api + + + + + +

+ Public API for creating New Project wizards based on the maven-archetype-plugin. +

+
+ + + + + +
+ +
+ + + + + + + Change History for Maven Archetype + + + + + + +

Introduction

+ +

This document lists changes made to the Maven Project API.

+ + +
+ + +

@FOOTER@

+ + +
+ +
--- maven.archetype/arch.xml +++ maven.archetype/arch.xml @@ -0,0 +1,1130 @@ + + + +]> + + + + &api-questions; + + + + + + + + + +

+ creating of New Project wizards based on the maven-archetype-plugin. +

+
+ + + + + + + +

+ "Projects/org-netbeans-modules-maven/Archetypes" folder contains fileobjects + that represent archetypes. The archetypes are defined by the following file attributes: +

+ + + + + + + + + +
groupIdmandatory
artifactIdmandatory
versionmandatory
repositoryoptionalurl of the archetype's repository
nameBundleKeyoptionalkey in bundle file that holds localized name
descriptionBundleKeyoptionalkey in bundle file that holds localized description
+
+ +
+ + + +

+ No environment or Java System properties. +

+
+ + + + + + +

+ XXX no answer for arch-quality +

+
+ + + + + +

+ XXX no answer for arch-time +

+
+ + + + + +

+ XXX no answer for arch-what +

+
+ + + + + + + + + + + + +

+ XXX no answer for compat-deprecation +

+
+ + + + + +

+ XXX no answer for compat-i18n +

+
+ + + + + +

+ XXX no answer for compat-standards +

+
+ + + + + +

+ XXX no answer for compat-version +

+
+ + + + + +

+ XXX no answer for dep-jre +

+
+ + + + + +

+ XXX no answer for dep-jrejdk +

+
+ + + + + + + + + + + + +

+ XXX no answer for dep-non-nb +

+
+ + + + + +

+ XXX no answer for dep-platform +

+
+ + + + + +

+ No special requirements here. +

+
+ + + + + +

+ XXX no answer for deploy-jar +

+
+ + + + + +

+ XXX no answer for deploy-nbm +

+
+ + + + + +

+ XXX no answer for deploy-packages +

+
+ + + + + +

+ XXX no answer for deploy-shared +

+
+ + + + + +

+ XXX no answer for exec-ant-tasks +

+
+ + + + + +

+ XXX no answer for exec-classloader +

+
+ + + + + +

+ XXX no answer for exec-component +

+
+ + + + + +

+ XXX no answer for exec-introspection +

+
+ + + + + +

+ XXX no answer for exec-privateaccess +

+
+ + + + + +

+ XXX no answer for exec-process +

+
+ + + + + +

+ XXX no answer for exec-reflection +

+
+ + + + + +

+ XXX no answer for exec-threading +

+
+ + + + + +

+ XXX no answer for format-clipboard +

+
+ + + + + +

+ XXX no answer for format-dnd +

+
+ + + + + +

+ XXX no answer for format-types +

+
+ + + + + +

+ XXX no answer for lookup-lookup +

+
+ + + + + +

+ XXX no answer for lookup-register +

+
+ + + + + +

+ XXX no answer for lookup-remove +

+
+ + + + + +

+ XXX no answer for perf-exit +

+
+ + + + + +

+ XXX no answer for perf-huge_dialogs +

+
+ + + + + +

+ XXX no answer for perf-limit +

+
+ + + + + +

+ XXX no answer for perf-mem +

+
+ + + + + +

+ XXX no answer for perf-menus +

+
+ + + + + +

+ XXX no answer for perf-progress +

+
+ + + + + +

+ XXX no answer for perf-scale +

+
+ + + + + +

+ XXX no answer for perf-spi +

+
+ + + + + +

+ XXX no answer for perf-startup +

+
+ + + + + +

+ XXX no answer for perf-wakeup +

+
+ + + + + +

+ XXX no answer for resources-file +

+
+ + + + + +

+ XXX no answer for resources-layer +

+
+ + + + + +

+ XXX no answer for resources-mask +

+
+ + + + + +

+ XXX no answer for resources-preferences +

+
+ + + + + +

+ XXX no answer for security-grant +

+
+ + + + + +

+ XXX no answer for security-policy +

+
+ +
--- maven.archetype/build.xml +++ maven.archetype/build.xml @@ -0,0 +1,50 @@ + + + + + Builds, tests, and runs the project org.netbeans.modules.maven.archetype + + --- maven.archetype/manifest.mf +++ maven.archetype/manifest.mf @@ -0,0 +1,6 @@ +Manifest-Version: 1.0 +OpenIDE-Module: org.netbeans.modules.maven.archetype +OpenIDE-Module-Specification-Version: 1.0 +OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/maven/archetype/Bundle.properties +AutoUpdate-Show-In-Client: false + --- maven.archetype/nbproject/project.properties +++ maven.archetype/nbproject/project.properties @@ -0,0 +1,46 @@ +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. +# +# Copyright 2010 Oracle and/or its affiliates. All rights reserved. +# +# Oracle and Java are registered trademarks of Oracle and/or its affiliates. +# Other names may be trademarks of their respective owners. +# +# The contents of this file are subject to the terms of either the GNU +# General Public License Version 2 only ("GPL") or the Common +# Development and Distribution License("CDDL") (collectively, the +# "License"). You may not use this file except in compliance with the +# License. You can obtain a copy of the License at +# http://www.netbeans.org/cddl-gplv2.html +# or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the +# specific language governing permissions and limitations under the +# License. When distributing the software, include this License Header +# Notice in each file and include the License file at +# nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this +# particular file as subject to the "Classpath" exception as provided +# by Oracle in the GPL Version 2 section of the License file that +# accompanied this code. If applicable, add the following below the +# License Header, with the fields enclosed by brackets [] replaced by +# your own identifying information: +# "Portions Copyrighted [year] [name of copyright owner]" +# +# If you wish your version of this file to be governed by only the CDDL +# or only the GPL Version 2, indicate your decision by adding +# "[Contributor] elects to include this software in this distribution +# under the [CDDL or GPL Version 2] license." If you do not indicate a +# single choice of license, a recipient has the option to distribute +# your version of this file under either the CDDL, the GPL Version 2 or +# to extend the choice of license to its licensees as provided above. +# However, if you add GPL Version 2 code and therefore, elected the GPL +# Version 2 license, then the option applies only if the new code is +# made subject to such option by the copyright holder. +# +# Contributor(s): +# +# Portions Copyrighted 2008 Sun Microsystems, Inc. + +javac.source=1.6 +javac.compilerargs=-Xlint -Xlint:-serial +javadoc.apichanges=${basedir}/apichanges.xml +javadoc.arch=${basedir}/arch.xml + + --- maven.archetype/nbproject/project.xml +++ maven.archetype/nbproject/project.xml @@ -0,0 +1,180 @@ + + + + org.netbeans.modules.apisupport.project + + + org.netbeans.modules.maven.archetype + + + org.netbeans.api.annotations.common + + + + 1 + 1.6 + + + + org.netbeans.api.progress + + + + 1 + 1.12 + + + + org.netbeans.modules.maven.embedder + + + + 2 + 2.44 + + + + org.netbeans.modules.maven.indexer + + + + 2 + 2.28 + + + + org.netbeans.modules.maven.model + + + + 1 + 1.36 + + + + org.netbeans.modules.swing.validation + + + + 2 + 1.16 + + + + org.netbeans.modules.xml.xam + + + + 1 + 1.4 + + + + org.openide.dialogs + + + + 7.22 + + + + org.openide.filesystems + + + + 7.49 + + + + org.openide.loaders + + + + 7.47 + + + + org.openide.util + + + + 8.39 + + + + org.openide.util.lookup + + + + 8.0 + + + + + + unit + + org.netbeans.libs.junit4 + + + + org.netbeans.modules.nbjunit + + + + + org.openide.util.lookup + + + + + + + org.netbeans.modules.maven.archetype.api + + + + --- maven.archetype/src/org/netbeans/modules/maven/archetype/Bundle.properties +++ maven.archetype/src/org/netbeans/modules/maven/archetype/Bundle.properties @@ -0,0 +1,43 @@ +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. +# +# Copyright 2010 Oracle and/or its affiliates. All rights reserved. +# +# Oracle and Java are registered trademarks of Oracle and/or its affiliates. +# Other names may be trademarks of their respective owners. +# +# The contents of this file are subject to the terms of either the GNU +# General Public License Version 2 only ("GPL") or the Common +# Development and Distribution License("CDDL") (collectively, the +# "License"). You may not use this file except in compliance with the +# License. You can obtain a copy of the License at +# http://www.netbeans.org/cddl-gplv2.html +# or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the +# specific language governing permissions and limitations under the +# License. When distributing the software, include this License Header +# Notice in each file and include the License file at +# nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this +# particular file as subject to the "Classpath" exception as provided +# by Oracle in the GPL Version 2 section of the License file that +# accompanied this code. If applicable, add the following below the +# License Header, with the fields enclosed by brackets [] replaced by +# your own identifying information: +# "Portions Copyrighted [year] [name of copyright owner]" +# +# If you wish your version of this file to be governed by only the CDDL +# or only the GPL Version 2, indicate your decision by adding +# "[Contributor] elects to include this software in this distribution +# under the [CDDL or GPL Version 2] license." If you do not indicate a +# single choice of license, a recipient has the option to distribute +# your version of this file under either the CDDL, the GPL Version 2 or +# to extend the choice of license to its licensees as provided above. +# However, if you add GPL Version 2 code and therefore, elected the GPL +# Version 2 license, then the option applies only if the new code is +# made subject to such option by the copyright holder. +# +# Contributor(s): +# +# Portions Copyrighted 2008 Sun Microsystems, Inc. + +OpenIDE-Module-Display-Category=Maven +OpenIDE-Module-Name=Maven Archetype +OpenIDE-Module-Long-Description=Maven archetypes related API. --- maven.archetype/src/org/netbeans/modules/maven/archetype/api/Archetype.java +++ maven.archetype/src/org/netbeans/modules/maven/archetype/api/Archetype.java @@ -0,0 +1,397 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2010 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2008 Sun Microsystems, Inc. + */ + +package org.netbeans.modules.maven.archetype.api; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.jar.JarFile; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.zip.ZipEntry; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.handler.ArtifactHandler; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.resolver.ArtifactNotFoundException; +import org.apache.maven.artifact.resolver.ArtifactResolutionException; +import org.apache.maven.repository.RepositorySystem; +import org.netbeans.api.annotations.common.NonNull; +import org.netbeans.api.progress.aggregate.AggregateProgressHandle; +import org.netbeans.modules.maven.embedder.EmbedderFactory; +import org.netbeans.modules.maven.embedder.MavenEmbedder; +import org.netbeans.modules.maven.embedder.exec.ProgressTransferListener; +import org.netbeans.modules.maven.indexer.api.RepositoryInfo; +import org.netbeans.modules.maven.indexer.api.RepositoryPreferences; +import org.openide.util.Exceptions; +import org.openide.util.Parameters; +import org.openide.xml.XMLUtil; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +/** + * Simple model class to describe a Maven archetype. To be created by ArchetypeProvider + * implementations, consumed by the New Maven Project wizard. + * + * @author mkleint + * @since 1.0 + */ +public final class Archetype { + + private static final Logger LOG = Logger.getLogger(Archetype.class.getName()); + + private final String artifactId; + private final String groupId; + private final String version; + private final String name; + private final String description; + private final String repository; + private Artifact artifact; + private Artifact pomArtifact; + + /** + * C'tor + * + * @param artifactId + * @param groupId + * @param version + * @since 1.0 + */ + public Archetype(String artifactId, String groupId, String version) { + this(artifactId, groupId, version, null, null, null); + } + + /** + * C'tor + * + * @param artifactId + * @param groupId + * @param version + * @param name + * @param description + * @param repository + * @since 1.0 + */ + public Archetype(String artifactId, String groupId, String version, String name, String description, String repository) { + this.artifactId = artifactId; + this.groupId = groupId; + this.version = version; + this.name = name; + this.description = description; + this.repository = repository; + } + + /** + * Returns the artifact id. + * + * @return the artifact id + * @since 1.0 + */ + public String getArtifactId() { + return artifactId; + } + + /** + * Returns the group id. + * + * @return the group id + * @since 1.0 + */ + public String getGroupId() { + return groupId; + } + + /** + * Returns the version. + * + * @return the version + * @since 1.0 + */ + public String getVersion() { + return version; + } + + /** + * Returns the name. + * + * @return the name + * @since 1.0 + */ + public @NonNull String getName() { + // #166884 + if ("${project.artifactId}".equals(name)) { //NOI18N + return artifactId; + } + if (name == null || name.trim().length() == 0) { + return artifactId; + } + return name; + } + + /** + * Returns the description. Is an optional property. + * + * @return the description or null if none available + * @since 1.0 + */ + public String getDescription() { + return description; + } + + /** + * Returns the repository. Is an optional property. + * + * @return the repository or null if none available + * @since 1.0 + */ + public String getRepository() { + return repository; + } + + /** + * Initially non resolved artifact, need to call resolveArtifacts() before getArtifact().getFile() can be used. + * + * @return the artifact + * @since 1.0 + */ + public synchronized Artifact getArtifact() { + if (artifact == null) { + MavenEmbedder online = EmbedderFactory.getOnlineEmbedder(); + artifact = online.createArtifact( + getGroupId(), + getArtifactId(), + getVersion(), + "jar", //NOI18N + "maven-archetype"); //NOI18N + + //hack to get the right extension for the right packaging without the plugin. + artifact.setArtifactHandler(new ArtifactHandler() { + @Override + public String getExtension() { + return "jar"; //NOI18N + } + + @Override + public String getDirectory() { + return null; + } + + @Override + public String getClassifier() { + return null; + } + + @Override + public String getPackaging() { + return "maven-archetype"; //NOI18N + } + + @Override + public boolean isIncludesDependencies() { + return false; + } + + @Override + public String getLanguage() { + return "java"; //NOI18N + } + + @Override + public boolean isAddedToClasspath() { + return false; + } + }); + } + return artifact; + } + + /** + * Initially non resolved artifact, need to call resolveArtifacts() before getArtifact().getFile() can be used. + * + * @return the pom artifact + * @since 1.0 + */ + public synchronized Artifact getPomArtifact() { + if (pomArtifact == null) { + MavenEmbedder online = EmbedderFactory.getOnlineEmbedder(); + pomArtifact = online.createArtifact( + getGroupId(), + getArtifactId(), + getVersion(), + "pom", //NOI18N + "pom"); //NOI18N + } + return pomArtifact; + } + + /** + * Resolve the artifacts associated with the archetype (ideally downloads them to the local repository) + * + * @param hndl a progress handle + * @throws ArtifactResolutionException + * @throws ArtifactNotFoundException + */ + public void resolveArtifacts(AggregateProgressHandle hndl) throws ArtifactResolutionException, ArtifactNotFoundException { + MavenEmbedder online = EmbedderFactory.getOnlineEmbedder(); + + List repos; + if (getRepository() == null) { + repos = Collections.singletonList(online.createRemoteRepository(RepositorySystem.DEFAULT_REMOTE_REPO_URL, RepositorySystem.DEFAULT_REMOTE_REPO_ID)); + } else { + repos = Collections.singletonList(online.createRemoteRepository(getRepository(), "custom-repo"));//NOI18N + for (RepositoryInfo info : RepositoryPreferences.getInstance().getRepositoryInfos()) { + if (getRepository().equals(info.getRepositoryUrl())) { + repos = Collections.singletonList(online.createRemoteRepository(getRepository(), info.getId()));//NOI18N + break; + } + } + } + try { + ProgressTransferListener.setAggregateHandle(hndl); + + hndl.start(); + +//TODO how to rewrite to track progress? +// try { +// WagonManager wagon = online.getPlexusContainer().lookup(WagonManager.class); +// wagon.setDownloadMonitor(new ProgressTransferListener()); +// } catch (ComponentLookupException ex) { +// Exceptions.printStackTrace(ex); +// } + online.resolve(getPomArtifact(), repos, online.getLocalRepository()); + online.resolve(getArtifact(), repos, online.getLocalRepository()); + } catch (ThreadDeath d) { // download interrupted + } catch (IllegalStateException ise) { //download interrupted in dependent thread. #213812 + if (!(ise.getCause() instanceof ThreadDeath)) { + throw ise; + } + } finally { + ProgressTransferListener.clearAggregateHandle(); + hndl.finish(); + } + } + + /** + * Parses the META-INF/maven/archetype-metadata.xml file within the archetype's jar + * to get the additional required properties. Assumes resolveArtifacts() was called beforehand + * + * @return required property name as key and default value as map value + * @since 1.0 + */ + public Map loadRequiredProperties() { + HashMap map = new HashMap(); + File fil = getArtifact().getFile(); + assert fil != null : "requires a resolved artifact"; + JarFile jf = null; + try { + jf = new JarFile(fil); + ZipEntry entry = jf.getJarEntry("META-INF/maven/archetype-metadata.xml"); + if (entry == null) { + entry = jf.getJarEntry("META-INF/maven/archetype.xml"); + } + if (entry != null) { + InputStream in = jf.getInputStream(entry); + try { + Document doc = XMLUtil.parse(new InputSource(in), false, false, XMLUtil.defaultErrorHandler(), null); + NodeList nl = doc.getElementsByTagName("requiredProperty"); + for (int i = 0; i < nl.getLength(); i++) { + Element rP = (Element) nl.item(i); + Element dV = XMLUtil.findElement(rP, "defaultValue", null); + map.put(rP.getAttribute("key"), dV != null ? XMLUtil.findText(dV) : null); + } + } finally { + in.close(); + } + } + } catch (IOException ex) { + LOG.log(Level.INFO, ex.getMessage(), ex); + } catch (SAXException ex) { + LOG.log(Level.INFO, ex.getMessage(), ex); + } finally { + if (jf != null) { + try { + jf.close(); + } catch (IOException ex) { + Exceptions.printStackTrace(ex); + } + } + } + return map; + } + + + @Override + public int hashCode() { + return getGroupId().trim().hashCode() + 13 * getArtifactId().trim().hashCode() + 23 * getVersion().trim().hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof Archetype)) { + return false; + } + Archetype ar1 = (Archetype)obj; + boolean gr = ar1.getGroupId().trim().equals(getGroupId().trim()); + if (!gr) { + return false; + } + boolean ar = ar1.getArtifactId().trim().equals(getArtifactId().trim()); + if (!ar) { + return false; + } + boolean ver = ar1.getVersion().trim().equals(getVersion().trim()); + return ver; + } + + @Override + public String toString() { + return groupId + ":" + artifactId + ":" + version; + } + +} --- maven.archetype/src/org/netbeans/modules/maven/archetype/api/ArchetypeProvider.java +++ maven.archetype/src/org/netbeans/modules/maven/archetype/api/ArchetypeProvider.java @@ -0,0 +1,68 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2010 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2008 Sun Microsystems, Inc. + */ + +package org.netbeans.modules.maven.archetype.api; + +import java.util.List; + +/** + * Componentized provider of list of available archetypes. + * It is used in New Maven project wizard to populate the list of available archetypes. + * The providers are expected to be registered using {@link org.openide.util.lookup.ServiceProvider}. + * There are 3 default implementations registered: One lists 1 basic archetype + * (simple and the other lists all archetypes it find in local and remote repository indexes. + *

For special archetypes to be visible in the UI, use {@link ArchetypeWizards#definedArchetype}. + * + * @author mkleint + * @since 1.0 + */ +public interface ArchetypeProvider { + + /** + * Returns Archetype instances known to this provider. Is called once per + * New Maven Project wizard invocation. + * + * @return list of archetypes + * @since 1.0 + */ + List getArchetypes(); +} --- maven.archetype/src/org/netbeans/modules/maven/archetype/api/ArchetypeWizards.java +++ maven.archetype/src/org/netbeans/modules/maven/archetype/api/ArchetypeWizards.java @@ -0,0 +1,244 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2010 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2010 Sun Microsystems, Inc. + */ + +package org.netbeans.modules.maven.archetype.api; + +import java.io.File; +import java.io.IOException; +import java.util.Map; +import java.util.Set; +import org.netbeans.api.annotations.common.NullAllowed; +import org.netbeans.api.templates.TemplateRegistration; +import org.netbeans.modules.maven.model.ModelOperation; +import org.netbeans.modules.maven.model.pom.POMModel; +import org.netbeans.validation.api.ui.ValidationGroup; +import org.openide.WizardDescriptor; +import org.openide.filesystems.FileObject; +import org.openide.util.Lookup; + +/** + * Utilities for creating New Project wizards based on maven-archetype-plugin. + * + * @author mkleint + * @since 1.0 + */ +public class ArchetypeWizards { + + private static final MavenArchetypeProvider wizardProvider = Lookup.getDefault().lookup(MavenArchetypeProvider.class);; + + private ArchetypeWizards() { } + + /** + * Provides functionality necessary for the archetype ArchetypeWizards api. + * + * Note that this is meant for internal use only. + * + * @author Tomas Stupka + * @since 1.0 + */ + public static abstract class MavenArchetypeProvider { + + /** + * Run a single archetype. + * + * @param projDir + * @param vi + * @param arch + * @param additionalProperties + * @param updateLastUsedProjectDir + * @throws IOException + * @since 1.0 + * @see ArchetypeWizards#createFromArchetype(java.io.File, org.netbeans.modules.maven.archetype.api.ProjectInfo, org.netbeans.modules.maven.archetype.api.Archetype, java.util.Map, boolean) + */ + protected abstract void createFromArchetype(File projDir, ProjectInfo vi, Archetype arch, @NullAllowed Map additionalProperties, boolean updateLastUsedProjectDir) throws IOException; + + /** + * Opens newly created Maven projects. + * + * @param dirF + * @param mainProjectDir + * @return + * @throws IOException + * @since 1.0 + * @see ArchetypeWizards#openProjects(java.io.File, java.io.File) + */ + protected abstract Set openProjects(File dirF, File mainProjectDir) throws IOException; + + /** + * Log new project usage. + * + * @param groupId + * @param artifactId + * @param version + * @since 1.0 + * @see ArchetypeWizards#logUsage(java.lang.String, java.lang.String, java.lang.String) + */ + protected abstract void logUsage(String groupId, String artifactId, String version); + + /** + * Adds a dependency operation. + * + * @param info + * @param type + * @return + * @since 1.0 + * @see ArchetypeWizards#addDependencyOperation(org.netbeans.modules.maven.archetype.api.ProjectInfo, java.lang.String) + */ + protected abstract ModelOperation addDependencyOperation(ProjectInfo info, String type); + + /** + * Creates the basic panel for a new project wizard. + * + * @param vg + * @param isFinish + * @param archetype + * @return + * @since 1.0 + * @see ArchetypeWizards#createBasicWizardPanel(org.netbeans.validation.api.ui.ValidationGroup, boolean, org.netbeans.modules.maven.archetype.api.Archetype) + */ + protected abstract WizardDescriptor.Panel createBasicWizardPanel(ValidationGroup vg, boolean isFinish, @NullAllowed Archetype archetype); + + /** + * Wizard iterator using a predetermined archetype. + * + * @param groupId + * @param artifactId + * @param version + * @param repository + * @param title + * @return + * @since 1.0 + * @see ArchetypeWizards#createDefinedArchetype(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String) + */ + protected abstract WizardDescriptor.InstantiatingIterator createDefinedArchetype(String groupId, String artifactId, String version, @NullAllowed String repository, String title); + } + + /** + * Customary location of Maven project templates. + * @since 1.0 + * @see TemplateRegistration#folder + */ + public static final String TEMPLATE_FOLDER = "Project/Maven2"; + + /** + * Run a single archetype. + * + * @param projDir the new project directory (must be normalized first!) (note: parent dir is actually passed to plugin, i.e. assumes that project name matches this basedir) + * @param vi metadata for new project + * @param arch the archetype to process + * @param additionalProperties any additional archetype properties, or null + * @param updateLastUsedProjectDir true to update last-used project directory for next wizard run + * @since 1.0 + * @throws java.io.IOException + */ + public static void createFromArchetype(File projDir, ProjectInfo vi, Archetype arch, @NullAllowed Map additionalProperties, boolean updateLastUsedProjectDir) throws IOException { + wizardProvider.createFromArchetype(projDir, vi, arch, additionalProperties, updateLastUsedProjectDir); + } + + /** + * Opens newly created Maven projects. + * + * @param dirF the top-level dir to check in + * @param mainProjectDir the dir containing the main project, or null + * @return set of project directories + * @since 1.0 + * @throws java.io.IOException + */ + public static Set openProjects(File dirF, File mainProjectDir) throws IOException { + return wizardProvider.openProjects(dirF, mainProjectDir); + } + + /** + * Log new project usage. + * + * @param groupId + * @param artifactId + * @param version + * @since 1.0 + */ + public static void logUsage(String groupId, String artifactId, String version) { + wizardProvider.logUsage(groupId, artifactId, version); + } + + /** + * Adds a dependency operation. + * + * @param info project info + * @param type dependency type + * @return an dependency operation + * @since 1.0 + */ + public static ModelOperation addDependencyOperation(ProjectInfo info, String type) { + return wizardProvider.addDependencyOperation(info, type); + } + + /** + * Creates the basic panel for a new project wizard. + * + * @param vg validation group + * @param isFinish determines whether the panel will have the finish button + * @param archetype the archetype to create a project from + * @return a wizard panel + * @since 1.0 + */ + public static WizardDescriptor.Panel createBasicWizardPanel(ValidationGroup vg, boolean isFinish, @NullAllowed Archetype archetype) { + return wizardProvider.createBasicWizardPanel(vg, isFinish, archetype); + } + + /** + * Wizard iterator using a predetermined archetype. + * + * @param groupId + * @param artifactId + * @param version + * @param repository + * @param title + * @return a wizard iterators + * @since 1.0 + * @see TemplateRegistration + * @see #TEMPLATE_FOLDER + */ + public static WizardDescriptor.InstantiatingIterator createDefinedArchetype(String groupId, String artifactId, String version, @NullAllowed String repository, String title) { + return wizardProvider.createDefinedArchetype(groupId, artifactId, version, repository, title); + } + +} --- maven.archetype/src/org/netbeans/modules/maven/archetype/api/ProjectInfo.java +++ maven.archetype/src/org/netbeans/modules/maven/archetype/api/ProjectInfo.java @@ -0,0 +1,116 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2010 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2009 Sun Microsystems, Inc. + */ + +package org.netbeans.modules.maven.archetype.api; + +import org.openide.util.Parameters; + +/** + * Information about one project to be created. + * + * @author mkleint + * @since 1.0 + */ +public final class ProjectInfo { + + private final String groupId; + private final String artifactId; + private final String version; + private final String packageName; + + /** + * C'tor + * + * @param groupId the group id + * @param artifactId the artifact id + * @param version the version + * @param packageName the package name + * @since 1.0 + */ + public ProjectInfo(String groupId, String artifactId, String version, String packageName) { + this.groupId = groupId; + this.artifactId = artifactId; + this.version = version; + this.packageName = packageName; + } + + /** + * Returns the group id. + * + * @return + * @since 1.0 + */ + public String getGroupId() { + return groupId; + } + + /** + * Returns the artifact id. + * + * @return + * @since 1.0 + */ + public String getArtifactId() { + return artifactId; + } + + /** + * Returns the version. + * + * @return + * @since 1.0 + */ + public String getVersion() { + return version; + } + + /** + * Returns the package name. + * + * @return + * @since 1.0 + */ + public String getPackageName() { + return packageName; + } + +} --- maven.archetype/test/unit/src/org/netbeans/modules/maven/archetype/api/ArchetypeTest.java +++ maven.archetype/test/unit/src/org/netbeans/modules/maven/archetype/api/ArchetypeTest.java @@ -0,0 +1,96 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2011 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2011 Sun Microsystems, Inc. + */ + +package org.netbeans.modules.maven.archetype.api; + +import org.netbeans.junit.NbTestCase; + +public class ArchetypeTest extends NbTestCase { + + public ArchetypeTest(String n) { + super(n); + } + + public void testArchetype() { + Archetype a = new Archetype("aid", "gid", "1.0"); + + assertEquals("aid", a.getArtifactId()); + assertEquals("gid", a.getGroupId()); + assertEquals("1.0", a.getVersion()); + assertEquals("aid", a.getName()); + assertNull(a.getDescription()); + assertNull(a.getRepository()); + + Archetype a2 = new Archetype("aid", "gid", "1.0"); + assertEquals(a, a2); + assertEquals(a.hashCode(), a2.hashCode()); + + a = new Archetype("aid", "gid", "1.0", "test", "test desc", "test repository"); + + assertEquals("aid", a.getArtifactId()); + assertEquals("gid", a.getGroupId()); + assertEquals("1.0", a.getVersion()); + assertEquals("test", a.getName()); + assertEquals("test desc", a.getDescription()); + assertEquals("test repository", a.getRepository()); + + a2 = new Archetype("aid", "gid", "1.0", "test", "test desc", "test repository"); + assertEquals(a, a2); + assertEquals(a.hashCode(), a2.hashCode()); + + } + + public void testGetName() { + Archetype a = new Archetype("aid", "gid", "1.0"); + assertEquals("aid", a.getName()); + + a = new Archetype("aid", "gid", "1.0", null, null, null); + assertEquals("aid", a.getName()); + + a = new Archetype("aid", "gid", "1.0", "${project.artifactId}", null, null); + assertEquals("aid", a.getName()); + + a = new Archetype("aid", "gid", "1.0", "test name", null, null); + assertEquals("test name", a.getName()); + } + +} --- maven.archetype/test/unit/src/org/netbeans/modules/maven/archetype/api/ArchetypeWizardTest.java +++ maven.archetype/test/unit/src/org/netbeans/modules/maven/archetype/api/ArchetypeWizardTest.java @@ -0,0 +1,172 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2011 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2011 Sun Microsystems, Inc. + */ + +package org.netbeans.modules.maven.archetype.api; + +import java.io.File; +import java.io.IOException; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import org.netbeans.junit.NbTestCase; +import org.netbeans.modules.maven.archetype.api.ArchetypeWizards.MavenArchetypeProvider; +import org.netbeans.modules.maven.model.ModelOperation; +import org.netbeans.modules.maven.model.pom.POMModel; +import org.netbeans.validation.api.Problem; +import org.netbeans.validation.api.ui.ValidationGroup; +import org.netbeans.validation.api.ui.ValidationUI; +import org.openide.WizardDescriptor; +import org.openide.filesystems.FileObject; +import org.openide.util.test.MockLookup; + +public class ArchetypeWizardTest extends NbTestCase { + + public ArchetypeWizardTest(String n) { + super(n); + } + + @Override + protected void setUp() throws Exception { + MockLookup.setInstances(new TestMavenArchetypeProvider()); + } + + public void testMavenArchetypeProviderDelegate() throws IOException { + + ProjectInfo pi = new ProjectInfo("gid", "aid", "1.0", "package.name"); + String type = "jar"; + ArchetypeWizards.addDependencyOperation(pi, type); + List l = TestMavenArchetypeProvider.calls.get("addDependencyOperation"); + assertNotNull(l); + assertEquals(pi, l.get(0)); + assertEquals(type, l.get(1)); + + Archetype a = new Archetype("aid", "gid", "1.0"); + ValidationGroup vg = ValidationGroup.create(new ValidationUI() { + @Override public void showProblem(Problem prblm) { } + @Override public void clearProblem() { } + }); + ArchetypeWizards.createBasicWizardPanel(vg, true, a); + l = TestMavenArchetypeProvider.calls.get("createBasicWizardPanel"); + assertNotNull(l); + assertEquals(vg, l.get(0)); + assertEquals(true, l.get(1)); + assertEquals(a, l.get(2)); + + String gid = "gid"; + String aid = "aid"; + String ver = "1.0"; + String repo = "repo"; + String title = "title"; + ArchetypeWizards.createDefinedArchetype(gid, aid, ver, repo, title); + l = TestMavenArchetypeProvider.calls.get("createDefinedArchetype"); + assertNotNull(l); + assertEquals(gid, l.get(0)); + assertEquals(aid, l.get(1)); + assertEquals(ver, l.get(2)); + assertEquals(repo, l.get(3)); + assertEquals(title, l.get(4)); + + File f = getWorkDir(); + + Map m = new HashMap(); + ArchetypeWizards.createFromArchetype(f, pi, a, m, true); + l = TestMavenArchetypeProvider.calls.get("createFromArchetype"); + assertNotNull(l); + assertEquals(f, l.get(0)); + assertEquals(pi, l.get(1)); + assertEquals(a, l.get(2)); + assertEquals(m, l.get(3)); + assertEquals(true, l.get(4)); + + ArchetypeWizards.logUsage(gid, aid, ver); + l = TestMavenArchetypeProvider.calls.get("logUsage"); + assertNotNull(l); + assertEquals(gid, l.get(0)); + assertEquals(aid, l.get(1)); + assertEquals(ver, l.get(2)); + + File f1 = new File(getWorkDir(), "f1"); + File f2 = new File(getWorkDir(), "f2"); + ArchetypeWizards.openProjects(f1, f2); + l = TestMavenArchetypeProvider.calls.get("openProjects"); + assertNotNull(l); + assertEquals(f1, l.get(0)); + assertEquals(f2, l.get(1)); + } + + private static class TestMavenArchetypeProvider extends MavenArchetypeProvider { + + static Map> calls = new HashMap>(); + + public void createFromArchetype(File projDir, ProjectInfo vi, Archetype arch, Map additionalProperties, boolean updateLastUsedProjectDir) throws IOException { + calls.put("createFromArchetype", Arrays.asList(projDir, vi, arch, additionalProperties, updateLastUsedProjectDir)); + } + + @Override + public Set openProjects(File dirF, File mainProjectDir) throws IOException { + calls.put("openProjects", Arrays.asList((Object) dirF, mainProjectDir)); + return null; + } + + public void logUsage(String groupId, String artifactId, String version) { + calls.put("logUsage", Arrays.asList((Object) groupId, artifactId, version)); + } + + public ModelOperation addDependencyOperation(ProjectInfo info, String type) { + calls.put("addDependencyOperation", Arrays.asList(info, type)); + return null; + } + + public WizardDescriptor.Panel createBasicWizardPanel(ValidationGroup vg, boolean isFinish, Archetype archetype) { + calls.put("createBasicWizardPanel", Arrays.asList(vg, isFinish, archetype)); + return null; + } + + public WizardDescriptor.InstantiatingIterator createDefinedArchetype(String groupId, String artifactId, String version, String repository, String title) { + calls.put("createDefinedArchetype", Arrays.asList((Object)groupId, artifactId, version, repository, title)); + return null; + } + + } +} --- maven.archetype/test/unit/src/org/netbeans/modules/maven/archetype/api/ProjectInfoTest.java +++ maven.archetype/test/unit/src/org/netbeans/modules/maven/archetype/api/ProjectInfoTest.java @@ -0,0 +1,62 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2011 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2011 Sun Microsystems, Inc. + */ + +package org.netbeans.modules.maven.archetype.api; + +import org.netbeans.junit.NbTestCase; + +public class ProjectInfoTest extends NbTestCase { + + public ProjectInfoTest(String n) { + super(n); + } + + public void testProjectInfo() { + ProjectInfo pi = new ProjectInfo("gid", "aid", "1.0", "package.name"); + assertEquals("gid", pi.getGroupId()); + assertEquals("aid", pi.getArtifactId()); + assertEquals("1.0", pi.getVersion()); + assertEquals("package.name", pi.getPackageName()); + } + + +}