Skip to content

Commit 64b5f36

Browse files
Copilotchrisrueger
andcommitted
Update tutorial for Bndtools 7.1.0+ compatibility
Co-authored-by: chrisrueger <[email protected]>
1 parent fa62a14 commit 64b5f36

File tree

1 file changed

+60
-8
lines changed

1 file changed

+60
-8
lines changed

tutorial.md

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ In the tutorial we create the top three bundles (rectangles):
1717
* The Provider bundle imports the interface and publishes an instance of the service.
1818
* The Command bundle imports the interface and binds to the service instance, and also publishes a `Command` service that is used by the Felix Shell bundle.
1919

20+
**Note:** This tutorial has been updated for Bndtools 7.1.0+ and reflects changes in recent versions. Some screenshots may show older versions of the UI. Key differences from older versions include:
21+
- Updated cnf directory structure with `cache/` and `ext/` folders
22+
- Use of standard OSGi annotations (`org.osgi.service.component.annotations`) instead of legacy aQute annotations
23+
- Changes in default bundle behavior and template availability
24+
- Different default file associations for `bnd.bnd` files
25+
2026
# Installing Bndtools
2127

2228
Please refer to the [Installation Instructions](installation.html).
@@ -42,6 +48,13 @@ If your current workspace is not configured as a Bnd Workspace yet, before creat
4248
7. You should end up with a new **cnf** folder in your workspace. If you expand it, you should see something similar to this figure.
4349

4450
![](images/tutorial/19.png)
51+
52+
**Note:** In Bndtools 7.1.0+, the cnf directory structure has been updated and now typically includes:
53+
- **cache/** - for caching downloaded artifacts
54+
- **ext/** - for workspace extension configuration files (like `build.mvn`, `central.mvn`, etc.)
55+
- **build.bnd** - the main workspace configuration file
56+
57+
This is slightly different from older versions but provides better organization for workspace settings.
4558

4659
If this is the case, you are now ready to create your first Bnd OSGi project!
4760

@@ -55,7 +68,9 @@ First we need to create a Bnd OSGi Project. This is just a standard Eclipse Java
5568

5669
![](images/tutorial/20.png)
5770

58-
3. As you see, you have the possibility to choose among a series of templates or to create a new project from scratch. For this first tutorial we will start with an empty project, thus, select the **Empty** option and press **Next**
71+
3. As you see, you have the possibility to choose among a series of templates or to create a new project from scratch. For this first tutorial we will start with an empty project, thus, select the **«Empty»** option (shown as «Empty» 1.0.0 — [built-in]) and press **Next**
72+
73+
**Note:** The available templates may vary depending on your Bndtools version and installed template bundles. In version 7.1.0+, you may see fewer templates initially compared to older versions
5974

6075
4. On the next page, you will be asked to enter the name of the project. Please, enter `org.example.api` in this case. Select then the JRE execution environment (at least J2SE-1.5) and press **Next**
6176

@@ -91,9 +106,13 @@ public interface Greeting {
91106

92107
The project we have created defines a single bundle with a Bundle Symbolic Name (BSN) of `org.example.api` (i.e., the same as the project name). As soon as we created the project, a bundle file named `org.example.api.jar` was created in the `generated` directory, and it will be rebuilt every time we change the bundle definition or its source code.
93108

94-
However, the bundle is currently empty, because we have not defined any Java packages to include in the bundle. This is an important difference of Bndtools with respect to other tools: bundles are always empty until we explicitly add some content. You can verify this by double-clicking the bundle file and viewing its contents: it will only have a `META-INF/MANIFEST.MF` entry.
109+
**Note:** In recent versions of Bndtools (7.0+), the bundle behavior has changed. Even with an empty `bnd.bnd` file, the bundle may include compiled classes from your source packages. The bundle is no longer completely empty by default. However, to make packages available to other bundles, you still need to explicitly export them via the **Export Packages** panel. Without explicit exports, the packages remain private to your bundle.
110+
111+
We want to add the package `org.example.api` to the exported packages of the bundle. So open the `bnd.bnd` file at the top of the project.
112+
113+
**Note:** In recent Eclipse/Bndtools versions, double-clicking `bnd.bnd` may open the source editor directly instead of the Bundle Editor UI. If this happens, right-click on `bnd.bnd` and select **Open With > Bnd Bundle Editor** to access the graphical editor.
95114

96-
We want to add the package `org.example.api` to the exported packages of the bundle. So open the `bnd.bnd` file at the top of the project and select the **Contents** tab. Now the package can be added in one of two ways:
115+
Once the editor is open, select the **Contents** tab. Now the package can be added in one of two ways:
97116

98117
* Click the "+" icon in the header of the **Export Packages** section, then select `org.example.api` from the dialog and click **OK**... *or*
99118
* Drag-and-drop the package `org.example.api` from Eclipse's Package Explorer view into the **Export Packages** list.
@@ -104,6 +123,8 @@ As soon as this is done, a popup dialog appears titled "Missing Package Info". T
104123

105124
![](images/tutorial/23.png)
106125

126+
**Note on Package Versioning:** When you click OK, Bndtools will create a `package-info.java` file (for Java 9+) or `packageinfo` file (for older Java versions) in your package. The `package-info.java` file will contain OSGi package versioning annotations. If you see compilation errors in this file (such as missing `org.osgi.annotation.bundle` or `org.osgi.annotation.versioning` packages), you may need to add the OSGi annotation bundle to your build path. This can be resolved by adding the appropriate OSGi compendium dependency to your `bnd.bnd` file's build path, or the errors may be cosmetic and not affect the actual build.
127+
107128
The **Contents** tab should now appear as in the following screenshot:
108129

109130
![](images/tutorial/24.png)
@@ -123,10 +144,19 @@ We will now create another project that defines two bundles: a provider and a cl
123144

124145
## Create the Project
125146

126-
Create another Bnd OSGi project, named `org.example.impl`. At the **Project Templates** step, select **Component Development** and then proceed as for the previous project.
147+
Create another Bnd OSGi project, named `org.example.impl`. At the **Project Templates** step:
148+
149+
- **If available**, select **Component Development** template from the OSGi Standard Templates category
150+
- **If the Component Development template is not visible**, you can:
151+
- Select the **«Empty»** template and manually create the necessary files, or
152+
- Use the **API Project** template as an alternative starting point
153+
154+
Then proceed as for the previous project.
127155

128156
![](images/tutorial/25.png)
129157

158+
**Note:** In some Bndtools installations (particularly version 7.1.0+), the **Component Development** template may not appear in the template list. This can happen if template bundles are not fully loaded. If you don't see this template, you can use the Empty template and create the component structure manually as shown in this tutorial.
159+
130160
## Add the API as a Build Dependency
131161

132162
We need to add the API project as a build-time dependency of this new project.
@@ -152,14 +182,18 @@ In either case, the `org.example.api` bundle will appear in the **Build Path** p
152182

153183
## Write an Implementation
154184

155-
We will write a class that implements the `Greeting` interface. When the project was created from the template, a Java source for a class named `org.example.impl.Example` was generated. Open this source file now and make it implement `Greeting`:
185+
We will write a class that implements the `Greeting` interface.
186+
187+
**If you used the Component Development template:** A Java source file for a class named `org.example.impl.Example` should have been generated. Open this source file now and make it implement `Greeting`.
188+
189+
**If you used the Empty template:** Create a new package `org.example.impl` in the `src` folder, then create a new Java class named `Example` in that package. Then make it implement `Greeting` as shown below:
156190

157191
```java
158192
package org.example.impl;
159193

160194
import org.example.api.Greeting;
161195

162-
import aQute.bnd.annotation.component.Component;
196+
import org.osgi.service.component.annotations.Component;
163197

164198
@Component
165199
public class Example implements Greeting {
@@ -169,11 +203,19 @@ public class Example implements Greeting {
169203
}
170204
```
171205

172-
Note the use of the `@Component` annotation. This enables our bundle to use OSGi Declarative Services to declare the API implementation class. This means that instances of the class will be automatically created and registered with the OSGi service registry. The annotation is build-time only, and does not pollute our class with runtime dependencies -- in other words, this is a "Plain Old Java Object" or POJO.
206+
Note the use of the `@Component` annotation from `org.osgi.service.component.annotations`. This enables our bundle to use OSGi Declarative Services to declare the API implementation class. This means that instances of the class will be automatically created and registered with the OSGi service registry. The annotation is build-time only, and does not pollute our class with runtime dependencies -- in other words, this is a "Plain Old Java Object" or POJO.
207+
208+
**Note:** Modern Bndtools versions use the standard OSGi annotations from `org.osgi.service.component.annotations` package instead of the legacy `aQute.bnd.annotation.component` annotations. Make sure to use the correct import as shown above.
173209

174210
## Test the Implementation
175211

176-
We should write a test case to ensure the implementation class works as expected. In the `test` folder, a test case class already exists named `org.example.ExampleTest`.
212+
We should write a test case to ensure the implementation class works as expected.
213+
214+
**If you used the Component Development template:** In the `test` folder, a test case class should already exist named `org.example.impl.ExampleTest`.
215+
216+
**If you used the Empty template:** Create a new package `org.example.impl` in the `test` folder, then create a new JUnit test class named `ExampleTest` in that package.
217+
218+
The test class structure should look like this:
177219

178220
```java
179221
package org.example.impl;
@@ -280,6 +322,16 @@ If is not already selected, please, select the **Execution Environment** in the
280322

281323
![](images/tutorial/32.png)
282324

325+
**Note on OSGi Framework Selection:** By default, you may only see the Felix framework in the OSGi Framework dropdown. If you want to use Equinox instead:
326+
327+
1. Add the Equinox OSGi framework to one of your repositories (e.g., in `cnf/ext/central.mvn`):
328+
```
329+
org.eclipse.platform:org.eclipse.osgi:3.23.200
330+
```
331+
(You can find the latest version on [Maven Central](https://central.sonatype.com/artifact/org.eclipse.platform/org.eclipse.osgi))
332+
333+
2. After adding it to your repository configuration, Equinox should appear as an option in the OSGi Framework dropdown in your run configuration.
334+
283335
Check **Auto-resolve on save** and then save the file. Returning to the **Console** view, type `lb` again:
284336

285337
```shell

0 commit comments

Comments
 (0)