• Stars
    star
    112
  • Rank 312,240 (Top 7 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created over 7 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

web3j Maven plugin

web3j-maven-plugin

build status codecov License

web3j maven plugin is used to create java classes based on the solidity contract files.

Usage

The base configuration for the plugin will take the solidity files from src/main/resources and generates the java classes into the folder src/main/java.

<build>
    <plugins>
        <plugin>
            <groupId>org.web3j</groupId>
            <artifactId>web3j-maven-plugin</artifactId>
            <version>4.10.0</version>
            <configuration>
                <soliditySourceFiles/>
            </configuration>
        </plugin>
    </plugins>
</build>

to run the plugin execute the goal generate-sources

mvn web3j:generate-sources

Configuration

The are several variable to select the solidity source files, define a source destination path or change the package name.

Name Format Default value
<packageName/> valid java package name org.web3j.model
<outputDirectory><java/></outputDirectory> relative or absolute path of the generated for 'Java files value in <sourceDestination/>
<outputDirectory><bin/></outputDirectory> relative or absolute path of the generated for 'Bin' files value in <sourceDestination/>
<outputDirectory><abi/></outputDirectory> relative or absolute path of the generated for 'ABI' files value in <sourceDestination/>
<sourceDestination/> relative or absolute path of the generated files (java, bin, abi) src/main/java
<outputFormat/> generate Java Classes(java), ABI(abi) and/or BIN (bin) Files (comma separated) java
<nativeJavaType/> Creates Java Native Types (instead of Solidity Types) true
<outputJavaParentContractClassName/> Sets custom(? extends org.web3j.tx.Contract) class as a parent for java generated code org.web3j.tx.Contract
<soliditySourceFiles> Standard maven fileset <soliditySourceFiles>
<directory>src/main/resources</directory>
<includes>
<include>**/*.sol</include>
</includes>
</soliditySourceFiles>
<abiSourceFiles> Standard maven fileset <abiSourceFiles>
<directory>src/main/resources</directory>
<includes>
<include>**/*.json</include>
</includes>
</abiSourceFiles>
<contract> Filter (<include> or <exclude>) contracts based on the name. <contract>
<includes>
<include>greeter</include>
</includes>
<excludes>
<exclude>mortal</exclude>
<excludes>
</contracts>
<pathPrefixes> A list (<pathPrefixe>) of replacements of dependency replacements inside Solidity contract.

Configuration of outputDirectory has priority over sourceDestination

Getting Started

Create a standard java maven project. Add following <plugin> - configuration into the pom.xml file:

<plugin>
    <groupId>org.web3j</groupId>
    <artifactId>web3j-maven-plugin</artifactId>
    <version>4.8.1</version>
    <configuration>
        <packageName>com.zuehlke.blockchain.model</packageName>
        <sourceDestination>src/main/java/generated</sourceDestination>
        <nativeJavaType>true</nativeJavaType>
        <outputFormat>java,bin</outputFormat>
        <soliditySourceFiles>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.sol</include>
            </includes>
        </soliditySourceFiles>
        <abiSourceFiles>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.json</include>
            </includes>
        </abiSourceFiles>
        <outputDirectory>
            <java>src/java/generated</java>
            <bin>src/bin/generated</bin>
            <abi>src/abi/generated</abi>
        </outputDirectory>
        <contract>
            <includes>
                <include>greeter</include>
            </includes>
            <excludes>
                <exclude>mortal</exclude>
            </excludes>
        </contract>
        <pathPrefixes>
            <pathPrefix>dep=../dependencies</pathPrefix>
        </pathPrefixes>
    </configuration>
</plugin>

Add your solidity contract files into the folder src/main/resources. Make sure that the solidity files ends with .sol.

Start the generating process:

> mvn web3j:generate-sources

[INFO] --- web3j-maven-plugin:0.1.2:generate-sources (default-cli) @ hotel-showcase ---
[INFO] process 'HotelShowCaseProxy.sol'
[INFO] 	Built Class for contract 'HotelShowCaseProxy'
[INFO] 	Built Class for contract 'HotelShowCaseV2'
[INFO] 	Built Class for contract 'Owned'
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.681 s
[INFO] Finished at: 2017-06-13T07:07:04+02:00
[INFO] Final Memory: 14M/187M
[INFO] ------------------------------------------------------------------------

Process finished with exit code 0

You find the generated java classes inside the directory src/main/java/generated/.

Next step is to interact with the smart contract. See for that deploying and interacting with smart contracts in the official web3j documentation.

For a multi module project configuration see following post from @fcorneli. In short: For pick up the generated java source files, you need the build-helper-maven-plugin configuration. Also, ${basedir} prefix is required within a multi-module project.

Changelog

4.8.1

  • Update web3j core version to 4.8.1

4.6.5

  • Replace SolC with Sokt (thx to @josh-richardson & @AlexandrouR)

4.5.11

  • Update web3j core version to 4.5.11
  • Update solcj version to 0.5.7

Changelog

4.2.0

  • Update web3j core version to 4.2.0

4.1.0

  • Update web3j core version to 4.1.0
  • Revert Java Update

4.0.3

  • Bringing all web3j-related projects up to the same build number to reduce confusion

0.3.7

  • Update solcj version to 0.4.25
  • Update web3j core version to 3.5.0
  • Destination Directory for ABI, BIN and Java can be different

0.3.5

  • Generate and Store ABI & BIN Files
  • Update to 3.5.0 web3j core version

0.3.1

  • Update to newest solcj version. Support for Solidity Version 0.4.24
  • Update to 3.4.0 web3j core version

0.3.0

  • Support of imported Files import './other.sol';

0.2.0

  • Update Core Version

0.1.4

  • Update Core Version

0.1.3

  • Update Core Version
  • Support Java Native Type creation

0.1.2

  • Better Contract Handling

0.1.1

  • Initial Release

More Repositories

1

web3j

Lightweight Java and Android library for integration with Ethereum clients
Java
4,890
star
2

sample-project-gradle

Sample web3j project using Gradle
Java
269
star
3

web3j-spring-boot-starter

Spring Boot Starter for web3j
Java
227
star
4

web3j-quorum

web3j integration layer for JP Morgan's Quorum
Java
111
star
5

libp2p

libp2p implementation in Kotlin for JVM platforms
Kotlin
78
star
6

svm

Solidity Version Manager. Like nvm or jabba, but for Solidity. Cross platform.
Kotlin
49
star
7

examples

Example code supporting articles
Java
48
star
8

web3j-gradle-plugin

web3j Gradle plugin
Java
38
star
9

sample-project-maven

Sample web3j Maven project
Java
34
star
10

web3j-openapi

OpenAPI compliant service generator for Solidity Smart contracts
Solidity
33
star
11

Intellij-solidity-debugger

Kotlin
27
star
12

solidity-gradle-plugin

Gradle plugin providing tasks to compile Solidity contracts.
Solidity
21
star
13

web3j-corda

Lightweight JVM library for integrating with Corda
Kotlin
14
star
14

web3j-sokt

Kotlin-based wrapper for multiple installations of the solidity compiler (solc)
Kotlin
12
star
15

web3j-eth2

Web3j integrations for Ethereum 2.0 (Eth2) network
Kotlin
10
star
16

web3j-evmexample

Minimal example for getting started with web3j-evm
Java
9
star
17

web3j.github.io

web3j web site
JavaScript
6
star
18

web3j-aion

web3j integration with the Aion network
Kotlin
5
star
19

web3j-unitexample

Minimal example for getting started with web3j-unit
Java
4
star
20

web3j-openapi-gradle-plugin

Kotlin
4
star
21

web3j-workshop-2019

JAX London 2019 Web3j Workshop
Java
4
star
22

ion

Java bindings for the Ion Interoperability Framework
Solidity
4
star
23

web3j-deployer-demo

Demo usage of web3j-deployer
Java
3
star
24

web3j-deployer

Library to define, configure and run Ethereum contract deployments and transactions
Kotlin
3
star
25

web3j-deployer-plugin

Gradle plugin for web3j-deployer
Kotlin
2
star
26

fabric

Kotlin
2
star
27

solidity-darwin-binaries

Static binaries of the Solidity compiler for macOS
2
star
28

web3j-unit-docker-compose-example

Example to spin up VMWare Concord using docker-compose to test contracts in web3j-unit
Java
2
star
29

homebrew-web3j

web3j homebrew tap
Ruby
2
star
30

aion-samples

web3j Aion integration samples
Java
1
star
31

epirus-gradle-plugin

Gradle Plugin for the Epirus Platform.
Kotlin
1
star
32

.github

Share workflow templates, secrets, and self-hosted runners.
1
star
33

quorum-spring-boot-starter

A Spring Boot starter for working with Quorum
1
star