我正在研究使用 Java native 接口(interface)的 OSGI 包。我使用这个 makefile 来构建 .so 模块:
#!/usr/bin/make -f
#
# Makefile for native stuff
#
# c files to compile
C_SOURCES := sqrt.c
# the name of the library to build
LIBNAME := sqrt
C_SOURCE_DIR := src
C_GENSOURCE_DIR := src
TARGET_DIR := ../../../target
C_BUILD_DIR = $(TARGET_DIR)/native
JAVA_BUILD_DIR = $(TARGET_DIR)/classes
# the name of the file we build
TARGET = $(JAVA_BUILD_DIR)/META-INF/lib/$(LIB_PREFIX)$(LIBNAME)$(LIB_EXTN)
# find the jdk. if this doesn't work for you, define JAVA_HOME in your
# environment or on the make command line
JAVA_HOME ?= /opt/jdk1.7.0_02
# classpath for javah
JAVAH_CLASSPATH = `cat $(TARGET_DIR)/compile-classpath`
# tools and options
CFLAGS = -Wall -fpic
CPPFLAGS = -I$(C_SOURCE_DIR) -I$(C_GENSOURCE_DIR) -Iinclude \
-I$(JAVA_HOME)/include
JAVAH = /opt/jdk1.7.0_02/bin/javah
JAVAH_FLAGS += -classpath $(JAVAH_CLASSPATH)
JAVAH_CMD = $(JAVAH) $(JAVAH_FLAGS) $(OUTPUT_OPTION)
LDFLAGS = -shared
LINK.so = $(CC) $(LDFLAGS) $(LD_LIBS)
ifdef DEBUG
CFLAGS += -g
LDFLAGS += -g
endif
# os-dependent bits
UNAME := $(shell uname)
ifeq ($(UNAME),Linux)
LIB_PREFIX = lib
LIB_EXTN = .so
CPPFLAGS += -I$(JAVA_HOME)/include/linux
else
ifeq ($(findstring CYGWIN,$(UNAME)),CYGWIN)
LIB_PREFIX =
LIB_EXTN = .dll
CPPFLAGS += -I$(JAVA_HOME)/include/win32
else
f := $(error Platform $(UNAME) not supported)
endif
endif
# we look in $(C_SOURCE_DIR) for c files...
vpath %.c $(C_SOURCE_DIR)
# convenience variables
C_OBJFILES = $(addprefix $(C_BUILD_DIR)/,$(subst .c,.o,$(C_SOURCES)))
# default target
all: $(TARGET)
# rule to compile the .c files
$(C_BUILD_DIR)/%.o: %.c
@mkdir -p `dirname $@`
$(COMPILE.c) $(OUTPUT_OPTION) $<
# link the C objects into a shared library
$(TARGET): $(C_OBJFILES) $(LDLIBS)
@mkdir -p `dirname $@`
$(LINK.so) $(OUTPUT_OPTION) $^
# a rule to build the .h file with javah
$(C_GENSOURCE_DIR)/org_DX_57_osgi_NB_27_impl_Sqrt.h: $(JAVA_BUILD_DIR)/org/DX_57/osgi/NB_27/impl/Sqrt.class
rm -f $@
$(JAVAH) $(JAVAH_FLAGS) $(OUTPUT_OPTION) org.DX_57.osgi.NB_27.impl.Sqrt
# the .o file depends on the .h file
$(C_BUILD_DIR)/sqrt.o: $(C_GENSOURCE_DIR)/org_DX_57_osgi_NB_27_impl_Sqrt.h
clean::
rm -f $(C_OBJFILES)
rm -f $(TARGET)
rm -f $(C_BUILD_DIR)/jnirules.mak
此Makefile 成功生成头文件并编译.o 文件。问题是当编译 bundle 时,.so 文件并没有打包成 bundle。这是 POM 文件的内容:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.DX_57.osgi.NB_27</groupId>
<artifactId>NB_27</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>NB_27-impl</artifactId>
<packaging>bundle</packaging>
<name>NB_27-impl</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.osgi.core</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>${pom.groupId}</groupId>
<artifactId>NB_27-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>build-native</id>
<phase>process-classes</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<property name="native.classpath" refid="maven.compile.classpath" />
<echo file="${project.build.directory}/compile-classpath" message="${native.classpath}" />
<exec dir="src/main/native" executable="make" failonerror="true" />
</tasks>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Import-Package>${pom.groupId}.api, org.osgi.framework, org.apache.commons.collections, org.apache.commons.collections.buffer</Import-Package>
<Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName>
<Bundle-Activator>${pom.groupId}.impl.NetworkBridgeApp</Bundle-Activator>
</instructions>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<groupId>org.DX_57.osgi.NB_27</groupId>
</project>
当我用 maven-assembly-plugin 替换 maven-bundle-plugin 和 maven-compiler-plugin 时, native 库包含在包中。我如何创建 OSGI 包并在构建期间包含 native 库?
我认为 Maven Bundle Plugin 没有针对 native 代码的特定规定,我认为您不需要它们:您需要的所有支持都已经存在。你需要
- 使用
<Include-Resource>
包含 native 库指令,和
- 包括
Bundle-NativeCode
说明中的 header ,就像您手动构建 list 时一样。
我是一名优秀的程序员,十分优秀!