Jersey + Gradle with maven dependencies not working - maven

Hi I am following the jersey sun documentation. I have deployed before this simple pom.xml before
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.14</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-grizzly2</artifactId>
<version>1.14</version>
</dependency>
and Add the repository
<url>https://maven.java.net/content/repositories/snapshots/</url>
Nevertheless when I try to do this with gradle, it does not seem to be working, is not downloading the rest of dependencies that requires and aparently I have to explicitly put javax.ws.rs:jsr311-api:1.1.1 and even jersey-core. This is my build.gradle.
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'jetty'
sourceCompatibility = 1.6
repositories {
mavenCentral()
maven {
url 'https://maven.java.net/content/repositories/snapshots/'
}
}
List compileLibraries =['com.sun.jersey:jersey-server:1.14',
'com.sun.jersey:jersey-grizzly2:1.14',
'com.sun.jersey:jersey-core:1.14',
'javax.ws.rs:jsr311-api:1.1.1']
dependencies {
compile (compileLibraries )
testCompile group: 'junit', name: 'junit', version: '4.+'
}
httpPort = 8888
stopPort = 9451
stopKey = 'foo'
Is this proper gradle behaviour? How can I do as same as with maven?
Edit
Just for the sake of this and if somebody is interesting in seeing a gradle build file that work with gradle you can go to
https://github.com/necronet/XTradeJerseyimpl/
Thanks!!

Seems that the dependency from jersey-server to jersey-core isn't being properly interpreted by Gradle. Looking at the pom shows that the jersey-core dependency is in a profile, which is likely why it's not being picked up. And jersey-core has the dependency on jsr311. Sounds like Gradle should probably account for the profile marked 'activeByDefault' in cases like these.
That said, you've already hit upon the solution, which is to specify the two jars directly - and it's still less lines to configure than the maven xml :)
Also, it looks like all the jars you need can be found in mavenCentral, so the snapshot repository isn't contributing anything.
This doesn't solve the need to explicitly mention those two extra jars, but I hope this explains why, and you might want to raise an issue on Gradle Jira if you think this should be addressed.

Related

Dependency table for spring boot 2.4.3

I have a difficulty defining what dependencies versions are compatible with each other.
In my case i have this
I don't know for the spring boot 2.4.3 what versions of the dependencies below go with it or the cloud version to 2020.0.1.
How can i fix this for example i want to migrate the spring boot and spring cloud to its latest versions.
It is a good rule of thumb to not define the versions yourself but use BOMs and let them define the versions for you:
spring-boot-dependencies
spring-cloud-dependencies
In order to find out which BOMs to use, you can use this compatibility matrix or the endpoint that #spencergibb mentioned: https://start.spring.io/actuator/info.
If you want to use a Spring Project that is not in the BOM, most probably that Spring Project is not supported (e.g.: Netflix libraries by latest Spring Cloud as #spencergibb mentioned).
Update: here's a Gradle example but you can generate a whole project using Spring Initializer:
plugins {
id 'org.springframework.boot' version '2.4.3'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
repositories {
mavenCentral()
}
dependencyManagement {
imports {
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:2020.0.1'
}
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.cloud:spring-cloud-starter-circuitbreaker-resilience4j'
}
It might be because there are collisions between dependencies included from the start and your explicitly defined dependencies. For example, take this dependency: spring-cloud-contract
Your version: 2.1.5
2020.0.1 Spring cloud's supported version: 3.0.1 (check this link mentioned by #Jonatan)
Try to exclude built-in dependencies that you have explicitly defined. For Maven it would be:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2020.0.1</version>
<type>pom</type>
<scope>import</scope>
<exclusions>
<exclusion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-verifier</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</dependencyManagement>
But that might not work because not all libraries are backward compatible. I would suggest using default libraries provided by spring cloud and remove explicit dependencies.

How to use gradle feature variant dependecies in tests?

I am migrating a Maven library project to Gradle. The original project also has optional dependencies. I use the java-library plugin but moving the formerly optional dependencies to implementation results in runtime dependencies instead of compile. So I tried the gradle feature variants which results in the right dependencies in the pom.xml. But doing so results is failing test compile as the dependencies of the feature variant are missing on the test compile classpath!
Here is my current setup in build.gradle:
apply plugin: 'java'
apply plugin: 'java-library'
apply plugin: 'maven-publish'
sourceCompatibility = 1.8
java {
registerFeature('oSupport') {
usingSourceSet(sourceSets.main)
}
}
dependencies {
api 'my.compile:dep-a:1.0.0'
implementation 'my.runtime:dep-i:1.0.0'
oSupportApi 'my.optional:dep-o:1.0.0'
}
Let's assume there is a class O available from my.optional:dep-o. If I import O in any class in src/main/java it works perfectly. Also the dependencies are exported right to Maven (using gradle generatePomFileForMavenJavaPublication, see the dependencies from the generated pom.xml below). But any test in src/test/java using class O will not compile (import my.optional.O; creates error: package my.optional does not exist)
<dependencies>
<dependency>
<groupId>my.compile</groupId>
<artifactId>dep-a</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>my.rintime</groupId>
<artifactId>dep-r</artifactId>
<version>1.0.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>my.optional</groupId>
<artifactId>dep-0</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
</dependencies>
How to solve this? I know I could have used the nebula.optional-base plugin instead of the buildin Gradle feature variant but I would prefer the new gradle builtin support for optional dependencies instead.
PS: I use Java 8 and Gradle 5.6.2
This looks like a bug when the feature source set uses the main source set. Can you report on https://github.com/gradle/gradle/issues?
In the meantime, this should fix it:
configurations {
testCompileClasspath.extendsFrom(oSupportApi)
testRuntimeClasspath.extendsFrom(oSupportApi)
testRuntimeClasspath.extendsFrom(oSupportImplementation)
}
Really weird, I agree with #melix this seems to be a Gradle bug.
The following will fix it but should not be needed, imho:
dependencies {
api 'my.compile:dep-a:1.0.0'
implementation 'my.runtime:dep-i:1.0.0'
oSupportApi 'my.optional:dep-o:1.0.0'
testImplementation(project(":${project.name}")) {
capabilities {
requireCapability("${project.group}:${project.name}-o-support")
}
}
}
For this simplified setup with only one feature dependency could be replaced by testImplementation 'my.optional:dep-o:1.0.0' but for a general larger dependency list this approch avoids repetition of the dependencies as the extendsFrom solution of #melix.

How to use a maven BOM for Spring in Gradle?

I am converting POM to Gradle and one of the things I am stuck at is having dependency management in Gradle like the following that I have in POM:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Is there a way to have Edgware.SR4 in Gradle as well?
I checked https://docs.gradle.org/4.6/release-notes.html#bom-import but that doesn't really tell me a way on how to utilize Edgware.SR4 BOM.
UPDATE
I finally have my build.gradle as follows that seems to work:
plugins{
id 'org.springframework.boot' version '1.5.8.RELEASE'
}
apply plugin: 'io.spring.dependency-management'
dependencyManagement {
imports {
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Edgware.SR4'
}
}
This seems to be working fine but wondering if there is any flaw in this approach. Documentation available at https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/html/ suggests to use apply false to begin with in
id 'org.springframework.boot' version '1.5.8.RELEASE'
I didn't do that and it worked fine. Wondering why it was suggested like that.
Assuming that you are using Spring Boot and, therefore, already have the Dependency Management Plugin applied, you can import Spring Cloud's bom by adding the following to your build.gradle file:
dependencyManagement {
imports {
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Edgware.SR4'
}
}
As of today, the latest versions of gradle have a built-in solution.
dependencies {
implementation(platform("org.springframework.cloud:spring-cloud-dependencies:Edgware.SR4"))
}

Add plugin dependency in grails

I am trying to add dependency of a plugin into my grails application, but it doesnot have any plugins in grails repo. It can be added to maven project as :
<dependency>
<groupId>com.plaid</groupId>
<artifactId>plaid-java</artifactId>
<version>0.2.12</version>
</dependency>
As my project is also maven based. How do i add this plugin into my project.
P.S. : IT cannot be added in plugins and dependencies since there is no grails plugin associated with that.
Any help is appreciated.
You can use the create-pom org.mycompany to create your pom.xml file to make grails read the pom.xml you need to set in BuildConfig.groovy this code
grails.project.dependency.resolution = {
/*YOUR CONFIG*/
pom true
repositories {
/*YOUR RESPOSITORIES*/
}
}
Then you need to add your dependency in this pom.xml
You can see the official doc. in this link
We can add dependency for any plugin in grails under dependencies{} in BuildConfig.groovy as:
<groupId>:<artifactId>[:<extension>[:<classifier>]]:<version>
For your case, the equivalent for:
<dependency>
<groupId>com.plaid</groupId>
<artifactId>plaid-java</artifactId>
<version>0.2.12</version>
</dependency>
is:
dependencies{
compile "com.plaid:plaid-java:0.2.12"
}
For more you can have a look into http://docs.grails.org/2.3.1/guide/conf.html

pom dependency fails in Gradle (ok in Maven)

I'm writing a standalone EJB client for JBoss 7.1 and as suggested I'm using the following dependency:
<dependency>
<groupId>org.jboss.as</groupId>
<artifactId>jboss-as-ejb-client-bom</artifactId>
<type>pom</type>
<version>7.1.1.Final</version>
</dependency>
This works as expected in Maven, however when used in Gradle like so:
dependencies {
compile 'org.jboss.as:jboss-as-ejb-client-bom:7.1.1.Final'
}
It fails with:
Could not find group:org.jboss, module:jboss-remote-naming, version:1.0.2.Final.
What is the reason for different behavior of Gradle vs. Maven?
Well the dependency you declare in Maven points to a pom packaging component, and the one in Gradle points to a jar. However there is no jar with this project since it is a pom packaging component so Gradle obviously fails.
http://search.maven.org/#browse%7C351478366
Using Gradle you probably have to either declare a dependency to the pom somehow (not sure if that is possible) or add the dependencies from the pom to your project yourself.
http://search.maven.org/remotecontent?filepath=org/jboss/as/jboss-as-ejb-client-bom/7.1.3.Final/jboss-as-ejb-client-bom-7.1.3.Final.pom
Use the #pom type:
dependencies {
compile 'org.jboss.as:jboss-as-ejb-client-bom:7.1.1.Final#pom'
}

Resources