读取OSGi Bundle jar包内的版本信息

通过buildnumber-maven-plugins和maven-jar-plugins能在bundle打包为jar时添加版本号,如svn的版本号

<build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>buildnumber-maven-plugin</artifactId>
                <version>1.2</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>create</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <doCheck>false</doCheck>
                    <doUpdate>true</doUpdate>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifestEntries>
                            <Subversion>${buildNumber}</Subversion>
                            <Build-Time>${maven.build.timestamp}</Build-Time>                            
                        </manifestEntries>
                        <manifestFile>META-INF/MANIFEST.MF</manifestFile>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
</build>

写入到jar以后,在OSGi环境中怎么读出来呢?

    public void start(BundleContext context) throws Exception {

        Manifest mf = new Manifest();
        try {
            URL resource = context.getBundle().getResource(
                    "/META-INF/MANIFEST.MF");
            InputStream is = resource.openStream();
            mf.read(is);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {
            System.out.println("未读到版本参数文件");
        }
        Attributes atts = mf.getMainAttributes();
        String version = atts.getValue("Bundle-Version") + "."
                + atts.getValue("Subversion");

        System.out.println("bundle start! version:" + version);

    }

done.

 

 

你可能感兴趣的:(读取OSGi Bundle jar包内的版本信息)