# Reading run requirements from POM (test) dependencies

**URL:** <https://bnd.discourse.group/t/reading-run-requirements-from-pom-test-dependencies/426>\
**Category:** GENERAL\
**Created:** [March 1, 2024, 10:44am UTC](https://bnd.discourse.group/t/reading-run-requirements-from-pom-test-dependencies/426 "2024-03-01T10:44:19Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![rec](https://yyz2.discourse-cdn.com/free1/user_avatar/bnd.discourse.group/rec/32/155_2.png) [@rec](https://bnd.discourse.group/u/rec)\
**Post date:** [March 1, 2024, 10:44am UTC](https://bnd.discourse.group/t/reading-run-requirements-from-pom-test-dependencies/426/1 "2024-03-01T10:44:19Z")

</div>

We can use statements like this in a `.bndrun` files:

```auto
-runrequires: \
	bnd.identity;id='${project.groupId}.${project.artifactId}',\
	bnd.identity;id='${project.groupId}.${project.artifactId}-tests',\

```

Now, typically I need a bunch of additional bundles / artifacts which do not have hard code-references in the main artifact - e.g. they may only contain optional resources. In my Maven POM, I have those as test dependencies. Is there some way to query the test dependencies from the Maven POM and inject them here under `runrequires` as well?

Currently, I have to manually update both, the `pom.xml` file and the `.bndrun` file when I need a new resource bundle test dependency.

---

<div class="post-metadata">

**Author:** ![juergen-albert](https://yyz2.discourse-cdn.com/free1/user_avatar/bnd.discourse.group/juergen-albert/32/20_2.png) [@juergen-albert](https://bnd.discourse.group/u/juergen-albert)\
**Post date:** [April 12, 2024, 1:20pm UTC](https://bnd.discourse.group/t/reading-run-requirements-from-pom-test-dependencies/426/2 "2024-04-12T13:20:01Z")

</div>

Do you have a lot of things that you need to add manually? Usually it is the resolvers job to collect this stuff. In most of our cases the we only have the `bnd.identity;id='${project.groupId}.${project.artifactId}-tests'` as runrequires and we let the resolver do the rest.

---

<div class="post-metadata">

**Author:** ![rec](https://yyz2.discourse-cdn.com/free1/user_avatar/bnd.discourse.group/rec/32/155_2.png) [@rec](https://bnd.discourse.group/u/rec)\
**Post date:** [April 12, 2024, 1:33pm UTC](https://bnd.discourse.group/t/reading-run-requirements-from-pom-test-dependencies/426/3 "2024-04-12T13:33:32Z")

</div>

Well, yes 🙂 I have various artifacts that contain only data (OSGI fragments without any Java code) and those are test dependencies in Maven and in the bndrun file I use for testing, I have to add them again.

Imagine a tool that supports different languages through language packs. The tool is sold to different markets, each only with one language pack. However, when testing, I want to test against all languages. So the dependency on the language packs is a test dependency in Maven. The language pack is an OSGI artifact which contains only some language files and no code.

So there is no hard code-wise dependency from the test code to the data fragments. I’m not sure how the resolver should be able to detect that I want them to be available - other by actually looking at the test-scoped dependencies in the POM - which I think it does not do.

While this is not exactly the situation I’m facing, it’s close enough.

---

<div class="post-metadata">

**Author:** ![rotty3000](https://yyz2.discourse-cdn.com/free1/user_avatar/bnd.discourse.group/rotty3000/32/16_2.png) [@rotty3000](https://bnd.discourse.group/u/rotty3000)\
**Post date:** [April 12, 2024, 2:52pm UTC](https://bnd.discourse.group/t/reading-run-requirements-from-pom-test-dependencies/426/4 "2024-04-12T14:52:10Z")

</div>

This is exactly what you’d use Requirements and Capabilities for as an end user of OSGi.

Define, in each Data bundle, that it provides a capability. There could be several tiers of capabilities defined such that you can pin down what you want on multiple levels; individuals, subset, entire set:

e.g. Let’s say you have bundle that has this header:

`Provide-Capability: data-file;data-file=lua;family=intepreted;language=lua`

and another:  
`Provide-Capability: data-file;data-file=c;family=compiled;language=c`

In a bundle that requires to have at least one data bundle of any particular type:

`Require-Capability: data-file;filter:=‘(data-file=\*)’

Or in a test suite where you want a specific one (bnd -runrequires for instance):  
`Require-Capability: data-file;filter:=‘(data-file=c)’

Or maybe just any interpreted ones:  
`Require-Capability: data-file;filter:=‘(family=intepreted)’

See [https://blog.osgi.org/2015/12/using-requirements-and-capabilities.html](https://blog.osgi.org/2015/12/using-requirements-and-capabilities.html) for a more thorough example.

---

<div class="post-metadata">

**Author:** ![rec](https://yyz2.discourse-cdn.com/free1/user_avatar/bnd.discourse.group/rec/32/155_2.png) [@rec](https://bnd.discourse.group/u/rec)\
**Post date:** [April 12, 2024, 3:44pm UTC](https://bnd.discourse.group/t/reading-run-requirements-from-pom-test-dependencies/426/5 "2024-04-12T15:44:55Z")

</div>

I do have to learn more about capabilities, but I do not think it helps me in this case.

The Maven POM defines which test artifacts I want to use. It does so via test dependencies.

Even with capabilities, I would need to duplicate the configuration from the POM in the bndrun file. I might be able to use some wildcards or such, but still I would have to duplicate the configuration.

Want I would prefer is to be able to say `hey bnd, use all those test-scoped dependencies from the POM and add them to your "runrequires" please` so I would have exactly those test dependencies - not more and not less.

So maybe something like:

```auto
-runrequires: \
	bnd.identity;id='${project.dependencies;scope=compile},\
	bnd.identity;id='${project.dependencies;scope=test},\

```

---

<div class="post-metadata">

**Author:** ![rotty3000](https://yyz2.discourse-cdn.com/free1/user_avatar/bnd.discourse.group/rotty3000/32/16_2.png) [@rotty3000](https://bnd.discourse.group/u/rotty3000)\
**Post date:** [April 12, 2024, 6:49pm UTC](https://bnd.discourse.group/t/reading-run-requirements-from-pom-test-dependencies/426/6 "2024-04-12T18:49:04Z")

</div>

> `hey bnd, use all those test-scoped dependencies from the POM and add them to your "runrequires" please`

The bnd-resolver-maven-plugin in combination with the bnd-testing-maven-plugin can do exactly that.

I call it “continuous resolution”. When running tests, your first real test should be “does this all resolve?”.

If you can’t even resolve, then your other “tests” aren’t likely going to make your users’ lives any easier and you’re probably already in deeper trouble than you think (assembling an application from your bundles will feel like just throwing spaghetti at the wall to see what sticks).

As an example of this in the real world, when we exposed tooling/infrastructure projects like JUnit(5), AssertJ, Mockito and so on to this mindset, they realized that “continuous resolution” prevented a slew of hard to detect OSGi problems and acts as a great safety net that you can point users to to say with confidence “this set of OSGi bundles is good, because not only do we run a battery of tests, but we resolve the entire runnable assembly every time we run tests.”

So, I recommend using “resolution” as your first line of checking, resolve your tests, which is what the bnd tester allows you to do, even in maven via the bnd-testing-maven-plugin.

Lastly, if you resolve, if you resolve your tests, you can use any requirements & capabilities to constrain what get’s into those test runtimes too.

There are several real world example projects that show how this is done (several Apache Aries subprojects operate this way using maven.)

---

<div class="post-metadata">

**Author:** ![chrisrueger](https://yyz2.discourse-cdn.com/free1/user_avatar/bnd.discourse.group/chrisrueger/32/18_2.png) [@chrisrueger](https://bnd.discourse.group/u/chrisrueger)\
**Post date:** [April 12, 2024, 6:54pm UTC](https://bnd.discourse.group/t/reading-run-requirements-from-pom-test-dependencies/426/7 "2024-04-12T18:54:01Z")

</div>

Requirements and capabilities would be my reaction too.  
I think it could look like this:

**TestBundleA** and **TestBundleB** , both declare and provide the capability “i-am-language” in their respective `bnd.bnd` files. Then you specify a requirement for this capability in a `.bndrun` file.

### TestBundleA’s bnd.bnd:

In the `bnd.bnd` file for TestBundleA, you need to specify that this bundle provides a capability. Here’s how this could look like:

```auto
Bundle-SymbolicName: TestBundleA
Bundle-Version: 1.0.0
Provide-Capability: i-am-language; i-am-language="german"

```

### TestBundleB’s bnd.bnd:

Similarly, for TestBundleB:

```auto
Bundle-SymbolicName: TestBundleB
Bundle-Version: 1.0.0
Provide-Capability: i-am-language; i-am-language="greek"

```

### .bndrun file:

For the `.bndrun` file, you can specify a requirement to use any bundle providing the “i-am-language” capability. Here’s how you could write this:

```auto
-runrequires: \
    osgi.identity;filter:='(i-am-language=*)'

```

This setup should instruct OSGi to include any available bundle that provides a matching capability.  
Not sure this is syntactically correct, but this is how I understand req & cap.

---

<div class="post-metadata">

**Author:** ![rec](https://yyz2.discourse-cdn.com/free1/user_avatar/bnd.discourse.group/rec/32/155_2.png) [@rec](https://bnd.discourse.group/u/rec)\
**Post date:** [April 12, 2024, 7:06pm UTC](https://bnd.discourse.group/t/reading-run-requirements-from-pom-test-dependencies/426/8 "2024-04-12T19:06:56Z")

</div>

On second though, I think my example was not close enough ☹

I wrote _I want to test against **all** languages_, but I should have written _I want to test against **some** languages_ - namely exactly those for which I also include the respective dependencies in the `pom.xml` file.

Otherwise, the capabilities might indeed offer a nice way to go. But the **some** here makes the whole difference I believe.

---

<div class="post-metadata">

**Author:** ![chrisrueger](https://yyz2.discourse-cdn.com/free1/user_avatar/bnd.discourse.group/chrisrueger/32/18_2.png) [@chrisrueger](https://bnd.discourse.group/u/chrisrueger)\
**Post date:** [April 12, 2024, 7:36pm UTC](https://bnd.discourse.group/t/reading-run-requirements-from-pom-test-dependencies/426/9 "2024-04-12T19:36:43Z")

</div>

> [@rec](#):
>
> But the **some** here makes the whole difference I believe.

ok 🙂

Sounds a bit like your approach is somehow `pom.xml` first. I have no experience with this.

Just a last tip for learning and visualising Requirements & Capabilities, in case you don’t know already:

The [Resolution view](https://bndtools.org/manual/resolution-view.html) makes Capabilities (and Requirements) of bundles visible. It allows you to **see** and the capabilities of specific bundles (and maybe it helps to find one that you maybe not be aware of).

- (multi-)select your bundles you want to observe in the Repository Browser
- then check the right side of the Resolution view for the capabilities of the selected bundles

 ![image](https://global.discourse-cdn.com/free1/uploads/bnd/original/1X/37929cef52e7bc96c2617b229f4879506d9833cd.jpeg)

The screenshot is from a 7.1 snapshot.

---

<div class="post-metadata">

**Author:** ![pkriens](https://yyz2.discourse-cdn.com/free1/user_avatar/bnd.discourse.group/pkriens/32/3_2.png) [@pkriens](https://bnd.discourse.group/u/pkriens)\
**Post date:** [April 17, 2024, 4:23pm UTC](https://bnd.discourse.group/t/reading-run-requirements-from-pom-test-dependencies/426/10 "2024-04-17T16:23:24Z")

</div>

If you have a property in bnd, you can use the [template](https://bnd.bndtools.org/macros/template.html) macro to build a new header.
