How to use the smart contract compilation plugin (solc-gradle-plugin)

The smart contract compilation plug-in can greatly simplify the process from solidity source code to java code, helping developers quickly compile and call contracts.
This article records how to compile versions 0.4.25 and 0.8.11.
1 Create a gradle project
Create a gradle project directly with IDEA:
The created build.gradle configuration file is as follows:
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}

test {
    useJUnitPlatform()
}
2 Introduce the contract compilation plug-in
The modified build.gradle is as follows, the yellow marked part is the newly added content.
The pkg parameter specifies the path of the compiled java code package, and you can modify the definition yourself.
buildscript {
    repositories {
        mavenCentral()
        maven { url "https://maven.aliyun.com/nexus/content/groups/public/"}
        maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
        mavenLocal()
    }
    dependencies {
        classpath 'com.webank:solc-gradle-plugin:3.0.1-SNAPSHOT'
        //classpath 'com.webank:solc-gradle-plugin:1.0.2-SNAPSHOT'
        //默认编译0.8.11.0版本,如果想编译0.4.25版本,请添加下述依赖
        //classpath 'org.fisco-bcos:solcJ:0.4.25.0'
        //classpath 'com.webank:solc-gradle-plugin:1.0.1'
        //默认编译0.8.11.0版本,如果想编译0.6.10.0版本,请添加下述依赖
        //classpath 'org.fisco-bcos:solcJ:0.6.10.0'
        //默认编译0.8.11.0版本,如果想编译0.5.2.0版本,请添加下述依赖
        //classpath 'org.fisco-bcos:solcJ:0.5.2.0'
    }
}

plugins {
    id 'java'
}

apply plugin: 'solc-gradle-plugin'

solc{
    pkg = 'org.example.contracts'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}

test {
    useJUnitPlatform()
}
Release notes:
Introduce version 0.8.11: Only one line is required, and the default is 0.8.11.  
classpath 'com.webank:solc-gradle-plugin:3.0.1-SNAPSHOT'
[Note] I tested that this line of statement can no longer compile other low-version contracts.
The version of classpath 'com.webank:solc-gradle-plugin:1.0.2-SNAPSHOT'  has a compilation error, and the contract function cannot return the structure type.
Introduce 0.4.25, this line is required, the default is 0.4.25.   
classpath 'com.webank:solc-gradle-plugin:1.0.1'
This sentence on the document is invalid classpath 'org.fisco-bcos:solcJ:0.4.25.0', it will report an error that solc-gradle-plugin is missing.
To introduce version 0.5.2/0.6.10, two lines are required,
classpath 'com.webank:solc-gradle-plugin:1.0.1'
classpath 'org.fisco-bcos:solcJ:0.5.2.0'
3 Add contract source code
The smart contract source code is copied to /src/main/contracts/, and /src/main/abi and /src/main/bin directories are automatically created after successful compilation.
Open the terminal and enter the compilation command:
PS E:\codes\solc-example> .\gradlew.bat solc

> Task :solc
Entering solc task, pid 12344@User-20220909JV
Solidity contracts compile complete


BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed
PS E:\codes\solc-example>
4 Specify the running parameters of the plug-in
The solc-gradle-plugin plug-in generates a solc task after parsing, and automatically invokes the specified task parameters in the configuration file build.gradle when the terminal executes the solc task. The parameters for solc are described as follows:
configuration item
required
illustrate
pkg
If onlyAbiBin is specified as false,
must be set. Otherwise no need to set
java contract package name
contracts
no
Smart contract file path, the default is src/main/contracts
output
no
Compile output path, the default is src/main
onlyAbiBin
no
Whether to only output abi and bin default false
selector
no
Default is empty, select all contracts;
If you choose to specify the contract, you can fill in the required contract file name, separated by commas, such as A.sol, B.sol
solc{
    pkg = 'org.example.contract'
    contracts='src/main/contracts'
    selector ='Users'
    output = 'src/main'
}
Each time the task is run ( .\gradlew.bat solc ), the above parameters are automatically loaded and executed.
Therefore, if we only modify part of the contract and only generate part of the contract file, we need to manually modify the above configuration parameters and run it again under the terminal.

Guess you like

Origin blog.csdn.net/u012084827/article/details/130445102