Importing Maven dependency - maven

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'

Related

Convert Maven `maven-assembly-plugin` to Gradle

I'm converting a Java project from Maven to Gradle.
One of the pom.xml is using maven-assembly-plugin to package dependencies into a single zip file.
I'm using custom configuration to specify the dependencies to package like so:
group = 'com.company'
description = 'projectA'
configurations {
ciPlugin
}
dependencies {
ciPlugin group: 'org.apache.httpcomponents', name: 'httpclient', version:'4.3.1'
}
jar {
into('plugins') {
from configurations.ciPlugin
}
}
This works almost fine beside the fact I have the following files in the resulting zip file:
commons-codec-1.10.jar
commons-logging-1.2.jar
httpclient-4.5.3.jar
httpcore-4.4.6.jar
and when Maven runs it only has httpclient-4.5.3.jar.
Just for clarification, I'm not really packaging httpclient, I'm packaging private artifact but the behavior is the same.
How can I get only the direct dependency without transient ones?
Add the following (e.g. after the dependencies block):
configurations.ciPlugin.transitive = false
This will turn off transitive dependencies for all artifacts in the ciPlugin configuration.

Grails Download zip file as dependency

I have a standalone custom code packed as a zip file(custom-1.0.zip) using maven-shade-plugin –uploaded to my company's artifactory
<dependency>
<groupId>com/mypackage/domain</groupId>
<artifactId>custom</artifactId>
<version>1.0</version>
<type>zip</type>
</dependency>
I want to use this zip file in my Grails 2.3.5 project which is configured to use maven for resolving dependencies. I believe I need to do following:
Download the zip file from artifactory to local maven repository
Copy the zip file from local maven repository to web-app/resources folder of my project
I added following to Build.groovy to download the zip file:
grails.project.dependency.resolver = "maven"
dependencies {
compile ("com.mypackage.domain:custom:1.0")
}
…
..
plugins {
compile ("com.mypackage.domain:custom:1.0")
}
Above code downloads the jar, pom and zip file to my local maven repository and then fails
Loading Grails 2.3.5
|Configuring classpath
|Downloading: com.mypackage.domain/custom/1.0/custom-1.0.pom
|Downloading: com.mypackage.domain/custom/1.0/custom-1.0.zip
|Downloading: com.mypackage.domain/custom/1.0/custom-1.0.jar
.
|Environment set to development
.................................
|Packaging Grails application
Error |
Zip C:\Users\userid\.m2\repository\com\mypackage\domain\custom\1.0\custom-
1.0.zip is not a valid plugin
Does anyone has a working example for downloading zip using Grails 2.3.5 and copying to desired folder?
You do not need the following unless the custom artifact is built as a plugin. You can remove
plugins {
compile ("com.mypackage.domain:custom:1.0")
}
and edit the dependencies to include the type:
dependencies {
compile ("com.mypackage.domain:custom:1.0:zip")
}
then compile your project.

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/Maven project fork provides original artifact (only download fork)

Description
I have forked another project and uploaded it to my own maven repository. The dependency graph, however, pulls the original project too, meaning I two versions of the projects. The compilation works fine in command-line but not in eclipse as it reads the original project's jar first and then skips my forked project.
Dependency graph
+--- com.spiddekauga.gdx:gdx-spiddekauga (fork)
+--- com.badlogicgames.gdx:gdx-box2d
| \--- com.badlogicgames.gdx:gdx (original)
What I've tried
First I tried using the original name of com.badlogicgames.gdx:gdx but the project was downloaded from the original maven repository instead of mine even if my maven repository is listed before.
build.gradle
allprojects {
...
repositories {
maven { url "http://maven.MY-DOMAIN.org/" }
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
maven { url "https://oss.sonatype.org/content/repositories/releases/" }
}
...
}
Another thing I've search for is to make com.spiddekauga.gdx:gdx-spiddekauga provide com.badlogicgames.gdx:gdx so that it doesn't have to be downloaded, but I haven't found a way to do this in neither in gradle or maven.
Questions
How can I solve so that only my forked project is pulled? Either by a) renaming to com.badlogicgames.gdx:gdx so it searches my maven repository first; or b) by excluding the dependency com.badlogicgames.gdx:gdx.
[minor question] Is it possible to set that a maven project provides another artifact? E.g. like this:
pom.xml
<project>
<groupId>com.spiddekauga.gdx</groupId>
<artifactId>gdx-spiddekauga</artifactId>
<provides>
<project>
<groupId>com.badlogicgames.gdx</groupId>
<artifactId>gdx</gdx>
</project>
</provides>
</project>
Forking the project does not change the project coordinates (groupId, projectId and version) in the pomor gradle files. You would need to search and replace all of them for it to work.
Also the dependencies that you are referring to are available in mavenCentral, which is the first in the repositories list.

Gradle Dependency loading from 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é

Resources