maintain library project for Maven and Eclipse RCP/ OSGi in the same repository

If you are working both in the Maven and the Eclipse RCP/ OSGi universe, you might find it quite frustrating that these two worlds cooperate so poorly. In order to use the same library in both worlds, a lot of error-prone copy-and-paste is needed, at least if you would like to use this project in a Maven and Maven Tycho build.

In principle, there is not much that is different for a Maven artifact and an Eclipse RCP/ OSGi Plug-in:
In an Eclipse RCP/ OSGi Plug-in you just need an additional META-INF/MANIFEST.MF and build.properties file. The problem comes with the Maven pom file, which looks completely different for standard Maven and Maven Tycho. Luckily, you can specify the name of the pom file when invoking Maven:

Therefore, you can just maintain two pom files and with only few limitations, it is possible to maintain a library that can be used in a standard Maven build and in a Maven Tycho build within the same repository:

  1. Modify your .gitignore so that it contains the following lines:
    bin
    .project
    .settings
    .classpath
    
    We don’t want to have any Eclipse project configuration information in the project, since we want to be able to import it both as ‘Maven project’ and ‘Eclipse/ OSGi project’.
  2. Remove all files from the git index that match the pattern above.
    • If your project is a Maven project, right-click on the project and select ‘Configure -> Convert to Plug-in project’. This will create the META-INF/MANIFEST.MF and build.properties files.
    • If your project is a Plug-in project, right-click on the project and select ‘Configure -> Convert to Maven project’. This will create the pom.xml file.
    These steps are optional and only needed if you are missing either pom.xml or META-INF/MANIFEST.MF and build.properties .
  3. Create the Tycho pom file, name it e.g. pom-tycho.xml. It’s content should look like this:
  4. Commit all changes and delete the project from your workspace.
  5. Import the project using the ‘Import Existing Maven Project’ or ‘Checkout Maven Project from scm’ wizard. To use the project as an Eclipse RCP/ OSGi Plug-in project, disable it’s Maven nature via ‘Maven -> disable Maven nature’.
  6. Done!
Note: You manually need to keep in sync the version strings in pom.xml, META-INF/MANIFEST.MF and pom-tycho.xml! E.g. after running a Maven release, you need to adapt the version numbers in META-INF/MANIFEST.MF and pom-tycho.xml so that they match the automatically changed version strings in pom.xml.
Also note that you cannot use a Bundle Activator in this project. If you need one, just create another Eclipse-RCP Plug-in that depends on your library and contains a Bundle Activator.

Troubleshooting: After importing as Maven project or converting to Maven project, you might run into build-path problems. Try to resolve using ‘right-click on project -> Maven -> Update Project..’ And/ or fix the Java Build Path of the project (Standard Eclipse source folder is src, in Maven it is src/main/java). java-build-path