Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TP] Update to Archetype 3.3.1 and unify archetypes OSGi metadata #1942

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion m2e-core-tests
15 changes: 7 additions & 8 deletions org.eclipse.m2e.core.ui/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,17 @@ Require-Bundle: org.eclipse.m2e.core;bundle-version="[2.0.0,3.0.0)",
org.eclipse.ui,
org.eclipse.ui.navigator,
org.eclipse.debug.ui
Import-Package: org.apache.maven.archetype.catalog;provider=m2e;version="[3.2.1,4.0.0)",
org.apache.maven.archetype.catalog.io.xpp3;provider=m2e;version="[3.2.1,4.0.0)",
org.apache.maven.archetype.common;provider=m2e;version="[3.2.1,4.0.0)",
org.apache.maven.archetype.exception;provider=m2e;version="[3.2.1,4.0.0)",
org.apache.maven.archetype.metadata;provider=m2e;version="[3.2.1,4.0.0)",
org.apache.maven.archetype.source;provider=m2e;version="[3.2.1,4.0.0)",
org.apache.maven.shared.transfer.artifact;provider=m2e;version="[0.13.1,1.0.0)",
Import-Package: org.apache.maven.archetype.catalog;provider=m2e;version="[3.3.0,4.0.0)",
org.apache.maven.archetype.catalog.io.xpp3;provider=m2e;version="[3.3.0,4.0.0)",
org.apache.maven.archetype.common;provider=m2e;version="[3.3.0,4.0.0)",
org.apache.maven.archetype.downloader;provider=m2e;version="[3.3.0,4.0.0)",
org.apache.maven.archetype.exception;provider=m2e;version="[3.3.0,4.0.0)",
org.apache.maven.archetype.metadata;provider=m2e;version="[3.3.0,4.0.0)",
org.apache.maven.archetype.source;provider=m2e;version="[3.3.0,4.0.0)",
org.eclipse.compare.rangedifferencer,
org.eclipse.ltk.core.refactoring,
org.slf4j;version="[1.7.0,3.0.0)"
Service-Component: OSGI-INF/component.xml,
OSGI-INF/org.eclipse.m2e.core.ui.internal.archetype.ArchetypeGenerator.xml,
OSGI-INF/org.eclipse.m2e.core.ui.internal.archetype.ArchetypePlugin.xml
Automatic-Module-Name: org.eclipse.m2e.core.ui

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package org.eclipse.m2e.core.ui.internal.archetype;

import java.io.File;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
Expand All @@ -24,6 +25,11 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.repository.LocalRepository;
import org.eclipse.aether.repository.NoLocalRepositoryManagerException;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.spi.localrepo.LocalRepositoryManagerFactory;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Status;
import org.eclipse.osgi.util.NLS;
Expand All @@ -34,12 +40,8 @@
import org.apache.maven.archetype.source.ArchetypeDataSource;
import org.apache.maven.archetype.source.ArchetypeDataSourceException;
import org.apache.maven.archetype.source.RemoteCatalogArchetypeDataSource;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.MavenArtifactRepository;
import org.apache.maven.project.ProjectBuildingRequest;

import org.eclipse.m2e.core.embedder.IMaven;
import org.eclipse.m2e.core.embedder.IMavenExecutionContext;
import org.eclipse.m2e.core.internal.Messages;


Expand Down Expand Up @@ -111,7 +113,7 @@ public static class InternalCatalogFactory extends ArchetypeCatalogFactory {
@Override
public ArchetypeCatalog getArchetypeCatalog() {
try {
return source.getArchetypeCatalog(null);
return source.getArchetypeCatalog(null, null);
} catch(ArchetypeDataSourceException e) {
return new ArchetypeCatalog();
}
Expand All @@ -137,9 +139,8 @@ public static class DefaultLocalCatalogFactory extends ArchetypeCatalogFactory {
@Override
public ArchetypeCatalog getArchetypeCatalog() throws CoreException {
return maven.createExecutionContext().execute((ctx, m) -> {
ProjectBuildingRequest buildingRequest = ctx.newProjectBuildingRequest();
try {
return source.getArchetypeCatalog(buildingRequest);
return source.getArchetypeCatalog(ctx.getRepositorySession(), ArchetypePlugin.getRemoteRepositories(ctx));
} catch(ArchetypeDataSourceException e) {
return new ArchetypeCatalog();
}
Expand Down Expand Up @@ -172,39 +173,38 @@ public ArchetypeCatalog getArchetypeCatalog() throws CoreException {
ArchetypeCatalog catalog = getEmbeddedCatalog();
if(catalog == null) {
//local but not embedded catalog
IMavenExecutionContext context = maven.createExecutionContext();
ArtifactRepository localRepository = new MavenArtifactRepository();
localRepository.setUrl(getLocalRepositoryURL());
context.getExecutionRequest().setLocalRepository(localRepository);
return context.execute((ctx, m) -> {
ProjectBuildingRequest buildingRequest = ctx.newProjectBuildingRequest();
buildingRequest.setLocalRepository(localRepository);
File localRepositoryPath = getLocalRepositoryPath();
return maven.createExecutionContext().execute((ctx, m) -> {
try {
return source.getArchetypeCatalog(buildingRequest);
} catch(ArchetypeDataSourceException e) {
var managerFactory = ctx.getComponentLookup().lookup(LocalRepositoryManagerFactory.class);
DefaultRepositorySystemSession session = new DefaultRepositorySystemSession(ctx.getRepositorySession());
session.setLocalRepositoryManager(
managerFactory.newInstance(session, new LocalRepository(localRepositoryPath)));
return source.getArchetypeCatalog(session, List.of());
} catch(ArchetypeDataSourceException | NoLocalRepositoryManagerException e) {
return new ArchetypeCatalog();
}
}, null);
}
return catalog;
}

private String getLocalRepositoryURL() {
private File getLocalRepositoryPath() {
Path path;
try { // First try to use the id as a path, then as a URI else as it is
path = Path.of(getId());
} catch(Exception e1) {
try {
path = Path.of(new URI(getId()));
} catch(Exception e2) {
return getId();
return new File(getId());
}
}
path = path.toAbsolutePath();
if(Files.isRegularFile(path)) {
path = path.getParent();
}
return path.toUri().toString();
return path.toFile();
}

private ArchetypeCatalog getEmbeddedCatalog() throws CoreException {
Expand Down Expand Up @@ -281,13 +281,12 @@ public ArchetypeCatalog getArchetypeCatalog() throws CoreException {
final String remoteUrl = url;

ArchetypeCatalog catalog = maven.createExecutionContext().execute((ctx, m) -> {
ProjectBuildingRequest buildingRequest = ctx.newProjectBuildingRequest();
try {
ArtifactRepository archeTypeRepo = new MavenArtifactRepository();
archeTypeRepo.setUrl(remoteUrl);
archeTypeRepo.setId(RemoteCatalogArchetypeDataSource.ARCHETYPE_REPOSITORY_ID);
buildingRequest.getRemoteRepositories().add(archeTypeRepo);
return source.getArchetypeCatalog(buildingRequest);
RemoteRepository archetypeRepo = new RemoteRepository.Builder(
RemoteCatalogArchetypeDataSource.ARCHETYPE_REPOSITORY_ID, "", remoteUrl).build();
List<RemoteRepository> remoteRepositories = new ArrayList<>(ArchetypePlugin.getRemoteRepositories(ctx));
remoteRepositories.add(0, archetypeRepo);
return source.getArchetypeCatalog(ctx.getRepositorySession(), remoteRepositories);
} catch(ArchetypeDataSourceException e) {
return new ArchetypeCatalog();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Map;
import java.util.stream.Collectors;

import org.osgi.framework.FrameworkUtil;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
Expand All @@ -34,33 +35,41 @@
import org.slf4j.LoggerFactory;

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.TypeLiteral;
import com.google.inject.util.Modules;
import com.google.inject.util.Types;

import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.sisu.space.BeanScanning;
import org.eclipse.sisu.space.BundleClassSpace;
import org.eclipse.sisu.space.ClassSpace;
import org.eclipse.sisu.space.SpaceModule;
import org.eclipse.sisu.wire.WireModule;

import org.codehaus.plexus.ContainerConfiguration;
import org.codehaus.plexus.DefaultContainerConfiguration;
import org.codehaus.plexus.DefaultPlexusContainer;
import org.codehaus.plexus.PlexusConstants;
import org.codehaus.plexus.PlexusContainerException;
import org.codehaus.plexus.classworlds.ClassWorld;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.codehaus.plexus.logging.LogEnabled;

import org.apache.maven.RepositoryUtils;
import org.apache.maven.archetype.catalog.Archetype;
import org.apache.maven.archetype.common.ArchetypeArtifactManager;
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.archetype.source.ArchetypeDataSource;
import org.apache.maven.archetype.source.ArchetypeDataSourceException;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.cli.logging.Slf4jLogger;

import org.eclipse.m2e.core.embedder.IMaven;
import org.eclipse.m2e.core.embedder.IMavenExecutionContext;
import org.eclipse.m2e.core.internal.MavenPluginActivator;
import org.eclipse.m2e.core.project.IArchetype;
import org.eclipse.m2e.core.ui.internal.M2EUIPluginActivator;
Expand Down Expand Up @@ -94,30 +103,40 @@ public class ArchetypePlugin {

private Map<String, ArchetypeDataSource> archetypeDataSourceMap;

private DefaultPlexusContainer container;

public ArchetypePlugin() {
this.configFile = new File(MavenPluginActivator.getDefault().getStateLocation().toFile(),
M2EUIPluginActivator.PREFS_ARCHETYPES);
this.writer = new ArchetypeCatalogsWriter();
}

@Activate
void activate() throws PlexusContainerException, ComponentLookupException {
final Module logginModule = new AbstractModule() {
void activate() {
Module localBindings = new AbstractModule() {
@Override
protected void configure() {
bind(ILoggerFactory.class).toInstance(LoggerFactory.getILoggerFactory());
try {
bind(RepositorySystem.class).toInstance(MavenPluginActivator.getDefault().getRepositorySystem());
} catch(CoreException ex) {
ex.printStackTrace();
}
}
};
final ContainerConfiguration cc = new DefaultContainerConfiguration() //
.setClassWorld(new ClassWorld("plexus.core", ArchetypeArtifactManager.class.getClassLoader())) //$NON-NLS-1$
.setClassPathScanning(PlexusConstants.SCANNING_INDEX) //
.setAutoWiring(true) //
.setName("plexus"); //$NON-NLS-1$
container = new DefaultPlexusContainer(cc, logginModule);
archetypeArtifactManager = container.lookup(ArchetypeArtifactManager.class);
archetypeDataSourceMap = container.lookupMap(ArchetypeDataSource.class);
ClassSpace space = new BundleClassSpace(FrameworkUtil.getBundle(ArchetypeArtifactManager.class));
WireModule wireModule = new WireModule(new SpaceModule(space, BeanScanning.INDEX));
Injector injector = Guice.createInjector(Modules.override(wireModule).with(localBindings));
@SuppressWarnings("unchecked")
TypeLiteral<Map<String, ArchetypeDataSource>> mapOfDataSourcesType = (TypeLiteral<Map<String, ArchetypeDataSource>>) TypeLiteral
.get(Types.mapOf(String.class, ArchetypeDataSource.class));
archetypeArtifactManager = injector.getInstance(ArchetypeArtifactManager.class);
archetypeDataSourceMap = injector.getInstance(Key.get(mapOfDataSourcesType));

if(archetypeArtifactManager instanceof LogEnabled logEnabled) {
logEnabled.enableLogging(new Slf4jLogger(LoggerFactory.getLogger(archetypeArtifactManager.getClass())));
}
archetypeDataSourceMap.values().stream().filter(LogEnabled.class::isInstance).map(LogEnabled.class::cast)
.forEach(d -> d.enableLogging(new Slf4jLogger(LoggerFactory.getLogger(d.getClass()))));

addArchetypeCatalogFactory(
new ArchetypeCatalogFactory.InternalCatalogFactory(archetypeDataSourceMap.get("internal-catalog")));
addArchetypeCatalogFactory(
Expand All @@ -135,7 +154,6 @@ protected void configure() {
@Deactivate
void shutdown() throws IOException {
saveCatalogs();
container.dispose();
}

public LocalCatalogFactory newLocalCatalogFactory(String path, String description, boolean editable,
Expand Down Expand Up @@ -219,36 +237,33 @@ public List<RequiredProperty> getRequiredProperties(IArchetype archetype, IProgr
final String artifactId = archetype.getArtifactId();
final String version = archetype.getVersion();

final List<ArtifactRepository> repositories = new ArrayList<>(maven.getArtifactRepositories());

return maven.createExecutionContext().execute((context, monitor1) -> {
ArtifactRepository localRepository = context.getLocalRepository();
if(archetypeArtifactManager.isFileSetArchetype(groupId, artifactId, version, null, localRepository, repositories,
context.newProjectBuildingRequest())) {
ArchetypeDescriptor descriptor;
try {
descriptor = archetypeArtifactManager.getFileSetArchetypeDescriptor(groupId, artifactId, version, null,
localRepository, repositories, context.newProjectBuildingRequest());
} catch(UnknownArchetype ex) {
throw new CoreException(Status.error("UnknownArchetype", ex));
try {
File archetypeFile = archetypeArtifactManager.getArchetypeFile(groupId, artifactId, version,
getRemoteRepositories(context), context.getRepositorySession());
if(archetypeArtifactManager.isFileSetArchetype(archetypeFile)) {
ArchetypeDescriptor descriptor = archetypeArtifactManager.getFileSetArchetypeDescriptor(archetypeFile);
return descriptor.getRequiredProperties();
}
return descriptor.getRequiredProperties();
} catch(UnknownArchetype ex) {
throw new CoreException(Status.error("UnknownArchetype", ex));
}
return null;
}, monitor);
}

public void updateLocalCatalog(Archetype archetype) throws CoreException {
maven.createExecutionContext().execute((ctx, m) -> {
ProjectBuildingRequest request = ctx.newProjectBuildingRequest();
try {
ArchetypeDataSource source = archetypeDataSourceMap.get("catalog");

source.updateCatalog(request, archetype);
source.updateCatalog(ctx.getRepositorySession(), archetype);
} catch(ArchetypeDataSourceException e) {
}
return null;
}, null);
}

static List<RemoteRepository> getRemoteRepositories(IMavenExecutionContext ctx) throws CoreException {
return RepositoryUtils.toRepos(ctx.getExecutionRequest().getRemoteRepositories());
}
}
4 changes: 0 additions & 4 deletions org.eclipse.m2e.feature/feature.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@
id="org.eclipse.m2e.archetype.descriptor"
version="0.0.0"/>

<plugin
id="org.eclipse.m2e.archetype.maven-artifact-transfer"
version="0.0.0"/>

<plugin
id="org.eclipse.m2e.model.edit"
version="0.0.0"/>
Expand Down
10 changes: 6 additions & 4 deletions org.eclipse.m2e.maven.runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
</parent>

<artifactId>org.eclipse.m2e.maven.runtime</artifactId>
<version>3.9.900-SNAPSHOT</version>
<version>3.9.901-SNAPSHOT</version>
<packaging>jar</packaging>

<name>M2E Embedded Maven Runtime (includes Incubating components)</name>
Expand Down Expand Up @@ -140,7 +140,7 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.16</version>
<version>2.0.17</version><!--$NO-MVN-MAN-VER$-->
<scope>provided</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -202,8 +202,10 @@
org.slf4j.*;version="[1.7.31,3.0.0)",\
javax.inject;version="[1.0.0,2.0.0)",\
javax.annotation;version="[1.2.0,2.0.0)", \
org.apache.commons.cli;version="[1.4.0,2.0.0)", \
org.apache.commons.codec*
org.apache.commons.cli;version="[1.4.0,2.0.0)",\
org.apache.commons.codec*,\
org.osgi.*,\

Require-Bundle: \
com.google.guava;bundle-version="32.2.1"

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
<dependency>
<groupId>org.eclipse.m2e</groupId>
<artifactId>org.eclipse.m2e.maven.runtime</artifactId>
<version>3.9.900-SNAPSHOT</version>
<version>3.9.901-SNAPSHOT</version>
</dependency>
</dependencies>

Expand Down
Loading
Loading