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

Only add matching fragments to the classpath #4408

Merged
merged 2 commits into from
Nov 4, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
Expand All @@ -41,8 +40,6 @@
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.logging.Logger;
import org.eclipse.equinox.p2.metadata.IRequirement;
import org.eclipse.osgi.container.namespaces.EclipsePlatformNamespace;
import org.eclipse.osgi.internal.framework.FilterImpl;
import org.eclipse.tycho.ArtifactDescriptor;
import org.eclipse.tycho.ArtifactKey;
import org.eclipse.tycho.ArtifactType;
Expand All @@ -54,7 +51,6 @@
import org.eclipse.tycho.ExecutionEnvironmentConfiguration;
import org.eclipse.tycho.OptionalResolutionAction;
import org.eclipse.tycho.PackagingType;
import org.eclipse.tycho.PlatformPropertiesUtils;
import org.eclipse.tycho.ReactorProject;
import org.eclipse.tycho.ResolvedArtifactKey;
import org.eclipse.tycho.TargetEnvironment;
Expand All @@ -79,8 +75,6 @@
import org.eclipse.tycho.model.classpath.LibraryClasspathEntry;
import org.eclipse.tycho.model.classpath.ProjectClasspathEntry;
import org.osgi.framework.Filter;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.InvalidSyntaxException;

@Component(role = TychoProject.class, hint = PackagingType.TYPE_ECLIPSE_PLUGIN)
public class OsgiBundleProject extends AbstractTychoProject implements BundleProject {
Expand Down Expand Up @@ -441,12 +435,18 @@ private void addExtraClasspathEntries(List<ClasspathEntry> classpath, ReactorPro
}
}
}
Collection<TargetEnvironment> environments = projectManager
.getTargetEnvironments(project.adapt(MavenProject.class));
//Fragments are like embedded dependencies...
for (ArtifactDescriptor fragment : artifacts.getFragments()) {
File location = fragment.getLocation(true);
if (location != null) {
classpath.add(new DefaultClasspathEntry(null, readArtifactKey(location),
Collections.singletonList(location), null));
OsgiManifest manifest = bundleReader.loadManifest(location);
Filter filter = manifest.getTargetEnvironmentFilter();
if (filter == null || environments.stream().anyMatch(env -> env.match(filter))) {
classpath.add(new DefaultClasspathEntry(null, readArtifactKey(location),
Collections.singletonList(location), null));
}
}
}
}
Expand Down Expand Up @@ -511,53 +511,12 @@ private File getNestedJarOrDir(ArtifactDescriptor bundle, String cp) {

@Override
public TargetEnvironment getImplicitTargetEnvironment(MavenProject project) {
String filterStr = getManifestValue(EclipsePlatformNamespace.ECLIPSE_PLATFORM_FILTER_HEADER, project);

if (filterStr != null) {
try {
FilterImpl filter = FilterImpl.newInstance(filterStr);

String ws = sn(filter.getPrimaryKeyValue(PlatformPropertiesUtils.OSGI_WS));
String os = sn(filter.getPrimaryKeyValue(PlatformPropertiesUtils.OSGI_OS));
String arch = sn(filter.getPrimaryKeyValue(PlatformPropertiesUtils.OSGI_ARCH));

// validate if os/ws/arch are not null and actually match the filter
if (ws != null && os != null && arch != null) {
Map<String, String> properties = new HashMap<>();
properties.put(PlatformPropertiesUtils.OSGI_WS, ws);
properties.put(PlatformPropertiesUtils.OSGI_OS, os);
properties.put(PlatformPropertiesUtils.OSGI_ARCH, arch);

if (filter.matches(properties)) {
return new TargetEnvironment(os, ws, arch);
}
}
} catch (InvalidSyntaxException e) {
// at least we tried...
}
}

return null;
return getManifest(DefaultReactorProject.adapt(project)).getImplicitTargetEnvironment();
}

@Override
public Filter getTargetEnvironmentFilter(MavenProject project) {
String filterStr = getManifestValue(EclipsePlatformNamespace.ECLIPSE_PLATFORM_FILTER_HEADER, project);
if (filterStr != null) {
try {
return FrameworkUtil.createFilter(filterStr);
} catch (InvalidSyntaxException e) {
// at least we tried...
}
}
return super.getTargetEnvironmentFilter(project);
}

private static String sn(String str) {
if (str != null && !str.isBlank()) {
return str;
}
return null;
return getManifest(DefaultReactorProject.adapt(project)).getTargetEnvironmentFilter();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,26 @@

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.jar.Attributes.Name;

import org.eclipse.osgi.container.builders.OSGiManifestBuilderFactory;
import org.eclipse.osgi.container.namespaces.EclipsePlatformNamespace;
import org.eclipse.osgi.framework.util.CaseInsensitiveDictionaryMap;
import org.eclipse.osgi.internal.framework.FilterImpl;
import org.eclipse.osgi.util.ManifestElement;
import org.eclipse.tycho.ArtifactKey;
import org.eclipse.tycho.ArtifactType;
import org.eclipse.tycho.DefaultArtifactKey;
import org.eclipse.tycho.PlatformPropertiesUtils;
import org.eclipse.tycho.TargetEnvironment;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;
import org.osgi.framework.Filter;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.Version;

/**
Expand Down Expand Up @@ -204,4 +212,51 @@ private String[] parseBundleClasspath() {
return result;
}

public Filter getTargetEnvironmentFilter() {
String filterStr = getValue(EclipsePlatformNamespace.ECLIPSE_PLATFORM_FILTER_HEADER);
if (filterStr != null) {
try {
return FrameworkUtil.createFilter(filterStr);
} catch (InvalidSyntaxException e) {
// at least we tried...
}
}
return null;
}

public TargetEnvironment getImplicitTargetEnvironment() {
String filterStr = getValue(EclipsePlatformNamespace.ECLIPSE_PLATFORM_FILTER_HEADER);
if (filterStr != null) {
try {
FilterImpl filter = FilterImpl.newInstance(filterStr);

String ws = sn(filter.getPrimaryKeyValue(PlatformPropertiesUtils.OSGI_WS));
String os = sn(filter.getPrimaryKeyValue(PlatformPropertiesUtils.OSGI_OS));
String arch = sn(filter.getPrimaryKeyValue(PlatformPropertiesUtils.OSGI_ARCH));

// validate if os/ws/arch are not null and actually match the filter
if (ws != null && os != null && arch != null) {
Map<String, String> properties = new HashMap<>();
properties.put(PlatformPropertiesUtils.OSGI_WS, ws);
properties.put(PlatformPropertiesUtils.OSGI_OS, os);
properties.put(PlatformPropertiesUtils.OSGI_ARCH, arch);

if (filter.matches(properties)) {
return new TargetEnvironment(os, ws, arch);
}
}
} catch (InvalidSyntaxException e) {
// at least we tried...
}
}
return null;
}

private static String sn(String str) {
if (str != null && !str.isBlank()) {
return str;
}
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?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-1.8">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="test" path="test">
<attributes>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
</classpath>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Manifest-Version: 1.0
Fragment-Host: compiler.fragments.unmatchinginp2; bundle-version="[1.0.0,1.1.0)"
Bundle-Name: %fragmentName
Bundle-Vendor: %providerName
Bundle-SymbolicName: compiler.fragments.unmatchinginp2.gtk.linux.x86; singleton:=true
Bundle-Version: 1.0.0.qualifier
Bundle-ManifestVersion: 2
Bundle-Localization: fragment
Export-Package: org.eclipse.swt.graphics
Eclipse-PlatformFilter: (& (osgi.ws=gtk) (osgi.os=linux) (osgi.arch=x86))
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bin.includes = .,META-INF/
source.. = src/
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.eclipse.tycho.compiler.fragments.unmatchinginp2</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<artifactId>compiler.fragments.unmatchinginp2.gtk.linux.x86</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>eclipse-plugin</packaging>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>target-platform-configuration</artifactId>
<configuration>
<dependency-resolution>
<optionalDependencies>ignore</optionalDependencies>
<profileProperties>
<org.eclipse.swt.buildtime>true</org.eclipse.swt.buildtime>
</profileProperties>
</dependency-resolution>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.eclipse.swt.graphics;

public final class Image {
public static void test() {
Image2 instanceOfClassFromUnmatchingFragment = new Image2();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.eclipse.swt.graphics;

public final class ImageTest {
public static void test() {
Image2 instanceOfClassFromUnmatchingFragment = new Image2();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?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-1.8">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Manifest-Version: 1.0
Fragment-Host: compiler.fragments.unmatchinginp2;bundle-version="[1.0.0,1.1.0)"
Bundle-Name: %fragmentName
Bundle-Vendor: %providerName
Bundle-SymbolicName: compiler.fragments.unmatchinginp2.unmatching; singleton:=true
Bundle-Version: 1.0.0.qualifier
Bundle-ManifestVersion: 2
Bundle-Localization: fragment
Export-Package: org.eclipse.swt.graphics
Eclipse-PlatformFilter: (& (osgi.ws=unknown) (osgi.os=unknown) (osgi.arch=unknown))
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bin.includes = .,META-INF/
source.. = src/
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.eclipse.tycho.compiler.fragments.unmatchinginp2</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<artifactId>compiler.fragments.unmatchinginp2.unmatching</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>eclipse-plugin</packaging>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>target-platform-configuration</artifactId>
<configuration>
<dependency-resolution>
<optionalDependencies>ignore</optionalDependencies>
<profileProperties>
<org.eclipse.swt.buildtime>true</org.eclipse.swt.buildtime>
</profileProperties>
</dependency-resolution>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.eclipse.swt.graphics;

public final class Image2 {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Manifest-Version: 1.0
Bundle-Name: %pluginName
Bundle-Vendor: %providerName
Bundle-SymbolicName: compiler.fragments.unmatchinginp2;singleton:=true
Bundle-Version: 1.0.0.qualifier
Bundle-ManifestVersion: 2
Bundle-Localization: plugin
Eclipse-ExtensibleAPI: true
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
requires.1.namespace = org.eclipse.equinox.p2.iu
requires.1.name = org.eclipse.swt.gtk.linux.x86
requires.1.range = [$version$,$version$]
requires.1.filter = (&(osgi.os=linux)(osgi.ws=gtk)(osgi.arch=x86)(!(org.eclipse.swt.buildtime=true)))

requires.2.namespace = org.eclipse.equinox.p2.iu
requires.2.name = org.eclipse.swt.unmatching
requires.2.range = [$version$,$version$]
requires.2.filter = (&(osgi.os=unmatching)(osgi.ws=unmatching)(osgi.arch=unmatching)(!(org.eclipse.swt.buildtime=true)))
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bin.includes = META-INF/

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.eclipse.tycho.compiler.fragments.unmatchinginp2</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<artifactId>compiler.fragments.unmatchinginp2</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>eclipse-plugin</packaging>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>target-platform-configuration</artifactId>
<configuration>
<dependency-resolution>
<optionalDependencies>ignore</optionalDependencies>
<profileProperties>
<org.eclipse.swt.buildtime>true</org.eclipse.swt.buildtime>
</profileProperties>
</dependency-resolution>
</configuration>
</plugin>
</plugins>
</build>

</project>
Loading
Loading