From 2a80d53b983d06c8bd643b6772974d4e04bff90e Mon Sep 17 00:00:00 2001 From: malachy Date: Wed, 25 Oct 2023 14:51:47 +0100 Subject: [PATCH 01/12] CORE-17494: Add check to CPB creation that two CPKs do not share a cordappName --- .../java/net/corda/plugins/cpb2/CpbTask.java | 20 ++++++ .../net/corda/plugins/cpk2/CordappUtils.java | 2 +- .../net/corda/plugins/cpb2/CpbSharedName.kt | 61 +++++++++++++++++ .../resources/cpb-shared-name/build.gradle | 65 +++++++++++++++++++ .../project-dependency/build.gradle | 23 +++++++ .../resources/cpb-shared-name/settings.gradle | 10 +++ 6 files changed, 180 insertions(+), 1 deletion(-) create mode 100644 cordapp-cpk2/src/test/kotlin/net/corda/plugins/cpb2/CpbSharedName.kt create mode 100644 cordapp-cpk2/src/test/resources/cpb-shared-name/build.gradle create mode 100644 cordapp-cpk2/src/test/resources/cpb-shared-name/project-dependency/build.gradle create mode 100644 cordapp-cpk2/src/test/resources/cpb-shared-name/settings.gradle diff --git a/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java b/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java index 79dab0fe0b..95556fd0b7 100644 --- a/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java +++ b/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java @@ -13,12 +13,15 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.util.HashMap; +import java.util.HashSet; import java.util.Set; import java.util.jar.JarInputStream; import static java.util.Collections.singleton; import static net.corda.plugins.cpk2.CordappUtils.CORDAPP_TASK_GROUP; import static net.corda.plugins.cpk2.CordappUtils.CORDA_CPK_TYPE; +import static net.corda.plugins.cpk2.CordappUtils.CPK_CORDAPP_NAME; import static net.corda.plugins.cpk2.CordappUtils.CPK_FILE_EXTENSION; @DisableCachingByDefault @@ -31,6 +34,7 @@ public class CpbTask extends Jar { public static final String CPB_VERSION_ATTRIBUTE = "Corda-CPB-Version"; public static final String CPB_FORMAT_VERSION = "Corda-CPB-Format"; public static final String CPB_CURRENT_FORMAT_VERSION = "2.0"; + private static HashMap cpkNames = new HashMap<>(); public CpbTask() { setGroup(CORDAPP_TASK_GROUP); @@ -55,6 +59,8 @@ public CpbTask() { m.getAttributes().put(CPB_NAME_ATTRIBUTE, getArchiveBaseName()); m.getAttributes().put(CPB_VERSION_ATTRIBUTE, getArchiveVersion()); }); + + cpkNames = new HashMap<>(); } @Override @@ -73,6 +79,20 @@ private boolean isCPK(@NotNull FileTreeElement element) { final Path cpkPath = element.getFile().toPath(); try (JarInputStream cpkStream = new JarInputStream(new BufferedInputStream(Files.newInputStream(cpkPath)))) { final String cpkType = cpkStream.getManifest().getMainAttributes().getValue(CORDA_CPK_TYPE); + final String cpkCordappName = cpkStream.getManifest().getMainAttributes().getValue(CPK_CORDAPP_NAME); + System.out.println("Processing CPK " + cpkType + " " + cpkCordappName); + if (cpkCordappName != null) { + System.out.println(cpkCordappName); + if (cpkNames.containsKey(cpkCordappName)) { + if (cpkNames.get(cpkCordappName) == 1) { + cpkNames.put(cpkCordappName, 2); + } else { + throw new InvalidUserDataException("Two CPKs may not share a cordappName. Error in " + cpkCordappName); + } + } else { + cpkNames.put(cpkCordappName, 1); + } + } return cpkType != null && EXCLUDED_CPK_TYPES.contains(cpkType.toLowerCase()); } catch (IOException e) { throw new InvalidUserDataException(e.getMessage(), e); diff --git a/cordapp-cpk2/src/main/java/net/corda/plugins/cpk2/CordappUtils.java b/cordapp-cpk2/src/main/java/net/corda/plugins/cpk2/CordappUtils.java index 9bcac02508..f3b09f6188 100644 --- a/cordapp-cpk2/src/main/java/net/corda/plugins/cpk2/CordappUtils.java +++ b/cordapp-cpk2/src/main/java/net/corda/plugins/cpk2/CordappUtils.java @@ -79,7 +79,7 @@ public final class CordappUtils { // These tags are for the CPK file. static final String CPK_PLATFORM_VERSION = "Corda-CPK-Built-Platform-Version"; - static final String CPK_CORDAPP_NAME = "Corda-CPK-Cordapp-Name"; + public static final String CPK_CORDAPP_NAME = "Corda-CPK-Cordapp-Name"; static final String CPK_CORDAPP_VERSION = "Corda-CPK-Cordapp-Version"; static final String CPK_CORDAPP_LICENCE = "Corda-CPK-Cordapp-Licence"; static final String CPK_CORDAPP_VENDOR = "Corda-CPK-Cordapp-Vendor"; diff --git a/cordapp-cpk2/src/test/kotlin/net/corda/plugins/cpb2/CpbSharedName.kt b/cordapp-cpk2/src/test/kotlin/net/corda/plugins/cpb2/CpbSharedName.kt new file mode 100644 index 0000000000..77ab8714e0 --- /dev/null +++ b/cordapp-cpk2/src/test/kotlin/net/corda/plugins/cpb2/CpbSharedName.kt @@ -0,0 +1,61 @@ +package net.corda.plugins.cpb2 + +import net.corda.plugins.cpk2.GradleProject +import net.corda.plugins.cpk2.cordaApiVersion +import net.corda.plugins.cpk2.expectedCordappContractVersion +import org.gradle.testkit.runner.UnexpectedBuildFailure +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.TestInstance +import org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS +import org.junit.jupiter.api.TestReporter +import org.junit.jupiter.api.assertThrows +import org.junit.jupiter.api.io.TempDir +import java.nio.file.Files +import java.nio.file.Path +import java.util.* + +@TestInstance(PER_CLASS) +class CpbSharedName { + private companion object { + private const val platformCordappVersion = "2.3.4" + private const val cordappVersion = "1.2.1" + } + + private lateinit var externalProject: GradleProject + + @Test + fun `Assert 2 CPKs don't share a cordappName`( + @TempDir externalCordappProjectDir: Path, + @TempDir testDir: Path, + reporter: TestReporter + ) { + val mavenRepoDir = Files.createDirectory(testDir.resolve("maven")) + val cpbProjectDir = Files.createDirectory(testDir.resolve("cpb")) + externalProject = GradleProject(externalCordappProjectDir, reporter) + .withTestName("external-cordapp") + .withSubResource("corda-platform-cordapp/build.gradle") + .withSubResource("external-cordapp-transitive-dependency/build.gradle") + .withTaskName("publishAllPublicationsToTestRepository") + .build("-Pmaven_repository_dir=$mavenRepoDir", + "-Pcorda_api_version=$cordaApiVersion", + "-Pplatform_cordapp_version=$platformCordappVersion", + "-Pcordapp_version=$cordappVersion", + "-Pcordapp_contract_version=$expectedCordappContractVersion") + val e = assertThrows { + GradleProject(cpbProjectDir, reporter) + .withTestName("cpb-shared-name") + .withSubResource("project-dependency/build.gradle") + .build( + "-Pmaven_repository_dir=$mavenRepoDir", + "-Pplatform_cordapp_version=$platformCordappVersion", + "-Pcordapp_version=$cordappVersion", + "-Pcordapp_contract_version=$expectedCordappContractVersion" + ) + } + + val expectedMessage = "Two CPKs may not share a cordappName." + assert(e.message!!.contains(expectedMessage)) { + "Error message does not match expected value. Error message should contain \"$expectedMessage\"" + } + } +} diff --git a/cordapp-cpk2/src/test/resources/cpb-shared-name/build.gradle b/cordapp-cpk2/src/test/resources/cpb-shared-name/build.gradle new file mode 100644 index 0000000000..be52ddde96 --- /dev/null +++ b/cordapp-cpk2/src/test/resources/cpb-shared-name/build.gradle @@ -0,0 +1,65 @@ +plugins { + id 'net.corda.plugins.cordapp-cpb2' + id 'org.jetbrains.kotlin.jvm' +} + +apply from: 'javaTarget.gradle' +apply from: 'kotlin.gradle' + +allprojects { + group = 'com.example' + version = cordapp_version + + repositories { + maven { + name = 'test-repository' + url = maven_repository_dir + } + } + apply from: "$rootDir/repositories.gradle" +} + +cordapp { + targetPlatformVersion = platform_version.toInteger() + + contract { + name = 'CorDapp CPB test' + versionId = cordapp_contract_version.toInteger() + licence = 'Test-Licence' + vendor = 'R3' + } +} + +configurations { + cpks { + canBeConsumed = false + } +} + +dependencies { + cordapp group: group, name: "external-cordapp", version: cordapp_version + cordapp project('project-dependency') + + cpks project(path: 'project-dependency', configuration: 'cordaCPK') + cpks group: "net.corda", name: "corda-platform-cordapp", version: platform_cordapp_version + cpks group: "com.example", name: "external-cordapp", version: cordapp_version + cpks group: "com.example", name: "external-cordapp-transitive-dependency", version: cordapp_version +} + + +def cpkDir = layout.buildDirectory.dir('cpks') +def copyCPKs = tasks.register('copyCPKs', Copy) { + from configurations.cpks + into cpkDir +} + +artifacts { + archives(cpkDir) { + builtBy copyCPKs + } +} + +cpb { + archiveBaseName = "customName" + archiveVersion = "customVersion" +} \ No newline at end of file diff --git a/cordapp-cpk2/src/test/resources/cpb-shared-name/project-dependency/build.gradle b/cordapp-cpk2/src/test/resources/cpb-shared-name/project-dependency/build.gradle new file mode 100644 index 0000000000..b16873ad52 --- /dev/null +++ b/cordapp-cpk2/src/test/resources/cpb-shared-name/project-dependency/build.gradle @@ -0,0 +1,23 @@ +plugins { + id 'net.corda.plugins.cordapp-cpk2' + id 'org.jetbrains.kotlin.jvm' +} + +apply from: '../javaTarget.gradle' +apply from: '../kotlin.gradle' + +cordapp { + targetPlatformVersion = platform_version.toInteger() + + contract { + name = 'Project CorDapp' + versionId = cordapp_contract_version.toInteger() + licence = 'Test-Licence' + vendor = 'R3' + cordappName = 'com.example.cordapp-cpb' + } +} + +tasks.named('jar', Jar) { + archiveBaseName = 'project-cordapp' +} diff --git a/cordapp-cpk2/src/test/resources/cpb-shared-name/settings.gradle b/cordapp-cpk2/src/test/resources/cpb-shared-name/settings.gradle new file mode 100644 index 0000000000..c41a15d928 --- /dev/null +++ b/cordapp-cpk2/src/test/resources/cpb-shared-name/settings.gradle @@ -0,0 +1,10 @@ +pluginManagement { + plugins { + id 'org.jetbrains.kotlin.jvm' version kotlin_version + } +} + +rootProject.name = 'cordapp-cpb' + +include 'project-dependency' +include 'project-dependency-2' From c58a82ade792c6417fdf72f455759d9366bfce57 Mon Sep 17 00:00:00 2001 From: malachy Date: Wed, 1 Nov 2023 14:22:26 +0000 Subject: [PATCH 02/12] Remove unused import --- cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java | 1 - 1 file changed, 1 deletion(-) diff --git a/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java b/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java index 95556fd0b7..b02381321a 100644 --- a/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java +++ b/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java @@ -14,7 +14,6 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.HashMap; -import java.util.HashSet; import java.util.Set; import java.util.jar.JarInputStream; From e7eece61055fd801157819b6f1048a3d0e428d27 Mon Sep 17 00:00:00 2001 From: malachy Date: Wed, 8 Nov 2023 11:50:50 +0000 Subject: [PATCH 03/12] Fixed test --- .../resources/cpb-shared-name/project-dependency/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cordapp-cpk2/src/test/resources/cpb-shared-name/project-dependency/build.gradle b/cordapp-cpk2/src/test/resources/cpb-shared-name/project-dependency/build.gradle index b16873ad52..3aa49fc744 100644 --- a/cordapp-cpk2/src/test/resources/cpb-shared-name/project-dependency/build.gradle +++ b/cordapp-cpk2/src/test/resources/cpb-shared-name/project-dependency/build.gradle @@ -14,7 +14,7 @@ cordapp { versionId = cordapp_contract_version.toInteger() licence = 'Test-Licence' vendor = 'R3' - cordappName = 'com.example.cordapp-cpb' + cordappCpkName = 'com.example.cordapp-cpb' } } From 1ebd3a2dde60e729387786d0a3d3fa0263362c56 Mon Sep 17 00:00:00 2001 From: malachy Date: Fri, 10 Nov 2023 14:59:34 +0000 Subject: [PATCH 04/12] Adressed feedback by renaming CpbTask.isCPK to isValidCPK and changing test to create 2 CPKs independently. --- .../java/net/corda/plugins/cpb2/CpbTask.java | 4 ++-- .../net/corda/plugins/cpb2/CpbSharedName.kt | 1 + .../resources/cpb-shared-name/build.gradle | 6 ++--- .../project-dependency/build.gradle | 4 ++-- .../second-project-dependency/build.gradle | 23 +++++++++++++++++++ .../resources/cpb-shared-name/settings.gradle | 4 ++-- 6 files changed, 32 insertions(+), 10 deletions(-) create mode 100644 cordapp-cpk2/src/test/resources/cpb-shared-name/second-project-dependency/build.gradle diff --git a/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java b/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java index b02381321a..b5c61a69ee 100644 --- a/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java +++ b/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java @@ -66,11 +66,11 @@ public CpbTask() { @NotNull public AbstractCopyTask from(@NotNull Object... args) { return super.from(args, copySpec -> - copySpec.exclude(this::isCPK) + copySpec.exclude(this::isValidCPK) ); } - private boolean isCPK(@NotNull FileTreeElement element) { + private boolean isValidCPK(@NotNull FileTreeElement element) { if (!element.getName().endsWith(CPK_FILE_SUFFIX)) { return false; } diff --git a/cordapp-cpk2/src/test/kotlin/net/corda/plugins/cpb2/CpbSharedName.kt b/cordapp-cpk2/src/test/kotlin/net/corda/plugins/cpb2/CpbSharedName.kt index 77ab8714e0..e936d91781 100644 --- a/cordapp-cpk2/src/test/kotlin/net/corda/plugins/cpb2/CpbSharedName.kt +++ b/cordapp-cpk2/src/test/kotlin/net/corda/plugins/cpb2/CpbSharedName.kt @@ -45,6 +45,7 @@ class CpbSharedName { GradleProject(cpbProjectDir, reporter) .withTestName("cpb-shared-name") .withSubResource("project-dependency/build.gradle") + .withSubResource("second-project-dependency/build.gradle") .build( "-Pmaven_repository_dir=$mavenRepoDir", "-Pplatform_cordapp_version=$platformCordappVersion", diff --git a/cordapp-cpk2/src/test/resources/cpb-shared-name/build.gradle b/cordapp-cpk2/src/test/resources/cpb-shared-name/build.gradle index be52ddde96..7ed738b176 100644 --- a/cordapp-cpk2/src/test/resources/cpb-shared-name/build.gradle +++ b/cordapp-cpk2/src/test/resources/cpb-shared-name/build.gradle @@ -37,13 +37,11 @@ configurations { } dependencies { - cordapp group: group, name: "external-cordapp", version: cordapp_version cordapp project('project-dependency') + cordapp project('second-project-dependency') cpks project(path: 'project-dependency', configuration: 'cordaCPK') - cpks group: "net.corda", name: "corda-platform-cordapp", version: platform_cordapp_version - cpks group: "com.example", name: "external-cordapp", version: cordapp_version - cpks group: "com.example", name: "external-cordapp-transitive-dependency", version: cordapp_version + cpks project(path: 'second-project-dependency', configuration: 'cordaCPK') } diff --git a/cordapp-cpk2/src/test/resources/cpb-shared-name/project-dependency/build.gradle b/cordapp-cpk2/src/test/resources/cpb-shared-name/project-dependency/build.gradle index 3aa49fc744..832c43a87b 100644 --- a/cordapp-cpk2/src/test/resources/cpb-shared-name/project-dependency/build.gradle +++ b/cordapp-cpk2/src/test/resources/cpb-shared-name/project-dependency/build.gradle @@ -14,10 +14,10 @@ cordapp { versionId = cordapp_contract_version.toInteger() licence = 'Test-Licence' vendor = 'R3' - cordappCpkName = 'com.example.cordapp-cpb' + cordappCpkName = 'com.example.cordapp-cpb.duplicate' } } tasks.named('jar', Jar) { - archiveBaseName = 'project-cordapp' + archiveBaseName = 'second-project-cordapp' } diff --git a/cordapp-cpk2/src/test/resources/cpb-shared-name/second-project-dependency/build.gradle b/cordapp-cpk2/src/test/resources/cpb-shared-name/second-project-dependency/build.gradle new file mode 100644 index 0000000000..c290432031 --- /dev/null +++ b/cordapp-cpk2/src/test/resources/cpb-shared-name/second-project-dependency/build.gradle @@ -0,0 +1,23 @@ +plugins { + id 'net.corda.plugins.cordapp-cpk2' + id 'org.jetbrains.kotlin.jvm' +} + +apply from: '../javaTarget.gradle' +apply from: '../kotlin.gradle' + +cordapp { + targetPlatformVersion = platform_version.toInteger() + + contract { + name = 'Project CorDapp' + versionId = cordapp_contract_version.toInteger() + licence = 'Test-Licence' + vendor = 'R3' + cordappCpkName = 'com.example.cordapp-cpb.duplicate' + } +} + +tasks.named('jar', Jar) { + archiveBaseName = 'project-cordapp' +} diff --git a/cordapp-cpk2/src/test/resources/cpb-shared-name/settings.gradle b/cordapp-cpk2/src/test/resources/cpb-shared-name/settings.gradle index c41a15d928..9c6542ea23 100644 --- a/cordapp-cpk2/src/test/resources/cpb-shared-name/settings.gradle +++ b/cordapp-cpk2/src/test/resources/cpb-shared-name/settings.gradle @@ -4,7 +4,7 @@ pluginManagement { } } -rootProject.name = 'cordapp-cpb' +rootProject.name = 'cpb-shared-name' include 'project-dependency' -include 'project-dependency-2' +include 'second-project-dependency' From 5fd082b7403034a7c3876a68b1dee8c4767ad124 Mon Sep 17 00:00:00 2001 From: malachy Date: Fri, 10 Nov 2023 15:43:37 +0000 Subject: [PATCH 05/12] Adressed feedback by converting CbpTask.cpkNames to an instance variable --- .../src/main/java/net/corda/plugins/cpb2/CpbTask.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java b/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java index b5c61a69ee..f759d5b3f0 100644 --- a/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java +++ b/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java @@ -33,7 +33,7 @@ public class CpbTask extends Jar { public static final String CPB_VERSION_ATTRIBUTE = "Corda-CPB-Version"; public static final String CPB_FORMAT_VERSION = "Corda-CPB-Format"; public static final String CPB_CURRENT_FORMAT_VERSION = "2.0"; - private static HashMap cpkNames = new HashMap<>(); + private final HashMap cpkNames = new HashMap<>(); public CpbTask() { setGroup(CORDAPP_TASK_GROUP); @@ -58,8 +58,6 @@ public CpbTask() { m.getAttributes().put(CPB_NAME_ATTRIBUTE, getArchiveBaseName()); m.getAttributes().put(CPB_VERSION_ATTRIBUTE, getArchiveVersion()); }); - - cpkNames = new HashMap<>(); } @Override From 05bcf80e63bdebfe7243df6f99db33aafb706548 Mon Sep 17 00:00:00 2001 From: malachy Date: Wed, 15 Nov 2023 15:30:06 +0000 Subject: [PATCH 06/12] Changed how CPB check works to instead verify against manifests --- .../java/net/corda/plugins/cpb2/CpbTask.java | 18 ++++++++---------- .../net/corda/plugins/cpb2/CpbSharedName.kt | 11 ----------- .../project-dependency/build.gradle | 2 +- .../second-project-dependency/build.gradle | 2 +- 4 files changed, 10 insertions(+), 23 deletions(-) diff --git a/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java b/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java index f759d5b3f0..51dd7103e8 100644 --- a/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java +++ b/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java @@ -16,6 +16,7 @@ import java.util.HashMap; import java.util.Set; import java.util.jar.JarInputStream; +import java.util.jar.Manifest; import static java.util.Collections.singleton; import static net.corda.plugins.cpk2.CordappUtils.CORDAPP_TASK_GROUP; @@ -33,7 +34,7 @@ public class CpbTask extends Jar { public static final String CPB_VERSION_ATTRIBUTE = "Corda-CPB-Version"; public static final String CPB_FORMAT_VERSION = "Corda-CPB-Format"; public static final String CPB_CURRENT_FORMAT_VERSION = "2.0"; - private final HashMap cpkNames = new HashMap<>(); + private final HashMap cpkNames = new HashMap<>(); public CpbTask() { setGroup(CORDAPP_TASK_GROUP); @@ -73,21 +74,18 @@ private boolean isValidCPK(@NotNull FileTreeElement element) { return false; } - final Path cpkPath = element.getFile().toPath(); + Path cpkPath = element.getFile().toPath(); try (JarInputStream cpkStream = new JarInputStream(new BufferedInputStream(Files.newInputStream(cpkPath)))) { - final String cpkType = cpkStream.getManifest().getMainAttributes().getValue(CORDA_CPK_TYPE); - final String cpkCordappName = cpkStream.getManifest().getMainAttributes().getValue(CPK_CORDAPP_NAME); - System.out.println("Processing CPK " + cpkType + " " + cpkCordappName); + Manifest manifest = cpkStream.getManifest(); + String cpkType = manifest.getMainAttributes().getValue(CORDA_CPK_TYPE); + String cpkCordappName = manifest.getMainAttributes().getValue(CPK_CORDAPP_NAME); if (cpkCordappName != null) { - System.out.println(cpkCordappName); if (cpkNames.containsKey(cpkCordappName)) { - if (cpkNames.get(cpkCordappName) == 1) { - cpkNames.put(cpkCordappName, 2); - } else { + if (!cpkNames.get(cpkCordappName).equals(manifest)) { throw new InvalidUserDataException("Two CPKs may not share a cordappName. Error in " + cpkCordappName); } } else { - cpkNames.put(cpkCordappName, 1); + cpkNames.put(cpkCordappName, manifest); } } return cpkType != null && EXCLUDED_CPK_TYPES.contains(cpkType.toLowerCase()); diff --git a/cordapp-cpk2/src/test/kotlin/net/corda/plugins/cpb2/CpbSharedName.kt b/cordapp-cpk2/src/test/kotlin/net/corda/plugins/cpb2/CpbSharedName.kt index e936d91781..0c659b9794 100644 --- a/cordapp-cpk2/src/test/kotlin/net/corda/plugins/cpb2/CpbSharedName.kt +++ b/cordapp-cpk2/src/test/kotlin/net/corda/plugins/cpb2/CpbSharedName.kt @@ -25,22 +25,11 @@ class CpbSharedName { @Test fun `Assert 2 CPKs don't share a cordappName`( - @TempDir externalCordappProjectDir: Path, @TempDir testDir: Path, reporter: TestReporter ) { val mavenRepoDir = Files.createDirectory(testDir.resolve("maven")) val cpbProjectDir = Files.createDirectory(testDir.resolve("cpb")) - externalProject = GradleProject(externalCordappProjectDir, reporter) - .withTestName("external-cordapp") - .withSubResource("corda-platform-cordapp/build.gradle") - .withSubResource("external-cordapp-transitive-dependency/build.gradle") - .withTaskName("publishAllPublicationsToTestRepository") - .build("-Pmaven_repository_dir=$mavenRepoDir", - "-Pcorda_api_version=$cordaApiVersion", - "-Pplatform_cordapp_version=$platformCordappVersion", - "-Pcordapp_version=$cordappVersion", - "-Pcordapp_contract_version=$expectedCordappContractVersion") val e = assertThrows { GradleProject(cpbProjectDir, reporter) .withTestName("cpb-shared-name") diff --git a/cordapp-cpk2/src/test/resources/cpb-shared-name/project-dependency/build.gradle b/cordapp-cpk2/src/test/resources/cpb-shared-name/project-dependency/build.gradle index 832c43a87b..c290432031 100644 --- a/cordapp-cpk2/src/test/resources/cpb-shared-name/project-dependency/build.gradle +++ b/cordapp-cpk2/src/test/resources/cpb-shared-name/project-dependency/build.gradle @@ -19,5 +19,5 @@ cordapp { } tasks.named('jar', Jar) { - archiveBaseName = 'second-project-cordapp' + archiveBaseName = 'project-cordapp' } diff --git a/cordapp-cpk2/src/test/resources/cpb-shared-name/second-project-dependency/build.gradle b/cordapp-cpk2/src/test/resources/cpb-shared-name/second-project-dependency/build.gradle index c290432031..832c43a87b 100644 --- a/cordapp-cpk2/src/test/resources/cpb-shared-name/second-project-dependency/build.gradle +++ b/cordapp-cpk2/src/test/resources/cpb-shared-name/second-project-dependency/build.gradle @@ -19,5 +19,5 @@ cordapp { } tasks.named('jar', Jar) { - archiveBaseName = 'project-cordapp' + archiveBaseName = 'second-project-cordapp' } From fe4ef2a2883d367f7b25f2f0fb941e1d4ba6687e Mon Sep 17 00:00:00 2001 From: malachy Date: Wed, 15 Nov 2023 16:26:55 +0000 Subject: [PATCH 07/12] Updated error message to reflect new name of value --- cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java | 2 +- .../src/test/kotlin/net/corda/plugins/cpb2/CpbSharedName.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java b/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java index 51dd7103e8..11505c2fa8 100644 --- a/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java +++ b/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java @@ -82,7 +82,7 @@ private boolean isValidCPK(@NotNull FileTreeElement element) { if (cpkCordappName != null) { if (cpkNames.containsKey(cpkCordappName)) { if (!cpkNames.get(cpkCordappName).equals(manifest)) { - throw new InvalidUserDataException("Two CPKs may not share a cordappName. Error in " + cpkCordappName); + throw new InvalidUserDataException("Two CPKs may not share a cordappCpkName. Error in " + cpkCordappName); } } else { cpkNames.put(cpkCordappName, manifest); diff --git a/cordapp-cpk2/src/test/kotlin/net/corda/plugins/cpb2/CpbSharedName.kt b/cordapp-cpk2/src/test/kotlin/net/corda/plugins/cpb2/CpbSharedName.kt index 0c659b9794..e884d5b58c 100644 --- a/cordapp-cpk2/src/test/kotlin/net/corda/plugins/cpb2/CpbSharedName.kt +++ b/cordapp-cpk2/src/test/kotlin/net/corda/plugins/cpb2/CpbSharedName.kt @@ -43,7 +43,7 @@ class CpbSharedName { ) } - val expectedMessage = "Two CPKs may not share a cordappName." + val expectedMessage = "Two CPKs may not share a cordappCpkName." assert(e.message!!.contains(expectedMessage)) { "Error message does not match expected value. Error message should contain \"$expectedMessage\"" } From 3d04b62fc2423200d8161f27c677bc5d0f160ac3 Mon Sep 17 00:00:00 2001 From: malachy Date: Mon, 20 Nov 2023 16:35:26 +0000 Subject: [PATCH 08/12] Move duplicate check out of exclude code --- .../net/corda/plugins/cpb2/CpbPlugin.java | 2 + .../java/net/corda/plugins/cpb2/CpbTask.java | 46 ++++++++++++------- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbPlugin.java b/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbPlugin.java index 8461b49f03..2931e03806 100644 --- a/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbPlugin.java +++ b/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbPlugin.java @@ -72,6 +72,8 @@ public void apply(@NotNull Project project) { cpbTask.getArchiveAppendix().convention(cpkTask.flatMap(Jar::getArchiveAppendix)); cpbTask.getArchiveVersion().convention(cpkTask.flatMap(Jar::getArchiveVersion)); + cpbTask.doFirst(task -> cpbTask.checkForDuplicates()); + cpbTask.doLast(task -> { if (cordappExtension.getSigning().getEnabled().get()) { sign(task, cordappExtension.getSigning().getOptions(), cpbTask.getArchiveFile().get().getAsFile()); diff --git a/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java b/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java index 11505c2fa8..b2c7b965be 100644 --- a/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java +++ b/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java @@ -2,6 +2,7 @@ import org.gradle.api.InvalidUserDataException; import org.gradle.api.file.DuplicatesStrategy; +import org.gradle.api.file.FileCollection; import org.gradle.api.file.FileTreeElement; import org.gradle.api.tasks.AbstractCopyTask; import org.gradle.api.tasks.bundling.Jar; @@ -10,13 +11,13 @@ import org.jetbrains.annotations.NotNull; import java.io.BufferedInputStream; +import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.util.HashMap; +import java.util.HashSet; import java.util.Set; import java.util.jar.JarInputStream; -import java.util.jar.Manifest; import static java.util.Collections.singleton; import static net.corda.plugins.cpk2.CordappUtils.CORDAPP_TASK_GROUP; @@ -34,7 +35,6 @@ public class CpbTask extends Jar { public static final String CPB_VERSION_ATTRIBUTE = "Corda-CPB-Version"; public static final String CPB_FORMAT_VERSION = "Corda-CPB-Format"; public static final String CPB_CURRENT_FORMAT_VERSION = "2.0"; - private final HashMap cpkNames = new HashMap<>(); public CpbTask() { setGroup(CORDAPP_TASK_GROUP); @@ -65,32 +65,44 @@ public CpbTask() { @NotNull public AbstractCopyTask from(@NotNull Object... args) { return super.from(args, copySpec -> - copySpec.exclude(this::isValidCPK) + copySpec.exclude(this::isCPK) ); } - private boolean isValidCPK(@NotNull FileTreeElement element) { + private boolean isCPK(@NotNull FileTreeElement element) { if (!element.getName().endsWith(CPK_FILE_SUFFIX)) { return false; } Path cpkPath = element.getFile().toPath(); try (JarInputStream cpkStream = new JarInputStream(new BufferedInputStream(Files.newInputStream(cpkPath)))) { - Manifest manifest = cpkStream.getManifest(); - String cpkType = manifest.getMainAttributes().getValue(CORDA_CPK_TYPE); - String cpkCordappName = manifest.getMainAttributes().getValue(CPK_CORDAPP_NAME); - if (cpkCordappName != null) { - if (cpkNames.containsKey(cpkCordappName)) { - if (!cpkNames.get(cpkCordappName).equals(manifest)) { - throw new InvalidUserDataException("Two CPKs may not share a cordappCpkName. Error in " + cpkCordappName); - } - } else { - cpkNames.put(cpkCordappName, manifest); - } - } + String cpkType = cpkStream.getManifest().getMainAttributes().getValue(CORDA_CPK_TYPE); return cpkType != null && EXCLUDED_CPK_TYPES.contains(cpkType.toLowerCase()); } catch (IOException e) { throw new InvalidUserDataException(e.getMessage(), e); } } + + public void checkForDuplicates() { + Set cpkNames = new HashSet<>(); + System.out.println("checking duplicates"); + FileCollection files = getInputs().getFiles(); + for (File file : files) { + Path path = file.toPath(); + if (path.toString().endsWith(CPK_FILE_SUFFIX)) { + try (JarInputStream cpkStream = new JarInputStream(new BufferedInputStream(Files.newInputStream(path)))) { + String cpkCordappName = cpkStream.getManifest().getMainAttributes().getValue(CPK_CORDAPP_NAME); + if (cpkCordappName != null) { + if (cpkNames.contains(cpkCordappName)) { + throw new InvalidUserDataException("Two CPKs may not share a cordappCpkName. Error in " + cpkCordappName); + } else { + cpkNames.add(cpkCordappName); + } + } + } catch (IOException e) { + throw new InvalidUserDataException(e.getMessage(), e); + } + } + } + } } From 00df3d578ff8bb53710e4e80c11e8cab0bd5a230 Mon Sep 17 00:00:00 2001 From: malachy Date: Tue, 21 Nov 2023 09:59:27 +0000 Subject: [PATCH 09/12] Removed unnecessary code --- .../src/main/java/net/corda/plugins/cpb2/CpbTask.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java b/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java index b2c7b965be..e1f629b6ee 100644 --- a/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java +++ b/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java @@ -74,9 +74,9 @@ private boolean isCPK(@NotNull FileTreeElement element) { return false; } - Path cpkPath = element.getFile().toPath(); + final Path cpkPath = element.getFile().toPath(); try (JarInputStream cpkStream = new JarInputStream(new BufferedInputStream(Files.newInputStream(cpkPath)))) { - String cpkType = cpkStream.getManifest().getMainAttributes().getValue(CORDA_CPK_TYPE); + final String cpkType = cpkStream.getManifest().getMainAttributes().getValue(CORDA_CPK_TYPE); return cpkType != null && EXCLUDED_CPK_TYPES.contains(cpkType.toLowerCase()); } catch (IOException e) { throw new InvalidUserDataException(e.getMessage(), e); @@ -85,7 +85,6 @@ private boolean isCPK(@NotNull FileTreeElement element) { public void checkForDuplicates() { Set cpkNames = new HashSet<>(); - System.out.println("checking duplicates"); FileCollection files = getInputs().getFiles(); for (File file : files) { Path path = file.toPath(); From 680578540735abc816e597efb86acd0b4f5d8b21 Mon Sep 17 00:00:00 2001 From: malachy <56348274+malachyb@users.noreply.github.com> Date: Wed, 6 Dec 2023 14:59:54 +0000 Subject: [PATCH 10/12] Update cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java Co-authored-by: Kyriakos Tharrouniatis --- cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java b/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java index e1f629b6ee..b466230bce 100644 --- a/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java +++ b/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java @@ -83,7 +83,7 @@ private boolean isCPK(@NotNull FileTreeElement element) { } } - public void checkForDuplicates() { + public void checkForDuplicateCpkCordappNames() { Set cpkNames = new HashSet<>(); FileCollection files = getInputs().getFiles(); for (File file : files) { From ab3e629f9c2481824397697dcb0cb53fbb99285d Mon Sep 17 00:00:00 2001 From: malachy <56348274+malachyb@users.noreply.github.com> Date: Wed, 6 Dec 2023 15:00:11 +0000 Subject: [PATCH 11/12] Update cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java Co-authored-by: Kyriakos Tharrouniatis --- cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java b/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java index b466230bce..d0f4b2eab2 100644 --- a/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java +++ b/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java @@ -84,7 +84,7 @@ private boolean isCPK(@NotNull FileTreeElement element) { } public void checkForDuplicateCpkCordappNames() { - Set cpkNames = new HashSet<>(); + Set cpkCordappNames = new HashSet<>(); FileCollection files = getInputs().getFiles(); for (File file : files) { Path path = file.toPath(); From a12c9c87dce6e363f1445f83fabc88af2379c39e Mon Sep 17 00:00:00 2001 From: malachy Date: Wed, 6 Dec 2023 15:26:52 +0000 Subject: [PATCH 12/12] Fix variable renames --- .../src/main/java/net/corda/plugins/cpb2/CpbPlugin.java | 2 +- .../src/main/java/net/corda/plugins/cpb2/CpbTask.java | 6 +++--- .../src/test/kotlin/net/corda/plugins/cpb2/CpbSharedName.kt | 2 +- .../cpb-shared-name/project-dependency/build.gradle | 2 +- .../cpb-shared-name/second-project-dependency/build.gradle | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbPlugin.java b/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbPlugin.java index 2931e03806..4664edc113 100644 --- a/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbPlugin.java +++ b/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbPlugin.java @@ -72,7 +72,7 @@ public void apply(@NotNull Project project) { cpbTask.getArchiveAppendix().convention(cpkTask.flatMap(Jar::getArchiveAppendix)); cpbTask.getArchiveVersion().convention(cpkTask.flatMap(Jar::getArchiveVersion)); - cpbTask.doFirst(task -> cpbTask.checkForDuplicates()); + cpbTask.doFirst(task -> cpbTask.checkForDuplicateCpkCordappNames()); cpbTask.doLast(task -> { if (cordappExtension.getSigning().getEnabled().get()) { diff --git a/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java b/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java index d0f4b2eab2..1debd46bb2 100644 --- a/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java +++ b/cordapp-cpk2/src/main/java/net/corda/plugins/cpb2/CpbTask.java @@ -92,10 +92,10 @@ public void checkForDuplicateCpkCordappNames() { try (JarInputStream cpkStream = new JarInputStream(new BufferedInputStream(Files.newInputStream(path)))) { String cpkCordappName = cpkStream.getManifest().getMainAttributes().getValue(CPK_CORDAPP_NAME); if (cpkCordappName != null) { - if (cpkNames.contains(cpkCordappName)) { - throw new InvalidUserDataException("Two CPKs may not share a cordappCpkName. Error in " + cpkCordappName); + if (cpkCordappNames.contains(cpkCordappName)) { + throw new InvalidUserDataException("Two CPKs may not share a cpkCordappName. Error in " + cpkCordappName); } else { - cpkNames.add(cpkCordappName); + cpkCordappNames.add(cpkCordappName); } } } catch (IOException e) { diff --git a/cordapp-cpk2/src/test/kotlin/net/corda/plugins/cpb2/CpbSharedName.kt b/cordapp-cpk2/src/test/kotlin/net/corda/plugins/cpb2/CpbSharedName.kt index e884d5b58c..205d495215 100644 --- a/cordapp-cpk2/src/test/kotlin/net/corda/plugins/cpb2/CpbSharedName.kt +++ b/cordapp-cpk2/src/test/kotlin/net/corda/plugins/cpb2/CpbSharedName.kt @@ -43,7 +43,7 @@ class CpbSharedName { ) } - val expectedMessage = "Two CPKs may not share a cordappCpkName." + val expectedMessage = "Two CPKs may not share a cpkCordappName." assert(e.message!!.contains(expectedMessage)) { "Error message does not match expected value. Error message should contain \"$expectedMessage\"" } diff --git a/cordapp-cpk2/src/test/resources/cpb-shared-name/project-dependency/build.gradle b/cordapp-cpk2/src/test/resources/cpb-shared-name/project-dependency/build.gradle index c290432031..caa3073ddd 100644 --- a/cordapp-cpk2/src/test/resources/cpb-shared-name/project-dependency/build.gradle +++ b/cordapp-cpk2/src/test/resources/cpb-shared-name/project-dependency/build.gradle @@ -14,7 +14,7 @@ cordapp { versionId = cordapp_contract_version.toInteger() licence = 'Test-Licence' vendor = 'R3' - cordappCpkName = 'com.example.cordapp-cpb.duplicate' + cpkCordappName = 'com.example.cordapp-cpb.duplicate' } } diff --git a/cordapp-cpk2/src/test/resources/cpb-shared-name/second-project-dependency/build.gradle b/cordapp-cpk2/src/test/resources/cpb-shared-name/second-project-dependency/build.gradle index 832c43a87b..2acce34a16 100644 --- a/cordapp-cpk2/src/test/resources/cpb-shared-name/second-project-dependency/build.gradle +++ b/cordapp-cpk2/src/test/resources/cpb-shared-name/second-project-dependency/build.gradle @@ -14,7 +14,7 @@ cordapp { versionId = cordapp_contract_version.toInteger() licence = 'Test-Licence' vendor = 'R3' - cordappCpkName = 'com.example.cordapp-cpb.duplicate' + cpkCordappName = 'com.example.cordapp-cpb.duplicate' } }