Bootstrap

Ubuntu22.04 解决fabric 安装gradle构建的Java合约超时

问题

fabric安装gradle构建的Java合约时遇到合约代码编译超时问题,如下:

$ peer lifecycle chaincode install cp.tar.gz 
Error: chaincode install failed with status: 500 - failed to invoke backing implementation of 'InstallChaincode': could not build chaincode: docker build failed: docker image build failed: docker build failed: Error returned from build: 1 "+ INPUT_DIR=/chaincode/input
+ OUTPUT_DIR=/chaincode/output
++ mktemp -d
+ TMP_DIR=/tmp/tmp.QxEGc5qu3R
++ find /chaincode/input -name '*.jar'
++ wc -l
+ NUM_JARS=1
+ for DIR in ${INPUT_DIR} ${INPUT_DIR}/src
+ '[' -f /chaincode/input/build.gradle -o -f /chaincode/input/build.gradle.kts ']'
+ '[' -f /chaincode/input/pom.xml ']'
+ for DIR in ${INPUT_DIR} ${INPUT_DIR}/src
+ '[' -f /chaincode/input/src/build.gradle -o -f /chaincode/input/src/build.gradle.kts ']'
+ buildGradle /chaincode/input/src /chaincode/output
+ echo 'Copying from /chaincode/input/src to /tmp/tmp.QxEGc5qu3R'
+ cd /chaincode/input/src
+ tar cf - .
+ cd /tmp/tmp.QxEGc5qu3R
+ tar xf -
Copying from /chaincode/input/src to /tmp/tmp.QxEGc5qu3R
+ cd /tmp/tmp.QxEGc5qu3R
+ echo 'Gradle build'
+ '[' -f ./gradlew ']'
+ chmod +x ./gradlew
Gradle build
+ ./gradlew build shadowJar -x test
Downloading https://services.gradle.org/distributions/gradle-6.5.1-bin.zip

Exception in thread "main" java.net.ConnectException: Connection timed out (Connection timed out)
	at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
	at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:412)
	at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:255)
	at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:237)
	at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
	at java.base/java.net.Socket.connect(Socket.java:609)
	at java.base/sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:305)
	at java.base/sun.security.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:173)
	at java.base/sun.net.NetworkClient.doConnect(NetworkClient.java:182)
	at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:509)
	at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:604)
	at java.base/sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:910)
	at java.base/sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:724)
	at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1637)
	at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1542)
	at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:250)
	at org.gradle.wrapper.Download.downloadInternal(Download.java:67)
	at org.gradle.wrapper.Download.download(Download.java:52)
	at org.gradle.wrapper.Install$1.call(Install.java:62)
	at org.gradle.wrapper.Install$1.call(Install.java:48)
	at org.gradle.wrapper.ExclusiveFileAccessManager.access(ExclusiveFileAccessManager.java:69)
	at org.gradle.wrapper.Install.createDist(Install.java:48)
	at org.gradle.wrapper.WrapperExecutor.execute(WrapperExecutor.java:107)
	at org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:63)
"

在logspout中输出为:
在这里插入图片描述
由此看到是下载https://services.gradle.org/distributions/gradle-6.5.1-bin.zip文件超时。

解决方案

替换镜像地址

把https://services.gradle.org/distributions/gradle-6.5.1-bin.zip替换成https://mirrors.cloud.tencent.com/gradle/gradle-6.5.1-bin.zip。

即 https://mirrors.cloud.tencent.com/gradle/{版本号}。

修改配置

修改Java工程目录下的gradle/wrapper/gradle-wrapper.properties文件,把distributionUrl=https://services.gradle.org/distributions/gradle-6.5.1-bin.zip 替换成 distributionUrl=https://mirrors.cloud.tencent.com/gradle/gradle-6.5.1-bin.zip

修maven仓库

在 build.gradle 文件中加入以下代码:

repositories {
    maven {
      url 'https://maven.aliyun.com/repository/public/'
    }
    maven {
      url 'https://maven.aliyun.com/repository/central'
    }
  }  

执行命令:

gradle dependencies 或 ./gradlew dependencies 安装依赖

替换后重新操作 链码打包 和安装流程。

;