Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

o first version of the m4e-maven-plugin

  • Loading branch information...
commit 3b886f31e55dfe6b1510bf7ba60eb008b19be89c 0 parents
Jason van Zyl authored
6 .gitignore
@@ -0,0 +1,6 @@
+target/
+bin/
+mavenRepo/
+.project
+.classpath
+.settings/
5 README.md
@@ -0,0 +1,5 @@
+To try the conversion of an Eclipse installation to a Maven repository:
+
+mvn clean install
+
+mvn io.jvz.maven.plugins:m4e-maven-plugin:e2m -DeclipseInstallation=${eclipseInstallation} -DmavenRepository=${mavenRepo}
3  convert.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+dir=`pwd`
+mvn io.jvz.maven.plugins:m4e-maven-plugin:e2m -DeclipseInstallation=/Users/jvanzyl/eclipse/eclipse-xtext -DmavenRepository=$dir/mavenRepo
59 pom.xml
@@ -0,0 +1,59 @@
+<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.sonatype.oss</groupId>
+ <artifactId>oss-parent</artifactId>
+ <version>6</version>
+ </parent>
+ <groupId>io.jvz.maven.plugins</groupId>
+ <artifactId>m4e-maven-plugin</artifactId>
+ <version>0.0.1-SNAPSHOT</version>
+ <packaging>maven-plugin</packaging>
+
+ <properties>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ </properties>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-plugin-api</artifactId>
+ <version>3.0.3</version>
+ </dependency>
+ <dependency>
+ <groupId>org.eclipse.tycho</groupId>
+ <artifactId>tycho-core</artifactId>
+ <version>0.12.0</version>
+ </dependency>
+ <dependency>
+ <groupId>org.codehaus.plexus</groupId>
+ <artifactId>plexus-utils</artifactId>
+ <version>2.0.7</version>
+ </dependency>
+ <!-- Testing -->
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>3.8.1</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.codehaus.plexus</groupId>
+ <artifactId>plexus-component-metadata</artifactId>
+ <version>1.5.5</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>generate-metadata</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+</project>
180 src/main/java/io/jvz/eclipse/DefaultRepositoryConverter.java
@@ -0,0 +1,180 @@
+package io.jvz.eclipse;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.Writer;
+import java.util.List;
+import java.util.Properties;
+
+import org.apache.maven.model.Dependency;
+import org.apache.maven.model.Model;
+import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
+import org.codehaus.plexus.component.annotations.Component;
+import org.codehaus.plexus.component.annotations.Requirement;
+import org.codehaus.plexus.logging.Logger;
+import org.codehaus.plexus.util.FileUtils;
+import org.codehaus.plexus.util.IOUtil;
+import org.eclipse.osgi.service.resolver.BundleDescription;
+import org.eclipse.osgi.service.resolver.State;
+import org.eclipse.osgi.service.resolver.StateObjectFactory;
+import org.eclipse.tycho.core.TargetEnvironment;
+import org.eclipse.tycho.core.osgitools.DependencyComputer;
+import org.eclipse.tycho.core.osgitools.DependencyComputer.DependencyEntry;
+import org.eclipse.tycho.core.osgitools.EquinoxResolver;
+import org.eclipse.tycho.core.osgitools.targetplatform.EclipseInstallationLayout;
+import org.eclipse.tycho.core.utils.ExecutionEnvironmentUtils;
+import org.eclipse.tycho.core.utils.PlatformPropertiesUtils;
+import org.osgi.framework.BundleException;
+
+@Component(role = RepositoryConverter.class)
+public class DefaultRepositoryConverter implements RepositoryConverter {
+
+ @Requirement
+ private Logger logger;
+
+ @Requirement
+ private EclipseInstallationLayout eclipseInstallation;
+
+ @Requirement
+ private EquinoxResolver resolver;
+
+ @Requirement
+ private DependencyComputer dependencyComputer;
+
+ private static StateObjectFactory factory = StateObjectFactory.defaultFactory;
+
+ public static final String SYSTEM_BUNDLE_SYMBOLIC_NAME = "system.bundle";
+
+ public void convert(File eclipseInstallationLocation, File mavenRepository) throws BundleException {
+
+ eclipseInstallation.setLocation(eclipseInstallationLocation);
+ Properties properties = getPlatformProperties(new Properties(), null);
+ State state = factory.createState(true);
+ state.setPlatformProperties(properties);
+
+ long id = 1;
+
+ for (File bundle : eclipseInstallation.getPlugins(eclipseInstallationLocation)) {
+ logger.debug("Adding: " + bundle);
+ resolver.addBundle(state, id++, bundle, false);
+ }
+
+ state.resolve();
+ BundleDescription[] bundles = state.getBundles();
+ logger.info("Number of bundles: " + bundles.length);
+ MavenXpp3Writer mavenWriter = new MavenXpp3Writer();
+ for (BundleDescription bundleDescription : bundles) {
+
+ //
+ // Right now we will only deal with bundles that are in the form of JARS. I'm not sure how to deal with
+ // bundles that are in exploded form.
+ //
+ File bundleLocation = new File(bundleDescription.getLocation());
+ if(bundleLocation.isDirectory()) {
+ continue;
+ }
+
+ Model model = createMavenModel(state, bundleDescription);
+ try {
+ // groupId = org.eclipse.ant
+ // artifactId = org.eclipse.ant.core
+ // version = 1.0.0
+ //
+ // org/eclipse/ant/org.eclilpse.ant.core/1.0.0/org.eclipse.ant.core-1.0.0.pom
+ //
+ String baseName = model.getArtifactId() + "-" + model.getVersion();
+ File baseDirectory = new File(mavenRepository, model.getGroupId().replace('.', '/') + '/' + model.getArtifactId() + '/' + model.getVersion());
+ if (!baseDirectory.exists()) {
+ baseDirectory.mkdirs();
+ }
+ File modelFile = new File(baseDirectory, baseName + ".pom");
+ File artifactFile = new File(baseDirectory, baseName + ".jar");
+ //
+ // Write out the POM
+ //
+ Writer writer = new FileWriter(modelFile);
+ mavenWriter.write(writer, model);
+ //
+ // Write out the artifact
+ //
+ FileUtils.copyFile(bundleLocation, artifactFile);
+
+ } catch (IOException e) {
+ logger.error("Error: "+e.getMessage());
+ }
+ }
+ }
+
+ private Model createMavenModel(State state, BundleDescription bd) {
+ Model model = createModel(bd);
+ List<DependencyEntry> bundleDependencies = dependencyComputer.computeDependencies(state.getStateHelper(), bd);
+ for (DependencyEntry d : bundleDependencies) {
+ model.addDependency(dependency(d.desc));
+ }
+ return model;
+ }
+
+ protected Model createModel(BundleDescription bd) {
+ Model model = new Model();
+ model.setModelVersion("4.0.0");
+ model.setGroupId(symbolicNameToGroupId(bd.getSymbolicName()));
+ model.setArtifactId(bd.getSymbolicName());
+ model.setVersion(bd.getVersion().toString());
+ return model;
+ }
+
+ protected Dependency dependency(BundleDescription bd) {
+ Dependency d = new Dependency();
+ d.setGroupId(symbolicNameToGroupId(bd.getSymbolicName()));
+ d.setArtifactId(bd.getSymbolicName());
+ d.setVersion(bd.getVersion().toString());
+ return d;
+ }
+
+ // sybmolicName = org.eclipse.ant.core
+ // groupId = org.eclipse.ant
+ // artifactId = org.eclipse.ant.core
+ /**
+ * For OSGi symbolicNames at Eclipse we will take the first three segments as
+ * the groupId.
+ */
+ protected String symbolicNameToGroupId(String symbolicName) {
+ int i = nthOccurrence(symbolicName, '.', 2);
+ if (i > 0) {
+ return symbolicName.substring(0, i);
+ } else {
+ return symbolicName;
+ }
+ }
+
+ //
+ // zero-based nth occurrence
+ //
+ public int nthOccurrence(String str, char c, int n) {
+ int pos = str.indexOf(c, 0);
+ while (n-- > 0 && pos != -1)
+ pos = str.indexOf(c, pos + 1);
+ return pos;
+ }
+
+ //
+ //
+ //
+
+ protected Properties getPlatformProperties(Properties properties, TargetEnvironment environment) {
+ if (environment != null) {
+ properties.put(PlatformPropertiesUtils.OSGI_OS, environment.getOs());
+ properties.put(PlatformPropertiesUtils.OSGI_WS, environment.getWs());
+ properties.put(PlatformPropertiesUtils.OSGI_ARCH, environment.getArch());
+ }
+
+ ExecutionEnvironmentUtils.loadVMProfile(properties);
+ //
+ // Put Equinox OSGi resolver into development mode.
+ // See http://www.nabble.com/Re:-resolving-partially-p18449054.html
+ //
+ properties.put(org.eclipse.osgi.framework.internal.core.Constants.OSGI_RESOLVER_MODE, org.eclipse.osgi.framework.internal.core.Constants.DEVELOPMENT_MODE);
+ return properties;
+ }
+}
11 src/main/java/io/jvz/eclipse/RepositoryConverter.java
@@ -0,0 +1,11 @@
+package io.jvz.eclipse;
+
+import java.io.File;
+
+import org.osgi.framework.BundleException;
+
+public interface RepositoryConverter
+{
+ void convert( File eclipseInstallation, File mavenRepository )
+ throws BundleException;
+}
44 src/main/java/io/jvz/maven/plugins/EclipseInstallationToMavenRepositoryMojo.java
@@ -0,0 +1,44 @@
+package io.jvz.maven.plugins;
+
+import io.jvz.eclipse.RepositoryConverter;
+
+import java.io.File;
+import java.util.Set;
+
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.eclipse.tycho.core.osgitools.targetplatform.EclipseInstallationLayout;
+import org.osgi.framework.BundleException;
+
+/**
+ * @goal e2m
+ * @requiresProject false
+ */
+public class EclipseInstallationToMavenRepositoryMojo extends AbstractMojo {
+ /**
+ * @component
+ */
+ private RepositoryConverter converter;
+
+ /**
+ * @parameter expression="${eclipseInstallation}"
+ */
+ private File eclipseInstallation;
+
+ /**
+ * @parameter expression="${mavenRepository}"
+ */
+ private File mavenRepository;
+
+ public void execute() throws MojoExecutionException, MojoFailureException {
+
+ getLog().info( "Converting " + eclipseInstallation + " to " + mavenRepository);
+
+ try {
+ converter.convert(eclipseInstallation, mavenRepository);
+ } catch (BundleException e) {
+ throw new MojoExecutionException(e.getMessage());
+ }
+ }
+}
38 src/test/java/io/jvz/eclipse/AppTest.java
@@ -0,0 +1,38 @@
+package io.jvz.eclipse;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * Unit test for simple App.
+ */
+public class AppTest
+ extends TestCase
+{
+ /**
+ * Create the test case
+ *
+ * @param testName name of the test case
+ */
+ public AppTest( String testName )
+ {
+ super( testName );
+ }
+
+ /**
+ * @return the suite of tests being tested
+ */
+ public static Test suite()
+ {
+ return new TestSuite( AppTest.class );
+ }
+
+ /**
+ * Rigourous Test :-)
+ */
+ public void testApp()
+ {
+ assertTrue( true );
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.