-1

i am developing spring boot microservices.
Then I generated a service-template and added a gradle wrapper.
Then I have a seperate bootstrap project with a gradle task wo copy the template to a generate a new project.
But if I copy the template, there appear errors with the gradle wrapper.
could not find or load main class org.gradle.wrapper.GradleWrapperMain

I think it's because I rename the project from "template" to "xyz"

This is the directory content after I copied it:
.gitignore
.gradle
README.md
build.gradle
gradle
gradle.properties
gradlew
gradlew.bat
src

My gradle task to copy the template:

task create(type: Copy) {

  def parentFolder = "../"
  def templateFolder = new File("$parentFolder/fmp-template")

  def serviceName = project.hasProperty('sn') ? "${sn}" : "dummy"
  def fmpServiceName = "fmp-${sn}"

  def newServiceFolder = new File("$parentFolder/${fmpServiceName}")

  def serviceNameReplaced = ""
  if ("${serviceName}".contains('-')) {
    serviceNameReplaced = "${serviceName}".replaceAll('-','')
  }
  else {
    serviceNameReplaced = serviceName
  }

  def servicePort = project.hasProperty('sp') ? sp : '9999'
  def maintainerName = "${defaultMaintainerName1}${defaultMaintainerName2}"
  def maintainerMail = "${defaultMaintainerMail}"
  maintainerName = getMaintainerName()
  maintainerMail = getMaintainerMail()

  // replace tokens in files
  filter ReplaceTokens, tokens: [
      'serviceName'           : serviceName.toString(),
      'serviceNameReplaced'   : serviceNameReplaced.toString(),
      'servicePort'           : servicePort.toString(),
      'maintainerName'        : maintainerName,
      'maintainerMail'        : maintainerMail,
  ]

  // copy template to new service
  from templateFolder
  into newServiceFolder

  // workaround because Copy Task is excluding some files
  doFirst {
    DirectoryScanner.defaultExcludes.each { DirectoryScanner.removeDefaultExclude it }
  }

  doLast {
    DirectoryScanner.resetDefaultExcludes()
    renameFolder("$parentFolder/${fmpServiceName}/src/main/java/de/microservices/fmp/@serviceNameReplaced@",
             "$parentFolder/${fmpServiceName}/src/main/java/de/microservices/fmp/${serviceNameReplaced}")
     renameFolder("$parentFolder/${fmpServiceName}/src/test/java/de/microservices/fmp/@serviceNameReplaced@",
             "$parentFolder/${fmpServiceName}/src/test/java/de/microservices/fmp/${serviceNameReplaced}")
    commandGitInit(newServiceFolder)
  }
}

My Question:
Is it possible to copy a gradle wrapper with a gradle task - and how?

Thank you.

Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219
m1well
  • 716
  • 4
  • 15
  • 28

2 Answers2

0

Also copy the gradle directory, not only the gradlew scripts.

Edit: After seeing your edit, I don't think it makes any sense for you to copy the wrapper, since in order to do that, you're using a Gradle task, which requires a local Gradle installation. Simply generate the wrapper by running gradle wrapper.

Tip: Next time you post a question, don't hold back useful information.

Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219
  • i also copied the `.gradle` directory. – m1well Mar 17 '18 at 20:53
  • @m1well Pardon me if I don't believe you. Post a reproducible project. – Abhijit Sarkar Mar 17 '18 at 20:54
  • sorry, I forgot to say that I copy it with a gradle task, not with the bash `cp` command. – m1well Mar 17 '18 at 21:01
  • @m1well You wrote a Gradle task to copy the Gradle wrapper? You must be paid by the hour :) Like I said, if you copied the directory with the executables, it works. If you claim it doesn't, show us. If you can't ,accept the answer. – Abhijit Sarkar Mar 17 '18 at 21:12
  • haha - nice humor :) now I didn't write a task for copying a gradle wrapper. i wrote a task to copy my microservice template (with some spring boot sources) and all work but the gradle wrapper exits with an error (see above). – m1well Mar 17 '18 at 21:26
  • @m1well - Initial triage of this is pretty straightforward - does your `.gradle` directory contain all of the files that you expect it to contain? – Oliver Charlesworth Mar 17 '18 at 21:55
  • yes It does - i know now the problem: copying the sources with gradle copy task corrupts the `gradle-wrapper.jar` - if i check the jar with `jar tvf gradle-wrapper.jar` there appears an error: `java.util.zip.ZipException: error in opening zip file` - so it is a problem with the gradle copy task - but I don't know why – m1well Mar 17 '18 at 22:20
0

sorry first for "wrong" question. but I thought this is a known behavior and someone has a solution.

again the problem in fact: the copy task in gradle has a problem with copying "zip/jar - files"

after a long research I have a solution now:
1. I excluded the jar file in the main copy task
2. I created a new copy task where I only copy the jar file

task copyGradleWrapper(type: Copy) {
  // copy gradle wrapper separately because of "unzipping problems"
  def templateWrapper = new File("../fmp-template/gradle/wrapper/gradle-wrapper.jar")
  def newServiceWrapper = new File("${newServiceFolder}/gradle/wrapper/gradle-wrapper.jar")
  from zipTree(templateWrapper)
  into newServiceWrapper
}

hopefully my solution answer is better than my question ;)

m1well
  • 716
  • 4
  • 15
  • 28