Commit 4c384c87 authored by Satheesh Kathamuthu's avatar Satheesh Kathamuthu
Browse files

Add support for Maven build in java chaincodes

FIX FAB-158

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



Change-Id: I79aae2b4e537a20a72b2fcc1bf83a40087cfeed6
Signed-off-by: default avatarSatheesh Kathamuthu <satheesh.ceg@gmail.com>
parent c6e56d68
......@@ -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"
......@@ -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
......
......@@ -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 {
......
/*
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 (
......
/*
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"
......@@ -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 {
......@@ -42,6 +84,10 @@ 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
......@@ -49,15 +95,14 @@ func writeChaincodePackage(spec *pb.ChaincodeSpec, tw *tar.Writer) error {
//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})
......
/*
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 (
......
......@@ -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) {
......@@ -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
}
}
}
\ No newline at end of file
......@@ -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/
/**
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;
......
......@@ -16,18 +16,17 @@ limitations under the License.
buildscript {
repositories {
mavenLocal()
mavenCentral()
jcenter()
}
repositories {
mavenLocal()
mavenCentral()
jcenter()
}
}
plugins {
id "java"
id "eclipse"
id "application"
id "java"
id "eclipse"
id "application"
}
......@@ -37,21 +36,21 @@ task printClasspath {
}
}
archivesBaseName = "java-example"
archivesBaseName = "chaincode"
mainClassName="example.Example"
run {
if (project.hasProperty("appArgs")) {
args = Eval.me(appArgs)
}
if (project.hasProperty("appArgs")) {
args = Eval.me(appArgs)
}
}
sourceSets {
main {
java {
srcDir 'src/main/java'
}
}
main {
java {
srcDir 'src/main/java'
}
}
}
repositories {
......@@ -60,19 +59,25 @@ repositories {
}
distZip {
archiveName='Chaincode.zip'
duplicatesStrategy='exclude'
jar.doFirst {
destinationDir=file("${buildDir}")
manifest {
attributes (
'Main-Class': mainClassName,
'Class-Path': configurations.runtime.collect { "libs/"+"$it.name" }.join(' ')
)
}
}
startScripts{
applicationName='runChaincode'
task copyToLib(type: Copy) {
into "$buildDir/libs"
from configurations.runtime
}
build.finalizedBy(copyToLib)
dependencies {
compile 'io.grpc:grpc-all:0.13.2'
compile 'commons-cli:commons-cli:1.3.1'
compile project(':core:chaincode:shim:java')
compile 'io.grpc:grpc-all:0.13.2'
compile 'commons-cli:commons-cli:1.3.1'
compile 'org.hyperledger:shim-client:1.0'
}
<!--
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<modelVersion>4.0.0</modelVersion>
<groupId>example</groupId>
<artifactId>chaincode-example</artifactId>
<packaging>jar</packaging>
<name>java-example</name>
<version>1.0</version>
<url>http://maven.apache.org</url>
<build>
<directory>${project.basedir}/build</directory>
<finalName>chaincode</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<outputDirectory>${dist.dir}</outputDirectory>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>libs/</classpathPrefix>
<mainClass>example.Example</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/libs</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-all</artifactId>
<version>0.13.2</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.hyperledger</groupId>
<artifactId>shim-client</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>
......@@ -16,18 +16,17 @@ limitations under the License.
buildscript {
repositories {
mavenLocal()
mavenCentral()
jcenter()
}
repositories {
mavenLocal()
mavenCentral()
jcenter()
}
}
plugins {
id "java"
id "eclipse"
id "application"
id "java"
id "eclipse"
id "application"
}
......@@ -37,21 +36,21 @@ task printClasspath {
}
}
archivesBaseName = "java-linkexample"
mainClassName="example.LinkExample"
archivesBaseName = "chaincode"
mainClassName="example.Link"
run {
if (project.hasProperty("appArgs")) {
args = Eval.me(appArgs)
}
if (project.hasProperty("appArgs")) {
args = Eval.me(appArgs)
}
}
sourceSets {
main {
java {
srcDir 'src/main/java'
}
}
main {
java {
srcDir 'src/main/java'
}
}
}