Author: fbricon
Date: 2011-10-24 15:02:10 -0400 (Mon, 24 Oct 2011)
New Revision: 35958
Modified:
trunk/maven/plugins/org.jboss.tools.maven.project.examples/src/org/jboss/tools/maven/project/examples/wizard/ArchetypeExamplesWizardPage.java
Log:
JBIDE-10018 fix Example Archetypes don't resolve correctly if their remote repository
is not declared in the settings.xml
Modified:
trunk/maven/plugins/org.jboss.tools.maven.project.examples/src/org/jboss/tools/maven/project/examples/wizard/ArchetypeExamplesWizardPage.java
===================================================================
---
trunk/maven/plugins/org.jboss.tools.maven.project.examples/src/org/jboss/tools/maven/project/examples/wizard/ArchetypeExamplesWizardPage.java 2011-10-24
19:01:35 UTC (rev 35957)
+++
trunk/maven/plugins/org.jboss.tools.maven.project.examples/src/org/jboss/tools/maven/project/examples/wizard/ArchetypeExamplesWizardPage.java 2011-10-24
19:02:10 UTC (rev 35958)
@@ -11,6 +11,7 @@
package org.jboss.tools.maven.project.examples.wizard;
import java.lang.reflect.InvocationTargetException;
+import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
@@ -19,14 +20,23 @@
import org.apache.maven.archetype.exception.UnknownArchetype;
import org.apache.maven.archetype.metadata.ArchetypeDescriptor;
import org.apache.maven.archetype.metadata.RequiredProperty;
+import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.codehaus.plexus.util.StringUtils;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.Status;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.m2e.core.MavenPlugin;
import org.eclipse.m2e.core.embedder.IMaven;
+import org.eclipse.m2e.core.embedder.IMavenConfiguration;
+import org.eclipse.m2e.core.internal.IMavenConstants;
import org.eclipse.m2e.core.internal.MavenPluginActivator;
+import org.eclipse.m2e.core.internal.Messages;
+import org.eclipse.m2e.core.internal.archetype.ArchetypeManager;
+import
org.eclipse.m2e.core.internal.archetype.ArchetypeCatalogFactory.RemoteCatalogFactory;
import org.eclipse.m2e.core.project.ProjectImportConfiguration;
import
org.eclipse.m2e.core.ui.internal.wizards.MavenProjectWizardArchetypeParametersPage;
import org.eclipse.osgi.util.NLS;
@@ -72,7 +82,7 @@
//as we don't have the guarantee archetypeModel holds ALL the required properties
//This really is an extra-safe guard, as I believe we'll probably always
//redefine all required properties in project-examples-maven-xxx.xml
- Properties defaultRequiredProperties = getRequiredProperties(archetype);
+ Properties defaultRequiredProperties = getRequiredProperties(archetype,
archetypeModel.getArchetypeRepository());
Properties properties = new Properties();
for (Object key : archetypeModel.getArchetypeProperties().keySet()) {
properties.put(key, archetypeModel.getArchetypeProperties().get(key));
@@ -109,7 +119,7 @@
return archetype;
}
- private Properties getRequiredProperties(Archetype archetype) {
+ private Properties getRequiredProperties(Archetype archetype, final String
archetypeRepositoryUrl) {
final String groupId = archetype.getGroupId();
final String artifactId = archetype.getArtifactId();
final String version = archetype.getVersion();
@@ -122,14 +132,22 @@
try {
IMaven maven = MavenPlugin.getMaven();
- ArtifactRepository localRepository = maven.getLocalRepository();
-
List<ArtifactRepository> repositories =
maven.getArtifactRepositories();
+
+ ArtifactRepository archetypeRepository = null;
+
+ if (StringUtils.isNotEmpty(archetypeRepositoryUrl)) {
+ archetypeRepository =
maven.createArtifactRepository("archetypeRepo", archetypeRepositoryUrl);
+ }
+ //JBIDE-10018 : remote archetypes need to be downloaded 1st or resolution
will fail
+ Artifact a = downloadArchetype(groupId, artifactId, version,
archetypeRepository, repositories);
+
ArchetypeArtifactManager aaMgr =
MavenPluginActivator.getDefault().getArchetypeArtifactManager();
- if(aaMgr.isFileSetArchetype(groupId, artifactId, version, null,
localRepository, repositories)) {
- ArchetypeDescriptor descriptor =
aaMgr.getFileSetArchetypeDescriptor(groupId, artifactId, version, null,
- localRepository, repositories);
+
+ ArchetypeDescriptor descriptor =
aaMgr.getFileSetArchetypeDescriptor(a.getFile());
+
+ if(descriptor != null && descriptor.getName() != null) {
List<?> properties = descriptor.getRequiredProperties();
if(properties != null) {
for(Object o : properties) {
@@ -142,12 +160,15 @@
}
} catch(UnknownArchetype e) {
MavenProjectExamplesActivator.log(e);
+ //TODO don't swallow exceptions
} catch(CoreException ex) {
+ //TODO don't swallow exceptions
MavenProjectExamplesActivator.log(ex);
} finally {
monitor.done();
}
}
+
});
} catch(InterruptedException ex) {
// ignore
@@ -158,4 +179,27 @@
}
return requiredProperties;
}
+
+
+ /**
+ * Apparently, Archetype#generateProjectFromArchetype 2.0-alpha-4 does not attempt to
resolve archetype
+ * from configured remote repositories. To compensate, we populate local repo with
archetype pom/jar.
+ * So we use the same hack as in m2e to force the download first.
+ * @see
http://git.eclipse.org/c/m2e/m2e-core.git/tree/org.eclipse.m2e.core/src/o...
+ */
+ private Artifact downloadArchetype(String groupId, String artifactId,
+ String version, ArtifactRepository archetypeRepository,
+ List<ArtifactRepository> repositories) throws CoreException {
+
+ IMaven maven = MavenPlugin.getMaven();
+ ArrayList<ArtifactRepository> repos = new
ArrayList<ArtifactRepository>();
+ if (archetypeRepository != null) {
+ repos.add(archetypeRepository);
+ }
+ repos.addAll(maven.getArtifactRepositories()); // see
org.apache.maven.archetype.downloader.DefaultDownloader#download
+ IProgressMonitor nullProgressMonitor = new NullProgressMonitor();
+ maven.resolve(groupId, artifactId, version, "pom", null, repos,
nullProgressMonitor); //$NON-NLS-1$
+ return maven.resolve(groupId, artifactId, version, "jar", null, repos,
nullProgressMonitor); //$NON-NLS-1$
+ }
+
}