Skip to content

Commit

Permalink
Make Tycho SBOM mapper configurable w.r.t. the project packaging types
Browse files Browse the repository at this point in the history
Instead of being applied on all packaging types (even those where it
doesn't make sense like the target platform), the user is now able to
configure for which project types the Tycho model converter is used.

By default (if nothing else is specified), this converter is only used
for plugins and features, to avoid issues when the e.g. the target
definition is defined in its own module or the the reactor contains
non-Tycho packaging types (jar/pom/bundle/...).

(cherry picked from commit e972652)
  • Loading branch information
ptziegler authored and eclipse-tycho-bot committed Sep 12, 2024
1 parent 03cb31d commit 900f0c6
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 2 deletions.
8 changes: 8 additions & 0 deletions tycho-its/projects/sbom/example.test1/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Test-Fragment with SBOM
Bundle-SymbolicName: example.test1
Bundle-Version: 1.0.0.qualifier
Fragment-Host: example.plugin
Automatic-Module-Name: example.test1
Bundle-RequiredExecutionEnvironment: JavaSE-17
5 changes: 5 additions & 0 deletions tycho-its/projects/sbom/example.test1/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pom.model.packaging = eclipse-test-plugin
source.. = src/
output.. = bin/
bin.includes = META-INF/,\
.
8 changes: 8 additions & 0 deletions tycho-its/projects/sbom/example.test2/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Test-Fragment with SBOM
Bundle-SymbolicName: example.test2
Bundle-Version: 1.0.0.qualifier
Fragment-Host: example.plugin
Automatic-Module-Name: example.test2
Bundle-RequiredExecutionEnvironment: JavaSE-17
5 changes: 5 additions & 0 deletions tycho-its/projects/sbom/example.test2/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pom.model.packaging = eclipse-test-plugin
source.. = src/
output.. = bin/
bin.includes = META-INF/,\
.
29 changes: 28 additions & 1 deletion tycho-its/projects/sbom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
<modules>
<module>example.feature</module>
<module>example.plugin</module>
<module>example.test1</module>
<module>example.test2</module>
<module>product</module>
<module>repository</module>
<module>target-platform</module>
</modules>

<build>
Expand All @@ -31,7 +34,11 @@
<version>${tycho-version}</version>
<configuration>
<target>
<file>../target-definition.target</file>
<artifact>
<groupId>tycho-demo</groupId>
<artifactId>target-definition</artifactId>
<version>1.0.0-SNAPSHOT</version>
</artifact>
</target>
<environments>
<environment>
Expand All @@ -50,6 +57,14 @@
<forceContextQualifier>today</forceContextQualifier>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-surefire-plugin</artifactId>
<version>${tycho-version}</version>
<configuration>
<failIfNoTests>false</failIfNoTests>
</configuration>
</plugin>
<plugin>
<groupId>org.cyclonedx</groupId>
<artifactId>cyclonedx-maven-plugin</artifactId>
Expand All @@ -72,6 +87,18 @@
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-sbom</artifactId>
<version>${tycho-version}</version>
<configuration>
<includes>
<include>eclipse-plugin</include>
<include>eclipse-feature</include>
<include>eclipse-repository</include>
</includes>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public class BomCreationTest extends AbstractTychoIntegrationTest {
public void setUp() throws Exception {
if (verifier == null) {
verifier = getVerifier("sbom", false);
// CycloneDX is logging an excessive amount of data on DEBUG level.
// Way too much for the verifier to handle properly...
verifier.getCliOptions().remove("-X");
verifier.executeGoal("verify");
verifyErrorFreeLog(verifier);
}
Expand Down
9 changes: 9 additions & 0 deletions tycho-sbom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</parent>
<artifactId>tycho-sbom</artifactId>
<name>Tycho SBOM model extension</name>
<packaging>maven-plugin</packaging>

<dependencies>
<dependency>
Expand Down Expand Up @@ -50,6 +51,14 @@
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<configuration>
<goalPrefix>tycho-sbom</goalPrefix>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
*/
@Component(role = ModelConverter.class)
public class TychoModelConverter extends DefaultModelConverter {
private static final String KEY_CONTEXT = TychoSBOMConfiguration.class.toString();
private static final Logger LOG = LoggerFactory.getLogger(TychoModelConverter.class);

@Inject
Expand Down Expand Up @@ -133,7 +134,8 @@ public String generateClassifierlessPackageUrl(Artifact artifact) {
*/
private String generatePackageUrl(Artifact artifact, boolean withVersion, boolean withClassifier,
Supplier<String> fallback) {
if (reactorReader.isTychoReactorArtifact(artifact)) {
TychoSBOMConfiguration sbomConfig = getOrCreateCurrentProjectConfiguration();
if (sbomConfig.getIncludedPackagingTypes().contains(reactorReader.getPackagingType(artifact))) {
ArtifactKey artifactKey = getQualifiedArtifactKey(artifact);
IArtifactKey p2artifactKey = ArtifactTypeHelper.toP2ArtifactKey(artifactKey);
boolean isReactorProject = reactorReader.getTychoReactorProject(artifact).isPresent();
Expand Down Expand Up @@ -299,4 +301,26 @@ private List<Repository> getTargetRepositories(MavenProject currentProject) {

return Collections.unmodifiableList(p2repositories);
}

/**
* The is created lazily based on the plugin configuration of this SBOM project.
* If is converter is used outside a project, a default configuration is
* returned. If no configuration has been created for the current project, a new
* instance is created and stored as context value, which is then returned on
* successive calls.
*
* @return The SBOM configuration of the current project. Never {@code null}.
*/
private synchronized TychoSBOMConfiguration getOrCreateCurrentProjectConfiguration() {
MavenProject currentProject = legacySupport.getSession().getCurrentProject();
if (currentProject == null) {
return new TychoSBOMConfiguration();
}
TychoSBOMConfiguration projectConfig = (TychoSBOMConfiguration) currentProject.getContextValue(KEY_CONTEXT);
if (projectConfig == null) {
projectConfig = new TychoSBOMConfiguration(currentProject);
currentProject.setContextValue(KEY_CONTEXT, projectConfig);
}
return projectConfig;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*******************************************************************************
* Copyright (c) 2024 Patrick Ziegler and others.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Patrick Ziegler - initial API and implementation
*******************************************************************************/
package org.eclipse.tycho.sbom;

import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.maven.model.Plugin;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.eclipse.tycho.PackagingType;

/**
* Configuration of the {@code tycho-sbom} plugin to configure the packaging
* types for which the {@link TychoModelConverter} is used. By default only
* plug-ins and features are supported, but user may include additional types
* using this class. Example:
*
* <pre>
* &lt;configuration&gt;
* &lt;includes&gt;
* &lt;include&gt;eclipse-plugin&lt;/include&gt;
* &lt;include&gt;eclipse-feature&lt;/include&gt;
* &lt;include&gt;eclipse-repository&lt;/include&gt;
* &lt;/includes&gt;
* &lt;/configuration>&gt;
* </pre>
*/
public class TychoSBOMConfiguration {
private static final Set<String> DEFAULT_TYPES = Set.of(PackagingType.TYPE_ECLIPSE_FEATURE,
PackagingType.TYPE_ECLIPSE_PLUGIN);
private static final String KEY_INCLUDES = "includes";
/**
* Contains all packaging types for which the {@link TychoModelConverter} should
* be used. Initialized with {@link #DEFAULT_TYPES} if no other configuration is
* specified.
*/
private Set<String> includes = DEFAULT_TYPES;

/**
* A default implementation that is used when used outside of a Maven project.
*/
public TychoSBOMConfiguration() {
// no-op
}

/**
* Initializes the configuration based on the configuration of the
* {@code tycho-sbom} plugin. If no configuration exists, this configuration is
* initialized with its default values.
*/
public TychoSBOMConfiguration(MavenProject currentProject) {
Plugin plugin = currentProject.getPlugin("org.eclipse.tycho:tycho-sbom");
if (plugin != null && plugin.getConfiguration() instanceof Xpp3Dom root) {
readIncludes(root.getChild(KEY_INCLUDES));
}
}

private void readIncludes(Xpp3Dom parent) {
if (parent == null) {
return;
}
// Overwrite default configuration
includes = Arrays.stream(parent.getChildren()) //
.map(Xpp3Dom::getValue) //
.collect(Collectors.toUnmodifiableSet());
}

/**
* Returns all packaging types for which the {@link TychoModelConverter} should
* be used.
*
* @return An unmodifiable list of {@link String}s.
*/
public Set<String> getIncludedPackagingTypes() {
// Set is already immutable
return includes;
}
}

0 comments on commit 900f0c6

Please sign in to comment.