<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.framework</artifactId>
<version>5.6.12</version>
</dependency>
LogService.java:
public interface LogService {
void log(String message);
}
ConsoleLogService.java:
public class ConsoleLogService implements LogService {
public void log(String message) {
System.out.println(message);
}
}
repo.url=http://example.com/osgi-repo
mvn clean install
mvn deploy:deploy-file -Durl=${repo.url} -Dfile=target/mymodule.jar
import org.apache.felix.framework.*;
import org.osgi.framework.*;
import org.osgi.util.tracker.ServiceTracker;
public class Example {
public static void main(String[] args) throws BundleException {
FelixFrameworkFactory factory = new FelixFrameworkFactory();
Map<String, String> config = new HashMap<>();
config.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, "org.osgi.framework");
config.put(Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);
config.put(Constants.FRAMEWORK_STORAGE, "target/cache");
Framework framework = factory.newFramework(config);
framework.init();
BundleContext context = framework.getBundleContext();
Bundle bundle = context.installBundle("http://example.com/osgi-repo/mymodule.jar");
bundle.start();
ServiceReference<LogService> reference =
context.getServiceReference(LogService.class);
LogService logService = context.getService(reference);
logService.log("Hello world!");
framework.stop();
framework.waitForStop(0);
}
}