Including Google Maps API Library in EAR File? - java

I’m using https://github.com/googlemaps/google-maps-services-java in an app which has a WAR file contained in an EAR file deployed to a web server. Following the readme, I added:
repositories {
mavenCentral()
}
dependencies {
implementation ‘com.google.maps:google-maps-services:(insert latest version)’
implementation ‘org.slf4j:slf4j-simple:1.7.25’
}
to build.gradle for my WAR. This builds successfully and I can see the library included in my Eclipse environment. However, when I build the EAR and deploy it, I see:
java.lang.NoClassDefFoundError: com.google.maps.GeoApiContext$Builder
indicating to me that the dependency isn’t being included in the EAR. Presumably, I need to edit build.gradle for the EAR, but it’s not clear to me how to do this. Copying the same edits from the WAR build file causes the compilation to fail:
Could not find method implementation() for arguments [com.google.maps:google-maps-services:0.10.1] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
Could anyone explain to me how to tell Gradle to include this library in the EAR?

Related

How to include only specific jar from libs into gradle build?

I'm using gradle V4.10.2 and eclipse for building WAR files. I have some common JAR files for all the wars. I need them to use only during compile time and need to exclude them while building into WAR file. Also I don't want to exclude all the JARs file.
I've put all the JARs in libs folder. In build.gradle, I have specified only the JARs to be included. Since the exclude list is bigger then include list of JARs I'm not using exclude
war {
rootSpec.include("**/test.jar")
}
The above results the WAR file without META-INF folder. Hence, webapp is not started. I have tried adding the libs as reference lib in eclipse but that results in compilation error.
Execution failed for task ':compileJava'.
Compilation failed; see the compiler error output for details
So I have to include all the JARs in libs folder and then need to use only required JARs in the WAR built. How to include only specific JARs into build?
I strongly recommend reading about building Java projects with Gradle and in particular the part about web applications
In short you should add the JARs needed for compilation only to the compileOnly configuration and the JARs needed for compilation and at runtime in the implementation configuration.
Gradle will then use all of them for compilation and only include the ones in implementation when packaging the WAR.
While you can do that by selecting them from your libs folder, I strongly recommend moving to libraries and its metadata sourced from a repository, private or public, so that the dependency management is done for you.

Create a WAR from the java files using Gradle script

My directory sturcture is,
C:\Grapher\src\ *.java
C:\Grapher\lib\ *.jar
There are many java files and many jar dependencies. I want to create a tomcat deployable WAR.
Can any one suggest me how to create a WAR using a gradle script? What are the dependencies to be included in Gradle to create a WAR.
Have you read and understood https://docs.gradle.org/current/userguide/war_plugin.html?
The War plugin adds two dependency configurations named providedCompile and providedRuntime. Those two configurations have the same scope as the respective compile and runtime configurations, except that they are not added to the WAR archive. It is important to note that those provided configurations work transitively. Let's say you add commons-httpclient:commons-httpclient:3.0 to any of the provided configurations. This dependency has a dependency on commons-codec. Because this is a “provided” configuration, this means that neither of these dependencies will be added to your WAR, even if the commons-codec library is an explicit dependency of your compile configuration. If you don't want this transitive behavior, simply declare your provided dependencies like commons-httpclient:commons-httpclient:3.0#jar.
If you use providedCompile or providedRuntime, those dependencies will not be added to your war archive.
The folder structure you need to have in your project is:
app
|-src
|-main
|-java
|-webapp
In the java folder you add your own packages and classes. In the webapp folder you add web content if your application makes use of such items.

Spring Boot 1.5.3 -- IllegalStateException

After deploying my project in IDE, all ok. But when I make 'package' at Maven, and tried to execute 'war' archive at the console, I get this exception:
Unable to open nested entry
'WEB-INF/lib/spring-aspects-4.3.8.RELEASE.jar'. It has been compressed
and nested jar files must be stored without compression. Please check
the mechanism used to create your executable jar file
At Spring Boot 1.5.2 was all good.
Looks like spring-aspects-4.3.8.RELEASE.jar that inside WEB-INF/lib/ is not extracted after compilation and your application can't extract classes from *.jar when you executing app.
Use maven dependencies instead direct *.jar files or specified plugin that will extract classes from *.jar's and add them to your app.
If you can't find public repository that provide required dependencies then use this instruction to add your external JAR files into your local maven repository. Then just add such dependency and build app again.

Maven dependency can't find zip file for sendgrid-java

I am getting an error while trying to include sendgrid-java during the build of a grails project.
I've added a compile time dependency as described in the documentation:
compile 'com.sendgrid:sendgrid-java:2.1.0'
But when I try to build it, it fails to resolve the dependency. I can see it is trying to download this zip file from mavenCentral, but it does not exist:
http://repo1.maven.org/maven2/com/sendgrid/sendgrid-java/2.1.0/sendgrid-java-2.1.0.zip
If I browse to the parent directory I can see many similar files but no .zip file. How can I get it to resolve the dependencies without the expected zip file? Can you tell gradle to use just the .pom file instead? I'm stuck!
There is lots of useful stuff here in this parent folder, just not zip file and i just can't get gradle to use it:
http://repo1.maven.org/maven2/com/sendgrid/sendgrid-java/2.1.0/
You have put this in the plugins{} block instead of the dependencies{} of your BuildConfig.groovy.
plugins is for grails plugins, which are distributed as .zip files, hence the error message with the file not being found.
"Regular" maven/ivy dependencies are handled just by the dependencies block (like in Gradle if you will).
The way you have it in the dependencies section of buildConfig.groovy is correct. It is in the Maven Central repository. So if you just have mavenCentral() in your repositories section of buildConfig.groovy all should be well.

Why does Gradle only include classes from my library with project dependency

I'm building an Android app that has a dependency on a custom library, and Gradle is only willing to include my custom library when I use a project dependency, not when I use a files dependency to include the library's jar file. I'm building both my app and the library with the API levee 19 SDK.
failing dependencies section from build.gradle:
dependencies {
compile 'com.android.support:appcompat-v7:+'
compile files('libs/MyLibrary.jar')
}
If I use the above dependencies section, none of the class in MyLibrary.jar are included in the build apk file, as verified by extracting its classes.dex and running dexdump. I have also verified that all of the classes are present in the jar file I'm using.
If I use the following dependencies section, then all of the classes in MyLibrary are included in the apk file:
dependencies {
compile 'com.android.support:appcompat-v7:+'
compile project(':MyLibrary')
}
I'm using Android Studio 0.4.0, Gradle 1.9, and I think the Gradle plugin 0.7.1.
What is going on here? I'd really like to build my app with the API level 18 sdk to test compatibility, but I can't get that working unless I'm able to just use the jar file for my library.
Okay, this was my fault. My library's build.gradle was configured to only include the source files in the jar output file. The following is incorrect Gradle code and will give you the same problems as I've had.
task jar(type: Jar) {
from android.sourceSets.main.java
}
This answer shows how to fix the jar file creation. It's ugly, but it seems to work.
Jar task does not include dependencies in the final jar artifact.
From Gradle documentation on jar task:
The jar task creates a JAR file containing the class files and
resources of the project.
It assumes that since you are building jar for your project, all dependencies will be provided during runtime. As opposed to war, where all dependencies are usually included in the final artifact.
If you need to create "fat jar", which will include the dependencies, then look into specific plugins, for example gradle-fatjar-plugin.
It's a little bit of a longshot, but if you're not using Android Studio 0.4.0 and you've just added the jar file, try cleaning your project and rebuilding from scratch. We've seen this bug: https://code.google.com/p/android/issues/detail?id=63366 where libraries don't get included without cleaning the project, though this bug refers to a dependency downloaded from Maven and not a local jar file (which may or may not be an important difference). This was fixed in Android Studio 0.4.0 (more specifically, in the Gradle plugin 0.7.0).

Categories