Redirect Gradle project dependency on Maven artifact to another Gradle project - gradle

Is there any way to redirect a Gradle project dependency on Maven artifact coordinates to the output of another Gradle project in the same build?
I don’t see the point in having dependencies on things like project(‘:xyz’), since that requires that project :xyz be on my file system & built, and for the referencing project to know that :xyz is also in the same build. It makes more sense to me to have dependencies on Maven artifact coordinates, and to use artifacts built from projects in the same build that specify matching coordinates, instead of downloading the artifact from a repository.
If this functionality exists, or if I were to make it myself, I’d imagine that it would be implemented as an object that can be listed in the repositories closure before all other repositories like mavenCentral().

What you are looking for is composite builds. It will allow you to define that a build includes another build and thus Gradle figures out that a dependency notation group:name:version gets replaced by an included build.
However I would still recommend using the project(:xyz) notation when the projects belong to the same unit, which usually is a single checkout from source control.

You can publish xyz in your local repository.
In the project xyz, use this plugin :
apply plugin: 'maven-publish'
And just run gradle publishToMavenLocal
Then, in the "root" project, use in addition the local repository and use xyz as a regular dependency :
repositories {
mavenLocal()
}
dependencies {
compile('your.company:xyz:1.0.0')
}

Related

Why gradle not creating local repository and downloading same dependencies for every project

I'm using Gradle with Java Project in IntelliJ Idea.
I see that Gradle downloading dependencies for first time on opening project.
But there is another project with same dependencies then also it's re-downloading those libs. why?
Why doesn't it maintain Maven like local repository even after configured?
repositories {
mavenLocal()
mavenCentral()
jcenter()
}
How can Gradle maintain local repository and next it should first check local repo and go for download if no matching dependencies found?
With that piece of code you instruct gradle to look at the local maven repository, then at the central maven repository and last in JCenter when looking for dependencies. The first one it finds your dependency it takes it from.
It does not instruct Gradle to put resolved dependencies to the local maven repository though. This is e. g. helpful if you have two projects in two separate builds, one depends on the other and you install the first dependency to the local maven repository with the respective Gradle task and then build the second one, depending on the version you just built and installed.
Gradle has a resolution cache though in ~/.gradle/caches/modules-2/files-2.1/ where it caches all downloaded dependencies and also reuses this from different builds.

Is gradle possible to use maven repository with higher priority than jcenter

Gradle can set multiple repositories, e.g. maven and jcenter. But I realized gradle always use jcenter first even if I put maven before jcenter (as below). Is it possible to make maven (local repo, and faster) have higher priority?
## in file build.gradle
allprojects {
repositories {
maven { url "http://nexus.mucompany.com/nexus/service/local/repositories/releases/content/" }
jcenter()
}
I would like to ask this as a comment, but haven't got enough rep yet :-/
Can you post the output from gradle --debug that makes you think it resolving from jcenter in preference?
The gradle docs contain the following:
A project can have multiple repositories. Gradle will look for a dependency in each repository in the order they are specified, stopping at the first repository that contains the requested module.
An quick confirmation locally shows that the order is being honored between custom maven repositories and jcenter()
aside the fact, that at least in newer gradle versions the order matters:
It’s even more important when considering that order of repositories matter.
…
✨ The order of declaration determines how Gradle will check for dependencies at runtime. If Gradle finds a module descriptor in a particular repository, it will attempt to download all of the artifacts for that module from the same repository. You can learn more about the inner workings of Gradle’s resolution mechanism.
source: gradle 5.4.1 Declaring multiple repositories
or it's a bug in your specific gradle version - my answer might come "a bit late" that day 2k19 as you question was from '15 ;D
(at least in Gradle 3.5 it was defined similar
A project can have multiple repositories. Gradle will look for a dependency in each repository in the order they are specified, stopping at the first repository that contains the requested module.
source: gradle 3.5 Dependency Management for Java Projects
)
a common problem is, that higher priorized repository AND / OR local cache are corrupt!
Often enough artifactory jcenter proxy contains unwillingly POM declarations but no jars. You have then to cleanup the artifactory.
Similar to local repositories.
you can try to ignore local caches by running gradle with --refresh-dependencies and best case -i logging enabled to see where requests go to and what are the results.
have a look at How can I force gradle to redownload dependencies?
Second option:
as of gradle 5.1 you can always declare filters
see gradle 5.4.1 Declaring a repository filter
for me, I ignore any com.android packages from jcenter, which speeds up searching a bit and reduces the known problem mentioned above

what's the difference between .ivy2 and .m2

Previously I am using sbt, and looks like it put jar under .ivy2. And then I used gradle, I have thought it also put jar under .ivy2, but the following link told me I need to delete corrupted package from .m2. So gridle is using .m2 and can not use ivy2?
spring boot mvc: failed after following the sample
Ivy, Maven and Gradle each have their own dependency cache in ~/.ivy2/cache, ~/.m2/repository, and ~/.gradle/caches, respectively. Gradle will only use Maven's dependency cache (known as the local Maven repository) if mavenLocal() is declared as a repository in the build script. This should only be done if the Gradle build needs to consume artifacts produced by a local Maven build. (There aren't any efficiency gains; in fact declaring mavenLocal() will make the build slower and less reliable.)

How to use gradle without maven

Is it possible to use gradle without maven?
I ask this question because I've encounered a case where it isn't possible. For example, I have a project(let it be project A) which results in a jar file after the build. This project is used by another project(project B). When I change smth in project A, project B has to see those changes. In maven we could simply make mvn install on project A, then refresh dependencies on project B and changes hapen to be seen there(in project B)
Gradle has an opportunity to use maven plugin which can do the descibed thing. But in that case we rely on maven(maven repo in particular). I was founding information(seems on stackoverflow also) that gradle filestore, which is located in GRADLE_USER_HOME, is only a cache and can't be used for such purpose.
So, how to achieve that functionality in gradle
Thanks
Gradle downloads dependencies from repositories. These repositories can be Maven repositories, Ivy repositories, local Maven repositories or file repositories. So, to solve your use-case, you would indeed have to publish A to a repository, and to use this repository as the source of the A dependency in B.
See the documentation for more details.

Importing "wildcard" Maven project dependencies?

I have quite a situation here: another team is building a Flex SDK and delivers it as a huge set of separate SWC files. At this point I cannot make them build a single SWC file library.
I'm trying to use these files as Maven dependencies to pass to Flexmojos but they are like 40 files and I don't want to manage each of them as a separate dependency (after all they are parts of the same thing).
Is there a way to:
Install all of the SWC files as artifacts in the local repo? (with the same groupId and version and auto-generated artifactId I guess)
Import these as dependencies using a "wildcard" or plugin that generates dependencies at runtime or something?
this won't work that way. Dependency declarations can't be dynamic, this would break the build. What you can do is have your deployers create a rollup pom with packaging pom containing all of the SWC dependencies and deploy that pom into your nexus repository. You'd be able to use that pom dependency. The benefit would be that the pom is maintained in a single central location.
You can use the build-helper-maven-plugin to add artifacts to the Flex SDK project. As a result you can use the SDK project as a dependency and got all files which you can use.

Resources