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
+
1
17
package java
2
18
3
19
import (
4
20
"archive/tar"
5
21
"fmt"
22
+ "io/ioutil"
6
23
"strings"
7
24
"time"
8
25
@@ -13,6 +30,31 @@ import (
13
30
"github.com/spf13/viper"
14
31
)
15
32
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
+
16
58
//tw is expected to have the chaincode in it from GenerateHashcode.
17
59
//This method will just package the dockerfile
18
60
func writeChaincodePackage (spec * pb.ChaincodeSpec , tw * tar.Writer ) error {
@@ -42,22 +84,25 @@ func writeChaincodePackage(spec *pb.ChaincodeSpec, tw *tar.Writer) error {
42
84
urlLocation = urlLocation [:len (urlLocation )- 1 ]
43
85
}
44
86
87
+ buildCmd , err := getBuildCmd (urlLocation )
88
+ if err != nil {
89
+ return err
90
+ }
45
91
var dockerFileContents string
46
92
var buf []string
47
93
48
94
if viper .GetBool ("security.enabled" ) {
49
95
//todo
50
96
} else {
51
97
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" )
56
102
}
57
- dockerFileContents = strings .Join (buf , "\n " )
58
103
104
+ dockerFileContents = strings .Join (buf , "\n " )
59
105
dockerFileSize := int64 (len ([]byte (dockerFileContents )))
60
-
61
106
//Make headers identical by using zero time
62
107
var zeroTime time.Time
63
108
tw .WriteHeader (& tar.Header {Name : "Dockerfile" , Size : dockerFileSize , ModTime : zeroTime , AccessTime : zeroTime , ChangeTime : zeroTime })
0 commit comments