Skip to content

Commit

Permalink
Merge pull request #440 from laeubi/issue_439
Browse files Browse the repository at this point in the history
#439 - Adopt PDE configurations for maven-bundle-plugins
  • Loading branch information
laeubi authored Jan 5, 2022
2 parents 0ca04e2 + 671ff1d commit 0493981
Show file tree
Hide file tree
Showing 13 changed files with 626 additions and 1 deletion.
7 changes: 7 additions & 0 deletions org.eclipse.m2e.pde.connector/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
28 changes: 28 additions & 0 deletions org.eclipse.m2e.pde.connector/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>org.eclipse.m2e.pde.connector</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ManifestBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.SchemaBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=11
org.eclipse.jdt.core.compiler.compliance=11
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=11
17 changes: 17 additions & 0 deletions org.eclipse.m2e.pde.connector/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: M2E PDE Connector
Bundle-SymbolicName: org.eclipse.m2e.pde.connector;singleton:=true
Bundle-Version: 1.20.0.qualifier
Automatic-Module-Name: org.eclipse.m2e.pde.connector
Bundle-RequiredExecutionEnvironment: JavaSE-11
Bundle-Vendor: Eclipse.org - m2e
Bundle-ActivationPolicy: lazy
Require-Bundle: org.eclipse.m2e.core;bundle-version="1.18.4",
org.eclipse.m2e.jdt;bundle-version="1.18.3",
org.eclipse.core.runtime;bundle-version="3.24.0",
org.eclipse.core.resources;bundle-version="3.16.0",
org.eclipse.m2e.maven.runtime;bundle-version="1.18.2",
org.eclipse.pde.core,
org.eclipse.jdt.core
Bundle-Activator: org.eclipse.m2e.pde.connector.Activator
6 changes: 6 additions & 0 deletions org.eclipse.m2e.pde.connector/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
source.. = src/
output.. = bin/
bin.includes = META-INF/,\
.,\
plugin.xml,\
lifecycle-mapping-metadata.xml
54 changes: 54 additions & 0 deletions org.eclipse.m2e.pde.connector/lifecycle-mapping-metadata.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2008 Sonatype, Inc.
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
which accompanies this distribution, and is available at
http://www.eclipse.org/legal/epl-v10.html
-->

<lifecycleMappingMetadata>
<lifecycleMappings>
<lifecycleMapping>
<packagingType>bundle</packagingType>
<lifecycleMappingId>org.eclipse.m2e.jdt.JarLifecycleMapping
</lifecycleMappingId>
</lifecycleMapping>
</lifecycleMappings>

<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<versionRange>[3.2.0,)</versionRange>
<goals>
<goal>manifest</goal>
<goal>bundle</goal>
</goals>
</pluginExecutionFilter>
<action>
<configurator>
<id>org.eclipse.m2e.pde.connector.configurator.bundle</id>
</configurator>
</action>
</pluginExecution>
<pluginExecution>
<pluginExecutionFilter>
<groupId>biz.aQute.bnd</groupId>
<artifactId>bnd-maven-plugin</artifactId>
<versionRange>[5.0.0,)</versionRange>
<goals>
<goal>jar</goal>
<goal>bnd-process</goal>
</goals>
</pluginExecutionFilter>
<action>
<configurator>
<id>org.eclipse.m2e.pde.connector.configurator.bundle</id>
</configurator>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>

16 changes: 16 additions & 0 deletions org.eclipse.m2e.pde.connector/plugin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension
point="org.eclipse.m2e.core.projectConfigurators">
<configurator
class="org.eclipse.m2e.pde.connector.PDEMavenBundlePluginConfigurator"
id="org.eclipse.m2e.pde.connector.configurator.bundle"
name="PDE integration for maven plugin projects">
</configurator>
</extension>
<extension
point="org.eclipse.m2e.core.lifecycleMappingMetadataSource">
</extension>

</plugin>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*******************************************************************************
* Copyright (c) 2021 Christoph Läubrich
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Christoph Läubrich - initial API and implementation
*******************************************************************************/
package org.eclipse.m2e.pde.connector;

import java.util.Optional;

import org.eclipse.pde.core.project.IBundleProjectService;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.util.tracker.ServiceTracker;

public class Activator implements BundleActivator {

private static volatile ServiceTracker<IBundleProjectService, IBundleProjectService> bundleProjectServiceTracker;

@Override
public void start(BundleContext context) throws Exception {
bundleProjectServiceTracker = new ServiceTracker<>(context, IBundleProjectService.class, null);
bundleProjectServiceTracker.open();
}

@Override
public void stop(BundleContext context) throws Exception {
bundleProjectServiceTracker.close();
}

public static Optional<IBundleProjectService> getBundleProjectService() {
if (bundleProjectServiceTracker == null) {
return Optional.empty();
}
return Optional.ofNullable(bundleProjectServiceTracker.getService());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*******************************************************************************
* Copyright (c) 2008, 2021 Sonatype, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.eclipse.m2e.pde.connector;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.project.MavenProject;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.m2e.core.MavenPlugin;
import org.eclipse.m2e.core.embedder.IMaven;
import org.eclipse.m2e.core.internal.IMavenConstants;
import org.eclipse.m2e.core.internal.markers.MavenProblemInfo;
import org.eclipse.m2e.core.internal.markers.SourceLocation;
import org.eclipse.m2e.core.internal.markers.SourceLocationHelper;
import org.eclipse.m2e.core.lifecyclemapping.model.IPluginExecutionMetadata;
import org.eclipse.m2e.core.project.IMavenProjectFacade;
import org.eclipse.m2e.core.project.configurator.AbstractBuildParticipant;
import org.eclipse.m2e.core.project.configurator.AbstractProjectConfigurator;
import org.eclipse.m2e.core.project.configurator.ILifecycleMappingConfiguration;
import org.eclipse.m2e.core.project.configurator.MojoExecutionBuildParticipant;
import org.eclipse.m2e.core.project.configurator.MojoExecutionKey;
import org.eclipse.m2e.core.project.configurator.ProjectConfigurationRequest;
import org.eclipse.m2e.jdt.IClasspathDescriptor;
import org.eclipse.m2e.jdt.IJavaProjectConfigurator;

/**
* This configurator performs the following tasks:
* <ul>
* <li>Enable the PDE nature for this project to make PDE aware of this
* project</li>
* <li>Set the location of the "bundle-root" where PDE looks for the
* manifest</li>
* </ul>
*/
@SuppressWarnings("restriction")
public class PDEMavenBundlePluginConfigurator extends AbstractProjectConfigurator implements IJavaProjectConfigurator {

private static final String FELIX_PARAM_MANIFESTLOCATION = "manifestLocation";
private static final String FELIX_PARAM_SUPPORTINCREMENTALBUILD = "supportIncrementalBuild";
private static final String FELIX_MANIFEST_GOAL = "manifest";
private static final String BND_PARAM_MANIFESTLOCATION = "manifestPath";
private static final String[] BND_MANIFEST_GOALS = { "bnd-process", "bnd-process-tests", "jar", "test-jar" };

@Override
public void configure(ProjectConfigurationRequest request, IProgressMonitor monitor) throws CoreException {
List<MojoExecution> executions = getMojoExecutions(request, monitor);
List<MavenProblemInfo> problems = new ArrayList<MavenProblemInfo>();
boolean hasManifestExecution = false;
for (MojoExecution execution : executions) {
Plugin plugin = execution.getPlugin();
if (isFelix(plugin)) {
MavenProject mavenProject = request.getMavenProject();
IMaven maven = MavenPlugin.getMaven();
if (isFelixBundleGoal(execution)) {
Boolean supportIncremental = maven.getMojoParameterValue(mavenProject, execution,
FELIX_PARAM_SUPPORTINCREMENTALBUILD, Boolean.class, monitor);
if (supportIncremental == null || !supportIncremental.booleanValue()) {
SourceLocation location = SourceLocationHelper.findLocation(execution.getPlugin(),
"configuration");
MavenProblemInfo problem = new MavenProblemInfo(
"Incremental updates are currently disabled, set supportIncrementalBuild=true to support automatic manifest updates for this project.",
IMarker.SEVERITY_WARNING, location);
problems.add(problem);
}
hasManifestExecution = true;
}
} else if (isBND(plugin)) {
if (isBNDBundleGoal(execution)) {
hasManifestExecution = true;
}
}
}
if (!hasManifestExecution) {
for (MojoExecution execution : executions) {
SourceLocation location = SourceLocationHelper.findLocation(execution.getPlugin(), "executions");
MavenProblemInfo problem = new MavenProblemInfo(
"There is currently no execution that generates a manifest, consider adding an execution for one of the following goal: "
+ (isFelix(execution.getPlugin()) ? FELIX_MANIFEST_GOAL
: Arrays.toString(BND_MANIFEST_GOALS))
+ ".",
IMarker.SEVERITY_WARNING, location);
problems.add(problem);
break;
}
}
if (problems.size() > 0) {
this.markerManager.addErrorMarkers(request.getPom(), IMavenConstants.MARKER_LIFECYCLEMAPPING_ID, problems);
}
IProject project = request.getProject();
IMavenProjectFacade facade = request.getMavenProjectFacade();

IPath metainfPath = getMetainfPath(facade, executions, monitor);

PDEProjectHelper.addPDENature(project, metainfPath, monitor);
}

private boolean isFelixBundleGoal(MojoExecution execution) {
return FELIX_MANIFEST_GOAL.equals(execution.getGoal());
}

private boolean isBNDBundleGoal(MojoExecution execution) {
String executionGoal = execution.getGoal();
for (String goal : BND_MANIFEST_GOALS) {
if (goal.equals(executionGoal)) {
return true;
}
}
return false;
}

@Override
public void configureClasspath(IMavenProjectFacade facade, IClasspathDescriptor classpath, IProgressMonitor monitor)
throws CoreException {
}

@Override
public void configureRawClasspath(ProjectConfigurationRequest request, IClasspathDescriptor classpath,
IProgressMonitor monitor) throws CoreException {
}

private IPath getMetainfPath(IMavenProjectFacade facade, List<MojoExecution> executions, IProgressMonitor monitor)
throws CoreException {
IMaven maven = MavenPlugin.getMaven();
for (MojoExecution execution : executions) {
Plugin plugin = execution.getPlugin();
MavenProject mavenProject = facade.getMavenProject(monitor);
File location = maven.getMojoParameterValue(mavenProject, execution,
isBND(plugin) ? BND_PARAM_MANIFESTLOCATION : FELIX_PARAM_MANIFESTLOCATION, File.class, monitor);
if (location != null) {
return facade.getProjectRelativePath(location.getAbsolutePath());
}
}
return null;
}

private boolean isBND(Plugin plugin) {
return plugin != null && "bnd-maven-plugin".equals(plugin.getArtifactId());
}

private boolean isFelix(Plugin plugin) {
return plugin != null && "org.apache.felix".equals(plugin.getGroupId())
&& "maven-bundle-plugin".equals(plugin.getArtifactId());
}

@Override
public boolean hasConfigurationChanged(IMavenProjectFacade newFacade,
ILifecycleMappingConfiguration oldProjectConfiguration, MojoExecutionKey key, IProgressMonitor monitor) {
return false;
}

@Override
public AbstractBuildParticipant getBuildParticipant(IMavenProjectFacade projectFacade, MojoExecution execution,
IPluginExecutionMetadata executionMetadata) {
if ((isFelix(execution.getPlugin()) && isFelixBundleGoal(execution))
|| (isBND(execution.getPlugin()) && isBNDBundleGoal(execution))) {
return new MojoExecutionBuildParticipant(execution, true, true);
}
return super.getBuildParticipant(projectFacade, execution, executionMetadata);
}
}
Loading

0 comments on commit 0493981

Please sign in to comment.