需求
hyperledger fabric(分佈式賬本)由於其本身的特性,目前主要應用於大型國企和銀行,其環境的部署一般是與外部網絡隔離的。如果採用docker的部署方式,docker鏡像可以通過導入即可,但是在安裝和實例化的時候需要下載相應的依賴,而且在1.4的環境中,fabric實例化鏈碼需要使用到shadowJar插件。
問題
如何將 Java鏈碼工程所需要的依賴引入工程指定路徑下
解決方案
目前網上能找到的解決方案是修改javaenv鏡像的dockerfile,將shadowJar插件的依賴直接在生成javaenv鏡像文件的時候加載(這就需要改鏡像了,看起來就很複雜,我很賴,所以你懂得)。測試這種方法的時候發現在重新生成javaenv鏡像文件的時候會缺少一些文件,導致生成的鏡像不可用。
參考文章地址:http://www.bubuko.com/infodetail-3024199.html
於是較爲簡單的方法是將所有依賴下載到指定目錄下:
build.gradle
文件內容:
/*
* Copyright IBM Corp. 2018 All Rights Reserved.
* 解決Fabric 鏈碼 離線安裝問題:將所依賴JAR包統一放入libs下
* Author: cuixiangyang
* Date: 2020-02-13
* SPDX-License-Identifier: Apache-2.0
*/
plugins {
id 'com.github.johnrengelman.shadow' version '2.0.3'
id 'java'
}
/*buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.3'
}
}
apply plugin: 'java'
apply plugin: 'com.github.johnrengelman.shadow'
*/
group 'org.hyperledger.fabric-chaincode-java'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenLocal()
/*flatDir{
dirs 'libs'
}*/
mavenCentral()
maven {
url "https://repository.mulesoft.org/nexus/content/repositories/public/"
}
}
dependencies {
compile group: 'org.hyperledger.fabric-chaincode-java', name: 'fabric-chaincode-shim', version: '1.+'
testCompile group: 'junit', name: 'junit', version: '4.12'
compile fileTree(dir:'libs',include:['*.jar'])
}
shadowJar {
baseName = 'chaincode'
version = null
classifier = null
manifest {
attributes 'Main-Class': 'org.hyperledger.fabric.example.SimpleChaincode'
}
}
//將依賴的jar包導入到項目的libs目錄下
task copyJars(type: Copy) {
from configurations.runtime
into 'libs' // 目標位置
}
build.dependsOn(copyJars)
然後Eclipse控制檯(我是在Eclipse安裝了Gradle插件和Maven插件)先後執行clean和build
在Console控制檯,出現如下日誌輸出(只提出了執行clean的日誌輸出,build的和他一樣,輸出SUCCESSFUL即成功)
然後在工程libs目錄下,可以看到Gradle Java鏈碼工程依賴的JAR包
然後將工程上傳Linux Fabric環境,由於環境是內網環境,無法聯網,所以需要修改build.gradle文件,將請求網絡獲取依賴Jar包的配置註釋,然後放開優先從本地指定目錄獲取依賴的配置。
/*
* Copyright IBM Corp. 2018 All Rights Reserved.
* 解決Fabric 鏈碼 離線安裝問題:將所依賴JAR包統一放入libs下
* Author: cuixiangyang
* Date: 2020-02-13
* SPDX-License-Identifier: Apache-2.0
*/
plugins {
id 'com.github.johnrengelman.shadow' version '2.0.3'
id 'java'
}
/*buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.3'
}
}
apply plugin: 'java'
apply plugin: 'com.github.johnrengelman.shadow'
*/
group 'org.hyperledger.fabric-chaincode-java'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenLocal()
flatDir{
dirs 'libs'
}
/*
mavenCentral()
maven {
url "https://repository.mulesoft.org/nexus/content/repositories/public/"
}*/
}
dependencies {
/*
compile group: 'org.hyperledger.fabric-chaincode-java', name: 'fabric-chaincode-shim', version: '1.+'
testCompile group: 'junit', name: 'junit', version: '4.12'*/
compile fileTree(dir:'libs',include:['*.jar'])
}
shadowJar {
baseName = 'chaincode'
version = null
classifier = null
manifest {
attributes 'Main-Class': 'org.hyperledger.fabric.example.SimpleChaincode'
}
}
//將依賴的jar包導入到項目的libs目錄下
task copyJars(type: Copy) {
from configurations.runtime
into 'libs' // 目標位置
}
build.dependsOn(copyJars)
然後上傳至Fabric網絡環境,進行離線安裝。
參考博客: