Search in sources :

Example 1 with MavenConfiguration

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)));
    });
}
Also used : MavenConfiguration(org.mule.maven.client.api.model.MavenConfiguration) RemoteRepository(org.mule.maven.client.api.model.RemoteRepository) URL(java.net.URL) Description(io.qameta.allure.Description) Test(org.junit.Test)

Example 2 with MavenConfiguration

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"));
}
Also used : MavenConfiguration(org.mule.maven.client.api.model.MavenConfiguration) RemoteRepository(org.mule.maven.client.api.model.RemoteRepository) URL(java.net.URL) Description(io.qameta.allure.Description) Test(org.junit.Test)

Example 3 with MavenConfiguration

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();
}
Also used : ArtifactIsolatedClassLoaderBuilder(org.mule.test.runner.api.ArtifactIsolatedClassLoaderBuilder) MavenConfiguration(org.mule.maven.client.api.model.MavenConfiguration) SettingsSupplierFactory(org.mule.maven.client.api.SettingsSupplierFactory) IOException(java.io.IOException) Properties(java.util.Properties) RunnerModuleUtils.getExcludedProperties(org.mule.test.runner.utils.RunnerModuleUtils.getExcludedProperties) URL(java.net.URL) AutoDiscoverWorkspaceLocationResolver(org.mule.test.runner.maven.AutoDiscoverWorkspaceLocationResolver) WorkspaceLocationResolver(org.mule.test.runner.api.WorkspaceLocationResolver) DependencyResolver(org.mule.test.runner.api.DependencyResolver) AetherClassPathClassifier(org.mule.test.runner.api.AetherClassPathClassifier) AutoDiscoverWorkspaceLocationResolver(org.mule.test.runner.maven.AutoDiscoverWorkspaceLocationResolver) MavenClientProvider(org.mule.maven.client.api.MavenClientProvider) DefaultWorkspaceReader(org.mule.test.runner.classification.DefaultWorkspaceReader) File(java.io.File) ClassPathUrlProvider(org.mule.test.runner.api.ClassPathUrlProvider) ArtifactClassificationTypeResolver(org.mule.test.runner.api.ArtifactClassificationTypeResolver)

Example 4 with MavenConfiguration

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);
    }
}
Also used : MavenConfiguration(org.mule.maven.client.api.model.MavenConfiguration)

Example 5 with MavenConfiguration

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);
    }
}
Also used : MavenConfiguration(org.mule.maven.client.api.model.MavenConfiguration) MalformedURLException(java.net.MalformedURLException) RemoteRepository(org.mule.maven.client.api.model.RemoteRepository) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) RuntimeGlobalConfigException(org.mule.runtime.globalconfig.api.exception.RuntimeGlobalConfigException) RuntimeGlobalConfigException(org.mule.runtime.globalconfig.api.exception.RuntimeGlobalConfigException) Authentication(org.mule.maven.client.api.model.Authentication) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ConfigObject(com.typesafe.config.ConfigObject) ConfigObject(com.typesafe.config.ConfigObject) File(java.io.File) Map(java.util.Map)

Aggregations

MavenConfiguration (org.mule.maven.client.api.model.MavenConfiguration)10 Description (io.qameta.allure.Description)7 URL (java.net.URL)7 Test (org.junit.Test)7 RemoteRepository (org.mule.maven.client.api.model.RemoteRepository)7 File (java.io.File)3 HashMap (java.util.HashMap)3 ConfigObject (com.typesafe.config.ConfigObject)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 Map (java.util.Map)1 Properties (java.util.Properties)1 MavenClientProvider (org.mule.maven.client.api.MavenClientProvider)1 SettingsSupplierFactory (org.mule.maven.client.api.SettingsSupplierFactory)1 Authentication (org.mule.maven.client.api.model.Authentication)1 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)1 RuntimeGlobalConfigException (org.mule.runtime.globalconfig.api.exception.RuntimeGlobalConfigException)1 AetherClassPathClassifier (org.mule.test.runner.api.AetherClassPathClassifier)1 ArtifactClassificationTypeResolver (org.mule.test.runner.api.ArtifactClassificationTypeResolver)1 ArtifactIsolatedClassLoaderBuilder (org.mule.test.runner.api.ArtifactIsolatedClassLoaderBuilder)1