原文:https://blog.csdn.net/u013830021/article/details/74360448
opendatlight 源碼中涉及到的知識有:
maven,OSGI,karaf,yang 技術;Config Subsystem,MD-SAL,MD-SAL Clustering 子系統;NETCONF,RESTCONF 協議。
# maven
At first glance Maven can appear to be many things, but in a nutshell Maven is an attempt to apply patterns to a project’s build infrastructure in order to promote comprehension and productivity by providing a clear path in the use of best practices. Maven is essentially a project management and comprehension tool and as such provides a way to help with managing:
Builds
Documentation
Reporting
Dependencies
SCMs
Distribution
Releases
maven 是項目管理工具,確實很好用。自己接觸過的使用 maven 管理的項目:opendaylight,spring,hibernate。
> **pom.xml 文件標簽詳解:https://maven.apache.org/pom.html
> 配置參考網址:https://maven.apache.org/settings.html**
可以打開一個項目的 pom.xml 查看,遇到不認得的標簽就去上面網頁查,這樣學習比較快。
我覺得理解了 pom.xml 文件標簽含義和 maven 工具的配置,就算 maven 入門了。
> 順便附一個官網入門指南:https://maven.apache.org/guides/getting-started/index.html
> 因為自己原來已經接觸過 maven 了,對自己用處不太大。
maven 插件
> 深入學習的話,需要學習 maven 強大的插件功能。
> https://maven.apache.org/plugins/index.html
我們都知道Maven本質上是一個插件框架,它的核心并不執行任何具體的構建任務,所有這些任務都交給插件來完成,例如編譯源代碼是由maven-compiler-plugin完成的。進一步說,每個任務對應了一個插件目標(goal),每個插件會有一個或者多個目標,例如maven-compiler-plugin的compile目標用來編譯位于src/main/java/目錄下的主源碼,testCompile目標用來編譯位于src/test/java/目錄下的測試源碼。
用戶可以通過兩種方式調用Maven插件目標。第一種方式是將插件目標與生命周期階段(lifecycle phase)綁定,這樣用戶在命令行只是輸入生命周期階段而已,例如Maven默認將maven-compiler-plugin的compile目標與compile生命周期階段綁定,因此命令mvn compile實際上是先定位到compile這一生命周期階段,然后再根據綁定關系調用maven-compiler-plugin的compile目標。第二種方式是直接在命令行指定要執行的插件目標,例如mvn archetype:generate 就表示調用maven-archetype-plugin的generate目標,這種帶冒號的調用方式與生命周期無關。
maven plugin 案例分析
在 opendaylight 的各個子項目中都引入了 maven-bundle-plugin,下面列出 routing 中 pom.xml 部分內容。
~~~
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Import-Package>org.slf4j,
org.opendaylight.controller.sal.routing,
org.opendaylight.controller.sal.core,
org.opendaylight.controller.sal.topology,
org.opendaylight.controller.sal.utils,
org.opendaylight.controller.sal.reader,
org.opendaylight.controller.clustering.services,
org.apache.commons.collections15,
org.opendaylight.controller.switchmanager,
org.opendaylight.controller.topologymanager,
edu.uci.ics.jung.graph,
edu.uci.ics.jung.algorithms.shortestpath,
edu.uci.ics.jung.graph.util,
org.apache.felix.dm,
org.osgi.framework,
org.apache.felix.service.command,
org.junit;resolution:=optional</Import-Package>
<Bundle-Activator>org.opendaylight.controller.routing.dijkstra_implementation.internal.Activator</Bundle-Activator>
</instructions>
<manifestLocation>${project.basedir}/META-INF</manifestLocation>
</configuration>
</plugin>
</plugins>
</build>
~~~
網上查看 maven-bundle-plugin 的執行目標信息如下:
General Information about the goals.
bundle:bundle Create an OSGi bundle from Maven project. (life-cycle goal)
bundle:manifest Generate an OSGi manifest for this project.
bundle:cleanVersions Convert a group of versions to OSGi format.
bundle:instructions Generate BND instructions for this project.
bundle:install Installs bundle details in the local OBR repository. (life-cycle goal)
bundle:deploy Deploys bundle details to a remote OBR repository. (life-cycle goal)
bundle:index Index the content of a maven repository using OBR.
bundle:install-file Installs bundle details in the local OBR repository. (command-line goal)
bundle:deploy-file Deploys bundle details to a remote OBR repository. (command-line goal)
bundle:clean Clean a local OBR repository by finding and removing missing resources.
bundle:remote-clean Clean a remote OBR repository by finding and removing missing resources.
bundle:ant Generate Ant script to create the bundle. (you should run ant:ant first)
可以看出這個插件中部分 goals 是與生命周期綁定的,部分沒有。我們執行 maven:install 其實調用的 bundle:install 功能。
再附上幾個我原來看過的中文網址:
[Apache Maven 入門篇 ( 上 )](http://www.oracle.com/technetwork/cn/community/java/apache-maven-getting-started-1-406235-zhs.html)
[Apache Maven 入門篇 ( 下 )](http://www.oracle.com/technetwork/cn/community/java/apache-maven-getting-started-2-405568-zhs.html)
[細致全面的Maven教程](https://www.yiibai.com/maven/)
[maven 插件介紹](http://www.infoq.com/cn/news/2011/04/xxb-maven-7-plugin)
[maven-bundle-plugin](http://felix.apache.org/components/bundle-plugin/index.html)
待解決疑惑:
opendaylight 提供的 settings.xml 文件中,有兩個 activeProfile 標簽,看網上的解釋說可以激活多個 profile,那都激活后哪一個會起作用?
~~~
<activeProfiles>
<activeProfile>opendaylight-release</activeProfile>
<activeProfile>opendaylight-snapshots</activeProfile>
</activeProfiles>
~~~
參考網址:http://blog.csdn.net/taiyangdao/article/details/52311257
OSGI 入門
OSGI 是一個標準,實現這個標準的產品有:apache felix,Equinox等。
OSGI 架構圖
OSGI 可以分為三層:模塊層,生命周期層,服務層。
每一層的學習可以看下面的入門教程。
參考書目和文檔:
《深入理解OSGi Equinox原理、應用與最佳實踐》,講的很詳細。
[官網文檔](http://felix.apache.org/documentation.html)
[中文社區](http://osgi.com.cn/)
[入門教程](https://course.tianmaying.com/osgi-toturial)
[OSGi Core Release 6 Specification](https://osgi.org/download/r6/osgi.core-6.0.0.pdf)
[里面有 Specifications,Javadoc,XML Schemas 等內容](https://www.osgi.org/developer/specifications/)
# karaf
Karaf Container is a modern and polymorphic container.It’s a lightweight, powerful, and enterprise ready container powered by OSGi.
karaf 架構圖
Karaf安裝目錄結構如下:
~~~
/bin: 啟動腳本
/etc: 初始化文件
/data: 工作目錄
/cache: OSGi框架包緩存
/generated-bundles: 部署使用的臨時文件夾
/log: 日志文件
/deploy: 熱部署目錄
/instances: 含有子實例的目錄
/lib: 包含引導庫
/lib/ext:JRE擴展目錄
/lib/endorsed: 贊同庫目錄
/system: OSGi包庫,作為一個Maven2存儲庫
~~~
Data文件夾包括karaf所有的工作和臨時文件,如果你想從一個初始狀態重啟,你可以清空這個目錄,這和“恢復初始化設置”一樣的效果。
摘自: http://www.osgi.com.cn/article/7289403
karaf 使用教程:http://karaf.apache.org/manual/latest/
上面教程學習了一部分,下次有需要再看。
待解決疑惑:
felix 是如何確定 bundle 的啟動級別的?
karaf 呢?它又是如何確定 bundle 的啟動級別的?
第一個問題,我覺得跟 OSGI 的依賴解析規則有關,根據解析的結果,確定啟動順序。