spring-cloud-contract icon indicating copy to clipboard operation
spring-cloud-contract copied to clipboard

Maven repository with stubs should be determined automatically

Open jkubrynski opened this issue 9 years ago • 10 comments

Maybe we can use pom.xml or settings.xml to automatically determine remote repositories? The question is how to deal in Gradle projects

jkubrynski avatar Apr 27 '17 15:04 jkubrynski

Local repositories too.

ksprider avatar Dec 20 '17 09:12 ksprider

@ksprider you're talking about the case where your local repo is in a different folder than the default?

marcingrzejszczak avatar Dec 20 '17 10:12 marcingrzejszczak

@marcingrzejszczak Yes.

ksprider avatar Dec 20 '17 11:12 ksprider

Maybe we can use pom.xml or settings.xml before use default /user.home/m2/repo if we have not set maven.repo.local env ?

ksprider avatar Dec 20 '17 11:12 ksprider

but we already do sth like this, don't we? https://github.com/spring-cloud/spring-cloud-contract/blob/090546986693da11225020c02bcd8633fb9e74d6/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/AetherFactories.java#L75-L96

although it doesn't really have any defaults so actually we don't

marcingrzejszczak avatar Dec 20 '17 11:12 marcingrzejszczak

Yes. We do this actually. Buy not perfect.

Well, i just set maven.home and add maven.home\bin to path. And i update localRepository of settings.xml. It can not be performed well in settings() method because i have not set MAVEN_USER_SETTINGS_LOCATION or MAVEN_USER_SETTINGS_LOCATION.

Of course i can set MAVEN_USER_SETTINGS_LOCATION or MAVEN_USER_SETTINGS_LOCATION, then it can read the localRepository.

Maybe we can read the localRepository from settings.xml before request.setUserSettingsFile...

ksprider avatar Dec 20 '17 11:12 ksprider

I still don't follow...

Maybe we can read the localRepository from settings.xml before request.setUserSettingsFile...

Here we build the local repo

LocalRepository localRepo = new LocalRepository(localRepositoryDirectory());

the directory is taken from

private static String localRepositoryDirectory() {
		String localRepoLocationFromSettings = settings().getLocalRepository();
		return System.getProperty(MAVEN_LOCAL_REPOSITORY_LOCATION, localRepoLocationFromSettings != null
			? localRepoLocationFromSettings
			: System.getProperty("user.home") + File.separator + ".m2" + File.separator + "repository");
	}

Now let's take a look at settings()

private static Settings settings() {
		SettingsBuilder builder = new DefaultSettingsBuilderFactory().newInstance();
		SettingsBuildingRequest request = new DefaultSettingsBuildingRequest();
		String user = System.getProperty(MAVEN_USER_SETTINGS_LOCATION);
		if (user == null) {
			request.setUserSettingsFile(new File(new File(System.getProperty("user.home")).getAbsoluteFile(),
					File.separator + ".m2" + File.separator + "settings.xml"));
		} else {
			request.setUserSettingsFile(new File(user));
		}
		String global = System.getProperty(MAVEN_GLOBAL_SETTINGS_LOCATION);
		if (global != null) {
			request.setGlobalSettingsFile(new File(global));
		}
		SettingsBuildingResult result;
		try {
			result = builder.build(request);
		} catch (SettingsBuildingException ex) {
			throw new IllegalStateException(ex);
		}
		return result.getEffectiveSettings();
	}

If you haven't overridden anything it will take into consideration your settings.xml. If you have the localRepository set there, it should be read.

marcingrzejszczak avatar Dec 20 '17 12:12 marcingrzejszczak

I am so sorry. I made a mistake. I mean that we can locate settings.xml from maven's default directory maven.home/conf after if (user == null) .

codes like this:

String user = System.getProperty(MAVEN_USER_SETTINGS_LOCATION);
if (user == null) {
    if(null != System.getProperty("maven.home")){
        request.setUserSettingsFile(new File(System.getProperty("maven.home") + File.separator + "conf" + File.separator + "settings.xml"));
    } else {
        request.setUserSettingsFile(new File(new File(System.getProperty("user.home")).getAbsoluteFile(), File.separator + ".m2" + File.separator + "settings.xml"));
    }
} else {
    request.setUserSettingsFile(new File(user));
}

ksprider avatar Dec 20 '17 14:12 ksprider

Ok, now I understood what you are talking about. So you're saying that we don't care about the maven.home property?

marcingrzejszczak avatar Dec 20 '17 14:12 marcingrzejszczak

ping @ksprider ?

marcingrzejszczak avatar Jan 30 '18 08:01 marcingrzejszczak