Skip to content

Commit

Permalink
Convert to multi-module project (#267)
Browse files Browse the repository at this point in the history
* Extract 'libp2p' module
* Sort out common and libp2p gradle dependencies
* Add example as modules (except Android)
* Publish artifacts only for modules declaring 'mavenArtifactId' property
* Extract versions to a dedicated file
* Move gradle JMH stuff to the libp2p module unless anyone wants to add JMH to other modules

Co-authored-by: Salvatore Testa <[email protected]>
  • Loading branch information
Nashatyrev and SalvatoreT authored Feb 17, 2023
1 parent c8b62b5 commit 89ba1c2
Show file tree
Hide file tree
Showing 314 changed files with 222 additions and 198 deletions.
241 changes: 108 additions & 133 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,180 +5,155 @@ import java.net.URL
// To publish the release artifact to CloudSmith repo run the following :
// ./gradlew publish -PcloudsmithUser=<user> -PcloudsmithApiKey=<api-key>

group = "io.libp2p"
version = "develop"
description = "a minimal implementation of libp2p for the jvm"

plugins {
kotlin("jvm").version("1.6.21")

id("com.github.ben-manes.versions").version("0.44.0")
id("com.google.protobuf").version("0.9.2")
id("idea")
id("io.gitlab.arturbosch.detekt").version("1.22.0")
id("java")
id("maven-publish")
id("org.jetbrains.dokka").version("1.7.20")
id("org.jmailen.kotlinter").version("3.10.0")
id("java-test-fixtures")
id("me.champeau.jmh").version("0.6.8")
id("io.spring.dependency-management").version("1.1.0")
}

repositories {
mavenCentral()
maven("https://artifacts.consensys.net/public/maven/maven/")
}

val guavaVersion = "31.1-jre"
val bouncyCastleVersion = "1.70"
val log4j2Version = "2.19.0"
val junitVersion = "5.9.2"
val mockitoVersion = "5.1.1"
val jmhVersion = "1.36"

dependencies {
api("io.netty:netty-all:4.1.87.Final")
api("com.google.protobuf:protobuf-java:3.21.12")

implementation(kotlin("stdlib-jdk8"))
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
implementation("tech.pegasys:noise-java:22.1.0")

implementation("com.google.guava:guava:$guavaVersion")
implementation("org.bouncycastle:bcprov-jdk15on:$bouncyCastleVersion")
implementation("org.bouncycastle:bcpkix-jdk15on:$bouncyCastleVersion")
implementation("commons-codec:commons-codec:1.15")

implementation("org.apache.logging.log4j:log4j-api:$log4j2Version")
implementation("org.apache.logging.log4j:log4j-core:$log4j2Version")
implementation("javax.xml.bind:jaxb-api:2.3.1")

testFixturesImplementation("org.apache.logging.log4j:log4j-api:$log4j2Version")
testFixturesImplementation("com.google.guava:guava:$guavaVersion")

testImplementation("org.junit.jupiter:junit-jupiter:$junitVersion")
testImplementation("org.junit.jupiter:junit-jupiter-params:$junitVersion")
testImplementation("io.mockk:mockk:1.13.3")
testRuntimeOnly("org.mockito:mockito-core:$mockitoVersion")
testImplementation("org.mockito:mockito-junit-jupiter:$mockitoVersion")
testImplementation("org.assertj:assertj-core:3.24.2")

jmhImplementation("org.openjdk.jmh:jmh-core:$jmhVersion")
jmhAnnotationProcessor("org.openjdk.jmh:jmh-generator-annprocess:$jmhVersion")
}
allprojects {
group = "io.libp2p"
version = "develop"

apply(plugin = "kotlin")
apply(plugin = "idea")
apply(plugin = "io.gitlab.arturbosch.detekt")
apply(plugin = "java")
apply(plugin = "maven-publish")
apply(plugin = "org.jetbrains.dokka")
apply(plugin = "org.jmailen.kotlinter")
apply(plugin = "java-test-fixtures")
apply(plugin = "io.spring.dependency-management")
apply(from = "$rootDir/versions.gradle")

protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.21.12"
repositories {
mavenCentral()
maven("https://artifacts.consensys.net/public/maven/maven/")
}

tasks["clean"].doFirst { delete(generatedFilesBaseDir) }
dependencies {

idea {
module {
sourceDirs.add(file("$generatedFilesBaseDir/main/java"))
}
implementation(kotlin("stdlib-jdk8"))
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core")

implementation("com.google.guava:guava")
implementation("org.apache.logging.log4j:log4j-api")

testFixturesImplementation("org.apache.logging.log4j:log4j-api")
testFixturesImplementation("com.google.guava:guava")

testImplementation("org.junit.jupiter:junit-jupiter")
testImplementation("org.junit.jupiter:junit-jupiter-params")
testImplementation("io.mockk:mockk")
testImplementation("org.assertj:assertj-core")
testImplementation("org.apache.logging.log4j:log4j-core")
}
}

java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}

tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "11"
kotlinOptions {
freeCompilerArgs = listOf("-Xjvm-default=all")
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "11"
kotlinOptions {
languageVersion = "1.6"
allWarningsAsErrors = true
freeCompilerArgs = listOf("-Xjvm-default=all")
}
}
tasks.withType<Copy> {
duplicatesStrategy = DuplicatesStrategy.INCLUDE
}
}
tasks.withType<Copy> {
duplicatesStrategy = DuplicatesStrategy.INCLUDE
}

// Parallel build execution
tasks.test {
description = "Runs the unit tests."
tasks.test {
description = "Runs the unit tests."

useJUnitPlatform {
excludeTags("interop")
}
useJUnitPlatform {
excludeTags("interop")
}

testLogging {
events("FAILED")
exceptionFormat = TestExceptionFormat.FULL
showCauses = true
showExceptions = true
showStackTraces = true
}
testLogging {
events("FAILED")
exceptionFormat = TestExceptionFormat.FULL
showCauses = true
showExceptions = true
showStackTraces = true
}

// disabling the parallel test runs for the time being due to port collisions
// If GRADLE_MAX_TEST_FORKS is not set, use half the available processors
// disabling the parallel test runs for the time being due to port collisions
// If GRADLE_MAX_TEST_FORKS is not set, use half the available processors
// maxParallelForks = (System.getenv("GRADLE_MAX_TEST_FORKS")?.toInt() ?:
// Runtime.getRuntime().availableProcessors().div(2))
}
}

kotlinter {
disabledRules = arrayOf("no-wildcard-imports", "enum-entry-name-case")
}
kotlinter {
disabledRules = arrayOf("no-wildcard-imports", "enum-entry-name-case")
}

val sourcesJar by tasks.registering(Jar::class) {
archiveClassifier.set("sources")
from(sourceSets.main.get().allSource)
}
val sourcesJar by tasks.registering(Jar::class) {
archiveClassifier.set("sources")
from(sourceSets.main.get().allSource)
}

tasks.dokkaHtml.configure {
outputDirectory.set(buildDir.resolve("dokka"))
dokkaSourceSets {
configureEach {
jdkVersion.set(11)
reportUndocumented.set(false)
externalDocumentationLink {
url.set(URL("https://netty.io/4.1/api/"))
tasks.dokkaHtml.configure {
outputDirectory.set(buildDir.resolve("dokka"))
dokkaSourceSets {
configureEach {
jdkVersion.set(11)
reportUndocumented.set(false)
externalDocumentationLink {
url.set(URL("https://netty.io/4.1/api/"))
}
}
}
}
}

val dokkaJar: TaskProvider<Jar> by tasks.registering(Jar::class) {
group = JavaBasePlugin.DOCUMENTATION_GROUP
val dokkaJavadocTask = tasks.getByName("dokkaJavadoc")
dependsOn(dokkaJavadocTask)
archiveClassifier.set("javadoc")
from(dokkaJavadocTask.outputs)
}
val dokkaJar: TaskProvider<Jar> by tasks.registering(Jar::class) {
group = JavaBasePlugin.DOCUMENTATION_GROUP
val dokkaJavadocTask = tasks.getByName("dokkaJavadoc")
dependsOn(dokkaJavadocTask)
archiveClassifier.set("javadoc")
from(dokkaJavadocTask.outputs)
}

publishing {
repositories {
maven {
name = "cloudsmith"
url = uri("https://api-g.cloudsmith.io/maven/libp2p/jvm-libp2p")
credentials {
username = findProperty("cloudsmithUser")
password = findProperty("cloudsmithApiKey")
publishing {
repositories {
maven {
name = "cloudsmith"
url = uri("https://api-g.cloudsmith.io/maven/libp2p/jvm-libp2p")
credentials {
username = findProperty("cloudsmithUser") as String?
password = findProperty("cloudsmithApiKey") as String?
}
}
}
}
publications {
register("mavenJava", MavenPublication::class) {
from(components["java"])
artifact(sourcesJar.get())
artifact(dokkaJar.get())
groupId = "io.libp2p"
artifactId = project.name
if (hasProperty("mavenArtifactId")) {
publications {
register("mavenJava", MavenPublication::class) {
from(components["java"])
artifact(sourcesJar.get())
artifact(dokkaJar.get())
groupId = "io.libp2p"
artifactId = project.property("mavenArtifactId") as String
}
}
}
}
}

fun findProperty(s: String) = project.findProperty(s) as String?

val compileKotlin: KotlinCompile by tasks
compileKotlin.kotlinOptions {
languageVersion = "1.6"
allWarningsAsErrors = true
}

detekt {
config = files("$projectDir/detekt/config.yml")
buildUponDefaultConfig = true
detekt {
config = files("$rootDir/detekt/config.yml")
buildUponDefaultConfig = true
}
}
6 changes: 0 additions & 6 deletions examples/build.gradle

This file was deleted.

11 changes: 1 addition & 10 deletions examples/chatter/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.3.31'
}

version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
compile 'io.libp2p:jvm-libp2p-minimal:0.4.0-SNAPSHOT'
api project(':libp2p')
}

33 changes: 17 additions & 16 deletions examples/chatter/src/main/kotlin/io/libp2p/example/chat/ChatNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import io.libp2p.core.PeerId
import io.libp2p.core.PeerInfo
import io.libp2p.core.Stream
import io.libp2p.core.dsl.host
import io.libp2p.core.multiformats.Multiaddr
import io.libp2p.discovery.MDnsDiscovery
import java.lang.Exception
import java.net.Inet4Address
import java.net.InetAddress
import java.net.NetworkInterface
Expand All @@ -16,8 +14,8 @@ typealias OnMessage = (String) -> Unit

class ChatNode(private val printMsg: OnMessage) {
private data class Friend(
var name: String,
val controller: ChatController
var name: String,
val controller: ChatController
)

private var currentAlias: String
Expand All @@ -30,20 +28,22 @@ class ChatNode(private val printMsg: OnMessage) {
+Chat(::messageReceived)
}
network {
listen("/ip4/${address}/tcp/0")
listen("/ip4/$address/tcp/0")
}
}

val peerId = chatHost.peerId
val address: String
get() { return privateAddress.hostAddress }
get() {
return privateAddress.hostAddress
}

init {
chatHost.start().get()
currentAlias = chatHost.peerId.toBase58()

peerFinder = MDnsDiscovery(chatHost, address = privateAddress)
peerFinder.onPeerFound { peerFound(it) }
peerFinder.newPeerFoundListeners += { peerFound(it) }
peerFinder.start()
} // init

Expand Down Expand Up @@ -81,8 +81,8 @@ class ChatNode(private val printMsg: OnMessage) {

private fun peerFound(info: PeerInfo) {
if (
info.peerId == chatHost.peerId ||
knownNodes.contains(info.peerId)
info.peerId == chatHost.peerId ||
knownNodes.contains(info.peerId)
)
return

Expand All @@ -97,21 +97,22 @@ class ChatNode(private val printMsg: OnMessage) {
printMsg("Connected to new peer ${info.peerId}")
chatConnection.second.send("/who")
peers[info.peerId] = Friend(
info.peerId.toBase58(),
chatConnection.second
info.peerId.toBase58(),
chatConnection.second
)
} // peerFound

@Suppress("SwallowedException")
private fun connectChat(info: PeerInfo): Pair<Stream, ChatController>? {
try {
val chat = Chat(::messageReceived).dial(
chatHost,
info.peerId,
info.addresses[0]
chatHost,
info.peerId,
info.addresses[0]
)
return Pair(
chat.stream.get(),
chat.controller.get()
chat.stream.get(),
chat.controller.get()
)
} catch (e: Exception) {
return null
Expand Down
Loading

0 comments on commit 89ba1c2

Please sign in to comment.