I am trying to deploy my Java app to aws lambda.
I created a jar file and upload it to s3 and used that to create the lambda function
Handler com.api.helloWorld::doRequest
I used the following in my build.gradle
```id 'com.github.johnrengelman.shadow' version '5.0.0'```
and created the zip file using
task buildZip(type: Zip) {
from compileJava
from processResources
into('/Users/Projects/helloWorld_api') {
from configurations.runtimeClasspath
}
}
But I am getting the following error on AWS console :
Error loading class com.api.HelloWorld: javax/servlet/http/HttpServlet: java.lang.NoClassDefFoundError
java.lang.NoClassDefFoundError: javax/servlet/http/HttpServlet
Related
I created a custom plugin using gradlew init --> selected plugin option. This created all the required dir structure and and one plugingroovy file which has the custom plugin class implementing Plugin interface and an apply() method. I successfully built and published it to my local maven reposistory using "gradlew publishToMavenLocal" and imported that plugin in other gradle projects Like below. When i tried to build the other project it printed "Hello World" perfectly.
Now I want to put my actual logic in it, for which I am getting filenot found exception. I kept the file under src/main/resources/coco.gradle and built the jar. Opened the jar file with 7zip ad also verified, plugin jar has the coco.gradle in it.
package com.jink.test
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.OutputFile
class FileDiffPlugin implements Plugin<Project> {
#Override
void apply(Project project) {
project.tasks.create('javaCoco') {
def file = new File('src/main/resources/coco.gradle')
ant.loadfile(srcFile: file , property: file.name)
println "Hello World"
}
}
}
I am getting filenotfound exception as it trying to locate that file in the directory of the project from where I am running but not from the jar.
* What went wrong:
An exception occurred applying plugin request [id: 'com.tomgregory.file-diff', version: '0.0.1-SNAPSHOT']
> Failed to apply plugin 'com.jinka.test'.
> E:\jinka\testgradle\src\main\resources\coco.gradle doesn't exist
coco.gradle has below code
apply plugin: "jacoco"
allprojects {
jacoco {
toolVersion = "0.8.2"
}
test {
jacoco {
destinationFile = file("$buildDir/../target/jacoco.exec")
}
jacocotestReport {
reports{
true xml.enabled
true html.enabled
}
}
}
I want to enclose the above jacoco gradle code within one custom plugin and ask teams to use that custom plugin instead of the entire code in build.gradle or the file.
./gradle -Prun=com.foo.Bar
./gradle -Prun=com.foo.Baz
Unable to run different class files for a java application in gradle as mentioned above using the following definition. What am I missing here
application {
mainClassName = project.findProperty("run").toString()
}
I have been using a Beam pipeline examples as a guide in an attempt to load files from S3 for my pipeline. Like in the examples I have defined my own PipelineOptions that also extends S3Options and I am attempting to use the DefaultAWSCredentialsProviderChain. The code to configure this is:
MyPipelineOptions options = PipelineOptionsFactory.fromArgs(args).as(MyPipelineOptions.class);
options.setAwsCredentialsProvider(new DefaultAWSCredentialsProviderChain());
options.setAwsRegion("us-east-1");
runPipeline(options);
When I run it from Intellij it works fine using the Direct Runner
but when I package it as a jar and it execute it (also using the Direct Runner) I see:
Exception in thread "main" java.lang.IllegalArgumentException: PipelineOptions specified failed to serialize to JSON.
at org.apache.beam.runners.direct.DirectRunner.run(DirectRunner.java:166)
at org.apache.beam.runners.direct.DirectRunner.run(DirectRunner.java:67)
at org.apache.beam.sdk.Pipeline.run(Pipeline.java:313)
at org.apache.beam.sdk.Pipeline.run(Pipeline.java:299)
at a.b.c.beam.CleanSkeleton.runPipeline(CleanSkeleton.java:69)
at a.b.c.beam.CleanSkeleton.main(CleanSkeleton.java:53)
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Unexpected IOException (of type java.io.IOException): Failed to serialize and deserialize property 'awsCredentialsProvider' with value 'com.amazonaws.auth.DefaultAWSCredentialsProviderChain#40f33492'
at com.fasterxml.jackson.databind.JsonMappingException.fromUnexpectedIOE(JsonMappingException.java:338)
at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsBytes(ObjectMapper.java:3247)
at org.apache.beam.runners.direct.DirectRunner.run(DirectRunner.java:163)
... 5 more
I am using gradle to build my jar with the following task:
jar {
manifest {
attributes (
'Main-Class': 'a.b.c.beam.CleanSkeleton'
)
}
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
from('src') {
include '/main/resources/*'
}
zip64 true
exclude 'META-INF/*.RSA', 'META-INF/*.SF', 'META-INF/*.DSA'
}
The problem was occuring because when the the fat/uber jar was being created, files in META-INF/serivces where being overwritten by duplicate files. Specifically com.fasterxml.jackson.databind.Module where a number of Jackson modules needed to be defined but where missing. These include org.apache.beam.sdk.io.aws.options.AwsModule and com.fasterxml.jackson.datatype.joda.JodaModule. The code in the DirectRunner instantiates the ObjectMapper like so :
new ObjectMapper()
.registerModules(ObjectMapper.findModules(ReflectHelpers.findClassLoader()));
ObjectMapper::findModules relies on java.util.ServiceLoader which locates services from META-INF/services/ files.
The solution was to use the gradle Shadow plugin to build the fat/uber jar and configure it to merge the services files:
apply plugin: 'com.github.johnrengelman.shadow'
shadowJar {
mergeServiceFiles()
zip64 true
}
I try to create gradle task with type JavaExec.
In gradle documentation I found than I need to aply java plugin. But this plugin is incompatible with android plugin.
If I'm not apply java plugin I receive this error:
Error: Could not find or load main class package.TestM
Example my task:
task testA(type: JavaExec) {
main = 'package.TestM'
}
If I create java library module with same task I receive same error:
Error: Could not find or load main class
Maybe I need set classpath variable? How can I get classpath in Java module in Android Studio?
task testA(type: JavaExec) {
classpath += files(getBuildDir().toString() + "/intermediates/classes/debug")
main = 'package.TestM'
}
maybe it is a good reason replace "debug" with a buildVariant variable.
I'm trying to do a http://spring.io/guides/gs/rest-service/ tutorial, and I did everything like it is in tutorial.
When I was trying to build with gradle with the gradle.build from the tutorial gradle build failed because of missing
springBoot {
mainClass = "main.java.hello.Application"
}
I did add it and now compilation start and finish correctly, but as soon as I'm trying to do
java -jar build/libs/gs-rest-service-0.1.0.jar
It throws an error
I have no idea what to do with it. Any help?
It should be hello.Application. main/java is a part of package name / project dir structure.
When added the following piece of code to build.gradle:
springBoot {
mainClass = "hello.Application"
}
both ./gradlew clean bootRun and ./gradlew clean build with java -jar build/libs/gs-rest-service-0.1.0.jar work well.
The above error is due to the build do not includes our Web RESTful Service main application class files into the gs-rest-service-0.1.0.jar file because of the src/main/java/hello, folder is not under the gradle build scope.
In order to avoid the above error or any other errors for https://spring.io/guides/gs/rest-service/ tutorial
Please follow the steps below.
My Folder structure as follows.
C:\MyWebService\src\main\java\hello
Put your build.gradle file under your main folder e.g "MyWebService" not in your hello or any other folder hence "gradle build' will be successful.
Using DOS cmd navigate to your main folder e.g C:\MyWebService\ where src should be the first sub folder.
Run the gradle commands.
gradle
gradle tasks
gradle wrapper
gradlew clean build -- final build
or gradlew clean bootRun -- run before build
You will find your gs-rest-service-0.1.0.jar under your C:\MyWebService\build\libs folder.
Finally invoke spring web service from main folder e.g C:\MyWebService\
java -jar build/libs/gs-rest-service-0.1.0.jar
To check Spring RESTful Web Service by hitting below url in the browser, JSON data will be returned.
http://localhost:8080/greeting
{"id":1,"content":"Hello, World!"}
Now you should be successful with completing the Spring RESTful Web Service tutorial.
N.B: Please do not modify your original build.gradle file provided
on the tutorial.
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'application'
Add the above highlighted line in the build.gradle