Skip to content

Commit

Permalink
Do not use Tycho in eclipse-cbi-plugin
Browse files Browse the repository at this point in the history
Currently the plugin relies on some of Tycho components and classes this
has the risk of breakage and incompatibility.

This do the following
- drop generate-api-build-xml mojo, if it is useful for anyone it should
be migrated to Tycho
- remove Tycho dependency
- embed relevant portions of Tycho code that are actually used here
  • Loading branch information
laeubi committed Sep 12, 2024
1 parent 2f79212 commit 8b64d71
Show file tree
Hide file tree
Showing 10 changed files with 510 additions and 371 deletions.
34 changes: 20 additions & 14 deletions maven-plugins/eclipse-cbi-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,33 @@
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
</dependency>

<dependency>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-core</artifactId>
<groupId>org.eclipse.platform</groupId>
<artifactId>org.eclipse.osgi</artifactId>
<version>3.21.0</version>
</dependency>
<dependency>
<groupId>de.pdark</groupId>
<artifactId>decentxml</artifactId>
<version>1.4</version>
</dependency>

<dependency>
<groupId>org.sonatype.plexus</groupId>
<artifactId>plexus-build-api</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<!-- workaround for
https://issues.apache.org/jira/browse/MPLUGIN-450 -->
<configuration>
<goalPrefix>eclipse-cbi-plugin</goalPrefix>
</configuration>
</plugin>
</plugins>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<!-- workaround for
https://issues.apache.org/jira/browse/MPLUGIN-450 -->
<configuration>
<goalPrefix>eclipse-cbi-plugin</goalPrefix>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -23,58 +23,51 @@

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.eclipse.tycho.core.osgitools.BundleReader;
import org.eclipse.tycho.core.osgitools.OsgiManifest;
import org.eclipse.tycho.core.osgitools.OsgiManifestParserException;

abstract class AbstractPluginScannerMojo extends AbstractMojo {
/**
* igorf: as of 2012-01-05, generated repository location is hardcoded to
* target/repository in tycho
**/
@Parameter(defaultValue = "${project.build.directory}/repository")
protected File repository;
/**
* igorf: as of 2012-01-05, generated repository location is hardcoded to
* target/repository in tycho
**/
@Parameter(defaultValue = "${project.build.directory}/repository")
protected File repository;

@Component
protected BundleReader bundleReader;
@Override
public void execute() throws MojoExecutionException {
try {
Properties properties = new Properties();

@Override
public void execute() throws MojoExecutionException {
try {
Properties properties = new Properties();
File[] plugins = new File(repository, "plugins").listFiles();

File[] plugins = new File(repository, "plugins").listFiles();
if (plugins != null) {
Map<File, OsgiManifest> manifests = new HashMap<>();
for (File plugin : plugins) {
String fileName = plugin.getName();
if (fileName.endsWith(".pack.gz") || fileName.endsWith(".asc")) {
continue;
}
try {
OsgiManifest manifest = DefaultBundleReader.loadManifest(plugin);
manifests.put(plugin, manifest);
} catch (OsgiManifestParserException e) {
getLog().error(e);
}
}

if (plugins != null) {
Map<File, OsgiManifest> manifests = new HashMap<>();
for (File plugin : plugins) {
String fileName = plugin.getName();
if (fileName.endsWith(".pack.gz") || fileName.endsWith(".asc")) {
continue;
}
try {
OsgiManifest manifest = bundleReader.loadManifest(plugin);
manifests.put(plugin, manifest);
} catch (OsgiManifestParserException e) {
getLog().error(e);
}
}
processPlugins(properties, manifests);
}

processPlugins(properties, manifests);
}
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(getDestination()))) {
properties.store(os, null);
}
} catch (Exception e) {
throw new MojoExecutionException("Could not write plugin versions", e);
}
}

try (OutputStream os = new BufferedOutputStream(new FileOutputStream(getDestination()))) {
properties.store(os, null);
}
} catch (Exception e) {
throw new MojoExecutionException("Could not write plugin versions", e);
}
}
protected abstract void processPlugins(Properties properties, Map<File, OsgiManifest> plugins) throws Exception;

protected abstract void processPlugins(Properties properties, Map<File, OsgiManifest> plugins) throws Exception;

protected abstract File getDestination();
protected abstract File getDestination();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*******************************************************************************
* Copyright (c) 2008, 2011 Sonatype Inc. 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:
* Sonatype Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.cbi.mojo;

import java.util.Objects;

class DefaultArtifactKey {
private final String type;

private final String id;

private final String version;

public DefaultArtifactKey(String type, String id) {
this(type, id, "0.0.0");
}

public DefaultArtifactKey(String type, String id, String version) {
this.id = id;
this.type = type;
this.version = version;
}

@Override
public int hashCode() {
return Objects.hash(getType(), getId(), getVersion());
}

@Override
public boolean equals(Object obj) {
return this == obj || //
(obj instanceof DefaultArtifactKey other && //
Objects.equals(getType(), other.getType()) && //
Objects.equals(getId(), other.getId()) && //
Objects.equals(getVersion(), other.getVersion()));
}

@Override
public String toString() {
return getType() + ":" + getId() + ":" + getVersion();
}

/**
* @see ProjectType
*/
public String getType() {
return type;
}

/**
* Eclipse/OSGi artifact id. Can differ from Maven artifactId.
*/
public String getId() {
return id;
}

/**
* Eclipse/OSGi artifact version. Can differ from Maven version. For maven
* projects, this
* version corresponds to version specified in the project sources and does not
* reflect
* qualifier expansion.
*/
public String getVersion() {
return version;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*******************************************************************************
* Copyright (c) 2008, 2022 Sonatype Inc. 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:
* Sonatype Inc. - initial API and implementation
* Christoph Läubrich - Issue #663 - Access to the tycho .cache directory is not properly synchronized
*******************************************************************************/
package org.eclipse.cbi.mojo;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

class DefaultBundleReader {

public static OsgiManifest loadManifest(File bundleLocation) {
try {
if (bundleLocation.isDirectory()) {
return loadManifestFromDirectory(bundleLocation);
} else if (bundleLocation.isFile()) {
return loadManifestFromFile(bundleLocation);
} else {
// file does not exist
throw new OsgiManifestParserException(bundleLocation.getAbsolutePath(), "Manifest file not found");
}
} catch (IOException e) {
throw new OsgiManifestParserException(bundleLocation.getAbsolutePath(), e);
}
}

private static OsgiManifest loadManifestFromFile(File bundleLocation) throws IOException {
if (!bundleLocation.getName().toLowerCase().endsWith(".jar")) {
// file but not a jar, assume it is MANIFEST.MF
return loadManifestFile(bundleLocation);
}
try ( // it is a jar, let's see if it has OSGi bundle manifest
ZipFile jar = new ZipFile(bundleLocation, ZipFile.OPEN_READ)) {
ZipEntry manifestEntry = jar.getEntry(JarFile.MANIFEST_NAME);
if (manifestEntry != null) {
InputStream stream = jar.getInputStream(manifestEntry);
return new OsgiManifest(stream, bundleLocation.getAbsolutePath() + "!/" + JarFile.MANIFEST_NAME);
}
}
throw new OsgiManifestParserException(bundleLocation.getAbsolutePath(),
"Manifest file not found in JAR archive");
}

private static OsgiManifest loadManifestFromDirectory(File directory) throws IOException {
File manifestFile = new File(directory, JarFile.MANIFEST_NAME);
if (!manifestFile.isFile()) {
throw new OsgiManifestParserException(manifestFile.getAbsolutePath(), "Manifest file not found");
}
return loadManifestFile(manifestFile);
}

private static OsgiManifest loadManifestFile(File manifestFile) throws IOException, OsgiManifestParserException {
return new OsgiManifest(new FileInputStream(manifestFile), manifestFile.getAbsolutePath());
}

}
Loading

0 comments on commit 8b64d71

Please sign in to comment.