Christian Bongiorno
Christian Bongiorno

Reputation: 5648

gradle dependency not being executed

My situation is this:

So, when I run this sample task, it pulls the dependency to the root folder but never copies it and I am just not seeing why. Below is my trimmed down kotlin build

import com.ullink.Msbuild
import java.io.File



plugins {
    id("com.ullink.msbuild") version "3.8" apply false

}
subprojects {
    apply(plugin = "com.ullink.msbuild")
    val solution = projectDir.walkTopDown().find { it.extension == "sln" }!!
    tasks {

        val restore by register("restore", Exec::class) {
            executable = "nuget"
            args = listOf(
                    "restore", solution.canonicalPath,
                    "-Source",
                    (getenv("NUGET_SOURCE") ?: project.properties["package.sources"]) as String
            )
        }

        val msbuild by named<Msbuild>("msbuild") {
            dependsOn(restore)
            solutionFile = solution
            projectName = project.name
            verbosity = "quiet"
            targets = listOf("Clean", "Rebuild")
            configuration = "Release"
        }

        val cpfms by register<Copy>("cpFms"){
            dependsOn(rootProject.tasks.named("pullfms"))
            doLast{
                println("cpFms do last")

                val from = projectDir
                        .listFiles()
                        .find{ it.isDirectory() && it.name.toLowerCase().startsWith("prism.core.fulfillmentmicroservice") }!!
                println("making copy ${from} -> ${projectDir}")
                from(from)

                into(projectDir)
            }
        }

        val build by named("build") {
            dependsOn(msbuild)
        }
        val cpdll by register<Copy>("cpFmsdll") {
            dependsOn(cpfms,build)

            doLast{
                println("cpFmsdll do last")
                val from = projectDir.walkTopDown().find{ it.name == "PRISMDataAdaptor.dll" }!!
                val to = projectDir.listFiles().find{ it.isDirectory() && it.name.toLowerCase().startsWith("prism.core.fulfillmentmicroservice") }!!
                println("cp dll ${from} -> ${to}")
                from(from)
                into(to)
            }
        }
        

    }
}


tasks{
    val pullFms by register("pullfms", Exec::class) {
        args = listOf(
                "install", "PRISM.Core.FulfillmentMicroService",
                "-Source", (getenv("NUGET_PUBLISH") ?: project.properties["publish.repo"]) as String
        )
        executable = "nuget"
    }
}

so when I run gradle :adaptor-cwi_ky_statewide_smd:cpFmsdll

I expect that it will

  1. pull my dependency as defined in the root project
  2. copy it to the local project
  3. build
  4. copy the build dll into the folder in step 2

The dry run shows what I would expect:

PS C:\Users\cbongiorno\dev\sterling\prism\prism-adaptors> gradle --dry-run :adaptor-cwi_ky_statewide_smd:cpFmsdll
:adaptor-cwi_ky_statewide_smd:assemble SKIPPED
:adaptor-cwi_ky_statewide_smd:check SKIPPED
:adaptor-cwi_ky_statewide_smd:restore SKIPPED
:adaptor-cwi_ky_statewide_smd:msbuild SKIPPED
:adaptor-cwi_ky_statewide_smd:build SKIPPED
:pullfms SKIPPED
:adaptor-cwi_ky_statewide_smd:cpFms SKIPPED
:adaptor-cwi_ky_statewide_smd:cpFmsdll SKIPPED

but the execution doesn't actually do it?! The directly isn't copied in the the local project but it is in the root project

PS C:\Users\cbongiorno\dev\sterling\prism\prism-adaptors> gradle :adaptor-cwi_ky_statewide_smd:cpFmsdll

> Task :adaptor-cwi_ky_statewide_smd:restore
MSBuild auto-detection: using msbuild version '15.5.180.51428' from 'C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\bin'.
All packages listed in packages.config are already installed.

> Task :pullfms
Feeds used:
  https://repo.dev.backgroundcheck.com/nexus/repository/nuget-group/

Installing package 'PRISM.Core.FulfillmentMicroService' to 'C:\Users\cbongiorno\dev\sterling\prism\prism-adaptors'.
  CACHE https://repo.dev.backgroundcheck.com/nexus/repository/nuget-group/FindPackagesById()?id='PRISM.Core.FulfillmentMicroService'&semVerLevel=2.0.0
Package "PRISM.Core.FulfillmentMicroService.0.0.77" is already installed.

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.3/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 1m 6s
3 actionable tasks: 3 executed
PS C:\Users\cbongiorno\dev\sterling\prism\prism-adaptors> ls | select -Last 20


    Directory: C:\Users\cbongiorno\dev\sterling\prism\prism-adaptors


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        7/14/2020  10:08 PM                gradle
d-----        7/21/2020   3:36 PM                infra
d-----        5/13/2020   4:17 AM                PRISM-Adaptor-QC_LA_SaintBernard
d-----        8/25/2020   4:25 PM                PRISM.Core.FulfillmentMicroService.0.0.77
d-----        7/14/2020   8:07 AM                routeadaptor-apinationwide_msmj1_dre
d-----        5/13/2020   4:18 AM                serviceadaptor-cwi_ky_statewide_smd
d-----        5/13/2020   4:18 AM                serviceadaptor-cwi_ny_statewide
d-----        5/13/2020   4:18 AM                serviceadaptor-qc_ny_statewide
-a----        7/14/2020   8:07 AM           2581 .gitattributes
-a----        8/25/2020   4:22 PM          13996 .gitignore
-a----        8/25/2020   4:51 PM           6653 build.gradle.kts
-a----        7/21/2020   3:36 PM            987 Dockerfile
-a----        8/14/2020   4:28 PM            206 gradle.properties
-a----        7/14/2020  10:08 PM           5764 gradlew
-a----        7/14/2020  10:08 PM           2942 gradlew.bat
-a----        7/21/2020   3:36 PM           2222 Jenkinsfile
-a----        5/13/2020   4:17 AM          11565 PRISM-Adaptors.txt
-a----        7/14/2020   8:07 AM            722 README.md
-a----        8/25/2020   3:24 PM            416 settings.gradle.kts
-a----        7/14/2020   8:07 AM            978 sh.exe.stackdump


PS C:\Users\cbongiorno\dev\sterling\prism\prism-adaptors> ls .\adaptor-cwi_ky_statewide_smd\


    Directory: C:\Users\cbongiorno\dev\sterling\prism\prism-adaptors\adaptor-cwi_ky_statewide_smd


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        7/21/2020   1:24 PM                build
d-----        7/21/2020   1:14 PM                CWI_KY_STATEWIDE_SMDSol
-a----        5/13/2020   4:17 AM           2581 .gitattributes


PS C:\Users\cbongiorno\dev\sterling\prism\prism-adaptors>

at the end I see to basically need to replicate this code (example):

nuget.exe restore CWI_TN_DAVIDSON_SMDSol\CWI_TN_DAVIDSON_SMDSol.sln -source "https://www.nuget.org/api/v2;http://prism-prod-oct/NuGetLocalServer/nuget"
MSBuild.exe C:\PRISMGit\adaptor-cwi_tn_davidson_smd\CWI_TN_DAVIDSON_SMDSol\CWI_TN_DAVIDSON_SMDSol.sln /t:Clean /p:Configuration=Release
MSBuild.exe C:\PRISMGit\adaptor-cwi_tn_davidson_smd\CWI_TN_DAVIDSON_SMDSol\CWI_TN_DAVIDSON_SMDSol.sln /t:Rebuild /p:Configuration=Release

Set-Location C:\PRISMGit\adaptor-cwi_tn_davidson_smd\CWI_TN_DAVIDSON_SMDSol\PRISM.Adaptor.CWI.TN.DAVIDSON.SMD\

$Package = Find-Package -Name PRISM.Core.FulfillmentMicroService -Source http://prism-prod-oct/NuGetLocalServer/nuget

NuGet.exe install $Package.Name  -Version $Package.Version -source http://prism-prod-oct/NuGetLocalServer/nuget

Copy-Item -Path .\bin\Release\PRISMDataAdaptor.dll -Destination (".\"+$Package.Name+ "." + $Package.Version+"\")   -Recurse -Force

Upvotes: 0

Views: 152

Answers (2)

Christian Bongiorno
Christian Bongiorno

Reputation: 5648

Ok, thanks to @dwursteisen for putting me on the right track. Solving this specific problem required use of delegation:

      val cpfms by register<Copy>("cpFms") {
            dependsOn(rootProject.tasks.named("pullfms"))

            val from by lazy {
                rootProject.projectDir.listFiles()
                        .find { it.isDirectory() && it.name.toLowerCase().startsWith("prism.core.fulfillmentmicroservice") }
            }
            from(from)
            into(projectDir)
        }

This now, actually works. However, the above sample may actually be better as it's just simply less complicated.

Upvotes: 0

dwursteisen
dwursteisen

Reputation: 11515

Regarding the configuration of your copy task:

val cpfms by register<Copy>("cpFms") {
    dependsOn(rootProject.tasks.named("pullfms"))
    doLast {
        // ...
        from(from)
        into(projectDir)
    }
}

val cpdll by register<Copy>("cpFmsdll") {
    dependsOn(cpfms,build)
    doLast {
        // …
        from(from)
        into(to)
    }
}

The issue is that you configuration your copy task during the its execution. So I think your tasks is not configured and then not executed.

You can see an example of configuration in the Gradle Copy documentation. There is no doLast part.

Your copy task are relying on file/directory that are created by an earlier task. I think you should considered that the file will be here and use include from regarding a pattern and not regarding that files are really here (which may explain why you used doLast)

Example:

val cpfms by register<Copy>("cpFms") {
    dependsOn(rootProject.tasks.named("pullfms"))
    from("**/PRISMDataAdaptor.dll")
    into("prism.core.fulfillmentmicroservice")
}

Upvotes: 1

Related Questions