Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Excerpt
hiddentrue

Using the Apache Felix Maven SCR Plugin to generate Declarative Services and Metatype Service descriptors during a Maven Build

...

Support for automatic generation of the compenent and metadata descriptors is embeded in the

org.apache.felix:maven-scr-plugin

...

plugin.

...

To

...

use

...

this

...

plugin,

...

it

...

has

...

to

...

be

...

declared

...

in

...

the

...

project

...

descriptor

...

as

...

a

...

<plugin>

...

element:

Code Block
xml
xml
 

{code:xml} 
<project>
  ...
  <build>
    ...
    <plugins>
      ...
      <plugin>
        <groupId>org.apache.felix</groupId>
        <artifactId>maven-scr-plugin</artifactId>
        <executions>
          <execution>
            <id>generate-scr-scrdescriptor</id>
            <goals>
              <goal>scr</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      ...
    </plugins>
    ...
  </build>
  ...
</project>
{code} 

The {{scr}} goal is bound to the {{generate-resources}} phase and will generate a single descriptor file as well as meta type file for all components found in the project. 

The plugin may be configured with the following properties (Check the version column to make sure you use at least this version for the mentioned feature): 
| *Property* | *Description* | *Version* | 
| {{specVersion}} | The plugin will generate a descriptor for the Declarative Service version (either 1.0 or 1.1). If no value is specified, the plugin will detect the version and only use 1.1 if features from this version are used. | >= 1.4.0 | 
| {{generateAccessors}} | If this switch is turned on, the bind and unbind methods for unary references are automatically generated by the plugin. By default this is set to {{true}}. | | 
| {{parseJavadoc}} | If this switch is turned on, the java source code and its javadoc tags are scanned for the scr tags (see below). The default value is {{true}}. | | 
| {{processAnnotations}} | If this switch is turned on, the java code is scanned for the scr annotations (see below). The default value is {{true}}. | >= 1.2.0 | 
| {{sourceExcludes}} | Comma separated list of classes to exclude when processing the source. | | 
| {{strictMode}} | The plugin distinguishes between errors and warnings. In strict mode warnings are treated as errors and cause the plugin to fail. The default value is {{false}}. | | 
| {{properties}} | A map of predefined properties. These properties are set to each component (if the component does not define the property already). This is a map where the property name is made up by the included element name and the value is the value of the element.| >= 1.2.0 | 
| {{finalName}} | The name of the descriptor file to create. This property defaults to the value of the {{scr.descriptor.name}} property if defined. Otherwise the default is {{serviceComponents.xml}}. | | 
| {{metaTypeName}} | The name of the descriptor file to create. This property defauls to {{metatype.xml}}. | | 
| {{outputDirectory}} | The directory where all files are generated in. This defaults to ${project.build.directory}/scr-plugin-generated. | | 
The meta type file is generated in the {{OSGI-INF/metatype/}} directory and the scr descriptor file in the {{OSGI-INF}} directory. 
_Note_: The location of the meta type descriptor may not be changed as the OSGi Metatype Service Specification prescribes the location of the descriptors. 

The plugin will look for component definition tags in all Java files found in the source directories of the project. 

h3. Using the descriptor 

Currently the {{maven-scr-plugin}} only creates the component descriptor file. Adding the descriptor to the bundle and setting the {{Service-Component}} manifest header accordingly is a different task. However, if you're using the {{

The scr goal is bound to the generate-resources phase and will generate a single descriptor file as well as meta type file for all components found in the project.

The plugin may be configured with the following properties (Check the version column to make sure you use at least this version for the mentioned feature):

Property

Description

Version

specVersion

The plugin will generate a descriptor for the Declarative Service version (either 1.0 or 1.1). If no value is specified, the plugin will detect the version and only use 1.1 if features from this version are used.

>= 1.4.0

generateAccessors

If this switch is turned on, the bind and unbind methods for unary references are automatically generated by the plugin. By default this is set to true.

 

parseJavadoc

If this switch is turned on, the java source code and its javadoc tags are scanned for the scr tags (see below). The default value is true.

 

processAnnotations

If this switch is turned on, the java code is scanned for the scr annotations (see below). The default value is true.

>= 1.2.0

sourceExcludes

Comma separated list of classes to exclude when processing the source.

 

strictMode

The plugin distinguishes between errors and warnings. In strict mode warnings are treated as errors and cause the plugin to fail. The default value is false.

 

properties

A map of predefined properties. These properties are set to each component (if the component does not define the property already). This is a map where the property name is made up by the included element name and the value is the value of the element.

>= 1.2.0

finalName

The name of the descriptor file to create. This property defaults to the value of the scr.descriptor.name property if defined. Otherwise the default is serviceComponents.xml.

 

metaTypeName

The name of the descriptor file to create. This property defauls to metatype.xml.

 

outputDirectory

The directory where all files are generated in. This defaults to scr-plugin-generated inside the build output directory.

 

The meta type file is generated in the OSGI-INF/metatype/ directory and the scr descriptor file in the OSGI-INF directory.
Note: The location of the meta type descriptor may not be changed as the OSGi Metatype Service Specification prescribes the location of the descriptors.

The plugin will look for component definition tags in all Java files found in the source directories of the project.

Using the descriptor

Currently the maven-scr-plugin only creates the component descriptor file. Adding the descriptor to the bundle and setting the Service-Component manifest header accordingly is a different task. However, if you're using the org.apache.felix:maven-bundle-plugin

...

to

...

construct

...

the

...

bundle

...

and

...

its

...

manifest,

...

then

...

the

...

maven-scr-plugin

...

will

...

add

...

the

...

following

...

settings

...

automatically

...

for

...

the

...

org.apache.felix:maven-bundle-plugin

...

(given

...

default

...

maven-scr-plugin

...

configuration),

...

so

...

you

...

don't

...

have

...

to

...

configure

...

this

...

yourself:

Code Block
xml
xml
 

{code:xml} 
...
<Include-Resource>
    src/main/resources,
    target/scr-plugin-generated
</Include-Resource>
<Service-Component>
  OSGI-INF/serviceComponents.xml
</Service-Component>
...
{code}