stempler / bnd-platform

Build OSGi bundles and Eclipse Update Sites from existing JARs, e.g. from Maven repositories (Plugin for Gradle)
Apache License 2.0
79 stars 30 forks source link

Source Bundle for current gradle Project #8

Closed bartdag closed 8 years ago

bartdag commented 8 years ago

Hi, I am trying to generate a source bundle for my current gradle project, but I am not sure how to proceed. The bundles and updateSite tasks create a bundle/feature/update site for the jar defined in my project, but it does not create a corresponding source bundle.

I am using bnd-platform 1.3 and gradle 2.11 on Java 8 (Mac OSX).

My platform config:

platform {
    bundle project
    bnd(project) {
        fetchSources = true
        symbolicName = 'py4j-java'
        bundleName = 'py4j-java'
        version = bundleVersion
        instruction 'Eclipse-BuddyPolicy', 'global'
        instruction 'Import-Package', '!javax.net'
        instruction 'Export-Package', 'py4j,py4j.commands,py4j.model,py4j.reflection'
        instruction 'Bundle-RequiredExecutionEnvironment', 'JavaSE-1.6'
        instruction 'Bundle-Description', 'Bridge between Python and Java'
        instruction 'Bundle-DocURL', 'http://www.py4j.org'
        addQualifier = true
        eclipseHome = new File(eclipseHomePath)
        featureId = 'org.py4j.feature'
        featureName = 'Py4J Library'
        featureVersion = bundleVersion
        featureProvider = 'Py4J'
        categoryId = 'py4j'
        categoryName = 'Py4J'
    }
}

The sources jar generated for my project:

task sourcesJar(type: Jar, dependsOn: classes) {
    classifier = 'sources'
    from sourceSets.main.allSource
}

artifacts {
    archives sourcesJar
    archives jarTests
    archives javadocJar
}

Thank you for this great gradle plug-in!

stempler commented 8 years ago

I admit I never tried adding a Java project directly to the platform. Really awesome that it actually works like this :grinning:

Does the rest work fine? i.e. are the instructions correctly represented in the bundle manifest?

Some of the settings you specify in the bnd-Closure are actually plugin settings, I'm not sure if they will work properly in there. Could you move those up to the platform call like this?

platform {
    fetchSources = true

    addQualifier = true
    eclipseHome = new File(eclipseHomePath)
    featureId = 'org.py4j.feature'
    featureName = 'Py4J Library'
    featureVersion = bundleVersion
    featureProvider = 'Py4J'
    categoryId = 'py4j'
    categoryName = 'Py4J'

    bundle project
    bnd(project) {
        symbolicName = 'py4j-java'
        bundleName = 'py4j-java'
        version = bundleVersion
        instruction 'Eclipse-BuddyPolicy', 'global'
        instruction 'Import-Package', '!javax.net'
        instruction 'Export-Package', 'py4j,py4j.commands,py4j.model,py4j.reflection'
        instruction 'Bundle-RequiredExecutionEnvironment', 'JavaSE-1.6'
        instruction 'Bundle-Description', 'Bridge between Python and Java'
        instruction 'Bundle-DocURL', 'http://www.py4j.org'
    }
}

Can you provide a link to a repo for your project? Then I might be able to take a look during the weekend how this could be made to work (if the configuration change does not help).

bartdag commented 8 years ago

Hi, thanks for your quick response. I admit I discovered how to add a java project directly to the platform after many trials and errors :-)

I moved the settings but it still did not create a source bundle. You can see the whole project here: https://github.com/bartdag/py4j/tree/gradle (the gradle config is in the gradle branch for now).

You can build the project like this:

cd py4j-java
gradle assemble
gradle bundles
gradle updateSite

As far as I can tell, the osgi bundle is valid and I can import it in Eclipse and other plugins can use it.

bartdag commented 8 years ago

Hi, did you have time to look into this issue? Thanks!

stempler commented 8 years ago

Hi. I had a look at it. The mechanism that I use for getting the sources seems to only work for external dependencies. At this point I'm not sure how I could find the source Jar for the project based on the dependency.

A workaround is the following:

bundle tasks.jar.outputs.files, {
  bnd {
    ...
  }
}
tasks.bundles.dependsOn('assemble')
configurations {
  platform {
    extendsFrom compile
  }
}

I tried this on your project with success, see my fork here: https://github.com/stempler/py4j/tree/gradle-wa1 The way you added the dependency is definitely the more elegant way, I'm open to ideas to include the source Jar in a different way, using the Gradle API.

bartdag commented 8 years ago

Thanks a lot for you help, that seems to work. I'm not familiar with gradle subprojects, but would having a global project that depends on subprojects (e.g., py4j that would depend on py4j-java and py4j-python) makes it look like an external dependency?

stempler commented 8 years ago

No, that would still be project dependencies, with transitive project dependencies probably complicating it more. I did a few more attempts yesterday to solve the issue in a better way, but always had problems when it came to resolving sources for project dependencies. As a workaround I tried also letting the bundles task depend on the install task, and adding the dependency by group and name to the platform configuration. That was a way of trying to externalise it. But that failed because it seems to be a conflict to have an external dependency that is the same group and name as a project. Thus externalising it is probably only possible when using two different builds.