Hello João and Matt.

Indeed, after taking a closer look at the custom command example command, it seems simpler to use the Karaf API than the OSGi API and SCR. Since my last post, I've successfully built and deployed a simple command using the "karaf-services-maven-plugin" to generate the additional entries in the bundle manifest. However, I had to add an "execution" to the plugin, whereas the example doesn't, because without it, the plugin isn't triggered at the expected maven phase. I see that you directly add "Karaf-Commands:*" to the bundle build configuration. Is this sufficient, and is it a good practice ?

In any case, there's no need to add any additional features like SCR to mine.

Thank you very much.


Ephemeris Lappis

Le 17/04/2025 à 18:52, João Assunção a écrit :
I checked, and the annotations package are not required during runtime. If I use the headers command to list the packages required by a bundle where SCR components are used, the package "org.osgi.service.component.annotations" is not required.
In the previous email I forgot to mention that I install the scr feature.
For karaf shell commands, I don't use SCR. I use Karaf's API for commands.
Follow a simple command using a service provided by an SCR component. For the command to be recognized by Karaf it is necessary to include a header  "Karaf-Commands:*"in the bundle manifest. I use bnd-maven-plugin to build my bundles

    import org.apache.karaf.shell.api.action.Action;
    import org.apache.karaf.shell.api.action.Argument;
    import org.apache.karaf.shell.api.action.Command;
    import org.apache.karaf.shell.api.action.lifecycle.Reference;
    import org.apache.karaf.shell.api.action.lifecycle.Service;

    import com.atobe.ort.photo.repository.PhotoRepositoryService;

    @Command(scope = "photorepo", name = "purge", description = "Purge")
    @Service
    public class PurgeCommand implements Action {

        @Reference
        PhotoRepositoryService repositoryService;

        @Argument(index = 0, name = "days", description = "Days",
    multiValued = false, required = true)
        Integer days;

        @Override
        public Object execute() throws Exception {

            Duration age = Duration.ofDays(days);
            repositoryService.purgePhotos(age);
            return null;
        }

    }


The corresponding "bnd.bnd" file:

    Bundle-Name: ${project.name <http://project.name>}
    Bundle-Description: ${project.description}
    Karaf-Commands:*

Best regards,
João



On Thu, Apr 17, 2025 at 4:41 PM Ephemeris Lappis <ephemeris.lap...@gmail.com> wrote:

    Hello João !

    First, thanks for your answer.

    I see your dependency as "provided", so I suppose that you assume
    that classes are exported by some bundle. In my case I didn't find
    any standard feature with a bundle that exports the required
    annotations packages. What did you do to get them at runtime ?

    As I aim to provide custom shell commands, I'd initially prefer to
    use a basic OSGi design, and using annotations, and. with the SCR
    files being generated by the bundle plugin.

    An alternate solution could be using Karaf packages, as described
    in these examples
    
(https://github.com/apache/karaf/blob/main/examples/karaf-command-example/karaf-command-example-command/src/main/java/org/apache/karaf/examples/command/command/AddCommand.java).


    But this also fails : my custom command is not correctly detected
    and installed.

    An idea of the best way to do that ?

    Thanks again.

    Regards.

    Le jeu. 17 avr. 2025 à 13:47, João Assunção
    <joao.assun...@exploitsys.com> a écrit :

        Hi,
        I'm almost sure the annotations are not needed during
        execution. During runtime, SCR uses the XML files included
        inside the bundle (jar).
        The bundle plugin uses the annotation in the classes to
        generate those xml descriptors.
        In my maven projects, I normally use the "bom" dependency
        provided by karaf to ensure I'm using the same dependencies
        used by Karaf. For example:

            <dependency>
                <groupId>org.apache.karaf</groupId>
                <artifactId>karaf-bom</artifactId>
                <version>${karaf.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>


        But for my convenience, I tend to use the Enroute dependency:
        <dependency>
            <groupId>org.osgi.enroute</groupId>
            <artifactId>osgi-api</artifactId>
            <version>7.0.0</version>
            <type>pom</type>
            <scope>provided</scope>

</dependency>

        Best regards,
        João





        On Thu, Apr 17, 2025 at 11:50 AM Ephemeris Lappis
        <ephemeris.lap...@gmail.com> wrote:

            Hello.

            I can't find if a Karaf feature exists to provide the
            annotations for SCR, such as Service, Reference, etc.

            Installing the feature "scr" just adds bundles to manage
            the runtime, not the annotations.

            Perhaps the packages are provided by  a Compendium bundle.

            Is there a provided feature that includes them, or do we
            have to add explicitly bundles for that ? In this case,
            what is the right version to match the OSGi framework of
            Karaf 4.4.x ?

            Thanks for your help.

            Regards.


--
Cet e-mail a été vérifié par le logiciel antivirus d'Avast.
www.avast.com

Reply via email to