Dynamic gradle command for Exec task fails - java

I am trying to publish Lambda zip file to s3- s3://my-aws-lambda/<projectName>/[release|SNAPSHOT]/. The task defined below, publishToS3 fails with the message
Caused by: java.io.FileNotFoundException: /Users/me/my-lambda/build/distributions
when I run
./gradlew clean build -x test -x release
Appreciate any help. Thanks.
task buildZip(type: Zip) {
from compileJava
from processResources
into('lib') {
from configurations.runtimeClasspath
}
}
task publishToS3(type: Exec, dependsOn: buildZip) {
onlyIf { file("${project.buildDir}/distributions").exist() }
def artifacts = new FileNameByRegexFinder().getFileNames("${project.buildDir}/distributions", /.*\.zip/)
assert artifacts.size() == 1
def isSnapShot = artifacts[0].endsWith('-SNAPSHOT.zip')
def releaseCmd = ("aws s3 cp " +
"${artifacts[0]} " +
"s3://my-aws-lambdas/${project.name}/${isSnapShot ? 'SNAPSHOT' : 'release'}/ ").trim().tokenize(' ') as List
workingDir "${project.buildDir}/distributions"
commandLine releaseCmd
}
build.dependsOn buildZip

If you are on UNIX environment, you can use the find command to search the files and use it's output.
task scanFiles() {
def a= "find ${project.buildDir} -name *.zip".execute().text
String[] files=a.split('\n')
if(files.length == 1){
println("We'v got a file :"+a)
}
else if(files.length ==0){
println("We've not no files ")
}
else{
println("We've got multiple files\n"+a)
}
}

Related

Generate YML file from Jenkinsfile

I'm learning Jenkinsfile and I'm trying to generate a YML file from the variables but it dosn't work properly.
pipeline {
agent any
parameters{
choice(
choices: ['Test1', 'Test2', 'Test3'],
name: 'select_parameter'
)
string(
defaultValue: 'string',
name: 'STRING_PARAMETER',
trim: true
)
def amap = ['select_parameter': ${params.select_parameter}]
writeYaml file: 'datas.yaml', data: amap
def read = readYaml file: 'datas.yaml'
assert read.choice == ${params.select_parameter}
}
stages {
stage('Build') {
steps {
echo 'Building..'
}
}
stage('Test') {
steps {
echo 'Test prueba'
}
}
stage('Deploy') {
steps {
echo 'Deploying....'
}
}
}
}
Can someone help me? I've read this documentation https://www.jenkins.io/doc/pipeline/steps/pipeline-utility-steps/
Use something like below and put it in a script block.
def filename = 'datas.yaml'
def data = readYaml file: filename
// Change something in the file
data.image.tag = applicationVersion
sh "rm $filename"
writeYaml file: filename, data: data

Differentiate Dependency based on profile in Gradle file

I wanted to differentiate my dependencies based on different environments. I'm not getting correct env value along with correct conditions to categories dependency.
def profile = project.hasProperty("spring.profiles.active") ?
project.property("spring.profiles.active") :
System.getProperty("spring.profiles.active", 'local')
bootRun {
systemProperty "spring.profiles.active", profile
}
I expect the output something like below but profile variable is not getting correct profile value
dependencies{
if(profile == "dev"){
compile('com.oracle:ojdbc6:+')
}
if(profile == "prod"){
compile('commons-dbcp:commons-dbcp:1.4')
}}
You can proceed differently by having build_[profile].gradle for each profile, where [profile] is the profile you pass as argument when you launch your application example ( via -P ) :
./gradlew -Pprod bootRun
So suppose you have 2 environment prod and local, in your build.gradle you will have :
def currentProfile;
if (project.hasProperty('prod')) {
currentProfile = 'production';
apply from: rootProject.file('gradle/build_prod.gradle');
} else if (project.hasProperty('local')) {
currentProfile = 'local';
apply from: rootProject.file('gradle/build_local.gradle');
} else {
currentProfile = 'default profile';
apply from: rootProject.file('gradle/build_default.gradle');
}
println 'Current profile: "' + currentProfile + '"
You should also have 2 gradle files; build_prod.gradle and build_local.gradle, and there you could have different dependencies and configuration as you want.

Unable to create jar using AntBuilder in groovy

I am trying to create Ant Build of my project using groovy. I am getting "Caught: : jar doesn't support the "destdir" attribute" error while trying to create jar using AntBuilder in groovy.
My groovy file is as follows
build.groovy
package com.groovy.core.utils
import groovy.util.AntBuilder
class build {
def ant = new groovy.util.AntBuilder()
def base_dir = "C:/Users/abc/neon/GroovyAntDateUtils/"
def src_dir = base_dir + "src"
def lib_dir = "C:/Jars/groovy-2.4.12/lib"
def build_dir = base_dir + "com/groovy/core/utils"
def dist_dir = base_dir + "dist"
def file_name = "DateUtils"
/*, includeantruntime : "false"*/
static void main(args){
println("hi welcome to groovy");
def b = new build()
b.jar()
//b.run(args)
}
def classpath = ant.path {
fileset(dir: "${lib_dir}"){
include(name: "*.jar")
}
pathelement(path: "${build_dir}")
}
def clean(){
ant.delete(dir : "${build_dir}")
ant.delete(dir : "${dist_dir}")
}
def jar(){
clean()
buildFolder()
ant.mkdir(dir: "${dist_dir}")
ant.jar(destdir: "${dist_dir}/${file_name}.jar", basedir: "${build_dir}")
}
def compile(){
ant.javac(destdir: "${build_dir}", srcdir: "${src_dir}", classpath: "${classpath}")
}
def buildFolder(){
ant.mkdir(dir: "${build_dir}")
compile()
}
}
JDK - 1.8.0_121
ant & ant launcher - 1.9.0
groovy - 2.4.12
there is no destdir attribute for jar task
https://ant.apache.org/manual/Tasks/jar.html
probably you want destfile ?

build spring framework source code encounter an error

I fetched the spring framework source code from github by using git clone command. When I build the source code by using gradle build in the source code directory,it takes a long time to download dependencies and compile the java code,but failed with an exception.The output is below.
:spring-webmvc-portlet:sourcesJar UP-TO-DATE
:spring-webmvc-tiles2:javadoc SKIPPED
:spring-webmvc-tiles2:javadocJar SKIPPED
:spring-webmvc-tiles2:sourcesJar SKIPPED
:spring-websocket:javadoc UP-TO-DATE
:spring-websocket:javadocJar UP-TO-DATE
:spring-websocket:sourcesJar UP-TO-DATE
:distZip
FAILURE: Build failed with an exception.
* What went wrong:
Failed to capture snapshot of input files for task 'distZip' during up-to-date c
heck. See stacktrace for details.
> java.io.FileNotFoundException: C:\Users\yuqing\workspace\spring-framework\buil
d\distributions\spring-framework-4.3.0.BUILD-SNAPSHOT-schema.zip
* Try:
Run with --info or --debug option to get more log output.
* Exception is:
org.gradle.api.UncheckedIOException: Failed to capture snapshot of input files f
or task 'distZip' during up-to-date check. See stacktrace for details.
at org.gradle.api.internal.changedetection.rules.TaskUpToDateState.<init
>(TaskUpToDateState.java:60)
at org.gradle.api.internal.changedetection.changes.DefaultTaskArtifactSt
ateRepository$TaskArtifactStateImpl.getStates(DefaultTaskArtifactStateRepository
.java:132)
at org.gradle.api.internal.changedetection.changes.DefaultTaskArtifactSt
ateRepository$TaskArtifactStateImpl.isUpToDate(DefaultTaskArtifactStateRepositor
y.java:70)
at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.exec
ute(SkipUpToDateTaskExecuter.java:52)
at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execut
e(ValidatingTaskExecuter.java:58)
at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecu
ter.execute(SkipEmptySourceFilesTaskExecuter.java:52)
at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter
.execute(SkipTaskWithNoActionsExecuter.java:52)
at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execut
e(SkipOnlyIfTaskExecuter.java:53)
at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter
.execute(ExecuteAtMostOnceTaskExecuter.java:43)
at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTa
skWorker.execute(DefaultTaskGraphExecuter.java:203)
at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTa
skWorker.execute(DefaultTaskGraphExecuter.java:185)
at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorW
orker.processTask(AbstractTaskPlanExecutor.java:66)
at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorW
orker.run(AbstractTaskPlanExecutor.java:50)
at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor.process(Defaul
tTaskPlanExecutor.java:25)
at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter.execute(Defau
ltTaskGraphExecuter.java:110)
at org.gradle.execution.SelectedTaskExecutionAction.execute(SelectedTask
ExecutionAction.java:37)
at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecute
r.java:37)
at org.gradle.execution.DefaultBuildExecuter.access$000(DefaultBuildExec
uter.java:23)
at org.gradle.execution.DefaultBuildExecuter$1.proceed(DefaultBuildExecu
ter.java:43)
at org.gradle.execution.DryRunBuildExecutionAction.execute(DryRunBuildEx
ecutionAction.java:32)
at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecute
r.java:37)
at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecute
r.java:30)
at org.gradle.initialization.DefaultGradleLauncher$4.run(DefaultGradleLa
uncher.java:154)
at org.gradle.internal.Factories$1.create(Factories.java:22)
at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(Defaul
tBuildOperationExecutor.java:90)
at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(Defaul
tBuildOperationExecutor.java:52)
at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(Default
GradleLauncher.java:151)
at org.gradle.initialization.DefaultGradleLauncher.access$200(DefaultGra
dleLauncher.java:32)
at org.gradle.initialization.DefaultGradleLauncher$1.create(DefaultGradl
eLauncher.java:99)
at org.gradle.initialization.DefaultGradleLauncher$1.create(DefaultGradl
eLauncher.java:93)
at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(Defaul
tBuildOperationExecutor.java:90)
at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(Defaul
tBuildOperationExecutor.java:62)
at org.gradle.initialization.DefaultGradleLauncher.doBuild(DefaultGradle
Launcher.java:93)
at org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLaun
cher.java:82)
at org.gradle.launcher.exec.InProcessBuildActionExecuter$DefaultBuildCon
....
Caused by: org.gradle.api.UncheckedIOException: java.io.FileNotFoundException: C
:\Users\yuqing\workspace\spring-framework\build\distributions\spring-framework-4
.3.0.BUILD-SNAPSHOT-schema.zip (系统找不到指定的文件。)
at org.gradle.internal.hash.HashUtil.createHash(HashUtil.java:39)
at org.gradle.api.internal.hash.DefaultHasher.hash(DefaultHasher.java:24
)
at org.gradle.api.internal.changedetection.state.CachingFileSnapshotter.
snapshot(CachingFileSnapshotter.java:57)
at org.gradle.api.internal.changedetection.state.CachingFileSnapshotter.
snapshot(CachingFileSnapshotter.java:46)
at org.gradle.api.internal.changedetection.state.CachingFileSnapshotter.
snapshot(CachingFileSnapshotter.java:29)
at org.gradle.api.internal.changedetection.state.DefaultFileCollectionSn
apshotter$1.run(DefaultFileCollectionSnapshotter.java:70)
at org.gradle.internal.Factories$1.create(Factories.java:22)
at org.gradle.cache.internal.DefaultCacheAccess.useCache(DefaultCacheAcc
ess.java:192)
at org.gradle.cache.internal.DefaultCacheAccess.useCache(DefaultCacheAcc
ess.java:175)
at org.gradle.cache.internal.DefaultPersistentDirectoryStore.useCache(De
faultPersistentDirectoryStore.java:106)
at org.gradle.cache.internal.DefaultCacheFactory$ReferenceTrackingCache.
useCache(DefaultCacheFactory.java:187)
at org.gradle.api.internal.changedetection.state.DefaultTaskArtifactStat
eCacheAccess.useCache(DefaultTaskArtifactStateCacheAccess.java:60)
at org.gradle.api.internal.changedetection.state.DefaultFileCollectionSn
apshotter.snapshot(DefaultFileCollectionSnapshotter.java:62)
at org.gradle.api.internal.changedetection.rules.TaskUpToDateState.<init
>(TaskUpToDateState.java:56)
... 57 more
Caused by: java.io.FileNotFoundException: C:\Users\yuqing\workspace\spring-frame
work\build\distributions\spring-framework-4.3.0.BUILD-SNAPSHOT-schema.zip
at org.gradle.internal.hash.HashUtil.createHash(HashUtil.java:34)
... 70 more
BUILD FAILED
Total time: 20.861 secs
The error indicates the file spring-framework-4.3.0.BUILD-SNAPSHOT-schema.zip not found.I wondered why this file is not found,it should be created by distZip task but it doesn't.
I have googled this problem but not found any question about this.
I have resovled this problem,the devil is path separator.Because this command runs on windows.
Modify schemaZip task definition from
task schemaZip(type: Zip) {
group = "Distribution"
baseName = "spring-framework"
classifier = "schema"
description = "Builds -${classifier} archive containing all " +
"XSDs for deployment at http://springframework.org/schema."
duplicatesStrategy 'exclude'
moduleProjects.each { subproject ->
def Properties schemas = new Properties();
subproject.sourceSets.main.resources.find {
it.path.endsWith("META-INF/spring.schemas")
}?.withInputStream { schemas.load(it) }
for (def key : schemas.keySet()) {
def shortName = key.replaceAll(/http.*schema.(.*).spring-.*/, '$1')
assert shortName != key
File xsdFile = subproject.sourceSets.main.resources.find {
it.path.endsWith(schemas.get(key))
}
assert xsdFile != null
into (shortName) {
from xsdFile.path
}
}
}
}
to
task schemaZip(type: Zip) {
group = "Distribution"
baseName = "spring-framework"
classifier = "schema"
description = "Builds -${classifier} archive containing all " +
"XSDs for deployment at http://springframework.org/schema."
duplicatesStrategy 'exclude'
moduleProjects.each { subproject ->
def Properties schemas = new Properties();
subproject.sourceSets.main.resources.find {
it.path.endsWith("META-INF\\spring.schemas")
}?.withInputStream { schemas.load(it) }
for (def key : schemas.keySet()) {
def shortName = key.replaceAll(/http.*schema.(.*).spring-.*/, '$1')
assert shortName != key
File xsdFile = subproject.sourceSets.main.resources.find {
it.path.endsWith(schemas.get(key).replaceAll('\\/','\\\\'))
}
assert xsdFile != null
into (shortName) {
from xsdFile.path
}
}
}
}

Gradle creating duplicate start scripts into bin directory

I am trying to create multiple start script files through gradle. But somehow one particular start script file is getting duplicated.
startScripts.enabled = false
run.enabled = false
def createScript(project, mainClass, name) {
project.tasks.create(name: name, type: CreateStartScripts) {
outputDir = new File(project.buildDir, 'scripts')
mainClassName = mainClass
applicationName = name
classpath = jar.outputs.files + project.configurations.runtime
doLast {
def windowsScriptFile = file getWindowsScript()
def unixScriptFile = file getUnixScript()
windowsScriptFile.text = windowsScriptFile.text.replace('%APP_HOME%\\lib\\conf', '%APP_HOME%\\conf')
unixScriptFile.text = unixScriptFile.text.replace('$APP_HOME/lib/conf', '$APP_HOME/conf')
}
}
project.tasks[name].dependsOn(project.jar)
project.applicationDistribution.with {
into("bin") {
from(project.tasks[name])
fileMode = 0755
}
}
}
// Call this for each Main class you want to expose with an app script
createScript(project, 'com.main.A', 'A')
createScript(project, 'com.main.B', 'B')
in bin directory I can see,
A.sh
A.sh
A.bat
A.bat
B.sh
B.bat
What am I missing here? How to fix this?
Thank you for help.
I solved this problem. Actually it was a mistake from my side and thanks to #Opal. I somehow forgot to delete 'mainClassName="com.main.A"' line from the header.
Also I have to add
distZip {
duplicatesStrategy = 'exclude'
}

Categories