From 07a31a052c9e569fd8fd32fef0ebc98f2aca29d9 Mon Sep 17 00:00:00 2001 From: Hanxiao Liu Date: Tue, 20 Apr 2021 13:41:15 +0800 Subject: [PATCH] Update archetype to keep consistent behavior with `func init` (#170) * Bump version * Using velocity template for dynamtic content * Throwing exception instead of show warning in post script * Make -Dtriger case insensitive and support parameter without 'Trigger' * Remove unused file and fix issue when path contains space * Fix inline expression issue * Fix default pricingtier for docker archetype --- azure-functions-archetype/pom.xml | 2 +- .../META-INF/archetype-post-generate.groovy | 85 ++++----- .../META-INF/maven/archetype-metadata.xml | 1 - .../archetype-resources/pom-docker.xml | 162 ------------------ .../resources/archetype-resources/pom.xml | 30 +++- pom.xml | 2 +- 6 files changed, 57 insertions(+), 225 deletions(-) delete mode 100644 azure-functions-archetype/src/main/resources/archetype-resources/pom-docker.xml diff --git a/azure-functions-archetype/pom.xml b/azure-functions-archetype/pom.xml index 942ccf2..14bbb19 100644 --- a/azure-functions-archetype/pom.xml +++ b/azure-functions-archetype/pom.xml @@ -6,7 +6,7 @@ com.microsoft.azure azure-functions-archetype - 1.37 + 1.38-SNAPSHOT jar Maven Archetype for Azure Functions diff --git a/azure-functions-archetype/src/main/resources/META-INF/archetype-post-generate.groovy b/azure-functions-archetype/src/main/resources/META-INF/archetype-post-generate.groovy index a676b0a..eaedaf8 100644 --- a/azure-functions-archetype/src/main/resources/META-INF/archetype-post-generate.groovy +++ b/azure-functions-archetype/src/main/resources/META-INF/archetype-post-generate.groovy @@ -1,42 +1,23 @@ import java.nio.file.Files import java.nio.file.StandardCopyOption; -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; import java.nio.file.Paths; -def isDocker = request.getProperties().get("docker") -def artifactId = request.getProperties().get("artifactId") -def dockerFile = "Dockerfile" -def pomFile = "pom.xml" -def dockerPomFile = "pom-docker.xml" def rootDir = Paths.get(request.getOutputDirectory(), artifactId).toFile() -if (Boolean.valueOf(isDocker)) { - // For docker, replace origin pom.xml with pom-docker.xml - Files.move(new File(rootDir, dockerPomFile).toPath(), new File(rootDir, pomFile).toPath(), StandardCopyOption.REPLACE_EXISTING) -} else { - // Otherwise, remove Dockerfile and pom-docker.xml - Files.deleteIfExists(new File(rootDir, dockerFile).toPath()) - Files.deleteIfExists(new File(rootDir, dockerPomFile).toPath()) -} -def appName = request.getProperties().get("appName") -def javaVersion = request.getProperties().get("javaVersion") -def pom = Paths.get(request.getOutputDirectory(), artifactId, pomFile).toFile() -def pomText = pom.text -// Update java compile version & java runtime version -// Supported values are 8/11, otherwise will use java 8 by default -pomText = pomText.replaceFirst(".*", String.format("%s", "11".equals(javaVersion) ? "11" : "1.8")) -pomText = pomText.replaceFirst(".*", String.format("%s", "11".equals(javaVersion) ? "11" : "8")) -// If user didn't modify appName, replace the expression with real value -// Set the values here as users will get prompt if we use expressions in of in archetype metadata -if (appName == null || appName.equals("\$(artifactId)-\$(timestamp)")) { - def finalAppName = String.format("%s-%s", artifactId, LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"))) - // Replace the string directly as use XmlNodePrinter will break origin file style and comments - pomText = pomText.replace("\$(artifactId)-\$(timestamp)", String.format("%s", finalAppName)) +def isDocker = request.getProperties().get("docker") +if (!Boolean.valueOf(isDocker)) { + def artifactId = request.getProperties().get("artifactId") + def dockerFile = "Dockerfile" + Files.deleteIfExists(new File(rootDir, dockerFile).toPath()) // Delete Dockerfile for none-docker project } -pom.text = pomText def trigger = request.getProperties().get("trigger"); +if ("HttpTrigger".equalsIgnoreCase(trigger) || "Http".equalsIgnoreCase(trigger)) { + return +} +// Remove origin source dir which contains unused test cases +def sourceFolder = new File(rootDir, "src") +sourceFolder.deleteDir() // todo: remove the parameter with default values, may need to update maven plugin def templateMap = [ "BlobTrigger" : "-Dfunctions.template=BlobTrigger -Dconnection=\"\" -Dpath=mycontainer", @@ -48,28 +29,24 @@ def templateMap = [ "ServiceBusQueueTrigger": "-Dfunctions.template=ServiceBusQueueTrigger -Dconnection=\"\" -DqueueName=mysbqueue", "ServiceBusTopicTrigger": "-Dfunctions.template=ServiceBusTopicTrigger -Dconnection=\"\" -DtopicName=mysbtopic -DsubscriptionName=mysubscription", ]; -if (!"HttpTrigger".equalsIgnoreCase(trigger)) { - println("Generating trigger from template, please replace the values with placeholder in annotation with real value if necessary") - def triggerParameter = templateMap.get(trigger) - if (triggerParameter == null) { - println(String.format("Invalid trigger type, supported values are %s and HttpTrigger, using HttpTrigger by default", String.join(",", templateMap.keySet()))); - return - } - def sourceFolder = new File(rootDir, "src") - sourceFolder.deleteDir(); // Remove origin source dir which contains unused test cases - def isWindows = System.properties['os.name'].toLowerCase().contains('windows') - def starter = isWindows ? "cmd.exe" : "/bin/sh" - def switcher = isWindows ? "/c" : "-c" - def command = "mvn azure-functions:add -f ${pom.getAbsolutePath()} -Dfunctions.package=${request.getProperties().get("groupId")} -Dfunctions.name=Function ${triggerParameter} -B" - if (!isWindows) { - command = command.replace("\$", "\\\$") - } - def output = new StringBuilder() - def proc = [starter, switcher, command].execute(); - proc.consumeProcessOutput(output, output) - proc.waitForOrKill(60 * 1000); // wait for 60s - if (proc.exitValue() != 0 || output == null || !output.contains("BUILD SUCCESS")) { - println("Failed to generate target trigger, please run `mvn azure-functions:add` manually in project root") - println("Output: \n${output}") - } +def triggerParameter = templateMap.keySet().stream() + .filter({ key -> key.equalsIgnoreCase(trigger) || key.substring(0, key.lastIndexOf("Trigger")).equalsIgnoreCase(trigger) }).findFirst() + .map(templateMap.&get) + .orElseThrow({ -> new RuntimeException(String.format("Invalid trigger type `%s`, supported values are %s and HttpTrigger", trigger, String.join(",", templateMap.keySet()))) }) +println("Generating trigger from template, please replace the values with placeholder in annotation with real value if necessary") +def pomFile = new File(rootDir, "pom.xml") +def isWindows = System.properties['os.name'].toLowerCase().contains('windows') +def starter = isWindows ? "cmd.exe" : "/bin/sh" +def switcher = isWindows ? "/c" : "-c" +def command = "mvn azure-functions:add -f \"${pomFile.getAbsolutePath()}\" -Dfunctions.package=\"${request.getProperties().get("groupId")}\" -Dfunctions.name=\"Function\" ${triggerParameter} -B" +if (!isWindows) { + command = command.replace("\$", "\\\$") +} +def output = new StringBuilder() +def proc = [starter, switcher, command].execute(); +proc.consumeProcessOutput(output, output) +proc.waitForOrKill(60 * 1000); // wait for 60s +if (proc.exitValue() != 0 || output == null || !output.contains("BUILD SUCCESS")) { + println("${output}") + throw new RuntimeException("Failed to generate target trigger, please run `mvn azure-functions:add` manually in project root") } diff --git a/azure-functions-archetype/src/main/resources/META-INF/maven/archetype-metadata.xml b/azure-functions-archetype/src/main/resources/META-INF/maven/archetype-metadata.xml index c3ef40d..65e4def 100644 --- a/azure-functions-archetype/src/main/resources/META-INF/maven/archetype-metadata.xml +++ b/azure-functions-archetype/src/main/resources/META-INF/maven/archetype-metadata.xml @@ -60,7 +60,6 @@ *.json .gitignore Dockerfile - pom-docker.xml diff --git a/azure-functions-archetype/src/main/resources/archetype-resources/pom-docker.xml b/azure-functions-archetype/src/main/resources/archetype-resources/pom-docker.xml deleted file mode 100644 index d84e8d1..0000000 --- a/azure-functions-archetype/src/main/resources/archetype-resources/pom-docker.xml +++ /dev/null @@ -1,162 +0,0 @@ - - - 4.0.0 - - ${groupId} - ${artifactId} - ${version} - jar - - Azure Java Functions - - - UTF-8 - 1.8 - 1.10.1 - 1.4.2 - ${appName} - ${project.build.directory}/azure-functions/${functionAppName} - - - - - com.microsoft.azure.functions - azure-functions-java-library - ${azure.functions.java.library.version} - - - - - org.junit.jupiter - junit-jupiter - 5.4.2 - test - - - - org.mockito - mockito-core - 2.23.4 - test - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.8.1 - - ${java.version} - ${java.version} - ${project.build.sourceEncoding} - - - - com.microsoft.azure - azure-functions-maven-plugin - ${azure.functions.maven.plugin.version} - - - ${functionAppName} - - ${resourceGroup} - - ${appServicePlanName} - - - ${appRegion} - - - EP1 - - - - - - docker - [hub-user/]repo-name[:tag] - - - - - - FUNCTIONS_EXTENSION_VERSION - ~3 - - - - - - package-functions - - package - - - - - - org.apache.maven.plugins - maven-resources-plugin - 3.1.0 - - - copy-resources - package - - copy-resources - - - true - ${stagingDirectory} - - - ${project.basedir} - - host.json - local.settings.json - - - - - - - - - org.apache.maven.plugins - maven-dependency-plugin - 3.1.1 - - - copy-dependencies - prepare-package - - copy-dependencies - - - ${stagingDirectory}/lib - false - false - true - runtime - azure-functions-java-library - - - - - - - maven-clean-plugin - 3.1.0 - - - - obj - - - - - - - diff --git a/azure-functions-archetype/src/main/resources/archetype-resources/pom.xml b/azure-functions-archetype/src/main/resources/archetype-resources/pom.xml index 9d6e5bc..b35d7e4 100644 --- a/azure-functions-archetype/src/main/resources/archetype-resources/pom.xml +++ b/azure-functions-archetype/src/main/resources/archetype-resources/pom.xml @@ -11,10 +11,18 @@ UTF-8 +#if(${javaVersion}=="11") + 11 +#else 1.8 +#end 1.10.1 1.4.2 +#if(${appName}=="$(artifactId)-$(timestamp)") + ${artifactId.toLowerCase()}-${package.getClass().forName("java.time.LocalDateTime").getMethod("now").invoke(null).format($package.Class.forName("java.time.format.DateTimeFormatter").getMethod("ofPattern", $package.Class).invoke(null, "yyyyMMddHHmmssSSS"))} +#else ${appName} +#end ${project.build.directory}/azure-functions/${functionAppName} @@ -69,19 +77,29 @@ ${appRegion} +#if(${docker}==true) + EP1 +#else - +#end +#if(${docker}==true) + docker + [hub-user/]repo-name[:tag] + + +#else windows - ${javaVersion} - - - - +#if(${javaVersion}=="11") + 11 +#else + 8 +#end +#end diff --git a/pom.xml b/pom.xml index 9c3114e..02ce32d 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.microsoft.azure azure-maven-archetypes - 1.37-SNAPSHOT + 1.38-SNAPSHOT pom Maven Archetypes for Azure Maven Archetypes for Microsoft Azure services