Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dependency resolution behind proxy #571

Closed
justinberry opened this issue Jun 5, 2013 · 14 comments
Closed

Dependency resolution behind proxy #571

justinberry opened this issue Jun 5, 2013 · 14 comments

Comments

@justinberry
Copy link

Hi guys,

I'm using Robolectric 2.1 and I've noticed you have a mechanism in your code for grabbing various dependencies, as listed in SdkConfig.java:

public Dependency[] getSdkClasspathDependencies() {
    return new Dependency[] {
        realAndroidDependency("android-base"),
        realAndroidDependency("android-kxml2"),
        realAndroidDependency("android-luni"),
        createDependency("org.json", "json", "20080701", "jar", null),
        createDependency("org.ccil.cowan.tagsoup", "tagsoup", "1.2", "jar", null)
    };
}

However this isn't working behind our proxy and I'm unable to find any way of configuring this. I'm able to get around this by manually listing these dependencies in our pom.xml file but it's not so nice. Can you offer some guidance here?

Some output for your eyeballs:

mvn clean install output

Cheers

@xian
Copy link
Member

xian commented Jun 5, 2013

Yup! You'll need to extend RobolectricTestRunner for this configuration change. Here's an example:

public class MyRobolectricTestRunner extends RobolectricTestRunner {
  @Override protected void configureMaven(DependenciesTask dependenciesTask) {
    // maybe you want to override this method and some settings?
    RemoteRepository remoteRepository = new RemoteRepository();
    remoteRepository.setId("my private nexus");
    remoteRepository.setUrl("http://my-private-nexus.example.com/nexus/content/repositories/releases");
    dependenciesTask.addConfiguredRemoteRepository(remoteRepository);
  }
}

Then say @RunWith(MyRobolectricTestRunner.class) at the top of your tests!

@xian xian closed this as completed Jun 5, 2013
@justinberry
Copy link
Author

Kudos for the awesome response time, that worked a treat!

@xian
Copy link
Member

xian commented Jun 6, 2013

Glad to hear it! :-)

@dmengelt
Copy link

Omg thanks for this response! I searched like a year for that! :)

@nfrankel
Copy link

Hi guys,
For me, it's pretty strange not to get the dependencies declared in the POM/build.gradle and do it hard-coded. I'm running behind a proxy, on Jenkins and there's no way we can open http://oss.sonatype.org/. The good thing is we have a Nexus having all libraries.
Would you be open to accept a pull request will necessary libraries set in the POM?

@nenick
Copy link
Contributor

nenick commented Apr 15, 2015

You can provide a custom MavenDependencyResolver at custom RobolectricTestRunner#getJarResolver where you use your repo url sonatypeRepository.setUrl(...)

Another approach is to provide a maven settings.xml where all calls are redirected to your nexus

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    <mirrors>
        <mirror>
            <id>nexus</id>
            <name>mirror of remote repositories</name>
            <mirrorOf>*</mirrorOf>
            <url>https://<your address>/nexus/content/groups/public</url>
        </mirror>
    </mirrors>
</settings>

@nfrankel
Copy link

Hi Nico,
Thanks for your feedback. The problem with the code approach is that it's not the place to handle dependencies. I guess I'll have to try with the settings.xml, but a colleague told me it didn't work.

@nenick
Copy link
Contributor

nenick commented Apr 15, 2015

I have the same situation (proxy and intranet nexus) and the settings.xml works for me ;)

@erd
Copy link
Member

erd commented Apr 15, 2015

Would you be open to accept a pull request will necessary libraries set in the POM?

The reason they're not listed in the pom is that the android-all jars are around 50MB each, so requiring users to download all of them could be prohibitive. There is a way to run robolectric in "offline" mode, where you manually download all the dependencies and tell robolectric where to find them. Unfortunately, it's not well documented.

@nfrankel
Copy link

I don't see your point. Every build system (Maven, Gradle, etc.) has a way to exclude dependencies. Besides, they are downloaded only once and put in a cache. Even better, if you have an entreprise repository like Artifactory or Nexus, you pay the download cost only once across all enterprise.

On the other hand, having a library downloading its dependencies in the code with the repository hard-coded (oss.sonatype.org) defeats the least surprise principle.

As for the offline mode, I stumbled upon it but you have to put every dependency in the same folder - flat hierarchy, which build systems are not made for.

@daj
Copy link

daj commented Sep 7, 2016

The configureMaven method no longer exists in RobolectricTestRunner, so the solution above doesn't work. For anybody looking for a solution for this problem using Gradle on Robolectric 3.0, see this Stackoverflow answer: http://stackoverflow.com/a/39360538/112705

@apouche
Copy link

apouche commented Aug 12, 2019

As of time of this writing, those previous answers are now obsolete. If you refer to the latest robolectric documentation you need to override the robolectric.dependency.repo.url property like so:

android {
  testOptions {
    unitTests.all {
      systemProperty 'robolectric.dependency.repo.url', 'https://local-mirror/repo'
      systemProperty 'robolectric.dependency.repo.id', 'local'
    }
  }
}

@robotsquidward
Copy link

robotsquidward commented Mar 3, 2020

While @apouche's solution is the one from the docs, it doesn't work. I've tried robolectric.properties and copy/pasting directly from the docs in my build.gradle and both approaches don't override the default URL (https://oss.sonatype.org/content/groups/public/). I've also tried using offline mode and specifying a robolectric.dependency.dir unsuccessfully. There is either a massive gap in the documentation or a pretty big bug around configuration.

@hqfranca
Copy link

hqfranca commented Jun 5, 2020

After navigate into framework classes, i found a temporary solution:

import org.robolectric.MavenRoboSettings
import org.robolectric.RobolectricTestRunner

class CustomRobolectricTestRunner(kClass: Class<*>) : RobolectricTestRunner(changeUrl(kClass))

fun changeUrl(kClass: Class<>): Class<> {
MavenRoboSettings.setMavenRepositoryUrl("my-repo-url")
MavenRoboSettings.setMavenRepositoryId("my-repo-id")
MavenRoboSettings.setMavenRepositoryUserName("user")
MavenRoboSettings.setMavenRepositoryPassword("password")

return kClass

}

MavenRoboSettings is deprecated, but only with this class is possible to change this configuration, was mentioned by @robotsquidward this sounds like a big bug in the configuration.

This solution works for me :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants