Gradle Dependency loading from maven - maven

I am new to gradle.
I have seen some examples about java dependency like the following example but my project will be simply a zip file.
I just want to download the zip file.
apply plugin: 'java'
dependencies {
compile 'commons-lang:commons-lang:2.6'
}
In the above example, it will automatically download the jar file. But it doesn't download my zip file if my maven repositories contains zip that mentioned in the pom.xml about that package.
Questions:
What is the flow when depend on a maven repository? It will first read the pom.xml and then download the zip file?
How to dynamically load the dependency? e.g 'commons-lang:commons-lang:2.6' will have dependency of 'commons-lang:en:1.0" in the pom.xml. How to make it automatically load and loop the dependency list?
Thanks all
I have tried the follwoing script but it gives me error on compile but I have apply the java plugin
My gradle file
apply plugin: 'java'
repositories {
mavenLocal()
maven {
url "http://nexus.com/myrepo/"
}
}
dependencies {
compile 'com.a.b:projectA:2.0#zip'
}
I can run without problem that files downloaded are inside .m2
Question about the transitive dependency
I have the pom.xml like this. But it is unable to load the dependency one. It will directly go to the new pom.xml first or download zip directly if i mention sth like this?
<dependencies>
<dependency>
<groupId>com.a.b.c</groupId>
<artifactId>base</artifactId>
<version>1.2</version>
<type>zip</type>
</dependency>
</dependencies>

When declaring a dependency and a maven repository, this maven repository will be used to resolve the artifact. this means that usually first the metadata is read and then the artifact will be downloaded. If no repository is declared the resolution will fail early.
Having a dependency notation like yours:
dependencies {
compile 'commons-lang:commons-lang:2.6'
}
gradle resolves the default artifact of that dependency. If you want to resolve additional declared zip files from maven central, you have to use this notation
repositories{
mavenCentral()
}
dependencies {
compile 'commons-lang:commons-lang:2.6#zip'
}
As a default, the a dependency is transitive. This means, that if e.g 'commons-lang:commons-lang:2.6' has a dependency on 'commons-lang:en:1.0" in its pom.xml the commons-lang library (and again its transitive dependencies if any) is also resolved.
cheers,
René

Related

Replicating Maven "dependencyManagement" tag from inside Gradle build

I am trying to follow this Spring Boot/Vaadin guide however I'm using Gradle, not Maven.
At the very top of that guide they say to use the following Maven XML:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-bom</artifactId>
<version>10.0.11</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
However I don't see a dependencyManagement task available via Gradle. So I ask: how do I replicate the same behavior as the <dependencyManagement/> XML element above over in "Gradle land"?
Update: current attempt
dependencyManagement {
imports {
mavenBom 'com.vaadin:vaadin-bom:10.0.11'
}
}
Only problem with this is that when I add it to my build.gradle and then run ./gradlew clean I get the following Gradle error:
"Could not find method dependencyManagement() for arguments..."
This should give you a working build:
plugins {
// the Gradle plugin which provides the “dependencyManagement” block
id 'io.spring.dependency-management' version '1.0.6.RELEASE'
// add Java build functionality to be able to follow the Vaadin guide
id 'java'
}
dependencyManagement {
imports {
// the Maven BOM which contains a coherent set of module versions
// for Vaadin dependencies
mavenBom 'com.vaadin:vaadin-bom:10.0.11'
}
}
repositories {
// find dependency modules on Maven Central
mavenCentral()
}
dependencies {
// the dependency module you need according to the Vaadin with
// Spring Boot guide; the version of the module is taken from the
// imported BOM; transitive dependencies are automatically taken
// care of by Gradle (just as with Maven)
compile 'com.vaadin:vaadin-spring-boot-starter'
}
Run ./gradlew dependencies --configuration compileClasspath to see that all dependencies are available on your Java compile classpath now.
Edited to reply to the question in the comments: indeed, the import of the BOM leads to a slightly different set of dependencies than would be used without it. You can see the dependencies difference like so:
./gradlew dependencies --configuration compileClasspath > with-BOM.txt
Remove the dependencyManagement block and add a version to the single dependency: compile 'com.vaadin:vaadin-spring-boot-starter:10.0.11'
./gradlew dependencies --configuration compileClasspath > without-BOM.txt
diff -u with-BOM.txt without-BOM.txt
You can see slight differences like org.webjars.bowergithub.webcomponents:webcomponentsjs:1.2.6 being used with the BOM and version 1.2.2 without it. The reason for that can be found in the BOM where version 1.2.6 is defined and where the authors also mention the reason for that: “Transitive webjar dependencies, defined here for repeatable builds”
Generally maven dependencyManagement tag is used to import bom or control transitive versions.
Gradle does that with platform component, like below example.
dependencies {
implementation platform('com.vaadin:vaadin-bom:10.0.11')
implementation ('com.vaadin:vaadin-core')
}
This does not force you to depend on spring plugin for importing bom.

gradle: importing local bom

We have a multi-module project with all dependency versions listed in the top-level pom.xml. Is there any way to make Gradle use it, without having to install this bom into local maven repo?
More specifically: there's a pom.xml listing all dependencies in the <dependencyManagement> section. Gradle build uses "io.spring.dependency-management" plugin's importBom to read it from the mavenLocal(). This works, but requires an extra step:
mvn install:install-file -Dfile=pom.xml -DpomFile=pom.xml
which publishes the bom to the local maven repository where gradle can find it. Is there any way to avoid it? Ideally I'd like to have something like this:
dependencyManagement {
imports {
mavenBom "./pom.xml"
}
}
..which of course is not a valid "string module notation". Any ideas?
To answer my own question: nebula-recommender-plugin allows import pom files directly:
dependencyRecommendations {
mavenBom file: file('pom.xml')
}

Gradle get dependency from pom file

How can get dependency from pom file. Also pom file has repositories tag So i also need to use that repository if dependency not found in mavenCentral()
How can i do this????

Gradle does not add transitive dependencies to war's WEB-INF/lib folder

I have a web project that produces a war. The war adds a jar (another project of mine posted on an external repo) as compile dependency. This jar has its own compile dependency, which is listed in the jar's pom file, as well as in the project's build script (this is a Gradle project). I would like to add the jar, as well as all of its own dependencies to my war's WEB-INF/lib folder. However, when I build the web project with Gradle, only the direct dependency is added, not the transitive ones.
Here is my web project's build script:
apply plugin: 'war'
repositories {
maven {url "http://repo-url"}
}
configurations.all {
resolutionStrategy {
cacheChangingModulesFor 0, 'seconds'
}
}
dependencies {
compile('com.company.group:artifact-name:1.0.0-SNAPSHOT') {
changing = true
transitive = true
}
}
The com.company.group:artifact-name:1.0.0-SNAPSHOT does have its own POM, which lists the following dependency:
<dependency>
<groupId>us.codecraft</groupId>
<artifactId>xsoup</artifactId>
<version>0.2.0</version>
<scope>compile</scope>
</dependency>
Also, I've set cacheChangingModulesFor to 0 seconds, so that, effectively, the latest version of the SNAPSHOT jar is used every time.
The issue I am having is the 'us.codecraft:xsoup:0.2.0' artifact does not get included in the war, even though it is a transitive dependency of the war.
Any thoughts? Ask for more details, if anything is unclear.
Ok, it turns out the problem lay in how Gradle resolved the snapshot artifact that my web project depended on. For some unknown reason, during some builds Gradle would not download the most up-to-date jar, which prevented it from downloading the transitive dependency that I needed in my war, because it was not a dependency of this older jar. Deleting the older snapshot jar from my repo fixed the issue.

Importing Maven dependency

I'm trying to include ViewPagerIndicator into my project and I'd rather use the Maven dependency rather than importing the android library project. There's some code posted for maven
<dependency>
<groupId>com.viewpagerindicator</groupId>
<artifactId>library</artifactId>
<version>2.4.1</version>
<type>apklib</type>
</dependency>
and inside the sample project, that code is in a pom.xml file, but I don't have that file. Can I translate the above code into my build.gradle file? Or can I just create a pom.xml file and put it in my project?
You should ask the author JakeWharton to make an aar available on Maven Central. (vote here: https://github.com/JakeWharton/Android-ViewPagerIndicator/pull/229). Until then there is a temporary solution to use maven repo from community.
repositories {
maven {
url 'https://github.com/Goddchen/mvn-repo/raw/master/'
}
mavenCentral()
}
dependencies {
compile 'com.actionbarsherlock:viewpagerindicator:2.4.1'
}
https://github.com/Goddchen/mvn-repo
You will find the file in Maven Central which means simple run the build and the file will be downloaded. This is also true for gradle where it looks like this:
'com.viewpagerindicator', name: 'library', version: '2.4.1', ext: 'apklib'

Resources