Skip to content

Commit 4c384c8

Browse files
committed
Add support for Maven build in java chaincodes
FIX FAB-158 https://jira.hyperledger.org/browse/FAB-158 Change-Id: I79aae2b4e537a20a72b2fcc1bf83a40087cfeed6 Signed-off-by: Satheesh Kathamuthu <[email protected]>
1 parent c6e56d6 commit 4c384c8

File tree

22 files changed

+925
-303
lines changed

22 files changed

+925
-303
lines changed

bddtests/java_shim.feature

+2-2
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ Scenario: java RangeExample chaincode single peer
126126
||
127127
||
128128
Then I should have received a chaincode name
129-
Then I wait up to "30" seconds for transaction to be committed to all peers
129+
Then I wait up to "240" seconds for transaction to be committed to all peers
130130

131131
When requesting "/chain" from "vp0"
132132
Then I should get a JSON response with "height" = "2"
@@ -178,7 +178,7 @@ Scenario: java RangeExample chaincode single peer
178178
When requesting "/chain" from "vp0"
179179
Then I should get a JSON response with "height" = "1"
180180
# TODO Needs to be replaced with an official test repo in the future.
181-
When I deploy lang chaincode "http://github.com/xspeedcruiser/javachaincode" of "JAVA" with ctor "init" to "vp0"
181+
When I deploy lang chaincode "http://github.com/xspeedcruiser/javachaincodemvn" of "JAVA" with ctor "init" to "vp0"
182182
| arg1 | arg2 | arg3 | arg4 |
183183
| a | 100 | b | 200 |
184184
Then I should have received a chaincode name

core/chaincode/chaincode_support.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ func (chaincodeSupport *ChaincodeSupport) getArgsAndEnv(cID *pb.ChaincodeID, cLa
298298
case pb.ChaincodeSpec_JAVA:
299299
//TODO add security args
300300
args = strings.Split(
301-
fmt.Sprintf("/root/Chaincode/bin/runChaincode -a %s -i %s",
301+
fmt.Sprintf("java -jar chaincode.jar -a %s -i %s",
302302
chaincodeSupport.peerAddress, cID.Name),
303303
" ")
304304
if chaincodeSupport.peerTLS {

core/chaincode/platforms/java/hash.go

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright DTCC 2016 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package java
218

319
import (

core/chaincode/platforms/java/package.go

+51-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
1+
/*
2+
Copyright DTCC 2016 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package java
218

319
import (
420
"archive/tar"
521
"fmt"
22+
"io/ioutil"
623
"strings"
724
"time"
825

@@ -13,6 +30,31 @@ import (
1330
"github.com/spf13/viper"
1431
)
1532

33+
var buildCmd = map[string]string{
34+
"build.gradle": "gradle -b build.gradle clean && gradle -b build.gradle build",
35+
"pom.xml": "mvn -f pom.xml clean && mvn -f pom.xml package",
36+
}
37+
38+
//return the type of build gradle/maven based on the file
39+
//found in java chaincode project root
40+
//build.gradle - gradle - returns the first found build type
41+
//pom.xml - maven
42+
func getBuildCmd(packagePath string) (string, error) {
43+
files, err := ioutil.ReadDir(packagePath)
44+
if err != nil {
45+
return "", err
46+
} else {
47+
for _, f := range files {
48+
if !f.IsDir() {
49+
if buildCmd, ok := buildCmd[f.Name()]; ok == true {
50+
return buildCmd, nil
51+
}
52+
}
53+
}
54+
return "", fmt.Errorf("Build file not found")
55+
}
56+
}
57+
1658
//tw is expected to have the chaincode in it from GenerateHashcode.
1759
//This method will just package the dockerfile
1860
func writeChaincodePackage(spec *pb.ChaincodeSpec, tw *tar.Writer) error {
@@ -42,22 +84,25 @@ func writeChaincodePackage(spec *pb.ChaincodeSpec, tw *tar.Writer) error {
4284
urlLocation = urlLocation[:len(urlLocation)-1]
4385
}
4486

87+
buildCmd, err := getBuildCmd(urlLocation)
88+
if err != nil {
89+
return err
90+
}
4591
var dockerFileContents string
4692
var buf []string
4793

4894
if viper.GetBool("security.enabled") {
4995
//todo
5096
} else {
5197
buf = append(buf, cutil.GetDockerfileFromConfig("chaincode.java.Dockerfile"))
52-
buf = append(buf, "COPY src /root")
53-
buf = append(buf, "RUN gradle -b build.gradle build")
54-
buf = append(buf, "RUN unzip -od /root build/distributions/Chaincode.zip")
55-
98+
buf = append(buf, "COPY src /root/chaincode")
99+
buf = append(buf, "RUN cd /root/chaincode && "+buildCmd)
100+
buf = append(buf, "RUN cp /root/chaincode/build/chaincode.jar /root")
101+
buf = append(buf, "RUN cp /root/chaincode/build/libs/* /root/libs")
56102
}
57-
dockerFileContents = strings.Join(buf, "\n")
58103

104+
dockerFileContents = strings.Join(buf, "\n")
59105
dockerFileSize := int64(len([]byte(dockerFileContents)))
60-
61106
//Make headers identical by using zero time
62107
var zeroTime time.Time
63108
tw.WriteHeader(&tar.Header{Name: "Dockerfile", Size: dockerFileSize, ModTime: zeroTime, AccessTime: zeroTime, ChangeTime: zeroTime})

core/chaincode/platforms/java/platform.go

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright DTCC 2016 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package java
218

319
import (

core/chaincode/shim/java/build.gradle

+69-56
Original file line numberDiff line numberDiff line change
@@ -15,65 +15,67 @@ limitations under the License.
1515
*/
1616

1717
buildscript {
18-
repositories {
19-
mavenLocal()
20-
mavenCentral()
21-
jcenter()
22-
}
23-
dependencies {
24-
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.7.6'
25-
}
18+
repositories {
19+
mavenLocal()
20+
mavenCentral()
21+
jcenter()
22+
}
23+
dependencies {
24+
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.7.6'
25+
}
2626
}
2727

2828
plugins {
29-
id "java"
30-
id "com.google.protobuf" version "0.7.6"
31-
id "eclipse"
29+
id "java"
30+
id "com.google.protobuf" version "0.7.6"
31+
id "eclipse"
32+
id "maven-publish"
33+
3234
}
33-
archivesBaseName = 'chaincode'
35+
archivesBaseName = 'shim-client'
3436
version = '1.0'
3537

3638

3739
sourceSets {
38-
main {
39-
java {
40-
srcDir 'src/main/java'
41-
}
42-
proto {
43-
srcDir 'src/main/proto'
44-
}
45-
}
40+
main {
41+
java {
42+
srcDir 'src/main/java'
43+
}
44+
proto {
45+
srcDir 'src/main/proto'
46+
}
47+
}
4648
}
4749

4850
repositories {
49-
mavenLocal()
51+
mavenLocal()
5052
mavenCentral()
5153
}
5254

5355
protobuf {
54-
generatedFilesBaseDir = "$projectDir/src"
55-
protoc {
56-
artifact = 'com.google.protobuf:protoc:3.0.0-beta-2'
57-
}
58-
plugins {
59-
grpc {
60-
artifact = 'io.grpc:protoc-gen-grpc-java:0.13.2'
61-
}
62-
}
63-
generateProtoTasks {
64-
all().each { task ->
65-
task.builtins {
66-
java {
67-
outputSubDir = 'java'
68-
}
69-
}
70-
task.plugins {
71-
grpc {
72-
outputSubDir = 'java'
73-
}
74-
}
75-
}
76-
}
56+
generatedFilesBaseDir = "$projectDir/src"
57+
protoc {
58+
artifact = 'com.google.protobuf:protoc:3.0.0-beta-2'
59+
}
60+
plugins {
61+
grpc {
62+
artifact = 'io.grpc:protoc-gen-grpc-java:0.13.2'
63+
}
64+
}
65+
generateProtoTasks {
66+
all().each { task ->
67+
task.builtins {
68+
java {
69+
outputSubDir = 'java'
70+
}
71+
}
72+
task.plugins {
73+
grpc {
74+
outputSubDir = 'java'
75+
}
76+
}
77+
}
78+
}
7779
}
7880

7981
task copyToLib(type: Copy) {
@@ -85,25 +87,36 @@ task copyToLib(type: Copy) {
8587
task copyProtos(type:Copy){
8688

8789
from ("${rootDir}/protos"){
88-
include '**/chaincodeevent.proto'
89-
include '**/chaincode.proto'
90+
include '**/chaincodeevent.proto'
91+
include '**/chaincode.proto'
9092
}
91-
from ("../") {
92-
duplicatesStrategy.EXCLUDE
93-
include '**/table.proto'
94-
exclude 'java'
95-
}
96-
into "${projectDir}/src/main/proto"
93+
from ("../") {
94+
duplicatesStrategy.EXCLUDE
95+
include '**/table.proto'
96+
exclude 'java'
97+
}
98+
into "${projectDir}/src/main/proto"
9799

98100
}
99101

100-
101102
tasks['build'].mustRunAfter tasks['copyProtos']
102103
build.dependsOn(copyProtos)
103104
build.finalizedBy(copyToLib)
105+
build.finalizedBy(publishToMavenLocal)
104106

105107
dependencies {
106-
compile 'com.google.protobuf:protobuf-java:3.0.0-beta-2'
107-
compile 'io.grpc:grpc-all:0.13.2'
108-
compile 'commons-cli:commons-cli:1.3.1'
108+
compile 'com.google.protobuf:protobuf-java:3.0.0-beta-2'
109+
compile 'io.grpc:grpc-all:0.13.2'
110+
compile 'commons-cli:commons-cli:1.3.1'
109111
}
112+
113+
publishing {
114+
publications {
115+
mavenJava(MavenPublication) {
116+
groupId 'org.hyperledger'
117+
artifactId 'shim-client'
118+
version '1.0'
119+
from components.java
120+
}
121+
}
122+
}

core/chaincode/shim/java/javabuild.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
#
1818
#
1919
set -e
20-
PARENTDIR=$(pwd)
21-
20+
PARENTDIR=$(pwd)
2221

2322
gradle -q -b ${PARENTDIR}/core/chaincode/shim/java/build.gradle clean
2423
gradle -q -b ${PARENTDIR}/core/chaincode/shim/java/build.gradle build
24+
cp -r ${PARENTDIR}/core/chaincode/shim/java/build/libs /root/

core/chaincode/shim/java/src/main/java/org/hyperledger/java/shim/Handler.java

+15-18
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
/**
2-
Licensed to the Apache Software Foundation (ASF) under one
3-
or more contributor license agreements. See the NOTICE file
4-
distributed with this work for additional information
5-
regarding copyright ownership. The ASF licenses this file
6-
to you under the Apache License, Version 2.0 (the
7-
"License"); you may not use this file except in compliance
8-
with the License. You may obtain a copy of the License at
9-
10-
http://www.apache.org/licenses/LICENSE-2.0
11-
12-
Unless required by applicable law or agreed to in writing,
13-
software distributed under the License is distributed on an
14-
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15-
KIND, either express or implied. See the License for the
16-
specific language governing permissions and limitations
17-
under the License.
18-
*/
1+
/*
2+
Copyright DTCC 2016 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
1916

2017
package org.hyperledger.java.shim;
2118

0 commit comments

Comments
 (0)