Skip to content

Commit

Permalink
Add support for Maven build in java chaincodes
Browse files Browse the repository at this point in the history
FIX FAB-158

https://jira.hyperledger.org/browse/FAB-158

Change-Id: I79aae2b4e537a20a72b2fcc1bf83a40087cfeed6
Signed-off-by: Satheesh Kathamuthu <satheesh.ceg@gmail.com>
  • Loading branch information
xspeedcruiser committed Sep 13, 2016
1 parent c6e56d6 commit 4c384c8
Show file tree
Hide file tree
Showing 22 changed files with 925 additions and 303 deletions.
4 changes: 2 additions & 2 deletions bddtests/java_shim.feature
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ Scenario: java RangeExample chaincode single peer
||
||
Then I should have received a chaincode name
Then I wait up to "30" seconds for transaction to be committed to all peers
Then I wait up to "240" seconds for transaction to be committed to all peers

When requesting "/chain" from "vp0"
Then I should get a JSON response with "height" = "2"
Expand Down Expand Up @@ -178,7 +178,7 @@ Scenario: java RangeExample chaincode single peer
When requesting "/chain" from "vp0"
Then I should get a JSON response with "height" = "1"
# TODO Needs to be replaced with an official test repo in the future.
When I deploy lang chaincode "http://github.com/xspeedcruiser/javachaincode" of "JAVA" with ctor "init" to "vp0"
When I deploy lang chaincode "http://github.com/xspeedcruiser/javachaincodemvn" of "JAVA" with ctor "init" to "vp0"
| arg1 | arg2 | arg3 | arg4 |
| a | 100 | b | 200 |
Then I should have received a chaincode name
Expand Down
2 changes: 1 addition & 1 deletion core/chaincode/chaincode_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func (chaincodeSupport *ChaincodeSupport) getArgsAndEnv(cID *pb.ChaincodeID, cLa
case pb.ChaincodeSpec_JAVA:
//TODO add security args
args = strings.Split(
fmt.Sprintf("/root/Chaincode/bin/runChaincode -a %s -i %s",
fmt.Sprintf("java -jar chaincode.jar -a %s -i %s",
chaincodeSupport.peerAddress, cID.Name),
" ")
if chaincodeSupport.peerTLS {
Expand Down
16 changes: 16 additions & 0 deletions core/chaincode/platforms/java/hash.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright DTCC 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package java

import (
Expand Down
57 changes: 51 additions & 6 deletions core/chaincode/platforms/java/package.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
/*
Copyright DTCC 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package java

import (
"archive/tar"
"fmt"
"io/ioutil"
"strings"
"time"

Expand All @@ -13,6 +30,31 @@ import (
"github.com/spf13/viper"
)

var buildCmd = map[string]string{
"build.gradle": "gradle -b build.gradle clean && gradle -b build.gradle build",
"pom.xml": "mvn -f pom.xml clean && mvn -f pom.xml package",
}

//return the type of build gradle/maven based on the file
//found in java chaincode project root
//build.gradle - gradle - returns the first found build type
//pom.xml - maven
func getBuildCmd(packagePath string) (string, error) {
files, err := ioutil.ReadDir(packagePath)
if err != nil {
return "", err
} else {
for _, f := range files {
if !f.IsDir() {
if buildCmd, ok := buildCmd[f.Name()]; ok == true {
return buildCmd, nil
}
}
}
return "", fmt.Errorf("Build file not found")
}
}

//tw is expected to have the chaincode in it from GenerateHashcode.
//This method will just package the dockerfile
func writeChaincodePackage(spec *pb.ChaincodeSpec, tw *tar.Writer) error {
Expand Down Expand Up @@ -42,22 +84,25 @@ func writeChaincodePackage(spec *pb.ChaincodeSpec, tw *tar.Writer) error {
urlLocation = urlLocation[:len(urlLocation)-1]
}

buildCmd, err := getBuildCmd(urlLocation)
if err != nil {
return err
}
var dockerFileContents string
var buf []string

if viper.GetBool("security.enabled") {
//todo
} else {
buf = append(buf, cutil.GetDockerfileFromConfig("chaincode.java.Dockerfile"))
buf = append(buf, "COPY src /root")
buf = append(buf, "RUN gradle -b build.gradle build")
buf = append(buf, "RUN unzip -od /root build/distributions/Chaincode.zip")

buf = append(buf, "COPY src /root/chaincode")
buf = append(buf, "RUN cd /root/chaincode && "+buildCmd)
buf = append(buf, "RUN cp /root/chaincode/build/chaincode.jar /root")
buf = append(buf, "RUN cp /root/chaincode/build/libs/* /root/libs")
}
dockerFileContents = strings.Join(buf, "\n")

dockerFileContents = strings.Join(buf, "\n")
dockerFileSize := int64(len([]byte(dockerFileContents)))

//Make headers identical by using zero time
var zeroTime time.Time
tw.WriteHeader(&tar.Header{Name: "Dockerfile", Size: dockerFileSize, ModTime: zeroTime, AccessTime: zeroTime, ChangeTime: zeroTime})
Expand Down
16 changes: 16 additions & 0 deletions core/chaincode/platforms/java/platform.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright DTCC 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package java

import (
Expand Down
125 changes: 69 additions & 56 deletions core/chaincode/shim/java/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,65 +15,67 @@ limitations under the License.
*/

buildscript {
repositories {
mavenLocal()
mavenCentral()
jcenter()
}
dependencies {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.7.6'
}
repositories {
mavenLocal()
mavenCentral()
jcenter()
}
dependencies {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.7.6'
}
}

plugins {
id "java"
id "com.google.protobuf" version "0.7.6"
id "eclipse"
id "java"
id "com.google.protobuf" version "0.7.6"
id "eclipse"
id "maven-publish"

}
archivesBaseName = 'chaincode'
archivesBaseName = 'shim-client'
version = '1.0'


sourceSets {
main {
java {
srcDir 'src/main/java'
}
proto {
srcDir 'src/main/proto'
}
}
main {
java {
srcDir 'src/main/java'
}
proto {
srcDir 'src/main/proto'
}
}
}

repositories {
mavenLocal()
mavenLocal()
mavenCentral()
}

protobuf {
generatedFilesBaseDir = "$projectDir/src"
protoc {
artifact = 'com.google.protobuf:protoc:3.0.0-beta-2'
}
plugins {
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:0.13.2'
}
}
generateProtoTasks {
all().each { task ->
task.builtins {
java {
outputSubDir = 'java'
}
}
task.plugins {
grpc {
outputSubDir = 'java'
}
}
}
}
generatedFilesBaseDir = "$projectDir/src"
protoc {
artifact = 'com.google.protobuf:protoc:3.0.0-beta-2'
}
plugins {
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:0.13.2'
}
}
generateProtoTasks {
all().each { task ->
task.builtins {
java {
outputSubDir = 'java'
}
}
task.plugins {
grpc {
outputSubDir = 'java'
}
}
}
}
}

task copyToLib(type: Copy) {
Expand All @@ -85,25 +87,36 @@ task copyToLib(type: Copy) {
task copyProtos(type:Copy){

from ("${rootDir}/protos"){
include '**/chaincodeevent.proto'
include '**/chaincode.proto'
include '**/chaincodeevent.proto'
include '**/chaincode.proto'
}
from ("../") {
duplicatesStrategy.EXCLUDE
include '**/table.proto'
exclude 'java'
}
into "${projectDir}/src/main/proto"
from ("../") {
duplicatesStrategy.EXCLUDE
include '**/table.proto'
exclude 'java'
}
into "${projectDir}/src/main/proto"

}


tasks['build'].mustRunAfter tasks['copyProtos']
build.dependsOn(copyProtos)
build.finalizedBy(copyToLib)
build.finalizedBy(publishToMavenLocal)

dependencies {
compile 'com.google.protobuf:protobuf-java:3.0.0-beta-2'
compile 'io.grpc:grpc-all:0.13.2'
compile 'commons-cli:commons-cli:1.3.1'
compile 'com.google.protobuf:protobuf-java:3.0.0-beta-2'
compile 'io.grpc:grpc-all:0.13.2'
compile 'commons-cli:commons-cli:1.3.1'
}

publishing {
publications {
mavenJava(MavenPublication) {
groupId 'org.hyperledger'
artifactId 'shim-client'
version '1.0'
from components.java
}
}
}
4 changes: 2 additions & 2 deletions core/chaincode/shim/java/javabuild.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
#
#
set -e
PARENTDIR=$(pwd)

PARENTDIR=$(pwd)

gradle -q -b ${PARENTDIR}/core/chaincode/shim/java/build.gradle clean
gradle -q -b ${PARENTDIR}/core/chaincode/shim/java/build.gradle build
cp -r ${PARENTDIR}/core/chaincode/shim/java/build/libs /root/
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
/**
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
/*
Copyright DTCC 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package org.hyperledger.java.shim;

Expand Down

0 comments on commit 4c384c8

Please sign in to comment.