|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2024 Christoph Läubrich |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + * |
| 10 | + * Contributors: |
| 11 | + * Christoph Läubrich - initial API and implementation |
| 12 | + *******************************************************************************/ |
1 | 13 | package org.eclipse.m2e.bnd.ui; |
2 | 14 |
|
3 | 15 | import java.util.List; |
4 | 16 | import java.util.Map; |
| 17 | +import java.util.Properties; |
5 | 18 |
|
| 19 | +import org.apache.maven.project.MavenProject; |
6 | 20 | import org.eclipse.core.resources.IProject; |
7 | 21 | import org.eclipse.core.runtime.AdapterTypes; |
8 | 22 | import org.eclipse.core.runtime.Adapters; |
|
13 | 27 | import org.osgi.service.component.annotations.Component; |
14 | 28 |
|
15 | 29 | import aQute.bnd.build.Project; |
| 30 | +import aQute.bnd.build.Workspace; |
| 31 | +import aQute.bnd.maven.lib.configuration.BeanProperties; |
| 32 | +import aQute.bnd.osgi.Processor; |
16 | 33 |
|
17 | 34 | /** |
18 | 35 | * Adapts eclipse projects managed by m2e to bnd projects |
19 | 36 | */ |
20 | 37 | @Component |
21 | 38 | @AdapterTypes(adaptableClass = IProject.class, adapterNames = Project.class) |
22 | | -public class BndPluginAdapter implements IAdapterFactory{ |
| 39 | +public class BndPluginAdapter implements IAdapterFactory { |
23 | 40 |
|
24 | 41 | @Override |
25 | 42 | public <T> T getAdapter(Object adaptableObject, Class<T> adapterType) { |
26 | | - if (adaptableObject instanceof IProject eclipseProject) { |
27 | | - IMavenProjectFacade mavenProject = Adapters.adapt(eclipseProject, IMavenProjectFacade.class); |
28 | | - if (isRelevantProject(mavenProject)) { |
29 | | - System.out.println(eclipseProject.getName() + " uses bnd plugin!"); |
| 43 | + try { |
| 44 | + if (adaptableObject instanceof IProject eclipseProject) { |
| 45 | + if (adapterType == Project.class) { |
| 46 | + IMavenProjectFacade mavenProject = Adapters.adapt(eclipseProject, IMavenProjectFacade.class); |
| 47 | + if (isRelevantProject(mavenProject)) { |
| 48 | + System.out.println(eclipseProject.getName() + " uses bnd plugin!"); |
| 49 | + BeanProperties beanProperties = new BeanProperties(); |
| 50 | + MavenProject mp = mavenProject.getMavenProject(); |
| 51 | + if (mp != null) { |
| 52 | + beanProperties.put("project", mp); |
| 53 | + } |
| 54 | + // TODO beanProperties.put("settings", settings); |
| 55 | + Properties processorProperties = new Properties(beanProperties); |
| 56 | + if (mp != null) { |
| 57 | + Properties projectProperties = mp.getProperties(); |
| 58 | + for (String key : projectProperties.stringPropertyNames()) { |
| 59 | + processorProperties.setProperty(key, projectProperties.getProperty(key)); |
| 60 | + } |
| 61 | + } |
| 62 | + Processor processor = new Processor(processorProperties, false); |
| 63 | + |
| 64 | + // TODO propertiesFile = new BndConfiguration(project, |
| 65 | + // mojoExecution).loadProperties(builder); |
| 66 | + |
| 67 | + Workspace standaloneWorkspace = Workspace.createStandaloneWorkspace(processor, |
| 68 | + mavenProject.getPomFile().getParentFile().toURI()); |
| 69 | + if (mp == null) { |
| 70 | + standaloneWorkspace.set("workspaceName", mavenProject.getArtifactKey().toPortableString()); |
| 71 | + } else { |
| 72 | + // TODO make part of IMavenProjectFacade |
| 73 | + standaloneWorkspace.set("workspaceName", mp.getName()); |
| 74 | + String description = mp.getDescription(); |
| 75 | + if (description != null) { |
| 76 | + standaloneWorkspace.set("workspaceDescription", description); |
| 77 | + } |
| 78 | + } |
| 79 | + Project bndProject = new Project(standaloneWorkspace, null, null); |
| 80 | + bndProject.setBase(null); |
| 81 | + return adapterType.cast(bndProject); |
| 82 | + } |
| 83 | + } |
30 | 84 | } |
| 85 | + } catch (Exception e) { |
| 86 | + e.printStackTrace(); |
31 | 87 | } |
32 | 88 | return null; |
33 | 89 | } |
34 | 90 |
|
35 | 91 | private boolean isRelevantProject(IMavenProjectFacade mavenProject) { |
36 | 92 | // TODO cache result inside IProject store |
37 | 93 | if (mavenProject != null) { |
38 | | - Map<MojoExecutionKey, List<IPluginExecutionMetadata>> mapping = mavenProject |
39 | | - .getMojoExecutionMapping(); |
| 94 | + Map<MojoExecutionKey, List<IPluginExecutionMetadata>> mapping = mavenProject.getMojoExecutionMapping(); |
40 | 95 | for (MojoExecutionKey key : mapping.keySet()) { |
41 | 96 | if ("biz.aQute.bnd".equals(key.groupId()) && "bnd-maven-plugin".equals(key.artifactId())) { |
42 | 97 | return true; |
|
0 commit comments