use of org.mule.maven.client.api.model.MavenConfiguration in project mule by mulesoft.
the class MavenConfigTestCase method loadFromFileWithAdditionalRepoFromSystemProperty.
@Description("Loads the configuration from mule-config.json and adds an additional maven repository using system properties")
@Test
public void loadFromFileWithAdditionalRepoFromSystemProperty() throws Exception {
String additionalRepositoryUrl = "http://localhost/host";
testWithSystemProperty("muleRuntimeConfig.maven.repositories.customRepo.url", additionalRepositoryUrl, () -> {
GlobalConfigLoader.reset();
MavenConfiguration mavenConfig = getMavenConfig();
List<RemoteRepository> mavenRemoteRepositories = mavenConfig.getMavenRemoteRepositories();
assertThat(mavenRemoteRepositories, hasSize(2));
assertThat(mavenRemoteRepositories.get(0).getId(), is("customRepo"));
assertThat(mavenRemoteRepositories.get(0).getUrl(), is(new URL(additionalRepositoryUrl)));
assertThat(mavenRemoteRepositories.get(1).getId(), is("mavenCentral"));
assertThat(mavenRemoteRepositories.get(1).getUrl(), is(new URL(MAVEN_CENTRAL_URL)));
});
}
use of org.mule.maven.client.api.model.MavenConfiguration in project mule by mulesoft.
the class MavenConfigTestCase method loadFromFileOnly.
@Description("Test a single file loaded from the classpath and verifies that the mule.conf and mule.properties json are not taken into account.")
@Test
public void loadFromFileOnly() throws MalformedURLException {
GlobalConfigLoader.reset();
MavenConfiguration mavenConfig = getMavenConfig();
List<RemoteRepository> remoteRepositories = mavenConfig.getMavenRemoteRepositories();
assertThat(remoteRepositories, hasSize(1));
RemoteRepository remoteRepository = remoteRepositories.get(0);
assertThat(remoteRepository.getId(), is(MAVEN_CENTRAL_REPO_ID));
assertThat(remoteRepository.getUrl(), is(new URL(MAVEN_CENTRAL_URL)));
assertThat(remoteRepository.getAuthentication().get().getPassword(), is("password"));
assertThat(remoteRepository.getAuthentication().get().getUsername(), is("username"));
}
use of org.mule.maven.client.api.model.MavenConfiguration in project mule by mulesoft.
the class ArtifactClassLoaderRunner method createClassLoaderTestRunner.
/**
* Creates the {@link ArtifactClassLoaderHolder} with the isolated class loaders.
*
* @param klass the test class being executed
* @param runnerConfiguration {@link RunnerConfiguration} based on annotated test class.
* @return creates a {@link ArtifactClassLoaderHolder} that would be used to run the test. This way the test will be isolated
* and it will behave similar as an application running in a Mule standalone container.
*/
private static synchronized ArtifactClassLoaderHolder createClassLoaderTestRunner(Class<?> klass, RunnerConfiguration runnerConfiguration) {
final File targetTestClassesFolder = new File(klass.getProtectionDomain().getCodeSource().getLocation().getPath());
ArtifactIsolatedClassLoaderBuilder builder = new ArtifactIsolatedClassLoaderBuilder();
final File rootArtifactClassesFolder = new File(targetTestClassesFolder.getParentFile(), "classes");
builder.setRootArtifactClassesFolder(rootArtifactClassesFolder);
builder.setPluginResourcesFolder(targetTestClassesFolder.getParentFile());
builder.setProvidedExclusions(runnerConfiguration.getProvidedExclusions());
builder.setTestExclusions(runnerConfiguration.getTestExclusions());
builder.setTestInclusions(runnerConfiguration.getTestInclusions());
builder.setExportPluginClasses(runnerConfiguration.getExportPluginClasses());
builder.setApplicationSharedLibCoordinates(runnerConfiguration.getSharedApplicationRuntimeLibs());
builder.setApplicationLibCoordinates(runnerConfiguration.getApplicationRuntimeLibs());
builder.setTestRunnerExportedLibCoordinates(runnerConfiguration.getTestRunnerExportedRuntimeLibs());
builder.setExtensionMetadataGeneration(true);
Properties excludedProperties;
try {
excludedProperties = getExcludedProperties();
} catch (IOException e) {
throw new RuntimeException("Error while reading excluded properties", e);
}
Set<String> excludedArtifactsList = getExcludedArtifacts(excludedProperties);
builder.setExcludedArtifacts(excludedArtifactsList);
builder.setExtraBootPackages(getExtraBootPackages(excludedProperties));
builder.setExtraPrivilegedArtifacts(runnerConfiguration.getExtraPrivilegedArtifacts());
final ClassPathUrlProvider classPathUrlProvider = new ClassPathUrlProvider();
List<URL> classPath = classPathUrlProvider.getURLs();
builder.setClassPathUrlProvider(classPathUrlProvider);
WorkspaceLocationResolver workspaceLocationResolver = new AutoDiscoverWorkspaceLocationResolver(classPath, rootArtifactClassesFolder);
final MavenClientProvider mavenClientProvider = discoverProvider(ArtifactClassLoaderRunner.class.getClassLoader());
final Supplier<File> localMavenRepository = mavenClientProvider.getLocalRepositorySuppliers().environmentMavenRepositorySupplier();
final SettingsSupplierFactory settingsSupplierFactory = mavenClientProvider.getSettingsSupplierFactory();
final Optional<File> globalSettings = settingsSupplierFactory.environmentGlobalSettingsSupplier();
final Optional<File> userSettings = settingsSupplierFactory.environmentUserSettingsSupplier();
final MavenConfiguration.MavenConfigurationBuilder mavenConfigurationBuilder = newMavenConfigurationBuilder().forcePolicyUpdateNever(true).localMavenRepositoryLocation(localMavenRepository.get());
if (globalSettings.isPresent()) {
mavenConfigurationBuilder.globalSettingsLocation(globalSettings.get());
} else {
LOGGER.info("Maven global settings couldn't be found, M2_HOME environment variable has to be set in order to use global settings (if needed)");
}
if (userSettings.isPresent()) {
mavenConfigurationBuilder.userSettingsLocation(userSettings.get());
} else {
LOGGER.info("Maven user settings couldn't be found, this could cause a wrong resolution for dependencies");
}
final MavenConfiguration mavenConfiguration = mavenConfigurationBuilder.build();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Using MavenConfiguration: {}", mavenConfiguration);
}
final DependencyResolver dependencyResolver = new DependencyResolver(mavenConfiguration, of(new DefaultWorkspaceReader(classPath, workspaceLocationResolver)));
builder.setClassPathClassifier(new AetherClassPathClassifier(dependencyResolver, new ArtifactClassificationTypeResolver(dependencyResolver)));
return builder.build();
}
use of org.mule.maven.client.api.model.MavenConfiguration in project mule by mulesoft.
the class MavenClassLoaderModelLoader method load.
@Override
public ClassLoaderModel load(File artifactFile, Map<String, Object> attributes, ArtifactType artifactType) throws InvalidDescriptorLoaderException {
long stamp = lock.readLock();
try {
MavenConfiguration updatedMavenConfiguration = getMavenConfig();
if (!mavenRuntimeConfig.equals(updatedMavenConfiguration)) {
long writeStamp = lock.tryConvertToWriteLock(stamp);
if (writeStamp == 0L) {
lock.unlockRead(stamp);
stamp = lock.writeLock();
} else {
stamp = writeStamp;
}
if (!mavenRuntimeConfig.equals(updatedMavenConfiguration)) {
mavenRuntimeConfig = updatedMavenConfiguration;
createClassLoaderModelLoaders();
}
}
if (deployableMavenClassLoaderModelLoader.supportsArtifactType(artifactType)) {
return deployableMavenClassLoaderModelLoader.load(artifactFile, attributes, artifactType);
} else if (pluginMavenClassLoaderModelLoader.supportsArtifactType(artifactType)) {
return pluginMavenClassLoaderModelLoader.load(artifactFile, attributes, artifactType);
} else if (libFolderClassLoaderModelLoader.supportsArtifactType(artifactType)) {
return libFolderClassLoaderModelLoader.load(artifactFile, attributes, artifactType);
} else {
throw new IllegalStateException(format("Artifact type %s not supported", artifactType));
}
} finally {
lock.unlock(stamp);
}
}
use of org.mule.maven.client.api.model.MavenConfiguration in project mule by mulesoft.
the class MavenConfigBuilder method buildMavenConfig.
/**
* @param mavenConfig the maven configuration set by the user
* @return a {@link MavenConfiguration} created by using the user configuration and default values set by mule.
*/
public static MavenConfiguration buildMavenConfig(Config mavenConfig) {
try {
String globalSettingsLocation = mavenConfig.hasPath("globalSettingsLocation") ? mavenConfig.getString("globalSettingsLocation") : null;
String userSettingsLocation = mavenConfig.hasPath("userSettingsLocation") ? mavenConfig.getString("userSettingsLocation") : null;
String repositoryLocation = mavenConfig.hasPath("repositoryLocation") ? mavenConfig.getString("repositoryLocation") : null;
boolean ignoreArtifactDescriptorRepositories = mavenConfig.hasPath("ignoreArtifactDescriptorRepositories") ? mavenConfig.getBoolean("ignoreArtifactDescriptorRepositories") : true;
File globalSettingsFile = findResource(globalSettingsLocation);
File userSettingsFile = findResource(userSettingsLocation);
File repositoryFolder = getRuntimeRepositoryFolder();
if (repositoryLocation != null) {
repositoryFolder = new File(repositoryLocation);
if (!repositoryFolder.exists()) {
throw new RuntimeGlobalConfigException(I18nMessageFactory.createStaticMessage(format("Repository folder %s configured for the mule runtime does not exists", repositoryLocation)));
}
}
MavenConfiguration.MavenConfigurationBuilder mavenConfigurationBuilder = MavenConfiguration.newMavenConfigurationBuilder().localMavenRepositoryLocation(repositoryFolder).ignoreArtifactDescriptorRepositories(ignoreArtifactDescriptorRepositories);
if (globalSettingsFile != null) {
mavenConfigurationBuilder.globalSettingsLocation(globalSettingsFile);
}
if (userSettingsFile != null) {
mavenConfigurationBuilder.userSettingsLocation(userSettingsFile);
}
ConfigObject repositories = mavenConfig.hasPath("repositories") ? mavenConfig.getObject("repositories") : null;
if (repositories != null) {
Map<String, Object> repositoriesAsMap = repositories.unwrapped();
repositoriesAsMap.entrySet().stream().sorted(remoteRepositoriesComparator()).forEach((repoEntry) -> {
String repositoryId = repoEntry.getKey();
Map<String, String> repositoryConfig = (Map<String, String>) repoEntry.getValue();
String url = repositoryConfig.get("url");
String username = repositoryConfig.get("username");
String password = repositoryConfig.get("password");
try {
RemoteRepository.RemoteRepositoryBuilder remoteRepositoryBuilder = RemoteRepository.newRemoteRepositoryBuilder().id(repositoryId).url(new URL(url));
if (username != null || password != null) {
Authentication.AuthenticationBuilder authenticationBuilder = Authentication.newAuthenticationBuilder();
if (username != null) {
authenticationBuilder.username(username);
}
if (password != null) {
authenticationBuilder.password(password);
}
remoteRepositoryBuilder.authentication(authenticationBuilder.build());
}
mavenConfigurationBuilder.remoteRepository(remoteRepositoryBuilder.build());
} catch (MalformedURLException e) {
throw new MuleRuntimeException(e);
}
});
}
return mavenConfigurationBuilder.build();
} catch (Exception e) {
if (e instanceof RuntimeGlobalConfigException) {
throw e;
}
throw new RuntimeGlobalConfigException(e);
}
}
Aggregations