2018-08-17 15:52:56 +00:00
Building and installing a CorDapp
=================================
2017-07-25 16:54:36 +00:00
2017-11-16 15:31:52 +00:00
.. contents ::
2017-07-25 16:54:36 +00:00
2017-11-16 15:31:52 +00:00
Cordapps run on the Corda platform and integrate with it and each other. This article explains how to build CorDapps.
To learn what a CorDapp is, please read :doc: `cordapp-overview` .
2017-07-25 16:54:36 +00:00
2017-11-16 15:31:52 +00:00
CorDapp format
--------------
A CorDapp is a semi-fat JAR that contains all of the CorDapp's dependencies *except* the Corda core libraries and any
other CorDapps it depends on.
2017-07-25 16:54:36 +00:00
2017-11-16 15:31:52 +00:00
For example, if a Cordapp depends on `` corda-core `` , `` your-other-cordapp `` and `` apache-commons `` , then the Cordapp
JAR will contain:
2017-07-25 16:54:36 +00:00
2017-11-16 15:31:52 +00:00
* All classes and resources from the `` apache-commons `` JAR and its dependencies
* *Nothing* from the other two JARs
2017-07-25 16:54:36 +00:00
2017-11-16 15:31:52 +00:00
Build tools
-----------
2018-08-01 16:55:00 +00:00
In the instructions that follow, we assume you are using Gradle and the `` cordformation `` plugin to build your
CorDapp. You can find examples of building a CorDapp using these tools in the
`Kotlin CorDapp Template <https://github.com/corda/cordapp-template-kotlin> `_ and the
`Java CorDapp Template <https://github.com/corda/cordapp-template-kotlin> `_ .
To ensure you are using the correct version of Gradle, you should use the provided Gradle Wrapper by copying across
the following folder and files from the `Kotlin CorDapp Template <https://github.com/corda/cordapp-template-kotlin> `_ or the
`Java CorDapp Template <https://github.com/corda/cordapp-template-kotlin> `_ to the root of your project:
* `` gradle/ ``
* `` gradlew ``
* `` gradlew.bat ``
2017-07-25 16:54:36 +00:00
2017-11-16 15:31:52 +00:00
Setting your dependencies
-------------------------
2017-07-25 16:54:36 +00:00
2018-02-27 12:48:08 +00:00
Choosing your Corda, Quasar and Kotlin versions
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Several `` ext `` variables are used in a CorDapp's `` build.gradle `` file to define which versions are used to build your CorDapp:
2017-12-21 11:22:32 +00:00
2018-02-27 12:48:08 +00:00
* `` ext.corda_release_version `` defines the version of Corda
* `` ext.corda_gradle_plugins_version `` defines the version of the Corda Gradle Plugins
* `` ext.quasar_version `` defines the version of Quasar
* `` ext.kotlin_version `` defines the version of Kotlin (if using Kotlin to write your CorDapp)
2017-07-25 16:54:36 +00:00
2018-02-27 12:48:08 +00:00
`` corda_gradle_plugins_versions `` are given in the form `` major.minor.patch `` . You should use the same `` major `` and
`` minor `` versions as the Corda version you are using, and the latest `` patch `` version. A list of all the available
versions can be found here: https://bintray.com/r3/corda/cordapp. If in doubt, you should base yourself on the version numbers used in the `` build.gradle `` file of the `Kotlin CorDapp Template <https://github.com/corda/cordapp-template-kotlin> `_ and the `Java CorDapp Template <https://github.com/corda/cordapp-template-kotlin> `_ .
2017-07-25 16:54:36 +00:00
2018-02-27 12:48:08 +00:00
For example, to use version 3.0 of Corda, version 3.0.8 of the Corda gradle plugins, version 0.7.9 of Quasar, and version 1.1.60 of Kotlin, you'd write:
2017-11-16 15:31:52 +00:00
2018-02-27 12:48:08 +00:00
.. sourcecode :: groovy
2017-07-25 16:54:36 +00:00
2018-02-27 12:48:08 +00:00
ext.corda_release_version = 'corda-3.0'
ext.corda_gradle_plugins_version = '3.0.8'
ext.quasar_version = '0.7.9'
ext.kotlin_version = '1.1.60'
2017-07-25 16:54:36 +00:00
In certain cases, you may also wish to build against the unstable Master branch. See :doc: `building-against-master` .
2017-11-16 15:31:52 +00:00
Corda dependencies
^^^^^^^^^^^^^^^^^^
2017-12-21 11:22:32 +00:00
The `` cordformation `` plugin adds two new gradle configurations:
2017-11-16 15:31:52 +00:00
2017-12-21 11:22:32 +00:00
* `` cordaCompile `` , which extends `` compile ``
* `` cordaRuntime `` , which extends `` runtime ``
2017-11-16 15:31:52 +00:00
2018-01-29 12:42:31 +00:00
`` cordaCompile `` and `` cordaRuntime `` indicate dependencies that should not be included in the CorDapp JAR. These
configurations should be used for any Corda dependency (e.g. `` corda-core `` , `` corda-node `` ) in order to prevent a
dependency from being included twice (once in the CorDapp JAR and once in the Corda JARs).
2017-12-21 11:22:32 +00:00
To build against Corda, you must add the following to your `` build.gradle `` file:
2017-07-25 16:54:36 +00:00
2017-12-21 11:22:32 +00:00
* `` net.corda:corda:$corda_release_version `` as a `` cordaRuntime `` dependency
* Each Corda compile dependency (eg `` net.corda:corda-core:$corda_release_version `` ) as a `` cordaCompile `` dependency
2017-07-25 16:54:36 +00:00
2017-12-21 11:22:32 +00:00
You may also want to add:
* `` net.corda:corda-test-utils:$corda_release_version `` as a `` testCompile `` dependency, in order to use Corda's test
frameworks
* `` net.corda:corda-webserver:$corda_release_version `` as a `` cordaRuntime `` dependency, in order to use Corda's
built-in development webserver
2017-07-25 16:54:36 +00:00
2017-11-16 15:31:52 +00:00
.. warning :: Never include `` corda-test-utils `` as a `` compile `` or `` cordaCompile `` dependency.
Dependencies on other CorDapps
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2017-12-21 11:22:32 +00:00
You CorDapp may also depend on classes defined in another CorDapp, such as states, contracts and flows. There are two
ways to add another CorDapp as a dependency in your CorDapp's `` build.gradle `` file:
* `` cordapp project(":another-cordapp") `` (use this if the other CorDapp is defined in a module in the same project)
* `` cordapp "net.corda:another-cordapp:1.0" `` (use this otherwise)
2017-11-16 15:31:52 +00:00
2018-01-29 12:42:31 +00:00
The `` cordapp `` gradle configuration serves two purposes:
* When using the `` cordformation `` Gradle plugin, the `` cordapp `` configuration indicates that this JAR should be
included on your node as a CorDapp
* When using the `` cordapp `` Gradle plugin, the `` cordapp `` configuration prevents the dependency from being included
in the CorDapp JAR
Note that the `` cordformation `` and `` cordapp `` Gradle plugins can be used together.
2017-11-16 15:31:52 +00:00
Other dependencies
^^^^^^^^^^^^^^^^^^
2017-07-25 16:54:36 +00:00
If your CorDapps have any additional external dependencies, they can be specified like normal Kotlin/Java dependencies
in Gradle. See the example below, specifically the `` apache-commons `` include.
For further information about managing dependencies, see
`the Gradle docs <https://docs.gradle.org/current/userguide/dependency_management.html> `_ .
Example
2017-11-16 15:31:52 +00:00
^^^^^^^
2018-08-01 16:55:00 +00:00
Below is a sample of what a CorDapp's Gradle dependencies block might look like. When building your own CorDapp, you should
base yourself on the `` build.gradle `` file of the
`Kotlin CorDapp Template <https://github.com/corda/cordapp-template-kotlin> `_ or the
`Java CorDapp Template <https://github.com/corda/cordapp-template-kotlin> `_ .
2017-07-25 16:54:36 +00:00
.. container :: codeset
.. sourcecode :: groovy
dependencies {
// Corda integration dependencies
2017-07-27 16:53:51 +00:00
cordaCompile "net.corda:corda-core:$corda_release_version"
cordaCompile "net.corda:corda-finance:$corda_release_version"
cordaCompile "net.corda:corda-jackson:$corda_release_version"
cordaCompile "net.corda:corda-rpc:$corda_release_version"
cordaCompile "net.corda:corda-node-api:$corda_release_version"
cordaCompile "net.corda:corda-webserver-impl:$corda_release_version"
2017-07-25 16:54:36 +00:00
cordaRuntime "net.corda:corda:$corda_release_version"
cordaRuntime "net.corda:corda-webserver:$corda_release_version"
testCompile "net.corda:corda-test-utils:$corda_release_version"
// Corda Plugins: dependent flows and services
2017-11-16 15:31:52 +00:00
// Identifying a CorDapp by its module in the same project.
cordapp project(":cordapp-contracts-states")
// Identifying a CorDapp by its fully-qualified name.
2017-07-25 16:54:36 +00:00
cordapp "net.corda:bank-of-corda-demo:1.0"
// Some other dependencies
compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
testCompile "org.jetbrains.kotlin:kotlin-test:$kotlin_version"
testCompile "junit:junit:$junit_version"
compile "org.apache.commons:commons-lang3:3.6"
}
2017-11-16 15:31:52 +00:00
Creating the CorDapp JAR
------------------------
2018-08-01 16:55:00 +00:00
Once your dependencies are set correctly, you can build your CorDapp JAR(s) using the Gradle `` jar `` task
2017-12-07 16:39:52 +00:00
* Unix/Mac OSX: `` ./gradlew jar ``
* Windows: `` gradlew.bat jar ``
2018-08-01 16:55:00 +00:00
Each of the project's modules will be compiled into its own CorDapp JAR. You can find these CorDapp JARs in the `` build/libs ``
folders of each of the project's modules.
2017-11-16 15:31:52 +00:00
2017-11-29 17:07:13 +00:00
.. warning :: The hash of the generated CorDapp JAR is not deterministic, as it depends on variables such as the
timestamp at creation. Nodes running the same CorDapp must therefore ensure they are using the exact same CorDapp
2017-12-07 16:39:52 +00:00
JAR, and not different versions of the JAR created from identical sources.
2017-11-16 15:31:52 +00:00
The filename of the JAR must include a unique identifier to deduplicate it from other releases of the same CorDapp.
This is typically done by appending the version string to the CorDapp's name. This unique identifier should not change
once the JAR has been deployed on a node. If it does, make sure no one is relying on `` FlowContext.appName `` in their
flows (see :doc: `versioning` ).
2017-12-07 16:39:52 +00:00
Installing the CorDapp JAR
2017-11-16 15:31:52 +00:00
--------------------------
.. note :: Before installing a CorDapp, you must create one or more nodes to install it on. For instructions, please see
2017-11-29 17:07:13 +00:00
:doc: `generating-a-node` .
2017-11-16 15:31:52 +00:00
2018-08-01 16:55:00 +00:00
At start-up, nodes will load any CorDapps present in their `` cordapps `` folder. In order to install a CorDapp on a node, the
CorDapp JAR must be added to the `` <node_dir>/cordapps/ `` folder (where `` node_dir `` is the folder in which the node's JAR
and configuration files are stored) and the node restarted.
2018-02-14 14:49:59 +00:00
CorDapp configuration files
---------------------------
CorDapp configuration files should be placed in `` <node_dir>/cordapps/config `` . The name of the file should match the
name of the JAR of the CorDapp (eg; if your CorDapp is called `` hello-0.1.jar `` the config should be `` config/hello-0.1.conf `` ).
Config files are currently only available in the `Typesafe/Lightbend <https://github.com/lightbend/config> `_ config format.
These files are loaded when a CorDapp context is created and so can change during runtime.
CorDapp configuration can be accessed from `` CordappContext::config `` whenever a `` CordappContext `` is available.
2018-08-16 09:01:26 +00:00
There is an example project that demonstrates in `` samples `` called `` cordapp-configuration `` and API documentation in
`<api/kotlin/corda/net.corda.core.cordapp/index.html>`_ .
2018-09-28 08:46:06 +00:00
Minimum and target platform version
-----------------------------------
CorDapps can advertise their minimum and target platform version. The minimum platform version indicates that a node has to run at least this version in order to be able to run this CorDapp. The target platform version indicates that a CorDapp was tested with this version of the Corda Platform and should be run at this API level if possible. It provides a means of maintaining behavioural compatibility for the cases where the platform's behaviour has changed. These attributes are specified in the JAR manifest of the CorDapp, for example:
.. sourcecode :: groovy
2018-10-15 20:11:52 +00:00
'Min-Platform-Version': 4
2018-09-28 08:46:06 +00:00
'Target-Platform-Version': 4
2018-10-15 20:11:52 +00:00
Using the `cordapp` Gradle plugin, this can be achieved by putting this in your CorDapp's `build.gradle` :
2018-09-28 08:46:06 +00:00
2018-10-15 20:11:52 +00:00
.. container :: codeset
.. sourcecode :: groovy
cordapp {
info {
targetPlatformVersion 4
minimumPlatformVersion 4
}
}
Without using the `cordapp` plugin, you can achieve the same by modifying the jar task as shown in this example:
2018-09-28 08:46:06 +00:00
.. container :: codeset
.. sourcecode :: groovy
jar {
manifest {
attributes(
2018-10-15 20:11:52 +00:00
'Min-Platform-Version': 4
2018-09-28 08:46:06 +00:00
'Target-Platform-Version': 4
)
}
}